|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
! U! C' T) q6 g7 _( K. Y9 s' j3 n3 u* f& D
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。" U+ m; M" w5 F) k' R( S
" ^# F4 h) B4 X4 s, R. u以下是创建自定义插件的步骤:. i/ H$ j6 Z% p8 l/ O2 N2 f
" |+ r8 O! m# _# M5 n, p1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:( `2 w. c0 N+ a3 T# e c; `
5 F- d+ N8 P/ f, u7 }3 U/ T5 Z ```& [7 K8 \5 E6 X! I2 G' \
<?php
# o# H7 a- s5 q t( Q3 l$ Z- w /*
9 \/ W& e; q8 X% m; T- N, ? Plugin Name: Site Wide Notices Plugin( j3 D' D: C& E2 o# |: ^2 d
Description: Adds a new custom post type for site-wide notices.
! q$ m" `, ?4 E) W5 | Version: 1.0
! r! Z: ] y: @+ `" d( C Author: Your Name
' _4 V4 j4 ]% C# `$ Q' c Author URI: http://example.com7 O0 @' }* S5 p8 G
*/
& p* E0 O1 i- T) V! W, i. f4 x X! |' r( ?( T5 ]
// Add plugin code here... ]% B) j, r- z0 Q+ f* R& ]
```
1 m% A3 ^5 y- w& \7 J# t V+ b
- N S2 ^- j; @' b 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
; f$ b: ], a. ~0 ]. @% R6 R6 \1 x( m' J; f. [' U
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
0 [1 A: j" p- M8 x& E5 W# b9 I1 m6 F" i; Y6 `1 h1 g5 c
```
' |( I8 T( ~! c2 d+ b add_action('init', 'create_custom_post_type');# p _7 [2 n- O# F2 y$ r
function create_custom_post_type() {
% y5 O8 L( V- a4 T6 O $labels = array( X. ~7 `0 h$ x: R
'name' => 'Site Wide Notices',
4 k# Z1 q9 P8 E+ D 'singular_name' => 'Site Wide Notice', T5 r. ^/ x9 U5 b& F+ s# O! J
'add_new' => 'Add New',6 }8 Q5 B+ D1 U9 e7 i, J; P/ ~
'add_new_item' => 'Add New Site Wide Notice',
, L" f. @9 k9 d" K4 k 'edit_item' => 'Edit Site Wide Notice',
+ i& k) c+ _ X) I% F1 C9 E 'new_item' => 'New Site Wide Notice',' N5 _6 m' M0 v0 j- h
'view_item' => 'View Site Wide Notice',
: j; |% m {6 g- b% ]; O 'search_items' => 'Search Site Wide Notices',8 G2 {- Z- {! e+ a. A
'not_found' => 'No site-wide notices found',' L0 |: J, m* B+ r, k
'not_found_in_trash' => 'No site-wide notices found in trash'- I3 x$ Y) ^$ T6 }' t6 k: m' Q$ g+ j
);& n3 D9 D n9 H2 p3 z( R0 y5 W
% i9 A& K) q$ H% c& x5 W. \
$args = array(4 h; Y) b3 t( H% O7 E2 l$ }
'labels' => $labels,
% ?$ t. p' i8 k( c) Y8 @ 'public' => true,4 x1 L( r2 K0 f/ F) g* g# o Q
'has_archive' => true,
6 k3 |( u4 a3 \" F" N0 B 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
2 a8 h) ^9 J1 t$ |# L/ j 'taxonomies' => array('category', 'post_tag'),7 `9 {4 {, c4 m; [5 V9 o( t2 s# [
'menu_icon' => 'dashicons-megaphone',3 c' m6 v5 o. o$ c( Y
'menu_position' => 5,
- z) _# _* M# `5 d! r3 _ 'rewrite' => array('slug' => 'site-wide-notices')
0 ?! [" Z% W" x) c. Q );
) y7 }4 \' s0 u; s. @/ z$ s$ ]2 @- U3 z4 o- T
register_post_type('site-wide-notices', $args);5 M) g4 ]3 h- ~5 I# R3 Z2 ]$ J
}
: V2 n; l- E$ b: M$ T# ^ ```
% v9 g( n+ ]" h# p) l; s: Z) v$ g) O: Q! A, \4 h
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。. L Q) C `/ n, O5 H0 B4 L
* M3 M% B) R5 X* V6 @" N. C3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
9 O. A: z9 S* W6 t. |. q* u( i" r
! s7 p( x2 g3 v) {; u# v. n- Z/ S ```
3 V- r& r+ g, L6 d6 x& E( V add_action('add_meta_boxes', 'add_site_wide_notices_boxes');6 z5 t- [" C0 X/ f5 `# H
function add_site_wide_notices_boxes() {9 L2 w' g! O, s7 G
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
% |# h& B* U( e) a' O W4 _ }
9 y1 w" Y: [. l$ v7 \$ G9 \5 q8 }6 ~" Y5 N7 L
function notice_details_meta_box($post) {
+ W, \; h8 }# V5 M- V& c wp_nonce_field(basename(__FILE__), 'notices_nonce');
& x1 j: _( e. o9 X $notice_title = get_post_meta($post->ID, 'notice_title', true);
* W- \* F l( ]3 V $notice_content = get_post_meta($post->ID, 'notice_content', true);* o, S6 h9 a1 \. e2 H+ ]
?># s4 ^/ O) p7 @! ~5 \3 q
<p>
h& |2 J" m6 l- N <label for="notice-title">Notice Title</label><br>6 ]) G( G7 M; |# i) C0 @- C$ x7 Q
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
0 Z: k/ F2 x5 T- G* x) `4 u </p>
) a& ~0 |1 Z6 U2 D' {7 n, {3 x P <p>/ _+ M/ J3 n! Q( z
<label for="notice-content">Notice Content</label><br>
0 E* n( D6 T! H% o) W' x <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
; J4 K a# G# M7 V2 L" X </p>
; R3 e5 M. [; i" v; m <?php) E: J; W4 p4 R" ^1 `: G. n4 r4 ]0 n
}
. P `- {) h( B0 u5 O- T
* t4 ^3 p. S+ a, C/ c- M' k add_action('save_post', 'save_site_wide_notice_meta_box');
# Q, {4 _5 D4 G# w function save_site_wide_notice_meta_box($post_id) {
: H* \0 D& E! [" h if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
! [9 Z% T& ?/ O5 M* H2 p5 v return;7 m3 o5 L5 r& [7 P7 a7 r% h5 D
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
5 J9 h' Y/ t: E6 X0 U return;
8 B9 P) c# p: E% ` d% w/ T& c& s; S) s8 C; C4 Q
if (isset($_POST['notice_title'])) {) N- c9 j/ V( }+ W b" G7 o4 F
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));# o! Z Z2 `% ?2 o) |, B- A, _. n
}
5 d4 y0 A) }) C7 D3 u7 A7 Y5 v7 d if (isset($_POST['notice_content'])) {
( z5 a! [! w; m3 O5 r9 M update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));7 ]/ g; K" o* y" C# I: I6 C
}
3 Q. E1 T, i, n% X% ~4 @ }
( f( E( Z4 R" \5 g* K6 S" h ```# R2 m) @$ S7 L5 J* n. N7 {+ d
) W% s0 a9 }+ T: ^- K" f- Q4 X
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
# d8 ?& J9 W" h1 z4 ]0 I# g+ v7 ]; S; b; d! q9 u
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
6 {( S2 c" ?6 q, W3 @& m9 W& P# Y. T
$ \% Z5 A( k: t: q1 x8 G0 m ```
% c4 M5 n; @- {. M1 B$ b $args = array(( }9 u! t1 [/ }
'post_type' => 'site-wide-notices',2 W! }' |; X4 H R, N
'posts_per_page' => 3,
0 P5 V0 C6 u. ~ I 'order' => 'DESC',
6 t8 e$ f% \* f9 h1 O& r' Z3 b! g 'orderby' => 'date'( x% {( `" v, _% p8 P
);9 m6 i7 X' z$ Z
$query = new WP_Query($args);
6 ?* [) d4 g' ~0 [: \ if ($query->have_posts()) :
" a1 G t$ X( l! P: E/ y, m# F+ e9 N while ($query->have_posts()) : $query->the_post(); ?>7 l# S: ^3 S( N
<div class="notice">
/ w5 ?# P$ }4 {! a) H# ` <h3><?php the_title(); ?></h3>
: p" x0 }+ u, U* x$ J <div class="notice-content"><?php the_content(); ?></div>
& |8 @1 p9 ^, T, C5 S+ S5 l </div>
* V) `& }# M& n s1 Z6 i) K <?php endwhile;
' @8 K+ H) H0 F7 P+ p* n% f wp_reset_postdata();# f2 a" {0 e7 N! ^& g
endif;
) _ m- C3 m" w0 Z) ~ ```7 t1 i/ ?$ a/ | m4 K7 ]
W: H: [0 O* V# a% I; h) D 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|