|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?. j6 T' D1 m0 W" C5 }
& C7 L9 [, q7 e0 A$ I; z
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。+ k' _; ^0 Y& Z% q7 Q
! y, G4 b0 T, x' i以下是创建自定义插件的步骤:' U& H! }+ o6 N; G
0 C8 a: r' w/ a0 \! N& v; P! z( X
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:% g- ^! [) ^& u
j/ M0 `) Q# p, H& w' B& Z
```
7 N" q2 P" J0 t# V/ ^ <?php0 \9 I! ]& Z A- w
/*7 \) [1 N' I+ B
Plugin Name: Site Wide Notices Plugin
1 l3 J# E0 i' J2 x+ h; g: [& L Description: Adds a new custom post type for site-wide notices.% }( d) \' |7 C0 K" t* A' w# Q
Version: 1.06 K3 G: ?! q2 Q' j, [2 E5 ^- c+ r+ B9 N
Author: Your Name
3 b+ x" B+ w& W& V# ]' r0 X3 A( S Author URI: http://example.com
5 C% j" S1 Q# j. j' m* y# m */
- r3 w! }6 f( A" B5 A, n# i' M- _- k* S5 v/ B5 Y8 s
// Add plugin code here..." O2 e0 G- j0 X8 c# m8 n) j
```
! n* s8 I; K& W: R* S, o, }
0 [, e& ?' F4 b+ q4 x' T. W 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
+ P" G7 y$ V$ Q; S4 U! z* I0 }( b9 @" H, n' q
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:* D7 y5 u2 h8 G, e9 Q0 ?8 O
( y+ U" s) O5 _0 `& N& z
```
) s0 ?* K U8 _1 | add_action('init', 'create_custom_post_type'); p8 p% o9 q: {- N- V8 `; f J
function create_custom_post_type() {
- R3 @4 M# s. K% o $labels = array(8 r! Z2 r6 T! e9 O1 D
'name' => 'Site Wide Notices',
" }* S$ k( Z; O$ h6 t ? 'singular_name' => 'Site Wide Notice',: v1 h4 O$ q4 x" W7 b, u4 B
'add_new' => 'Add New',8 f8 r! Q, ]% p2 p: v5 A2 H
'add_new_item' => 'Add New Site Wide Notice',! j4 M0 }! n: s0 L
'edit_item' => 'Edit Site Wide Notice',; e5 o ?7 {* t" Q1 x( f
'new_item' => 'New Site Wide Notice',- S* V% p! u8 H4 j6 W" d$ N
'view_item' => 'View Site Wide Notice',
3 I! x5 J. Z; ?- `" l 'search_items' => 'Search Site Wide Notices',. {6 y: t$ Y. l
'not_found' => 'No site-wide notices found',
% H# P5 T/ ~( _; g 'not_found_in_trash' => 'No site-wide notices found in trash'" |2 O0 n$ b3 @: ~2 E& j7 W
);
4 i5 v) o6 K/ Y9 E
1 M6 x6 d7 Z# x0 q $args = array(
# ]' o0 h8 G+ n% ^" r3 \9 W 'labels' => $labels,
8 ~1 g9 k( v9 {1 t 'public' => true,
0 E' V3 ?+ n1 Y! d2 E4 D 'has_archive' => true,: ^: e4 {5 O; U4 K6 f Z
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
9 a M. l: a2 Q, J# G+ Y 'taxonomies' => array('category', 'post_tag'),
1 F1 n B5 c) i+ g 'menu_icon' => 'dashicons-megaphone',
p* K5 Q7 N8 m 'menu_position' => 5,
1 X9 F! N8 l" B* ?$ s) O 'rewrite' => array('slug' => 'site-wide-notices'): G. I8 F# x' h4 d- U4 ]
);0 k! R8 T' T8 h3 o( t
. o7 Y2 z- s+ T3 ?5 Q, f
register_post_type('site-wide-notices', $args);# g1 B. b4 E4 _! L) N
}% Y* Z- R# R9 e4 H; U. {
```8 x* K) h' l! _/ |: R( [
) ]1 H8 H6 @) Z. A2 v
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。2 V# u2 ?: ^3 [* U
& O* G5 a f, f8 H3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
3 D/ Z, w2 v! Q. Y$ o i2 ~2 q( f8 ?( X! _* ]8 I
```
; C$ z* {6 i/ [0 ~# V' m; j2 S+ [ add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
) V6 d8 R4 ?3 w+ |. z; u7 p/ N- k6 ~ function add_site_wide_notices_boxes() {8 F }3 i+ u* i( W# g& N5 f9 y# K
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');& F B1 h& \& c
}
% }8 G% D8 B4 F3 w- f% {% A* u% P0 j. t8 L2 M
function notice_details_meta_box($post) {! m7 V# R5 g0 F0 g
wp_nonce_field(basename(__FILE__), 'notices_nonce');: d2 W6 |! {1 S: P& @1 P
$notice_title = get_post_meta($post->ID, 'notice_title', true);
9 ^5 W3 e1 D; B# e: L# q/ D $notice_content = get_post_meta($post->ID, 'notice_content', true);
* a6 |$ h3 m. a+ T ?>
, ^9 e# F8 y& M7 |1 P* t <p>* a9 o7 p) M& z! w& m
<label for="notice-title">Notice Title</label><br>0 h* U' K7 P) y
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
2 K+ ]2 n( t1 r7 Q </p>
) q* u# A$ m+ l <p>- g$ E, F, Z) c% i! e
<label for="notice-content">Notice Content</label><br>5 ^( X* R' Z3 E
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>/ c( h7 ^! Z* f/ f
</p>
6 w& T; C- x1 s% g( p% o! N <?php3 p% p0 B1 n h" r; f. d
}2 c! L4 n/ ]6 c6 W, o! S6 P1 b
" |4 C2 j- c& H! _* H) o
add_action('save_post', 'save_site_wide_notice_meta_box');$ K' @) z$ `+ p* D8 ^
function save_site_wide_notice_meta_box($post_id) {' l. Q8 t# z! r# M
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))( |0 g0 {3 Z* `5 }7 Z
return;" d! h0 x; o. M: v! y
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)- j. g/ U/ p) t, j0 L
return;8 ?5 T1 T& B( i2 H
c$ v; }' o! q$ ~5 J. J# Q1 t. \
if (isset($_POST['notice_title'])) {
& O0 B3 u8 `* B9 R- K* } update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));7 G- G4 O* ?0 q% d6 s) y
}
4 u" c3 U7 v. _* F7 G if (isset($_POST['notice_content'])) { v# z& j- C9 }) ?& z; `
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));$ T% d- ?- v* e. R+ M
}$ a) D% _# m* ~" z* t3 O3 i# g
}
$ D! f1 h4 d- n a. v ```! m; N8 n" N: Q
- \4 o% s7 B5 V( Q0 T% L 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。& |8 V+ p5 y, B* K; \0 h8 o
& Y: ^+ A5 Z) @# n3 G* ]4 ^9 p1 D
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:0 ]9 s$ J7 O: a }8 x1 i- ~
2 y/ I; N: t, g
```
( R& g+ o% w: p2 d) ] $args = array($ y) H% D2 d$ e8 g- X7 s& e
'post_type' => 'site-wide-notices',
7 T' {4 g8 Q! Z 'posts_per_page' => 3,
, b0 p# `, \+ L# u3 C; f 'order' => 'DESC',
/ `5 N0 ?# W3 T+ [ 'orderby' => 'date'% i# ?) L& H7 Z7 l2 J
);
$ E) `) w1 R9 D/ ^7 c $query = new WP_Query($args);: V2 z' E' J9 y
if ($query->have_posts()) :
/ S, z9 }7 S2 e while ($query->have_posts()) : $query->the_post(); ?>
0 o# N0 G2 O$ i) e+ K4 I/ f2 C0 }0 B <div class="notice">
% G9 P" p5 ~* @, Z- @ <h3><?php the_title(); ?></h3>
8 R9 A, {# B& w7 B" u/ ^9 Z" @3 _ <div class="notice-content"><?php the_content(); ?></div>' J4 X6 D! {" h+ e$ k2 k
</div>
6 Q2 a$ e1 @7 c1 `( U <?php endwhile;
7 v% G9 t& n; b( p! ]+ m wp_reset_postdata();% M1 f* c j& z7 I4 T
endif;
; Q- Z4 [) ]5 j ```
4 c4 ^4 [ g% _! p6 N9 S5 n
; U; S3 W5 M3 K- F7 J 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|