|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
; Q E9 S4 I6 V6 _2 W: f
$ ~* \' w4 } _) X如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
. R6 A3 x: n" p2 a! g( E% w4 P6 I0 _
以下是创建自定义插件的步骤:7 q$ X9 C) O" U1 g1 q
: u& n6 w0 ]% ]
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
/ D& H9 i, E8 d5 u% n
4 q, [4 z3 c$ y ```
/ V) o9 f2 K1 t, u/ E' M: o t <?php( H5 v. N8 b; j% G) h
/*
! q+ I1 \/ t* Y5 S0 A Plugin Name: Site Wide Notices Plugin$ h/ z- f/ W1 B' H9 |4 F
Description: Adds a new custom post type for site-wide notices.; @$ j [( f/ q- f/ g$ Z5 ?4 m" |
Version: 1.0
! r8 G. } n& ^$ G! Q! M" ] Author: Your Name
2 R+ |8 M# ?5 j* O* m$ M Author URI: http://example.com
; a& [: O* m; v. y h/ @ h, @ */* O4 N" V& R2 E) ]6 c
5 d' r" G8 ^, e% m0 m( R( z! u // Add plugin code here...1 }3 U% ]; Z' j. Q
```
1 X# o. S4 R) K) [( a6 |. D+ d
9 E- ^: v" C* q7 t; ? 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
" r1 a6 [- P- Y/ o, @9 K: H( o# q% Y. t
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:2 W4 z: \- u5 I& Z/ i' x
1 W4 f/ ~' ~/ g$ i; w
```
/ |9 ?/ q8 S( m add_action('init', 'create_custom_post_type');; Q# I! ^# G8 n+ ?1 z- W0 P. S& V
function create_custom_post_type() {
* B1 x0 z* t# g- {/ s: o# x7 r $labels = array(1 ^2 e1 c4 B3 L8 x- n
'name' => 'Site Wide Notices',
7 v8 x6 z7 J' @5 b" Y1 E: W 'singular_name' => 'Site Wide Notice',
3 b9 O1 R/ [) ]" t5 D4 S5 h, m2 y% H 'add_new' => 'Add New',2 }! e- Z Z# e
'add_new_item' => 'Add New Site Wide Notice',) k3 Z2 u4 J0 A% X2 w7 h
'edit_item' => 'Edit Site Wide Notice',
# f( _% S) T" Q j N 'new_item' => 'New Site Wide Notice',
0 g( J( @) z) q: J! }$ e2 Z 'view_item' => 'View Site Wide Notice',
2 M- o! [, f8 u+ ~ 'search_items' => 'Search Site Wide Notices',3 B/ b* ~% r' W J. u' j- X
'not_found' => 'No site-wide notices found',9 ~: K d( y/ q M7 \/ }7 Y
'not_found_in_trash' => 'No site-wide notices found in trash'
3 J/ X# q. ~2 Y );* `( C, G C* F, F
# a* K% R# e+ I' u $args = array(
# N! J, B: M& g0 ^ 'labels' => $labels,
( {* Z P- }& Q2 k+ |% B2 d 'public' => true,$ R( V7 q @- y/ S, J: x; T! \
'has_archive' => true,
& B6 }. k8 O6 x2 s. g# R& I( T0 ] 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),9 p% K. r0 e+ q7 s* z
'taxonomies' => array('category', 'post_tag'),% Q6 o+ L( J( W" W
'menu_icon' => 'dashicons-megaphone',
# p4 R( N; U; p$ p0 j( O3 _ 'menu_position' => 5,
0 E3 r9 r* o. M* |; `/ d 'rewrite' => array('slug' => 'site-wide-notices')1 Y; M V+ [$ |/ p
);
8 O2 @' G. U( ?' C) |0 n1 W
: S- p$ G& C) `0 C6 l register_post_type('site-wide-notices', $args);
4 `4 o6 p' j' K) K }2 H* {; c; Z9 {4 u- F5 e: q
```/ a, s0 }- Z; Y" I) j# f
9 {( K/ B2 b) ^; t0 t6 d 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。- }3 j5 N( N- [/ c) c
6 [) S0 l w! q3 j: W- q3 S( @4 P
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
+ ~4 y$ d5 s4 c" Z0 O+ c& g1 U6 m" F6 V. q! e, r: t
```
- p* W1 {5 f) W2 |& Z; d' u" o add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
# O9 \9 z; ]1 J1 @" n function add_site_wide_notices_boxes() { f6 } b- ~. Y9 j' S+ z
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');6 r7 m; i6 Z4 v' X/ E; l
}5 J3 S5 I: y5 q* X* @, _- {: a& \
/ N0 U8 n/ D6 Q, T
function notice_details_meta_box($post) {
]8 q; D4 t- o2 F, j. M wp_nonce_field(basename(__FILE__), 'notices_nonce');
1 T7 I: e v7 J- M) \9 i $notice_title = get_post_meta($post->ID, 'notice_title', true);5 `8 D$ K# i8 f1 I; f2 ~
$notice_content = get_post_meta($post->ID, 'notice_content', true);
$ J; m2 R& T3 {! W ?>
0 D( [+ o, O8 D( h+ P/ x9 F <p>
0 Q: v; G$ S+ ?' O <label for="notice-title">Notice Title</label><br>
6 ]& \% ?0 r. B) e# x: |* u <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
7 e* S. }3 @ f </p>
0 n9 [3 |" M) R( W% W7 S2 v7 S <p>- y. X3 w5 [: P
<label for="notice-content">Notice Content</label><br>
3 U' P$ s, g5 b: }5 b. ]# \ <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?> K0 n9 V* \+ ?0 T# c
</p>' H5 H% Y7 p+ ~ O% J/ d( |' N w
<?php
2 F* D+ `3 D- G# p# `- z }
& L! ^! P1 V y- x3 F, }6 y
" ]+ Q: G% t5 }3 P6 `6 g* W add_action('save_post', 'save_site_wide_notice_meta_box');
8 d- _+ u& p( d" S0 w: i0 o function save_site_wide_notice_meta_box($post_id) {
$ j2 R, V- D: H9 s4 `: x if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
& A5 _9 R1 L' M% p0 P+ S+ X return;! p. w. a. M% _! g1 P
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)$ X6 c7 l$ R6 ^ w
return;3 Q9 O6 K. S# s. l0 K c, S
" N. C& `7 F. ~& a) P$ N4 \* w8 j" W% C
if (isset($_POST['notice_title'])) {! p; l' H0 R% {7 A: F
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));& T( y* h5 U* W
}0 W% _% ]" M9 g- {- x; s) g
if (isset($_POST['notice_content'])) {
4 F. x, u5 z( {# ]7 @9 o1 a update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
4 Y% o0 B) ~" R; N# c& V0 X; b' i4 d }+ K6 F5 @1 g0 l# [, _: {' k
}
2 g3 x+ \. h9 h# v$ |5 F3 x ```& h) {$ d h$ [# h
, o" u2 U# q' m. N4 s5 o 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。+ k* U y" J# [
7 g+ \5 F1 d) m: j9 |( q
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:7 A x3 h& ]; x0 M
/ C! k: h+ I) Q8 N9 L G' v5 d" Q
```. D) [! O5 ~: u, w, @- T
$args = array(
3 j$ s4 U5 g9 G7 D 'post_type' => 'site-wide-notices',8 I) Q5 }; S( R5 ]$ f
'posts_per_page' => 3,
- g% f4 a; o- @1 u. i1 T 'order' => 'DESC',7 a4 y" U \8 V. l6 B4 L3 Q) d
'orderby' => 'date'
7 _8 x8 M. P% B8 F );
/ G- k' `# E( {7 o3 ]/ q $query = new WP_Query($args);
+ T/ P) C2 h; I0 z1 J% w7 b' ^" V: L if ($query->have_posts()) :
0 T `! [, I0 U6 _! p9 G6 g while ($query->have_posts()) : $query->the_post(); ?>
0 i/ L: u: @ S0 N% H3 d. U8 t* {4 Z% A <div class="notice">) d. A, ^ j9 H+ [0 ~
<h3><?php the_title(); ?></h3>. J& L6 ^7 K/ _: i$ L# k) g( y) |
<div class="notice-content"><?php the_content(); ?></div>7 y- t7 d( f" v9 o: I" V# W( Y, E
</div>0 X/ S P% `. _4 D" |! x+ _
<?php endwhile;- a. \+ N, E+ I) S# o% X5 \* u
wp_reset_postdata();0 Y. ^- d/ x2 b, S4 g( `5 f, z
endif;( A0 _' x1 b% l# v1 w6 Y% |) v( A
```) u2 q4 u3 j- t& z4 d" o
( |- L: q/ h5 t" Y5 N& ?) M
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|