|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?" o$ H& X9 G% v* k! L7 T& ]
9 [) v8 X- x( M" I如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。$ a L) V+ `2 E
+ O0 P7 o8 n2 i; @
以下是创建自定义插件的步骤:" V8 p) G( o( c7 \8 T1 U" P
" c! h$ R0 b4 J( l
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
/ I" Y. [; G' |+ m8 ~4 e4 N; Q% M7 a! `: e
```4 }7 C4 z6 M: W0 K
<?php
+ z$ Q5 F( I! Y& u j& p2 d* E b /*
1 r1 V+ O6 k# e Plugin Name: Site Wide Notices Plugin
5 T0 ?+ y9 Y6 i/ ?9 M! J* X6 B% Q Description: Adds a new custom post type for site-wide notices.. T" S0 M* n- m, l7 u/ n9 f( E T
Version: 1.02 i/ g& D$ N& y J. Q( [5 k
Author: Your Name
& T" A3 R6 P, L1 _# T E2 K Author URI: http://example.com
3 V- d8 t; k9 C# E; a, O */' Q" a# V" T- j/ _% Z
4 k( X* U) O& y5 L& a
// Add plugin code here...
* @$ o) X, Q0 |3 E0 K! m: T ```- k3 W0 e: |/ z, x5 \
$ ~) z0 z5 J) H" y7 q p 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。) W$ N0 M6 ~+ q k% [8 ~. w
( ]6 `4 _5 X. E2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:. q! ]9 T* _( _* v/ h9 P7 j
r% e2 y6 W3 z) s. C: ] ```
5 B3 S/ R" n* B, M/ O add_action('init', 'create_custom_post_type');
" R4 @0 ^: }, C; x, g function create_custom_post_type() {
: s, z0 ] J: U2 u2 n3 p2 m9 b $labels = array(
9 C6 C$ h6 E- ]- Y" k' b+ b. P2 A# s 'name' => 'Site Wide Notices',
; c9 R+ u0 x* U1 D4 u ~& h 'singular_name' => 'Site Wide Notice', N$ m6 m7 c t$ d
'add_new' => 'Add New',1 R1 s( D0 o5 N! d) ^/ o" @
'add_new_item' => 'Add New Site Wide Notice',1 `% k5 i2 Y0 f8 p, f
'edit_item' => 'Edit Site Wide Notice',, U0 o8 E* ?6 l0 }
'new_item' => 'New Site Wide Notice',
1 i2 q" B7 T2 o 'view_item' => 'View Site Wide Notice',
3 u3 E# {. T! M i& _ 'search_items' => 'Search Site Wide Notices',! u% ~+ d. W4 v3 T, O! l+ i$ m5 I/ h
'not_found' => 'No site-wide notices found',. D' ~. J& D' O y H0 j0 _
'not_found_in_trash' => 'No site-wide notices found in trash'
/ B" w0 g* w# a- I! n );
9 Z Q& A6 s3 S9 @ i$ g8 d* |9 O e# `9 o7 R1 R- l
$args = array(
0 n1 i* R6 {* x4 w 'labels' => $labels,) P; a& e5 p2 a6 i4 C
'public' => true,
0 C/ l. e% F0 q" \6 a 'has_archive' => true,
) a0 p$ [8 |8 `- a4 T 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
* s; @8 E9 W8 J+ T2 K$ h1 o6 h6 D" q+ K 'taxonomies' => array('category', 'post_tag'),# O/ P) N6 k* {8 N* @) ~! X
'menu_icon' => 'dashicons-megaphone',) Y" z- Y+ X! l
'menu_position' => 5,
/ m6 L! d: y3 m* t5 x L 'rewrite' => array('slug' => 'site-wide-notices')
0 k- T6 p8 [9 b* L7 o );5 n, ~: y3 |! U' E
% T5 O8 s& N# l- y+ e
register_post_type('site-wide-notices', $args);+ C8 R* P# U. i; K% u
}3 B" S# z9 u. p. c7 y$ f" e
```7 R8 z$ i3 I8 M# G1 a0 ~
. Q" I- M2 P# ~0 s# T; v3 o. Z/ L5 E
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
. r/ D/ @; a' ^9 ^5 P
0 S5 c4 Q( }/ a3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
; D/ z) K0 L$ v$ l/ H& O3 ~1 ?
```
8 n' s- U) P# U add_action('add_meta_boxes', 'add_site_wide_notices_boxes');: y8 }4 g1 [4 V5 Y( I: C
function add_site_wide_notices_boxes() {2 T. Y" ?1 Q* q: w/ H
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
/ W& ~3 `, j* w1 R* n' W- Z }% a. Q( X( p6 g& q
7 o& b& N# u7 i. m+ O
function notice_details_meta_box($post) {0 X, R6 S, v2 u6 u
wp_nonce_field(basename(__FILE__), 'notices_nonce');
$ t% e! f; l% \+ f: N3 u, x8 M $notice_title = get_post_meta($post->ID, 'notice_title', true);
1 n- a4 o- ~! I7 |1 P $notice_content = get_post_meta($post->ID, 'notice_content', true);+ K' [. h' \4 @- j7 H
?>; o9 l. c5 }. q+ m5 E
<p>
; K" z0 v0 B2 a! c7 L$ {8 ^ <label for="notice-title">Notice Title</label><br>" m: j( A" O4 s' e" D6 R
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
7 Q2 E3 g& N* s; j3 _2 Y$ K </p>
; g9 J1 {1 }5 U' L0 W9 I <p>
8 x- V5 T. @% A5 W7 f <label for="notice-content">Notice Content</label><br>
6 [ _) {# |+ B% W/ l4 C. W <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
/ ?5 l) ^: a" g8 y8 O </p>
# H' m0 J* c3 J/ r& u4 O0 Y! q <?php4 l. F9 y' A6 H/ C5 q
}
1 k( Q: P7 `. ~, K) D/ \; P) X; k- a% \- V1 y8 J1 c
add_action('save_post', 'save_site_wide_notice_meta_box');
) Y# _; [4 Z/ @( e function save_site_wide_notice_meta_box($post_id) {% a+ r9 u& k s" v) J/ e
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
/ d" R: D, d o* G) s4 G return;4 z6 S0 u% F) g1 f: L
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
% h- I7 n* i; A" m return;
% i6 h7 W' r2 L0 T% Q0 V" R q& w
if (isset($_POST['notice_title'])) {3 k }* {4 |0 O7 o$ {7 S
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));& A8 ]" x+ u! a; V7 h
}0 V( W# K/ f9 V+ s- ?; Q: B
if (isset($_POST['notice_content'])) {/ |2 W2 n$ P$ K8 O1 ~
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));9 I: c9 n0 p; H4 c% Y) @; L
}
7 ^, o' ~ n" |* P# q; o }
2 W8 x5 V/ G5 l6 H, C7 L' Z ```0 `8 v3 O" Y- R7 L& R# w* A
3 v0 w N, G' d5 s7 x+ V/ u* W- W
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
$ t, {& E2 F- q9 N" f8 F( k l: P/ J/ m% ~" O$ l5 ~1 V4 p9 [
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:4 r; W7 v2 f9 o2 a0 Y/ j
# d7 |; G/ ^* ^2 @! N( c
```4 L7 H3 _1 E5 t7 ^1 q
$args = array(; n6 o) |6 V) u9 K: [' V1 h" i) w
'post_type' => 'site-wide-notices',# @" |3 ^0 n( [6 ^. ^
'posts_per_page' => 3,6 I8 \; U f1 @! X, _
'order' => 'DESC',
* `/ h3 h% T& G6 S+ t$ J( ? 'orderby' => 'date'
8 f9 g" \: M& g: I1 ~" r );
* ~- W5 o1 Q; E, @6 @- ^! Y' v $query = new WP_Query($args);1 t& m5 b+ @$ I
if ($query->have_posts()) :
) a; S1 A- U* w2 \( K+ c- ` while ($query->have_posts()) : $query->the_post(); ?>, o7 r: T/ C+ S# a+ M
<div class="notice">
N# H8 A: i" c7 P( l <h3><?php the_title(); ?></h3>
+ Q+ v" B1 Z0 u8 r3 t <div class="notice-content"><?php the_content(); ?></div>: O$ u- I1 t$ `- K1 r t W, s3 l
</div>
2 F9 j. [! S% _# c* n <?php endwhile;, q- k5 i+ v- ]1 E
wp_reset_postdata();
/ u; {, z9 P X" [; P endif;2 m4 C/ l& y8 T0 i; W
```9 Q( |3 p2 y0 j' J" v3 M2 X( q7 |
: j" t) ^( X6 v8 L! _* U0 x
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|