|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?% Z1 q; B$ [# b8 h0 n2 E/ c
( |* r5 \9 o( Y; G如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。; M$ |$ N5 X1 P+ k
- |3 `- [% n0 _; ?2 z. \% J
以下是创建自定义插件的步骤:. e- m' P, \2 N& J7 g1 _
5 h& G7 ]; k- C; A$ Z1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:! s+ s# B- t% t
3 y: C; l2 k/ f8 t* D
```+ ?, E3 q G j$ |5 ~
<?php
- r) D3 u0 r( X/ |# M) }0 D /*3 m! u) K% w3 b' A
Plugin Name: Site Wide Notices Plugin
$ _( k/ Z* B9 e, g Description: Adds a new custom post type for site-wide notices.% p+ ^/ L" {4 k% h
Version: 1.0
. f$ G$ J$ n5 S( q5 v1 @, X Author: Your Name8 s9 H! h- f% v) h) r) g
Author URI: http://example.com
; a3 y# m0 x Y. i' l8 P */
" d/ r, C# B' G$ \. C' X# W9 ^( x3 B9 j" X
// Add plugin code here...
J% f2 l8 P6 R# b- o; x ```; q1 O& T0 s5 v0 f0 q
/ L& F$ R! M2 {' ]1 `! j( ~! r 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
8 l, A- n8 ]; `3 n* ^6 ?! ^
1 k3 u& s, \" i8 I9 b2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:9 s% n1 A4 h( ^* D( U0 z
: x. c) {& a8 { Z( o6 G( C ```' u1 L1 L1 Q7 q0 T& B1 r
add_action('init', 'create_custom_post_type');9 L- n8 M, Y8 b/ C( f M& R
function create_custom_post_type() {! s4 q2 z1 n1 R7 }1 A1 d
$labels = array(
, W7 K7 y+ T: N1 J 'name' => 'Site Wide Notices',
5 p2 K2 }3 j! L1 J0 n 'singular_name' => 'Site Wide Notice',
! p% o! A' e8 X$ S: U; {. d 'add_new' => 'Add New',8 s O4 }, W& J9 x
'add_new_item' => 'Add New Site Wide Notice',
" P9 Q) m; P! y& D& K 'edit_item' => 'Edit Site Wide Notice',
. W/ n, J1 C% K4 d" E2 k! ^: } 'new_item' => 'New Site Wide Notice',
i9 y8 `+ b, Y; b* Z 'view_item' => 'View Site Wide Notice',9 }9 s/ r# P6 T9 Z* X
'search_items' => 'Search Site Wide Notices',4 V4 c* ?9 w6 h9 r$ q+ o
'not_found' => 'No site-wide notices found',
7 j( O6 B* E2 y7 q p. H 'not_found_in_trash' => 'No site-wide notices found in trash'
9 P3 S6 b* ~. K: L" S/ @0 q* D0 V );
O+ ^+ ^" o% ~. i/ [0 l' D2 z# {# t" a5 S
$args = array(" a6 u9 T) T/ d' G7 ~! L
'labels' => $labels,
3 }! V1 D' F/ l+ v3 L0 O 'public' => true,8 n v; V- f, C: u7 e( k' I
'has_archive' => true,
0 I& U$ i. b0 L 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
2 p) Y" E" t# B. ~ 'taxonomies' => array('category', 'post_tag'),
/ e; B$ ]+ ^5 H# ?% h, A 'menu_icon' => 'dashicons-megaphone',& \2 H0 h/ Z5 r% f6 q7 k# L/ a
'menu_position' => 5,% B- U+ o( |5 ~+ ?% R% x: a5 f
'rewrite' => array('slug' => 'site-wide-notices')
2 w' P' B# O6 L$ X0 w, K );6 t/ H1 J" w: @4 G) _# x4 ]% Z
2 S9 E Q% T0 ^( W6 U
register_post_type('site-wide-notices', $args);% j" [8 B: x. y. e: P: p
}
& B6 t/ P9 y; _ ```9 j3 |5 N ], j! `$ N4 l
5 ~. u2 _* W- l8 z( G+ Q, c 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。: c J% e- f- V9 H( ^
3 |8 e. e/ T/ l: {3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
$ |' O+ f$ A* p4 i% ?; M1 j) _
1 i ]$ ?" V! `. M, | ```
4 i9 d0 i' j* v. I0 @! j v P add_action('add_meta_boxes', 'add_site_wide_notices_boxes');4 [- c6 V; S+ J
function add_site_wide_notices_boxes() {" T0 @ z2 @ D7 n' _
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');$ C% X3 V" J/ K* _" w
}$ b+ `6 s! |5 }/ }& m9 B. P
1 B5 B% D3 o1 L$ Y( B4 w function notice_details_meta_box($post) {5 i: A5 B( y* d" D
wp_nonce_field(basename(__FILE__), 'notices_nonce');
% u+ j4 x/ I9 B7 ^9 H0 e# N2 I $notice_title = get_post_meta($post->ID, 'notice_title', true);
- b# N" u1 }# v( F7 s# D6 V K $notice_content = get_post_meta($post->ID, 'notice_content', true);+ m; L1 o8 R& [- S
?>9 {5 m: y/ c. t- m8 J; l, ~5 p5 Z0 d
<p>
0 b- A' N4 G$ U9 Y; ]( w% [ <label for="notice-title">Notice Title</label><br>) a0 R) R# D7 L5 O6 T7 Q$ N" y. {! C# C
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
+ r1 Y4 W9 |: r: x3 H1 ^ </p>
9 S# u" }! o% h2 t' U' ` <p>
! A- f, j' I4 \6 a* y <label for="notice-content">Notice Content</label><br>- m2 S9 t' R* y+ M1 F4 W0 m
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
' u" ] o# S7 n8 B+ O: b5 i </p>) b' {/ M8 L( m& K
<?php
) C: ?& y+ d @- h( h! |$ ] } i3 \/ p1 k2 I7 q8 r6 L+ l
F8 f3 ]/ b. O
add_action('save_post', 'save_site_wide_notice_meta_box');
- U" u/ E6 q( w( O- t# ~ function save_site_wide_notice_meta_box($post_id) { Q5 y; @# i( d$ j6 ?) [% p
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))8 K' m8 U, O% A3 Q, n% S
return;( \; s6 {$ A S. m" L9 J, T% p
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
L) j! B- `( G" J0 U, x return;# |! W4 N/ f% H, F3 _, X
# f) m! Y6 R- L3 m& u. H4 u" P$ J
if (isset($_POST['notice_title'])) {7 y1 F8 ]9 ~. |5 V0 L
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));2 v% H2 Q( b, i/ G6 A Q& S, U! b
}6 f! e2 z+ j1 e# U& l
if (isset($_POST['notice_content'])) {$ E# s- n1 |7 t' |8 G3 f
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));* ^! W- s$ C' M( x$ \
}
8 l ?0 [# y; h$ O$ G' K }
( u. M1 m \. c$ `) Z2 Y8 E ```
: D) Z* j0 w% z( O' {1 V- b; I1 L W( y- P7 q) s
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。1 [ V6 u" d; n- D! n+ ~, H+ X! p
( F# ~; H, Z/ k0 S/ q4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:7 l! j% K' l2 P
8 \& E" i, O+ R6 p3 L
```
& ]& k6 ^' P0 {+ \ $args = array(
/ ]! Q0 \, D% i+ q' J: f 'post_type' => 'site-wide-notices',
8 I L5 c) q" o- V) k3 } L7 r& C 'posts_per_page' => 3,% \$ g# X/ u4 W. R4 H0 s# A, Z
'order' => 'DESC',/ \- r3 Y( O5 [- W/ t8 n
'orderby' => 'date'& T1 ]. h* R, p9 y& H
);! ]# h- w s2 V0 g" T6 F
$query = new WP_Query($args); e) X# w$ t7 k! [3 }
if ($query->have_posts()) :
: Q Y1 F% J: H3 D# W0 i5 n while ($query->have_posts()) : $query->the_post(); ?>
, E% z* c9 n5 g) c! y' I: p; D0 }1 T <div class="notice">
8 M( H7 M) v! D, E9 C <h3><?php the_title(); ?></h3>
; Q, ^. c- c, p. d <div class="notice-content"><?php the_content(); ?></div>
: O9 W8 X L# z2 l1 \ </div>
$ x) m5 } K# m; R' K2 T# Y <?php endwhile;5 n+ y0 f0 H- [9 g L
wp_reset_postdata();
$ y0 V3 { z ?. c: _# D/ T4 P endif;
6 z$ t" o, P( U) F" l+ r ```7 U1 r% i8 e5 b* Q `, i
* G" G7 ]+ h9 Z1 m- ? 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|