|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗? D, o6 k# Q6 Z; _: `* i
3 |3 f4 r6 c& n9 d
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。5 J6 ~( v- t" |9 A5 U3 R
6 f# | H+ o' n! D以下是创建自定义插件的步骤:
3 }* S" e w9 ~8 H' N( |. }/ O a' `
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:. P3 Q% a' ` x; s' `/ l
1 x. x9 @% v9 \6 |5 F/ O ```; H8 m/ U% B+ `# x6 Y
<?php
. M" z$ o, `8 g& h+ `" F- B, m! C /*
4 J/ J" p: d3 S9 y Plugin Name: Site Wide Notices Plugin
4 {; m6 T# @/ T! R$ E Description: Adds a new custom post type for site-wide notices." r9 }3 f- b. ]: U& W
Version: 1.0
2 X" g5 p" S+ ]3 N& o/ f+ M$ M Author: Your Name
; J, L+ c% z8 N; j/ B, f: ?- m Author URI: http://example.com) l9 d p+ |% f& z" X
*/
% B8 M" b1 R7 o* p6 q M* l$ x$ C- l1 D% V) H' i
// Add plugin code here...
% Y6 Q+ m8 r) q; m1 e; P6 _9 u ```1 I8 i6 W* }" H+ m
$ W# u1 B! {9 B P2 w( W 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
; r3 g, A/ M, Z* S) {3 X/ }0 ~+ T6 _3 R
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
; h: x4 q! j3 X9 x P
" `% D U: K' Z- j ```
$ e0 u4 Y: S# q; c" a9 P* @ add_action('init', 'create_custom_post_type');! e7 Q* t5 A' T+ l5 o
function create_custom_post_type() {* ?+ x2 M! _& m! e
$labels = array(% R% U2 c5 j9 q5 `/ i- \- o a
'name' => 'Site Wide Notices',
) m9 ^4 q, E, j! I 'singular_name' => 'Site Wide Notice',) f2 F$ t3 g7 J/ }
'add_new' => 'Add New',3 i. P+ ^; U: I/ c. v0 x1 s3 E
'add_new_item' => 'Add New Site Wide Notice',
2 C- b5 j5 u7 k3 }, _ 'edit_item' => 'Edit Site Wide Notice',
4 `4 p" c N0 ^ 'new_item' => 'New Site Wide Notice',% [& M9 [# ~6 v! @% K
'view_item' => 'View Site Wide Notice',
! k* o! j4 w& N8 I 'search_items' => 'Search Site Wide Notices',
% A( H& l; {" i* W 'not_found' => 'No site-wide notices found',
$ u0 b8 `) S) N: ~ V 'not_found_in_trash' => 'No site-wide notices found in trash'- j/ f! V! G) e5 s6 L! l- b
);5 I4 {$ y& Y; x* {' }- D
5 i3 {8 }1 T. @4 f# C $args = array(
" v8 C: E( y' ?) p, R6 C. k! v 'labels' => $labels,
0 f9 ^0 [ d4 v: g; x+ @ 'public' => true,0 ?' g, x0 [$ E, N+ ?+ i$ d1 U
'has_archive' => true,; X) P- w* e3 G+ G% k$ E
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
[, M) `$ ?+ g7 j- v9 Y$ i 'taxonomies' => array('category', 'post_tag'),0 Z# {: E0 P2 f$ E, q$ a
'menu_icon' => 'dashicons-megaphone',& g5 N) L9 M& U
'menu_position' => 5,- F3 g- t& y4 V& @# t+ t
'rewrite' => array('slug' => 'site-wide-notices')
5 n2 }* Y1 j& t );
* T" i( j4 X* K2 s; j4 E* _% L0 s8 |2 g9 e. J" N! K. I) R
register_post_type('site-wide-notices', $args);
. K, D/ E2 c) }) ~ }. \( K9 H4 C: j! g/ S% t$ d
```
2 P) m+ r. b6 `0 L6 l9 f: r; p1 W# b- w
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。& b" J1 r# V$ \0 l: \
7 [8 C1 E- z2 {
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:! a, _# A# ~% N) m; N& J+ N
8 H. I5 k* X* x. C! Z4 y u) _6 j ```
+ z) x0 f: m& {1 {9 E4 { add_action('add_meta_boxes', 'add_site_wide_notices_boxes');4 H& P1 |" c( Z# w$ m
function add_site_wide_notices_boxes() {
7 Z" c. N1 k& |0 P* }5 B: y) x add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
) b! m; t6 g7 R9 Y7 h2 J- @+ V }
6 g4 |8 ?3 v& R. [4 Z7 h2 ]: ~: i$ w& v& ]- {
function notice_details_meta_box($post) {
8 {$ O; h9 a" I/ A wp_nonce_field(basename(__FILE__), 'notices_nonce');" o2 I! S3 i9 M& G& O$ F
$notice_title = get_post_meta($post->ID, 'notice_title', true);
+ d- E# W4 L" A# _ $notice_content = get_post_meta($post->ID, 'notice_content', true);. l% C1 v" Y( x2 o( T7 ]/ z/ W
?>
" y8 o( y' I3 r" ] <p>, |3 S6 X8 ~/ I' ~
<label for="notice-title">Notice Title</label><br>
# w; Y" W. ~# u* V) s <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">8 |' \: t/ A) z3 r+ C1 M8 R8 ]' b
</p>
# J7 t) E! l% g* E k <p>
% ^- ~- S v1 C <label for="notice-content">Notice Content</label><br>
+ A& C3 g1 a. M. Z( Q <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
3 z6 h3 F+ H+ T8 b5 i </p>
0 q- b2 o% x5 }( f: G5 \9 K7 u. ? <?php
0 _' V$ J% }( m# X1 {1 p) r }
. C# w9 q* ]/ T# Y! R, n2 [0 |5 y6 q
# z/ Q: ^% Z2 l; w- w% C4 \3 j q add_action('save_post', 'save_site_wide_notice_meta_box');0 t: q) Y3 }% O6 o0 P
function save_site_wide_notice_meta_box($post_id) {$ U, p; v2 N4 w& U& O
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))8 L( L6 @( @8 F4 `; n' A. S
return;+ E( z7 G0 q( z0 l1 U% M
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) ]: w% z( y9 s: a, ]% B) ?/ b# N- q
return;& V# @8 B" ?4 d
6 J6 z8 Q) c, B3 W2 G/ z
if (isset($_POST['notice_title'])) {. o/ ` a; Y0 U0 ^+ t! x+ V
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));- M; k4 K, L/ ~9 S; S. Y
}
' z. x `0 c9 {8 ]4 Z9 O3 e if (isset($_POST['notice_content'])) {+ N1 \$ E! {: D* i
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));6 K8 N6 h3 a* W, @: g$ [1 a
}- |. N+ f T4 m( [! q9 X
}
: h2 W4 j* R. x1 x- T& A ```; v) k9 w1 ]/ h9 d7 z9 v: @- ]) z: a
9 ]/ i9 E- X% B8 N" x" A0 d; [ 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。& X" w. Y$ [' o* p6 N
8 Q6 ?! [: o6 [# n6 y; M; n
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:4 C& l$ H' O h( G0 M. Z) x
- G" e# }4 G- m. V8 [+ ]0 s f3 p
```
3 h% n+ [; n9 c. h$ r% \% T. G $args = array(
8 ~2 f! G. m. i3 ^ 'post_type' => 'site-wide-notices',
( [$ d0 p! M. F5 J, O5 ]* j" z 'posts_per_page' => 3,
& Y7 k' M' f- B4 @( b 'order' => 'DESC',- s1 r9 V; A3 Q8 e- k( Y
'orderby' => 'date'. g7 ^. ^. V8 a" J0 y
);
8 }1 y! K1 k, s: D' i $query = new WP_Query($args);8 P: p1 {2 y, e6 L. F% R# y
if ($query->have_posts()) :
+ h5 H5 @3 Y8 e0 V' u) ] while ($query->have_posts()) : $query->the_post(); ?>
/ f' h2 E: G% _" \6 f2 ]! p <div class="notice">
' S' }% j. O( v) X& q( l <h3><?php the_title(); ?></h3>
1 ^8 y3 J! C8 k <div class="notice-content"><?php the_content(); ?></div>% _+ \! S2 ~& h% @) V
</div>
; m3 d) c. h& X <?php endwhile;! ]- O3 I) y8 W8 h
wp_reset_postdata();
2 I% B- q, O3 t5 i endif;
& E6 w3 O1 x7 s; W& l* R$ d; Z ```
: I9 g J1 K7 |" b9 G1 d1 G
& @/ M& ^$ z; I. B7 g' k- g1 Q: t* I 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|