|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?2 Y' ~& @: Y ~+ q2 _
2 {3 q" @# n6 A7 p7 ^, ?8 g如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
) ?4 `$ }; Q& L; J3 [
) z' I- a7 y8 @8 F以下是创建自定义插件的步骤:
0 `* `! g, P* u) V# m4 K: o9 f% ^+ B
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
* ~1 ?: F- R* U. x1 \) ]; n; Q( O
: @& ^+ m) L y5 W ```
* b# g. r2 v# T% \/ ~. I <?php
+ y( w$ Q0 \( N$ E /*# j1 i0 r) m5 x8 e4 l! w4 W: U6 T8 Y
Plugin Name: Site Wide Notices Plugin* x7 I2 _* k3 w W2 a5 t. e
Description: Adds a new custom post type for site-wide notices.' h* j: n8 Z8 }1 M" A
Version: 1.0
5 k' J1 l4 {. [, X) w Author: Your Name0 n: ?2 f, ?9 ~; D
Author URI: http://example.com$ t6 t0 g& `+ P1 ~: H" [6 l
*/
* L: W0 d! Y% p0 ]7 n
5 u9 z' L6 c* J0 O: N8 n4 p // Add plugin code here...
u8 ?% ?7 X2 F3 v4 i# e ```
/ W# l4 e$ c) h% v [* ~
+ {8 L- ] I2 H+ T- p 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
: @3 Q/ X2 @' k9 q
* l$ m' n" J1 o& v3 Z+ d' Z& a, ?3 H% ~2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:4 L, |; F' W A5 ~# X; E# v, @
2 s7 r" L3 N {
```
3 ^3 A4 w* C. R' G ~6 }5 L( \. z9 _ add_action('init', 'create_custom_post_type');4 _. }+ O3 C1 V/ C) }( O% n- E! |& P
function create_custom_post_type() {! l. F" s7 u+ _
$labels = array(
1 V& J& Q! _, R/ I8 Z 'name' => 'Site Wide Notices',
) a/ @& K7 m2 y 'singular_name' => 'Site Wide Notice',
9 J' e7 X3 n! j; f 'add_new' => 'Add New',6 {% G- G& J: B: C+ W% F' o
'add_new_item' => 'Add New Site Wide Notice',7 K) V0 z9 s; R
'edit_item' => 'Edit Site Wide Notice',6 b/ T( O6 N: W/ ?, X0 ^4 U
'new_item' => 'New Site Wide Notice',
F6 I6 P; K/ a6 I5 P' V 'view_item' => 'View Site Wide Notice',
" q" g0 `( U- g2 B 'search_items' => 'Search Site Wide Notices',
2 I! Y* m1 d4 X 'not_found' => 'No site-wide notices found',
; X+ A7 w6 o( @7 g6 } 'not_found_in_trash' => 'No site-wide notices found in trash'; O2 {) g- \) o
);
7 m( z% | s5 q; r6 I6 n$ ^) y( r3 \
( c g/ z( ?- H3 |% w# E6 |. B $args = array(2 t6 \. W, E# @3 L1 j
'labels' => $labels,
$ ]8 _4 ^" S0 W; ]* X) ] 'public' => true,
' f/ c9 N" I$ ?& E/ T% O% V% v: w 'has_archive' => true,
0 S8 w! X6 Q$ m, \! Z# t; f 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
9 F$ @. h' {3 G) z' ?' _ 'taxonomies' => array('category', 'post_tag'),# i. R( W9 l0 O, }
'menu_icon' => 'dashicons-megaphone',
X8 a2 [& u% ?+ P 'menu_position' => 5,2 T: y* ?+ S3 B
'rewrite' => array('slug' => 'site-wide-notices')
" B2 M( F# s* d/ T+ \$ Q- F );8 {" ^( v( @' J8 j7 }' B
3 p3 _5 q- n, W7 p1 e
register_post_type('site-wide-notices', $args);
3 Q' a' G4 w* g) b q+ [- S. J& |6 r }. |& ?; w2 o; B" G+ B
```
' z( L7 I5 F/ X3 ~. Q5 V; B/ e
6 |! D% G" L3 s' z/ S 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。- A ]' H5 {' z. U9 L/ f5 k E
, T0 n. o# e5 x; ^7 a3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如: O6 e% z5 B" O: C
2 C' k9 S: k& `2 J
```
& h- s) _3 N+ \ add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
9 W8 N- e6 O: j: D& `* ~ function add_site_wide_notices_boxes() {
$ F4 l5 G3 P& H8 [/ V* z2 ? add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
, K! y2 B" \2 C1 R% t }# F6 I: ^" a$ }; e$ }
% u4 {9 r" Y+ F2 h1 ], X1 a: V function notice_details_meta_box($post) {1 n" `4 K2 W% g+ {% K
wp_nonce_field(basename(__FILE__), 'notices_nonce');
' L; p& y$ p: p" s; [- e $notice_title = get_post_meta($post->ID, 'notice_title', true);
* n0 Y; ]7 R! Q$ Q $notice_content = get_post_meta($post->ID, 'notice_content', true);0 J2 U- G8 i3 V6 @3 [3 S# v
?>
3 N$ o* k% s8 N& J W <p>- `& q' b0 J5 x% w, W
<label for="notice-title">Notice Title</label><br>
5 x j, H' L$ g5 I2 I <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
9 ?+ d9 x: R6 b( I2 j1 h z </p>& i! O1 W2 ^5 @9 z, L }
<p>+ U( ?& R7 W c0 u- p( H. V& C
<label for="notice-content">Notice Content</label><br>
! E* s; a1 p: _( L0 t+ d4 i' {% K <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>/ P' w7 D h- ]0 n, [% G
</p>* E+ O/ [6 O* w" A( L" H
<?php$ h+ C; c; d1 C& v$ V
}
8 e" O/ o, S% q5 Q, o2 l
$ |; u; m6 |1 S' o' W add_action('save_post', 'save_site_wide_notice_meta_box');: \" V P6 Q. {2 _' O( ^7 q
function save_site_wide_notice_meta_box($post_id) {
! Q4 W7 x. x% R+ V+ d3 `# c0 P3 T$ I if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
O6 J k) c- p" J. [1 I1 p7 M return;& J& F1 B( y4 e+ P+ _1 i5 n; l
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)* D3 }7 G3 y9 \; d- U: U- p1 V+ M
return;' T: r+ C! w+ m3 y1 V. r H
4 I4 [5 _, Z2 g, P4 W, [& g
if (isset($_POST['notice_title'])) {
2 ] i( N, \3 e update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));" |; L0 J1 |+ a' z& a
}
" ^4 Q' k. _9 s# l if (isset($_POST['notice_content'])) {) |; ^3 W& t, v
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));, z" l8 _) }7 K: O+ R ~
} i) Y1 ^7 v1 p
}( e1 E" g& b3 C/ M- Y4 x9 @
```
3 L) W5 |( }; }8 a( I3 T6 J7 _1 _) m% N
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
1 O6 K& f0 t& c, j3 g" v" O7 B; I. M
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
# ~6 k* @+ I! G0 j. B! P; R8 d+ v8 I I, _& \+ ~( e) k: w
```, d: S: \- i9 [8 i B9 r
$args = array(3 r2 a5 l1 I9 v; O/ e4 a
'post_type' => 'site-wide-notices',
2 V$ V5 f$ E* [5 g( D3 [1 I 'posts_per_page' => 3,/ Z L( a) d" v: U* o; H
'order' => 'DESC',
2 x: p4 @" O- i% d# D 'orderby' => 'date'. d* n9 f5 s: Y7 V
);
/ n: l8 n7 a" |- g6 C$ S/ G) x $query = new WP_Query($args);
8 Y7 N; q/ @" |' Y if ($query->have_posts()) :+ `" K/ k+ @4 o2 x7 }9 e
while ($query->have_posts()) : $query->the_post(); ?>
3 ?3 }2 d( @6 d/ P5 b( b, G <div class="notice">
2 U) n! h* I; e3 }& L: u8 q4 Z <h3><?php the_title(); ?></h3>
3 ]; U$ D$ S- Z) q5 x" j1 o <div class="notice-content"><?php the_content(); ?></div>
! X/ h+ d5 e6 x. e' L </div>( ?* S8 T, r4 l
<?php endwhile;
& ~ M$ C( J7 E& N* R5 Q i" V' E wp_reset_postdata();' M! T8 c4 z2 G
endif;
3 m' v/ n8 ]4 L" ]( Y: [ ```: J2 ~- x7 f5 q' |+ Y% F
" {+ E7 |+ ^# c! o
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|