|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?; d p- v9 _7 L$ @# z3 k
( }. h8 F" Q; r0 o
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
- f$ C' W6 O! H
) i" n+ n& n5 T/ k3 I. O, p; _以下是创建自定义插件的步骤:
5 z5 ~/ i2 K- w8 L
4 R, v& t+ T* g4 Z; o7 F- F1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
/ h" L& ?1 R* ?% e: x1 F7 P
4 n6 A Y* T% T8 \/ V# k! ?$ ~ ```% q+ }, C' l6 M( S
<?php
! ?' z8 W5 R: F* u9 m4 z" j+ e; L /*' d3 R+ i# `/ G0 i
Plugin Name: Site Wide Notices Plugin
% C! U* i) i5 X2 D; ? Description: Adds a new custom post type for site-wide notices.
- x/ S" a' _# Z2 H2 _ Version: 1.0! g* U @) F* S
Author: Your Name
1 p: f$ H8 n' I Author URI: http://example.com
" t, V; `/ U* A5 J */
8 E8 |$ j: ?1 U# O* V& K+ n, N) q. f' z5 q! K1 }/ L; g% S$ U3 ~- {
// Add plugin code here...
- ?9 I& S+ ~1 r! ]: z5 k; o ```1 [6 T; _+ `* z5 P0 i, W1 P+ A2 Y* ]
$ L. O" B5 ^8 o' Z 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
4 `; C8 Q' T5 Y( Q- v0 j9 j1 d! M: ^: w0 J) S# t. A
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:/ N, U/ z( g! B8 x! J
: }" f! p9 v; R! O9 R ```# k% T$ U! n% U+ J& V: x
add_action('init', 'create_custom_post_type');! \: Y, {0 w# {/ M$ Y
function create_custom_post_type() {) U, Q3 ~) k7 b8 s" Q
$labels = array(6 _7 t4 I8 [* h H4 e6 ~
'name' => 'Site Wide Notices',
. L( n6 I2 {& I; D 'singular_name' => 'Site Wide Notice',
/ l& y" Z' h; C" A6 u7 L5 f3 r& Q: t 'add_new' => 'Add New',3 I: \3 R3 k* z% Y2 ~
'add_new_item' => 'Add New Site Wide Notice',
8 q: }" l7 d3 B! D. w+ u 'edit_item' => 'Edit Site Wide Notice',
1 u& @3 a; \: S: b1 X" V! z 'new_item' => 'New Site Wide Notice',! Y$ l- _% b, N. Y2 W" o9 W: ]
'view_item' => 'View Site Wide Notice',
$ Q5 S" I# \# M. R3 A/ V- g F 'search_items' => 'Search Site Wide Notices',
% J9 Q5 v7 G6 U; \2 [3 c# P 'not_found' => 'No site-wide notices found',
! J- H5 R8 X9 d% ~) Z" N 'not_found_in_trash' => 'No site-wide notices found in trash'3 I( q2 o$ D" f6 C1 z3 F4 O
);
4 r! z: E/ X- u
4 G5 J( k I- K9 d6 v $args = array( T- S2 z' `$ Y7 d
'labels' => $labels,
7 Z. V; t* Q3 r7 B$ z' ` 'public' => true,
* S# P. k5 ?7 ?( ~9 J' }4 H 'has_archive' => true,* W& T6 A; y8 B* k
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),4 C" ?8 w; Z' f, Y9 g- K0 e _" ]3 u3 b
'taxonomies' => array('category', 'post_tag'),/ R. i2 ~2 ^" `
'menu_icon' => 'dashicons-megaphone',# Y q, F0 \- L& ]
'menu_position' => 5,7 ~& l& B9 n4 \
'rewrite' => array('slug' => 'site-wide-notices')
2 `/ v1 B: e/ S' E* s# @* ~ );
2 X6 y$ i1 i, r: M1 e( y1 W4 w) L# d) T, ~+ Q
register_post_type('site-wide-notices', $args);5 g; O' o& _+ s. p8 ^( q, R
}
7 U" S# p r7 Z, r5 S p |+ C0 g ```
( _+ z( J8 B+ d2 j4 e" Z0 f# S3 {/ T+ ]+ f6 y/ n
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。: Q7 P8 {5 q* \& h" `
" r. f8 s8 y* h3 E3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:5 F5 e/ L- ]/ @) P
% O j" z0 d2 @
```$ Z6 A+ ]5 D% n s
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');0 n/ V/ Z7 q) m
function add_site_wide_notices_boxes() {
6 f5 |3 v4 c; T( N- B add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high'); u) `" W/ i/ k; F
}
" f% N+ K6 }4 ~. p' v, r) g) L! Y, X. v' J. l
function notice_details_meta_box($post) {
, e# e4 \1 b! J/ S% K" H h wp_nonce_field(basename(__FILE__), 'notices_nonce');
& l% }5 w" f, W" V6 K5 P6 g& { $notice_title = get_post_meta($post->ID, 'notice_title', true);
% u9 H. \" T2 n* A9 d- } $notice_content = get_post_meta($post->ID, 'notice_content', true);
& j% k+ \! ~. T* @, r" A ?>
8 M) a/ A) k& P/ c0 [ <p>
5 }; G; B( J! m7 V& I0 d6 o( R) f <label for="notice-title">Notice Title</label><br>
, @2 d* }" Z- \ <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>"> u, O# x9 z1 d) g* F' x7 U3 p' L, t
</p>+ `# u3 l; K' ~- L% a' t+ R
<p>3 h- ^' Y; y, S
<label for="notice-content">Notice Content</label><br>
3 z% a- [( ?1 T! X6 p* T. @ <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>9 ?& s* O& D; {( z& }
</p>
9 V6 Z1 ?' C4 P. G' _ <?php
* Y; G5 M$ D. n! I$ W2 [ }) f" ]- c0 c: y
) Z1 o7 x7 k6 Z, h- n
add_action('save_post', 'save_site_wide_notice_meta_box');
+ k9 a8 ]9 k- N0 y6 L0 e/ N3 x function save_site_wide_notice_meta_box($post_id) {! q" u% x. K7 O4 f- x ?0 q; P
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
& t8 h8 ?/ w+ i* g) @0 k return;
- ?% n' p6 S7 s% w$ l/ q0 w if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)9 F) h$ R3 X* \# h) W* M& D
return;1 a+ d ~" r' j9 t" s4 C- T
# }, s8 \3 k5 c+ B! M% T4 E# U* V if (isset($_POST['notice_title'])) {! T7 K- v; Q6 X6 f6 Q
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
2 t# [# ~2 h5 L( |6 p9 D }
* \) T0 p. U9 h5 u d if (isset($_POST['notice_content'])) {
# x/ |& @, n; F' r2 A update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
+ r" X- q2 q V }
7 @" o" H! J: p; J6 Q; N8 R }
, L4 H* ?" q4 S- D0 q4 L# S ```
_* O7 P' B! x" h! w- f" d& i4 N" g7 \
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
+ o& c3 r3 G- j0 X( c. t, |: r
: i# _5 U9 V/ c) x9 ^6 ^9 A4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:6 o+ z7 h7 \4 N
6 C% E( l: _' h2 \( M
```
/ J/ R8 u2 N, a0 b $args = array(9 C5 } Z7 }" r* N/ I
'post_type' => 'site-wide-notices',
& Q& h; P) ~; u6 m5 ~3 c/ P% {# f7 E 'posts_per_page' => 3,
8 l s, |" o* R# W 'order' => 'DESC',! M- o7 p: d7 ~
'orderby' => 'date'
+ H' w. e$ s1 }6 o );+ M" q7 y$ M1 A; K1 W5 S3 }; ?7 t
$query = new WP_Query($args);; i5 o. p! t- h% q) x( o6 b% m
if ($query->have_posts()) :4 @0 [ A+ ?9 l# [7 `; R" Z6 G
while ($query->have_posts()) : $query->the_post(); ?>
. M/ y1 {& Q8 d# j <div class="notice">' S" ~3 n& L2 j' h
<h3><?php the_title(); ?></h3>1 y" s; g+ z+ Z, F9 l" H
<div class="notice-content"><?php the_content(); ?></div>) g6 h! u5 _2 ~( r# f$ r/ [0 H
</div>" ]2 n- J/ d) L
<?php endwhile;
! u3 Y& @* K' e, r* l3 a. o+ n wp_reset_postdata();
- Q& i7 [9 ~ `# i% x) K& ` endif;. S( n% K0 J0 o `, T7 U
```5 ?) F7 H, L! t2 B6 |* `
/ y1 `! d$ t @' m- W 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|