|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
: v, @/ ^ n7 {, Y0 b
9 N0 ]( f# p& Q' \7 ~9 N2 f如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
# J$ w& z+ G6 C2 X" A. Z" [+ }" B8 C: K' L
以下是创建自定义插件的步骤:
5 R; U. y4 _! x. V* X' ~; S1 ^
& E [5 x C) l6 `) \1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:+ \. W8 ]+ K' M- s! P* ]
/ j" V5 e) i( K1 n. v9 ~ ```" D' a1 q/ i4 _- D' c
<?php
. M5 _; W' q k: t# @' B /*, p+ r9 E6 s/ D5 b
Plugin Name: Site Wide Notices Plugin
+ j5 ]" Z0 S; T Description: Adds a new custom post type for site-wide notices.* a! Q7 D$ _* e+ U5 ^$ U% R9 a
Version: 1.0' D" i, i1 H5 s
Author: Your Name( _. n" J+ f# N1 Z, S1 Y. n
Author URI: http://example.com
( H) z" t4 I& y9 _; N */$ P8 O% n% F5 F0 [9 K% P7 k
% ?) @4 T) N# N. N* U, Z) m* E* c+ |& A // Add plugin code here...0 }! h% R. f' |( P* T6 j
``` R8 q3 v& a1 r1 x
( _' `6 u& j5 v: z/ f; u
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
4 y6 S7 {. R" _, K" C% u0 ~+ b9 S* U# m. X, S) c
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
2 ]3 |+ M5 \- [! l
; b- Y. W! s3 \# }2 O( z ```
+ g( r( Q1 X* W: A/ F! D( U+ W add_action('init', 'create_custom_post_type');
5 ~! b2 |8 i+ y* ^: _$ q, d function create_custom_post_type() {
$ j# r4 }. Q6 Q* W9 v, r! L $labels = array(
/ K; G; @& }; G) m8 r' s 'name' => 'Site Wide Notices',
) B6 C: U' _) t4 p! R& B" H- H 'singular_name' => 'Site Wide Notice',
/ q3 q8 N. R! a1 X# ?& D9 ] 'add_new' => 'Add New',3 ~ l: [+ B% V/ Y9 t1 s( C: K
'add_new_item' => 'Add New Site Wide Notice', U* o( N5 T" C
'edit_item' => 'Edit Site Wide Notice',3 M5 N0 k: \) \
'new_item' => 'New Site Wide Notice',) S: x7 B! P' R% S$ x
'view_item' => 'View Site Wide Notice',
7 O2 b- ?7 b7 ?9 }& a4 V 'search_items' => 'Search Site Wide Notices',2 z; {. y7 ]5 X5 n. W5 a
'not_found' => 'No site-wide notices found',! b+ ?' s5 ~8 N! Q, V
'not_found_in_trash' => 'No site-wide notices found in trash') d8 j/ T) v7 ?7 m; Z- }
);1 [2 l' E$ s! z- p! h
9 F; Y2 h0 q/ { $args = array( c' m- X8 B3 f- z
'labels' => $labels,
/ F% T. n; m5 F# U9 K 'public' => true,
3 O3 k# _, n$ A) b4 J 'has_archive' => true,5 `% h; J3 I/ ^( i' j6 _1 W
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),1 c' g& u Z8 X7 ^/ p+ c
'taxonomies' => array('category', 'post_tag'),
8 u* x5 z/ P c& j3 ?9 i 'menu_icon' => 'dashicons-megaphone',
4 R7 v8 q% h) e/ U6 L 'menu_position' => 5,! q: a5 t6 G- T8 A2 V
'rewrite' => array('slug' => 'site-wide-notices')
, x! _3 l" C9 T/ `7 |$ {( t' B );
1 g3 C+ _% _9 r# _/ I. C4 x5 k J$ I1 R' E
register_post_type('site-wide-notices', $args);
! L+ |6 T' G5 }9 O6 L7 u% N& d8 x }# W/ R" \$ X% W. y6 p$ d% O
```
, h! p; Z/ p! N& R) q9 T. R& n& r
, M2 A% G7 q" \ 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。' z$ s5 X$ S" e8 J! |, S' ^7 Y
+ _* E) {! G! X9 ^! a$ H3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
! _0 V2 o' V0 Z2 ]
( [/ `% _5 q4 R5 N: h) U ```
4 n2 N/ D4 g+ N9 x/ `( l add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
" Y/ b0 G* M% y# ] function add_site_wide_notices_boxes() {
0 x) _, j) u$ L% J ~ add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');& {1 r. i; f$ _3 W: S9 x
}& A; W/ `. W0 |
2 N" M, X% u( v" g2 O
function notice_details_meta_box($post) {# I q+ \7 o$ ~0 ^3 ~- ?. r" B8 c1 O
wp_nonce_field(basename(__FILE__), 'notices_nonce');
4 h s) `! T: P- f3 @- C $notice_title = get_post_meta($post->ID, 'notice_title', true);% x* k* ]. Z4 ]
$notice_content = get_post_meta($post->ID, 'notice_content', true);% ?( i& j7 `( j" t5 v' z# {- b( G
?>$ I* @* V0 ?7 X: ]
<p>/ _* ~6 Y: x0 E
<label for="notice-title">Notice Title</label><br>! ~% _1 ?+ f [/ u @& |0 }
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">* q T* q/ l5 T7 N: d3 P
</p>
6 [! _2 g- M& Y; s K/ j <p>
' d1 i7 G! b& U/ ]; k" v <label for="notice-content">Notice Content</label><br>
4 j" u- o5 @8 o% @4 W) J- G <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
/ @9 p) `2 x+ s$ G0 R </p>* M/ I% |9 _" Q# V8 p- [/ u
<?php
( f$ \8 R+ z, K8 A+ B! C }0 H: J( @( T) M# x" P
2 p6 _$ {6 L6 i; N! p add_action('save_post', 'save_site_wide_notice_meta_box');
2 H4 \; h/ x9 ?$ I* k function save_site_wide_notice_meta_box($post_id) {
% n" P$ c1 V) _! L if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
4 w4 G' o: ] z( V return;2 R2 R& q% D7 [
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
3 { G/ ?% j [6 h& ?1 V. b( f- K return;: ^% M6 F# A" Q0 E/ _
* W ^( p4 [' `$ i+ X/ _ if (isset($_POST['notice_title'])) {
8 o c! _$ B! I. c7 J update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
# G( y6 ]8 J- c' v. O }& n+ I- u# }3 w6 [5 P4 U
if (isset($_POST['notice_content'])) {7 E& c" f3 B, j& r, X' O. l9 J
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));+ L) x) b( F3 ^$ d
}( j; p& j7 v' O/ H2 M
}
0 w1 C Y: ~9 O9 U7 V- M* i3 V9 `" c) B ```" T0 h6 M" c4 u+ u6 {6 R6 T1 W
# V2 M' \5 X. y. q
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。9 ?- l/ @+ _2 v% C# X# B% a
* _5 M' I- p9 V; T
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
9 M( K. a2 _. V+ N) e& D" X
) R, S% @3 X* i- f7 y9 S* R7 ] ```
; \9 }+ P4 r; c# ^+ M $args = array() L* }* E2 i8 N5 ?( _
'post_type' => 'site-wide-notices',' w( g/ j1 _! H: n
'posts_per_page' => 3,
8 h1 g: c# Z% ], W, ^6 l 'order' => 'DESC',; D6 Q k! ` g( l, u' C3 e X
'orderby' => 'date'% Z# a& k% o! A' S( e
);2 }' O$ {/ q6 P7 S# {% J. }
$query = new WP_Query($args);
p+ r! b& y0 j& f' ]3 V if ($query->have_posts()) :
- F& H. l1 ]* r% d while ($query->have_posts()) : $query->the_post(); ?>% f9 Q" A; ^1 R% ?* X
<div class="notice">- p0 e6 {! F8 o+ D8 n
<h3><?php the_title(); ?></h3>
. M9 V4 g. }) f" E* F" m <div class="notice-content"><?php the_content(); ?></div>
; ^' i- ~7 _3 {. j0 w4 H </div>' J8 G6 b% L! W1 W$ t
<?php endwhile;
& J/ P' u+ H" ~7 k4 { wp_reset_postdata();. [3 ]: t% \/ L3 m+ L' W* n
endif;
2 z. T8 v9 a1 `. q, t ```
+ a! y0 y9 _% G( e, I2 P
Q+ a) B& W: ^8 q" Q/ W2 w 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|