|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?. @7 H+ y, A- C1 P% J$ z
Z: Y6 ~8 P' V; T( j" l如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
9 V4 x* h$ R* I9 M: e$ b: F) j2 n. I- h' P
以下是创建自定义插件的步骤:
5 I" B" Z: k( A' k% A& a
$ K: P; P$ Q5 z/ C; [1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
8 b2 s1 V- F6 j& n& n% a( P/ T/ u. p2 n8 z
```' ^$ v" r _2 ?& q" E
<?php2 ^" [* V( d9 R. s1 {
/*2 \# }8 q; _# B
Plugin Name: Site Wide Notices Plugin
. _ e+ _) m6 @' Y g4 V$ q0 ^4 e Description: Adds a new custom post type for site-wide notices.. U9 t; Y3 L* ]
Version: 1.09 \, A/ O$ n& P- c
Author: Your Name h: i' a, T, J! z" ^( P. l
Author URI: http://example.com K" j7 L, i/ N4 @% v. m: M
*/
! v h4 {& G2 g3 {' B& g |# `# T: G6 Y+ d$ l
// Add plugin code here...
) ~# L$ ?; C9 W ```& y1 B: V6 s- O1 e$ m ~
; u' w/ {4 ~3 U, h 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
/ M/ h/ d& _8 ^2 F5 r5 W- ~# z+ t$ Q+ I
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:( [# Y7 n9 }& S; x
$ _ t7 i9 ~! ]: r1 W5 ?+ m8 S3 V7 u ```
# Y0 w8 S. I1 s+ @6 y add_action('init', 'create_custom_post_type');. d! p) |% \, D$ j3 l( R4 |- g/ z
function create_custom_post_type() {4 R' ?- w1 L9 r% _* I# T8 ~! c
$labels = array(
1 z1 x; g; [+ [ 'name' => 'Site Wide Notices',
8 h( ?8 L: k0 { 'singular_name' => 'Site Wide Notice',
8 p+ a7 H, }( s; U2 Z 'add_new' => 'Add New',
( V6 M& M' u& ]$ E- |% I% _4 \ 'add_new_item' => 'Add New Site Wide Notice',$ f; q1 v \5 X/ I+ s0 C& k: ^0 _
'edit_item' => 'Edit Site Wide Notice',
. |& s: c' [6 A- P0 u4 @6 j 'new_item' => 'New Site Wide Notice',* n8 r+ r" F+ H' u/ o1 K" K# Q S3 p& s
'view_item' => 'View Site Wide Notice',7 H. Q: ^! f7 E' j. O
'search_items' => 'Search Site Wide Notices',2 U; T; ^" {& [% m0 ?8 h
'not_found' => 'No site-wide notices found',
' t5 I& H8 {4 D! k" t0 z 'not_found_in_trash' => 'No site-wide notices found in trash'+ l. T0 A+ d, C/ X
);) b* _# S8 [( q
# G. Y" Y/ J2 u $args = array(! j1 n Z8 Z, T: {
'labels' => $labels,
. d% V7 e; J8 m+ T! Z 'public' => true,$ F/ t5 J4 E; A3 }
'has_archive' => true,3 K8 P z/ q/ v1 `4 Z. ^9 P
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
/ P9 T$ ]0 B* a* N$ S# u 'taxonomies' => array('category', 'post_tag'),
# x) x8 K7 _ ~2 s' z 'menu_icon' => 'dashicons-megaphone',$ w# ]; D& I4 @, i9 J
'menu_position' => 5,
% I; p; c1 w) v3 M# r 'rewrite' => array('slug' => 'site-wide-notices')+ Q' d5 Y3 @: C \
);
* o0 {' G, _$ P* }! `
) v' K! X: T! A register_post_type('site-wide-notices', $args);
+ Q% m- W8 q+ |" j }; ^0 g# U* d3 C4 B' t6 m& S
```
- [, c- L# o f5 n% B9 L5 X: d& |8 Y1 V3 M6 ~1 w5 Y! A4 f
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。( e0 B+ q+ t' Z! R3 j
- X9 P$ }: x! g) }2 \2 k2 V( B
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:8 i8 u7 r$ z! W4 r; D
' [$ D6 ]8 Y, _% @6 _( z" i
```
% o! S4 b. k. }- N* p& P5 z" W add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
1 j! h5 M/ c: j# i6 N function add_site_wide_notices_boxes() {/ Y) a" ?7 p* A# N1 b- i
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');; f j% y8 u) { [: `
}
3 X/ H* y6 T* d* m, Y
6 g: d" S/ v& D6 q% | function notice_details_meta_box($post) {# ]* Y$ v$ }- {5 o6 \
wp_nonce_field(basename(__FILE__), 'notices_nonce');
% `* Q4 k! x+ P7 |) A6 G $notice_title = get_post_meta($post->ID, 'notice_title', true);
8 e% y: E, a% p $notice_content = get_post_meta($post->ID, 'notice_content', true);% |4 D& E/ b7 u2 f: l& u0 N
?> w5 P, D& r2 Q0 ?! W3 h, t/ ^! Z
<p>
9 ?5 S& W3 x& R3 @ <label for="notice-title">Notice Title</label><br>6 O5 o0 R/ s6 H5 ^
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">1 V4 l0 i$ E9 ]; ^& `
</p>
# I8 Y: c& I. Y' o% b. d# J" L <p>
9 k2 R4 O% r0 E( i3 w v- C2 u: J <label for="notice-content">Notice Content</label><br>' I+ V3 w; V& W$ ?7 c
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?> Z# y ~7 G- q2 V$ C* I
</p>9 [0 o8 }3 E# c+ u7 `
<?php0 m7 p+ i4 V" G! h, F# `# ~
}
: U4 S! Q+ q$ W0 z4 @0 Y
: N! N+ }. k' s! u8 c4 ~ add_action('save_post', 'save_site_wide_notice_meta_box');/ L) \* [7 q+ [7 ^/ G8 w8 V
function save_site_wide_notice_meta_box($post_id) {
% `& B7 E2 r5 Z5 Q h& k( x if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))8 K+ P* V; N. W; d% i5 M9 ]- l
return;
: f( A6 z$ E' g2 O if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
7 N$ m6 g. s4 q; }* r, ]2 s return;
# x# N: c1 @: A! r& Q$ A; c
$ Y7 U [2 y7 k& E1 f$ W if (isset($_POST['notice_title'])) {
4 k/ X: ?2 U9 Z! Y update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));* K) e3 w- A( Y4 e' k2 e
}
h# H- `7 S4 H if (isset($_POST['notice_content'])) {
4 c& c) Z/ v0 F/ C- g3 f update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
) c. k7 |" a. |% l }+ G& G8 `: Z# v' y8 \
}" @" P- P$ a- t( H
```
/ `5 ]% y& V2 j G0 P6 e. K) w' ~6 g) ~4 Q
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。9 g: t! V3 m4 \( ~
' U, k G# O' _4 ^. ?+ W
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:- ^3 q2 f, ? G% J# K9 b% h: m
9 a' ]: ~# C0 V0 }7 }
```
; C5 F2 X) ^7 j! r1 Z' X $args = array(
7 z+ o8 G6 J1 u2 G0 Y 'post_type' => 'site-wide-notices',: D- C% ?0 j+ V+ ?
'posts_per_page' => 3,6 U) b+ U; k; Z: V1 f& y3 M
'order' => 'DESC',$ l" b7 ~3 {- d8 V5 R- w& s
'orderby' => 'date'
. t& J1 N2 G: M! u, J );; N; m2 N3 l' E! v: Q
$query = new WP_Query($args);2 y9 J1 m+ a9 c
if ($query->have_posts()) :
9 h# [2 G9 i8 |3 i2 E/ s& t4 z' | while ($query->have_posts()) : $query->the_post(); ?>4 W2 F+ `( c+ H1 c
<div class="notice">. B, }2 }4 c3 m5 R- r5 M& O4 }
<h3><?php the_title(); ?></h3>- o. y" I; D w0 S6 C8 k
<div class="notice-content"><?php the_content(); ?></div>- R3 x. u& G/ M3 J
</div>
* X: Z) L! t" m9 U4 r q. e <?php endwhile;
2 S" f' f/ n8 h o& f4 T3 i wp_reset_postdata();1 H' ^& T/ O3 j; k3 D0 d( {
endif;
$ k& j- h3 }7 ^ e) M* [5 N+ c# I ```3 o* L: J" d5 Q8 U* _7 a
& j8 h8 B1 T0 r/ k+ ?3 j 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|