|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?- g* L8 B2 s- N1 e) f7 M4 \
$ p' C! G/ H: \) o2 ]( o8 ~1 E7 ^
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。3 N" w; L+ y$ k
, r' W6 b/ r8 K& V# f7 Y
以下是创建自定义插件的步骤:
9 o1 A: P- N+ a N) |0 G2 k
) x& Q* r2 f+ w$ S* X5 u% U1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:1 U3 Q% L# [; V: h' ], W5 t w6 ~( D
9 ^& }8 Y1 p. b- Q7 l% Y
```
; O# f" M, M6 F9 @5 B. u) L# K: t <?php
0 A, n1 _! x5 P C I1 d( z /*
1 T& V$ B2 c7 B. ~2 H Plugin Name: Site Wide Notices Plugin
, d- c+ n# }2 n4 M4 O8 y4 m Description: Adds a new custom post type for site-wide notices.6 r( d: q* j5 i5 k1 d
Version: 1.02 x. d2 B A6 r C0 a5 K% k7 t
Author: Your Name
' P' u9 g& D9 K1 |: X5 C( {: y! G9 U Author URI: http://example.com
8 U9 O* I5 f! e D9 J* x i */
+ d* r2 j( r |9 f9 w) Z" L9 r( i% {; g
// Add plugin code here.../ y2 D) H" j' V
```4 V7 T- z# h2 [$ \% q) k
7 P% O0 ?/ d8 E6 R, l$ A3 @/ s( m: | 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
6 S7 I$ E) C" ~$ m2 d$ x6 n! }- ]8 j7 E: Q4 h# b3 n
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
) u, m2 H# v) M- ]/ T5 N8 M( \1 U' t6 Q$ i% i6 p% G7 z
```
' b5 o8 y& B8 s( l6 P add_action('init', 'create_custom_post_type');, |4 `" j, _* Z$ `+ T% w" k
function create_custom_post_type() {
. y) G: H) p' h$ S( w' T1 a $labels = array(
0 s. }7 x q: s$ _+ k) J 'name' => 'Site Wide Notices',
+ F( z1 @3 D/ M& T+ W 'singular_name' => 'Site Wide Notice',) V9 f8 W" u8 `. a2 {9 e! ]* O5 h& B
'add_new' => 'Add New',2 Z* g" Y" N v+ t! j9 G+ p+ y
'add_new_item' => 'Add New Site Wide Notice',9 I2 k* w# R3 ^7 ^
'edit_item' => 'Edit Site Wide Notice',
: G! { T5 u6 w 'new_item' => 'New Site Wide Notice',6 _" K& \' q% }! }, c+ i; R
'view_item' => 'View Site Wide Notice',
5 I$ I/ V I$ r9 N9 e: x 'search_items' => 'Search Site Wide Notices',4 p, p: w6 f" R
'not_found' => 'No site-wide notices found',& t2 S+ q+ F" s0 l: Y( e
'not_found_in_trash' => 'No site-wide notices found in trash') B4 V& }7 W- V
);
$ \/ e2 G$ u8 O- Z0 {2 {2 e) R9 E6 L6 A0 b' g; W7 D5 m+ |! l# q
$args = array(
) G* D& `) k5 d. o# E* Q% { 'labels' => $labels,
, C8 q3 z6 j1 @" d5 ^: L' z 'public' => true,
* f j; D% Q: l3 m$ h 'has_archive' => true,
, b# x( b0 @( g4 F 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),. ?- F; {5 T8 C/ j0 j6 u, E
'taxonomies' => array('category', 'post_tag'),( p; M; u* P7 t- N9 j
'menu_icon' => 'dashicons-megaphone',3 {$ w2 [1 v. G2 `. C. H
'menu_position' => 5,+ C( B2 X, @9 d) [7 ~
'rewrite' => array('slug' => 'site-wide-notices')
, m# Q* M" e4 X( R );4 X1 p/ t' Z- A3 P. q/ i" U8 \6 B% P
6 H! G# p5 E6 a6 Z. {6 _. W" N
register_post_type('site-wide-notices', $args);3 e/ J. `2 h& H2 r; C& b
}
4 c, l6 a4 s. {, R1 j, Q0 m ```+ h- A; @6 q# ~* X5 n( S
6 Z! Y$ H! \- J' v9 h. Q 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
3 Y3 M+ e* y. b- B2 G' ]) J8 Y( \7 P1 `6 v6 U5 d: q
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
1 `) V; w+ T1 r4 {! ^9 O! N4 r7 ~% T2 R( u/ k$ j
```
, d/ A1 b& D. X+ ~ add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
3 g% p; c; r3 E, D function add_site_wide_notices_boxes() {! U$ ?3 c( e/ \4 m9 H9 [
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
1 d8 N6 F6 U9 p }
1 h& g( J' `- J( N9 p4 _5 p" ~( u# g) B" l
function notice_details_meta_box($post) {
7 w) k9 s( X! |0 [ E wp_nonce_field(basename(__FILE__), 'notices_nonce');
7 y7 t, z! O' e& b $notice_title = get_post_meta($post->ID, 'notice_title', true);
4 z2 V( x# J. Z' G, N5 b* A $notice_content = get_post_meta($post->ID, 'notice_content', true);
2 j4 g$ h; h+ @ ?>$ Z% C3 ^6 L3 i3 k0 F
<p>8 y) H* G8 l3 h2 m) M" U
<label for="notice-title">Notice Title</label><br>3 b4 }) B. i/ ]: Q
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
( l6 V z; a$ T </p>1 u k& T) C$ q* `. o5 E
<p>7 G( K% O* m2 n* `( ~, L0 O, G9 f
<label for="notice-content">Notice Content</label><br>
, m t7 n$ l. ^1 O, c1 t <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>9 x8 i, V1 n2 A/ O$ A, n) E
</p>1 r8 @2 r, ?6 q, A1 D
<?php
' v0 }! j2 H9 ~( h, J }
6 v( G( H9 T7 N9 f7 ~2 q8 X1 ~0 \; Z# l K; B
add_action('save_post', 'save_site_wide_notice_meta_box');
Y. j7 t, x6 J X9 R, p# Y function save_site_wide_notice_meta_box($post_id) {
, ~+ `1 y9 I' A0 @ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
4 N) G* C5 G4 S2 ^ return;( h' ~5 S( d+ x" a* p0 y) X" f! x8 q
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)/ Z7 ?) h' F3 ?, y5 @
return;
4 J# p* @3 e+ a. A- M4 f5 W8 ]2 Q$ }5 A* w
if (isset($_POST['notice_title'])) {
6 }& @9 O; M3 x$ N @8 f update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
2 A2 t5 o9 P: Z3 x1 V g }
/ K0 d3 B+ A/ Y$ Z if (isset($_POST['notice_content'])) {
* |# q0 c$ O0 l; V/ g: A update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));# S4 m7 k, l2 a8 P' T0 E6 x, [
}3 O. ?0 |2 q6 x' V
}0 }! E3 r9 }6 m$ \ P0 r3 e
```
1 V$ W% x, ~+ f3 u$ B! E# Q; W; q/ @3 e3 q7 ?' ^. i2 f% n
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
+ _6 {$ P' l" B+ m9 Y4 P2 @, l( ]1 D* Y" }7 t
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
]( b# ]: E+ q$ |; g! J/ l6 ]; z' {+ t; ]9 J
```
$ z/ Q" [8 n$ }0 x+ C9 | $args = array(% O" c2 c' a) T- s
'post_type' => 'site-wide-notices',- P+ e* S$ s, n+ l
'posts_per_page' => 3,9 d" i+ X4 f1 \ p; V; f# [2 }
'order' => 'DESC',
* H6 ?8 S, P7 X$ E( k' w 'orderby' => 'date'
8 E3 W4 T) v7 a7 m5 t' T9 P );
0 N4 b' t$ K6 l9 S" A0 p/ t $query = new WP_Query($args);
9 R' L& ]8 P5 L" V2 o$ H if ($query->have_posts()) :
( W* }" Q0 P# S1 H while ($query->have_posts()) : $query->the_post(); ?>
- K, B2 h B3 l! j O: I. r <div class="notice">
$ f" }5 }& y0 G3 R4 G4 L <h3><?php the_title(); ?></h3>
5 x; t' j/ V" w% R2 b <div class="notice-content"><?php the_content(); ?></div>+ Y. u5 M. G, X$ @6 `3 L
</div>* @7 j; T1 I P( ]) f
<?php endwhile;
# r2 N5 x/ S8 g( e' Q wp_reset_postdata();
2 _9 R7 o" W' N5 q, _; s. o endif;. {7 O1 n9 a" a8 O: f
```# ?* v5 u/ ?, r
4 [* f3 a% z! g
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|