|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
$ I' x: K/ z8 K, n( Z0 }- [$ K- P8 ^ T( T# v. R. M- Q! D$ N$ k S
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。) T+ [& c2 ?. E& a& L0 M! N
0 C* d! ?: t2 I: V5 r( B以下是创建自定义插件的步骤:
; L0 c/ r) _% k/ q
; }' Q g, ~7 r1 y1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
: H& I6 e* G4 w8 e
; ?# z+ K+ }+ l. N1 r ```" {9 J$ O* N! O1 F S# c4 v+ f
<?php
8 |& {+ m3 Q! c$ s, z3 k) _& I) H9 a /*5 s! U2 {/ R1 w+ [
Plugin Name: Site Wide Notices Plugin
9 X) g( t1 N! L( L" G# I Description: Adds a new custom post type for site-wide notices.
; Y* C+ ~/ w; T% Q Version: 1.0
; t+ O: p- d1 a# S Author: Your Name5 Y2 u1 q) A# i$ d7 M, r
Author URI: http://example.com$ O, w/ |3 ~2 b$ q' t
*/* u) I4 O( C" r0 r' }8 |
+ g+ C: {0 i: R, b! o; D% u" m
// Add plugin code here...( x: w, Z7 }2 W: S7 ^
```5 \; `8 i) Z+ ] `/ m6 c. W
' T% x1 |( V9 [+ \ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。8 N" \0 k" N4 J3 v
4 P0 {1 C- a; H: v2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:- d: [2 a1 C7 T% X% F2 G
* W( W& X& ^# ]: r6 n' o ```
! Z' T' c) }1 h3 t8 [ add_action('init', 'create_custom_post_type');: N9 E V: w4 Q/ I$ g. }
function create_custom_post_type() {. v# F+ ^) e& Q6 F: @
$labels = array(6 ]! X& G' t! |( n. n- N2 Y8 K
'name' => 'Site Wide Notices',* L) @# j1 W6 i2 Z y+ y/ u7 |+ y2 Q( o
'singular_name' => 'Site Wide Notice',
- |1 B; i4 c% |, j 'add_new' => 'Add New',
7 a* p6 }8 m/ q( K. B 'add_new_item' => 'Add New Site Wide Notice',
. _; H( Q4 V4 P9 Z1 \+ M 'edit_item' => 'Edit Site Wide Notice',
6 N, B8 @& @4 u( ~ 'new_item' => 'New Site Wide Notice',8 K1 q$ I1 g8 v
'view_item' => 'View Site Wide Notice',+ ]8 ^3 s- R; y' b
'search_items' => 'Search Site Wide Notices',6 p, n9 _! {# J, K- F
'not_found' => 'No site-wide notices found',
( o7 ]* U) ]) s3 [& S 'not_found_in_trash' => 'No site-wide notices found in trash'
$ q g4 e- U, [& H. U );- G, P* E5 b2 G. p
6 U( \/ \, u. ~) d $args = array(
' E& t+ _: ]! s5 `; s/ P$ z 'labels' => $labels,
! `- G* y+ k8 f" e 'public' => true,
' e1 |! a/ W! `+ O0 V 'has_archive' => true,
; ?2 i# m6 t" j' K: r/ Z/ k& ` 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
- i; [2 R) x' q 'taxonomies' => array('category', 'post_tag'),
( Z A' K$ G2 s2 ?* @; r' r( I 'menu_icon' => 'dashicons-megaphone', R2 G' J) q0 w% _4 B8 n& [$ ~, a0 a( {
'menu_position' => 5,- I# K/ ~9 @- G; S
'rewrite' => array('slug' => 'site-wide-notices')' m2 k3 Z3 \5 e( A: S' R
);7 Z* P4 B% t# F6 N1 z m6 Q$ C! ]
! w' d3 L; J' u/ [3 W register_post_type('site-wide-notices', $args);$ t$ K4 A* D: r7 U+ v3 E( r
}
* ~4 E1 T/ \' k+ T/ N ```1 l* }' t! v1 C6 B/ z+ C" @, w, L
5 p2 v3 h6 _: y) \
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。- T# u. Y4 a( p0 m, @% Z
) q# d# d+ D$ U/ N" h3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:! J2 O: M. l' c- c/ [5 Y @( U9 @
4 P$ B6 w8 [5 [" B8 j/ q- ^ ```
, S$ j, }7 K2 a add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
' ~7 s* r2 B! X* u2 S function add_site_wide_notices_boxes() {
0 W" s7 |/ Z1 K, u add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
8 a3 Q* j6 N3 ?: u }: @7 p d' M/ U1 ^- t
" I% J7 v: ]2 `7 e
function notice_details_meta_box($post) {
4 W" f1 O/ F8 t1 X& @2 f1 G# q wp_nonce_field(basename(__FILE__), 'notices_nonce');
; s( o8 G* \1 }4 }: y( L8 n $notice_title = get_post_meta($post->ID, 'notice_title', true);) E) J6 A5 `2 B$ ~" V
$notice_content = get_post_meta($post->ID, 'notice_content', true); l9 _4 }2 K6 |6 ]3 s( ~/ C: R* k' n
?>( a" g8 q; @" K7 B- e2 V9 [
<p>6 v0 C3 e4 V$ y" v9 E
<label for="notice-title">Notice Title</label><br>
& y7 f% M% k# a7 g$ ]% T0 @! p5 ? <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">9 c! y7 r. f1 r7 |, U! L5 b. \
</p>2 M# J. l' N: ?8 j: x
<p>* i2 M" I& @7 l
<label for="notice-content">Notice Content</label><br>6 F. Q* J9 N6 h8 t7 d
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>/ N m+ d( r* N( s Y1 D3 D& f0 F
</p>
0 N5 l% `( O/ t2 B6 a) W9 {* r <?php1 `! }9 j/ ~$ }1 V& u2 w! h, T
}7 R0 [ c) y e" D8 d, F" T
7 V2 D) \* `3 C1 g# t5 F
add_action('save_post', 'save_site_wide_notice_meta_box');0 F+ \0 B% Q9 f% ?
function save_site_wide_notice_meta_box($post_id) {7 S v$ t3 L. W: }0 |$ {9 q, e
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))- ~- w6 J+ `# S. E- p. D7 P1 c
return;7 A) w/ N) i! o; i% U
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
" ?" I. h8 y3 Q return;9 r; U3 X& H+ R
) m8 ?5 I4 S8 Y5 r7 D
if (isset($_POST['notice_title'])) {
7 G# E2 s( D8 {/ X1 T update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
* R/ k9 K& X! T! B/ n4 M! o }' Y& ^6 e7 P Y N1 ~
if (isset($_POST['notice_content'])) {
8 d& H/ H; e* @7 b update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
' s# b: \. [8 u" Y7 B: g* g2 y }
9 }7 f0 r4 O! x5 u6 j } H, E) O6 T/ Z- s1 P! ?
```
3 Y! n+ D6 P* ~/ r) A6 K) Q6 \/ s' l
R( x9 J' O# N5 q8 T! m8 a 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。- P+ q7 K2 [+ x$ D5 K4 I
& e- f. p6 ^7 A( q6 }" R4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
* d% w, v! h5 i. b0 v7 [* {- C t7 A0 G% `/ H) O
```9 v# c/ A3 x2 `/ M
$args = array(
: _8 S9 L# E5 J9 ?! o( o 'post_type' => 'site-wide-notices',# m% l' J4 }7 f6 X' |5 f! G8 B
'posts_per_page' => 3,
* g8 R7 o* J3 @) s, ^ 'order' => 'DESC',
; z9 S3 V9 K5 H9 H 'orderby' => 'date'6 s8 ~$ U5 Q# ]
);# v( d$ V& V0 ^% K+ ?$ r
$query = new WP_Query($args);# K) s0 N' ]( p& h" _
if ($query->have_posts()) :
/ B6 \0 W, C! F- i* `" u" Y- S while ($query->have_posts()) : $query->the_post(); ?>
6 F R- ?- V8 \7 J, `+ {6 d <div class="notice">
# ~$ ?* d4 B, Q2 s4 Y <h3><?php the_title(); ?></h3>$ O# ]+ a m5 m
<div class="notice-content"><?php the_content(); ?></div>
! E8 `, e5 x. \, W* X6 h </div>: |/ _6 X4 H4 w& L+ v
<?php endwhile;: e; `- x% X& l! p$ U
wp_reset_postdata();& {, t% k1 L0 l! o! C' q' u, P
endif;
( _: S8 j2 c" `" s8 P ```8 F5 y# u X# ] e- i8 d3 @% F
4 d7 p. k4 T2 @4 y& u b3 j
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|