|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?- Q" Z$ j; A& M& C4 \( ]* H
+ _* J' {# o! s3 f+ Q9 }/ o
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
* x6 _9 `. R7 h. D3 [; J. s8 S; ?) P
以下是创建自定义插件的步骤:
; G+ t, Z6 C. G0 @$ u% H- F5 o7 {
2 Q. s# c5 w! B1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
! t2 e8 x0 g4 e% I7 t7 s
: t! n7 X! o y! m* A ```
3 _, U( f/ ^' {. {& o; n0 `- S! V <?php
3 u8 v* p' X+ e1 M) h; k /*! ~ y. N( n' O- i. ?( h
Plugin Name: Site Wide Notices Plugin
" H* y- G. r0 L X1 h% n Description: Adds a new custom post type for site-wide notices.1 V7 e1 \6 v* W' u5 l
Version: 1.0% Y5 O+ N8 g+ K2 z' i+ p
Author: Your Name8 w' n2 f8 x9 p3 t
Author URI: http://example.com5 d6 X+ K# O7 n% u6 L
*/, w6 f. |: w' ]
, a5 j7 M+ c, D0 w( g
// Add plugin code here...
2 w* l$ g, z8 ^ }8 { ```
) ?! ]2 M5 s& ~& w2 [# o! K3 Y& d# J
- d0 a5 L `8 n5 i: y) l 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
) @0 \4 W3 U& X, |5 X
5 `( d. b1 V, h# U# O2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
0 a7 w0 N! I }: |# P- n" P$ b& T3 L' N6 _* P) t9 L
```9 [* c- n+ ]! y# o. e* `
add_action('init', 'create_custom_post_type');# f* H T& p$ Z
function create_custom_post_type() {
6 X, T' o5 U4 J4 H% q $labels = array(5 ?. g' g( O: y" g
'name' => 'Site Wide Notices',
0 y( X9 L8 ~# D z3 b0 _4 z 'singular_name' => 'Site Wide Notice',
0 t& K7 Z1 ~; L* J 'add_new' => 'Add New',
8 h2 u. h1 w" s 'add_new_item' => 'Add New Site Wide Notice',
; H6 O+ s) _5 ?* P @) {; t; ` 'edit_item' => 'Edit Site Wide Notice',
$ X& z. p M! N3 B' I 'new_item' => 'New Site Wide Notice',8 x" o, o% ]! ?/ R
'view_item' => 'View Site Wide Notice',8 _ C, R9 @- J, \$ G
'search_items' => 'Search Site Wide Notices',
- @0 {! ^& m' r9 @5 m9 v$ P 'not_found' => 'No site-wide notices found', v7 w& r3 r! W/ U: f
'not_found_in_trash' => 'No site-wide notices found in trash'+ s9 N: e# U" l! s% R
);
8 \/ M* d0 K, O z0 d: d# g i* O$ O
$args = array(6 I/ r7 W" w; Z. Q: e9 D8 E* H
'labels' => $labels,
/ i/ r5 X" H, h- F0 Y 'public' => true,
) T$ Q$ _" i7 n% Z' y+ c8 t 'has_archive' => true, D1 K5 r0 l7 G% o! j& \" ~% U- J, T
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),7 Q( L# [3 R2 Y b
'taxonomies' => array('category', 'post_tag'),8 q) Y- f* U, h) ?8 k
'menu_icon' => 'dashicons-megaphone',
8 g" H3 B1 Z1 k2 @% m 'menu_position' => 5,) v% s& L4 h9 n
'rewrite' => array('slug' => 'site-wide-notices')
9 g* z" P% W) _ );% o- F. Y5 v' M* R9 Y; K
6 p9 O. R9 `4 j& \$ J7 [) V5 `
register_post_type('site-wide-notices', $args);
* H3 D6 V. L$ l/ _1 Q) z, a }
3 B- {" Z! x0 Q4 l5 Z ```, o6 x7 J; W9 l7 Z! f, M
3 M4 z: |& E; @- A: b" v$ _" Y 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。7 V1 U: \1 ^; M2 H1 N
: f, N- Q3 \" P; h4 d4 f
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
; s4 ~. e2 x; K3 q9 {5 G3 X% [% g% g% n6 k. f- V+ ]
```
: b. ?. |: ?+ s0 |) J/ t add_action('add_meta_boxes', 'add_site_wide_notices_boxes');2 W" Q% p8 J* G; b+ I
function add_site_wide_notices_boxes() {
C7 O' U7 s o! v6 i+ E5 M5 U( s( w add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
/ h' O; r/ I; o/ P* \' U }
9 G8 i- }% o! g3 e! U! l: m/ [6 X) C* T
function notice_details_meta_box($post) {0 X T: C: S/ Y$ [7 l; o" Q5 O I
wp_nonce_field(basename(__FILE__), 'notices_nonce');# ^; `* K+ T! ?+ r( w5 }; X
$notice_title = get_post_meta($post->ID, 'notice_title', true);
; j+ G: G- R% T9 P; X $notice_content = get_post_meta($post->ID, 'notice_content', true);, }# V3 n# E4 x' |
?>
) i/ y: d0 E' [ <p>
" W( u1 j1 P+ c o2 e' a+ X <label for="notice-title">Notice Title</label><br>
- [' q% y$ j) V* w! I7 R <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">" K O$ V0 V/ t& m, ~- j
</p>
( n; G @- Z7 F7 X <p>1 k: P$ k: |/ t- [" `
<label for="notice-content">Notice Content</label><br>* B7 i! _; A% F0 {- G
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
- z) \: b' k+ e7 b </p>4 s5 m9 v. i f3 R3 m6 _& Z" D
<?php5 J! V3 w6 K2 N! R
}
( R+ n% [& c- J& N. x4 w. g8 F& ]+ g, \& E* F! G" B9 O# ]
add_action('save_post', 'save_site_wide_notice_meta_box');0 q% |9 \5 z- O! Q0 A1 I
function save_site_wide_notice_meta_box($post_id) {
5 @+ A3 |5 O6 P, L if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
: o* a* \4 Z/ P, U5 Q( {3 z return;8 O3 `+ {* d X% D; f
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)0 W5 W7 o F, d, R+ M$ a$ F
return;
& k5 L X/ h# O1 n8 u3 [% _- O2 z
& S; ~1 w( z, S5 n2 N+ Q5 ~ f if (isset($_POST['notice_title'])) {
2 \& m* `% W% \8 t0 X update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
$ }8 X/ P; u/ i* _& r8 j7 ? }6 A9 @1 E" P' B) E, I" f
if (isset($_POST['notice_content'])) {
" N' Q- K3 H8 ?* h8 p: |& u update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
) g/ _" N4 V: k: O4 O }
! x) |8 x# O! D% f }
5 e, Z- y. L" s5 b( X- J# ? ```3 V" a( ~4 z& S
, ~* q' K! ^% `1 P# G 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
2 x& M! S# e: Q4 o0 m3 m7 @. G8 ~: n1 C4 s8 `* E" r+ \* ]2 ]
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
) v6 g* T" z* O7 v, g; d, @- H' u2 k% d
```& V" e/ Q8 Z6 Z* N4 f8 a
$args = array($ t/ ^# J1 V. D
'post_type' => 'site-wide-notices',
- F/ p( L3 ^% I6 h 'posts_per_page' => 3,
* O% ]( o+ o: j2 i8 X" M8 T 'order' => 'DESC',
l8 ?+ U$ z' \1 R, S 'orderby' => 'date'
, T! U h( W3 F* T5 W. j );
% F2 P# m1 }0 x; {# e1 b& {7 X $query = new WP_Query($args);
" i* v) z! K2 O; A, d# A' H if ($query->have_posts()) :' Z; A) |$ q6 c: P2 W& _. z' v
while ($query->have_posts()) : $query->the_post(); ?>9 u V5 @, R# Y& ^2 \- _& m
<div class="notice">$ Y9 A1 v7 n) x% H0 l/ J* X
<h3><?php the_title(); ?></h3>7 i1 e" {$ W6 P
<div class="notice-content"><?php the_content(); ?></div>5 ~5 \2 P4 {1 ?) f
</div>
9 ?# z8 A7 y. ^: T l/ \: T <?php endwhile;
5 |' F! B4 n( b' E; E+ B wp_reset_postdata();
, P) C$ Q* |/ k5 F1 Z; B9 D6 z/ w endif;( K K% [: K1 u' x @ D
```# s6 M* P. F, u8 L
$ M% {8 q5 Q4 d( [9 ^, d 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|