|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?5 Y- v5 Q, t0 F
4 Q% R2 d: C2 O
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
, w% V3 Z1 Z" |4 `: \! Q/ ^
8 Y8 ? Z7 m- j2 t( ]# @以下是创建自定义插件的步骤:
* F2 A* q* U! e9 m: _+ W e. l) e, B& s% p% q8 Q
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
& [* v+ \8 U. O% J& H/ L* \+ F0 U3 x/ `4 e
```
# ?2 X z( g) w- S( v; Q2 B <?php
8 F. ` v/ w5 G0 ]' A, w z$ a /*
4 C5 h; e t( M0 }% F. l1 A Plugin Name: Site Wide Notices Plugin. L q& P% x$ Q* B) H; R+ S, ^; X( Y0 Y
Description: Adds a new custom post type for site-wide notices.* H" l" Y9 y; q# ~7 z! t
Version: 1.0
) A& K. {7 P' f7 E/ d" _ Author: Your Name, w; b: s$ l' n' D" G) C! b0 I. H
Author URI: http://example.com$ J" E$ E) P6 z/ I H4 }& j
*/
3 V* k+ d) _0 E' q" q* p1 o% ~7 I
// Add plugin code here..., Y" @; }$ s# s, H+ {2 H
```2 V2 J) {3 J/ Z/ I
8 Y1 R8 G& h# [" l' e0 O7 H; O
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。5 ~$ Q8 \' K7 \% G: m
~4 x3 w* D$ f" X! I1 Z4 L2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型: I! A6 `+ N8 C$ k/ [8 G
* z+ z- r, ^1 Y. u1 @# l ```& ~+ M: c: q' W' H( i
add_action('init', 'create_custom_post_type');& Z* }' i! L4 {
function create_custom_post_type() {
9 m9 n: `% _: f* X _ $labels = array(2 D2 I1 J ~6 b! c# x9 @
'name' => 'Site Wide Notices',
$ o4 i ~- ~" ] 'singular_name' => 'Site Wide Notice',
& Z, C$ F; a: N$ v 'add_new' => 'Add New',
( |& U# k4 p# x7 r& ]& l9 V 'add_new_item' => 'Add New Site Wide Notice',* g. E2 ~+ o) v7 N, f
'edit_item' => 'Edit Site Wide Notice',
; ]; M. `" x0 d( ^3 A2 g 'new_item' => 'New Site Wide Notice',# \7 J1 [( s {- f& L- J! @
'view_item' => 'View Site Wide Notice',
$ T9 a' L+ W" v6 E5 H. G! w 'search_items' => 'Search Site Wide Notices',) r+ u4 u, u, F4 z7 S3 E: t
'not_found' => 'No site-wide notices found',9 T5 h$ u4 O$ |# S$ S, h
'not_found_in_trash' => 'No site-wide notices found in trash'
* L. Z7 O- R! d" s: k );% E7 J6 K: z2 s) e9 U/ ?
( N2 R$ \- K5 N' i2 m8 B; Z1 Y( u $args = array(
9 n7 W! r6 h& j 'labels' => $labels,6 V& g7 A1 ]5 ~0 w1 M. ^- F! e
'public' => true,7 x* O5 n5 S: d& [8 n) X. C% r% [
'has_archive' => true,
% |8 t, Z: E7 X. C" Z% _/ s* n 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
; N1 Y( l ~' @/ [: ? 'taxonomies' => array('category', 'post_tag'),
9 W k _/ _4 @( Z* p 'menu_icon' => 'dashicons-megaphone',( D5 |! N: b! Q" p! s) J) p
'menu_position' => 5,6 \. P1 c9 b# f$ a% V3 ]
'rewrite' => array('slug' => 'site-wide-notices')
3 A6 E1 X$ }* d& u );; Y! z8 E! }+ F" j7 A2 Y8 W
& ^2 D5 w4 f: J register_post_type('site-wide-notices', $args);/ H* G* L/ \3 o* T+ L5 _
}
E/ D$ t! \2 U) T, b9 y ```, v8 e# p, Y" \* O
, M. ^# `" H8 P+ h. j
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。, i8 z+ Q6 H, r6 S
- [7 n6 w4 k+ E; T0 A$ }
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:$ n! z* L% o0 s! t3 y" }; g
) s5 B! W( J; Q5 G& w9 q ```" p8 v* }( Z# c0 \' |3 U, D
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
7 Z% e9 z7 ]" M h& @+ H% | function add_site_wide_notices_boxes() {
; ], n4 V7 I0 b add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
, t& ~8 [9 H9 i1 p1 U* j" | }
3 S' N& f% a% P9 a' g7 T; Q9 z8 k' `7 f0 K3 b/ s
function notice_details_meta_box($post) {: T( m& R7 v- M4 i" i, ~- w0 n$ [
wp_nonce_field(basename(__FILE__), 'notices_nonce');
3 Z2 i9 w* x* ?$ q: {* H' a. s, I) D $notice_title = get_post_meta($post->ID, 'notice_title', true);6 o0 E3 J% B; S
$notice_content = get_post_meta($post->ID, 'notice_content', true);
, K5 Z8 m2 A" K9 ?" I% q ?>7 O; B( D) |% ^8 k
<p> v# N) x) y" J7 e* b0 p
<label for="notice-title">Notice Title</label><br>
7 Y1 e+ W2 b1 a/ U) C: e- A <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">8 O" e& H s. k5 Z/ K1 ?1 l
</p>
' I6 b. @& m- p5 y Y$ b <p>
2 c! \: q7 Z/ g* a. J <label for="notice-content">Notice Content</label><br>
/ R7 V3 S2 _8 T: a* k" h <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
. v% f- d! w3 ~; V0 j3 u </p>
4 g8 D0 a W+ i( q <?php; v- D1 j8 f1 J5 D# [( \
}+ I1 L: t: A9 S" J7 Q
8 L; ~; a/ D9 F. t5 z add_action('save_post', 'save_site_wide_notice_meta_box');
+ b3 N S1 d3 ^9 p. M function save_site_wide_notice_meta_box($post_id) {( y* o3 g6 u: N
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))) [# v9 s' z' Z4 E% |+ i
return;
7 X0 \: g" o' N3 s7 x b. j2 F2 v: ` if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
$ l+ W4 t2 ~- {2 Q return;2 V' s: E) s. M* Y0 |
9 f$ s8 b" `9 K- i2 d" e
if (isset($_POST['notice_title'])) {
& ?, S& f/ I) o0 o update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));0 I. A5 J$ ~3 ~* U% T( n) p/ o t
}/ I3 q& _/ [* c" |% C
if (isset($_POST['notice_content'])) {, L$ \ E/ Y. p( E# a4 ^
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));" r& k. u, T5 E: J
}0 N2 [! Z* c9 D" \4 e7 S. s& e
}% ]& i- E7 O- E2 Q4 X' ~' Z4 }
```2 i! d- ?7 x" k. { z( @; H/ ]
$ J' s4 C9 i% }8 E h0 F 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
* [ e; q( ~: A4 i& F$ S3 r
1 q5 Z4 I; u/ S6 u4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
! f( H' ?2 g1 Q) h) U) I" U- x% l4 V
9 A( f& U* `$ A; u' } ```
' j' o1 f8 D" w k z $args = array(
% B( P' w' l! g9 | 'post_type' => 'site-wide-notices',6 u' n" W7 ]2 Z- _7 V
'posts_per_page' => 3,, Z* J3 t) y1 T' |/ O5 `6 u
'order' => 'DESC',
+ f2 D/ z7 Q+ |) X2 u 'orderby' => 'date'5 F6 M& X: h; F3 U- a
);* N6 ~( p7 H+ }2 G
$query = new WP_Query($args);
6 {, U4 ?0 Z/ X: f3 `+ b8 a$ N: D if ($query->have_posts()) :
, M1 Q( A9 U( f1 r+ V! j while ($query->have_posts()) : $query->the_post(); ?>2 W# T+ g: J. q1 @
<div class="notice">
8 y+ A8 b7 T! W6 ?/ `5 k q- h <h3><?php the_title(); ?></h3>
3 _+ B5 J" X' l& Z' l6 M <div class="notice-content"><?php the_content(); ?></div>( @. }' Q& V; N0 ?9 v( k% u2 [ Y
</div>
* E S+ E: L: y6 m, j4 ] <?php endwhile;, j" `3 n# @/ m' A0 ?& @2 y
wp_reset_postdata();
" l( G9 J. Y/ d' J- Y+ ?3 Q' _" ?' s endif;8 G0 ?, t# v* q0 G
```+ A4 [, |' k4 l$ ^ D
1 f3 `8 S9 B' `
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|