|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
6 j7 r% J- w0 s. ~! {
8 o/ F! t D7 _/ L如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
) e% [( [, S0 p# f6 m7 f+ G
# {8 L1 i5 W' B0 `! v以下是创建自定义插件的步骤:: E0 ?! S/ m( w) Y8 W8 Y
+ C6 @3 I( n( Q( t% k1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:; _: D/ @+ n! f' _$ R& i
! L3 v a3 r: T ```
0 a0 [2 K& L* ~+ g9 t9 \$ z( B <?php4 H7 O! b$ R2 ^# N& ]( [
/*( ` s0 v0 L: l
Plugin Name: Site Wide Notices Plugin: k5 Q( w J4 U3 }$ D
Description: Adds a new custom post type for site-wide notices.9 V6 {5 _* E9 V
Version: 1.0% k5 G: e/ g# U5 F) |! r* v+ h8 ~
Author: Your Name7 {* r1 Z0 d h" Z6 |+ y
Author URI: http://example.com
1 R) K7 `% X$ R$ X/ {# Z, E! L */
$ m9 B4 n' S! B& ^3 B% j. b" W: R: p# y4 Q# z9 h1 ]- ~0 d% `+ w
// Add plugin code here...7 l9 p% g" I) N5 Z2 v
```
6 Q# B3 d9 a5 _! X: D" P% X1 c0 q, M# {6 d& N+ }. D
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
3 s3 W* `% ~0 E4 E5 a
" r; F5 @; V, s9 y# k: R2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
* D3 c J) ?% b% a% c4 y/ X4 y1 |3 ?4 t! h
```
8 ]! g- H# n* \7 ^6 q add_action('init', 'create_custom_post_type');
$ H |( f* Q/ h/ h8 o function create_custom_post_type() {
% v* x/ e& T7 I; C6 T: u- Y2 @9 q $labels = array(
, d. R& F, P! i" { 'name' => 'Site Wide Notices',
5 u* b5 q, h: Z2 z" d2 y$ k 'singular_name' => 'Site Wide Notice',* @1 h( z7 A* t/ s. i* t: A$ a; P
'add_new' => 'Add New',
# l7 q" Q( k7 v w& J& l$ r# D 'add_new_item' => 'Add New Site Wide Notice',
5 a& P% c* \" [, r5 r 'edit_item' => 'Edit Site Wide Notice',% Y* M* @6 S7 z! m' v
'new_item' => 'New Site Wide Notice',
8 h7 W$ B3 ]& n! F M 'view_item' => 'View Site Wide Notice',5 S1 m/ f, k" @$ `$ ^# O: e4 ]
'search_items' => 'Search Site Wide Notices',. l6 j: g/ `% c
'not_found' => 'No site-wide notices found',
+ d4 Y5 k, q5 g# D4 F 'not_found_in_trash' => 'No site-wide notices found in trash'
5 t/ h8 P- \. ^5 R' c );8 V6 f2 ~: t+ w1 h, u" C# @
/ k6 V3 \$ D9 Z4 F1 t# Z $args = array(
6 ` }- Y6 b/ u# N4 a ?7 V 'labels' => $labels,% ^4 N. T) W6 A) f) h+ p
'public' => true,+ z! F) p. R4 m2 o: y
'has_archive' => true,
1 M, f4 N5 |7 [. Z 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),& F: T7 ^: ]% e+ ? x
'taxonomies' => array('category', 'post_tag'),8 r3 F4 h- G3 h0 l$ C) |
'menu_icon' => 'dashicons-megaphone',
/ x3 Q8 `! s1 T/ v6 F: C 'menu_position' => 5,
) A& A- R j0 z) I. n 'rewrite' => array('slug' => 'site-wide-notices')
9 h* q. s [4 s! V );3 K/ k) F2 `6 v' \" a
4 k, h% H" w( \
register_post_type('site-wide-notices', $args);
- J$ H" s7 ~1 I5 `0 `* ` }
- x- z" _, V5 q1 F6 Y ```
1 U" }5 ~. N# k. h$ @0 c
4 k5 f2 |5 o! v 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
$ z- N3 B4 [2 B; S( K5 l8 S: i5 }
5 Y+ ]0 w. ]0 _7 i/ I2 n3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
6 H$ S! I3 d+ Q( g D
! h- ~: A; I2 o& G1 j$ O ```+ o) V' ~9 G% S+ K$ t. x4 w" t
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
! x6 P: L4 t6 e; u' I: D# f function add_site_wide_notices_boxes() {0 j* p3 ? U S4 P3 g
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
9 `7 [) Z: V8 F6 z3 Y- j }
d3 D2 j. ?* e% ^
2 k4 x9 g6 p" z- j' t3 ~ function notice_details_meta_box($post) {
1 k5 ?& \( G, N$ G wp_nonce_field(basename(__FILE__), 'notices_nonce');7 u& ~! S2 G$ c/ G3 o
$notice_title = get_post_meta($post->ID, 'notice_title', true);
4 u( b2 o, i' m7 O( r7 g $notice_content = get_post_meta($post->ID, 'notice_content', true);
; q6 h8 T$ H) q, o! E6 w ?>* f2 w- Z9 T% a- G$ X
<p>( x9 |5 v# z6 O
<label for="notice-title">Notice Title</label><br>: q) Y6 z5 E- [* `: h
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
+ d' u5 o( y) n6 |. N5 V </p>
: {8 G' a; C% h) k7 |4 i <p>
) J1 D2 F& f5 l% U <label for="notice-content">Notice Content</label><br>
. B: I2 Y) E8 T2 g; S4 V! O <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
: P. M+ E; j" U3 K# Y1 j </p>
; f W% S7 H! a0 P. x <?php g. r; L; o( R/ X6 H
}( b: N9 q$ U7 @6 f
6 f7 t; T8 | ~3 g5 f
add_action('save_post', 'save_site_wide_notice_meta_box');& I! @$ E5 D, z& R: @, `$ g
function save_site_wide_notice_meta_box($post_id) {
$ D6 F! x" Q8 |2 _. A) L, r if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))5 Q4 o& M+ e3 e' O' |
return;& n+ c1 e) d0 \/ f
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
4 G0 ]; n2 @# N d% {" ?2 Z return;& S+ h3 o% R" d
. [* v3 y3 D: K+ z/ S
if (isset($_POST['notice_title'])) {1 _& P7 J1 F. R U: i
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));+ F) s' ?) D1 g L. q
} T8 e/ G5 N0 c0 W$ P
if (isset($_POST['notice_content'])) {
1 L& D0 Z" A- S' ~$ V4 S$ Y update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
# l6 N0 j9 H# t2 n0 i }
( b+ J9 x6 W, p( C0 a- U }9 k5 i x/ w: L
```
: e" H) a- _/ d: B- J/ y; Z
6 t- m/ `" y, K8 [" f' _ 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
* M) G! T$ s4 ~0 I
1 @+ o+ P. K7 w) ]4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:- o- z+ y# }. X' _# a6 r
, {' S f( L, _ ```+ Z& j, a5 Y# K7 B
$args = array(
" ] T0 |5 g2 u6 F 'post_type' => 'site-wide-notices',1 V" o( i8 _9 S( @# X* t ~# ~
'posts_per_page' => 3,
/ m+ h- {2 ]! L9 Z 'order' => 'DESC',
+ n$ i) A4 I! O2 y3 d: M 'orderby' => 'date'0 n& p a# t) ?' \& c
);6 I4 X* ]0 T1 l5 E
$query = new WP_Query($args);2 N4 a" F0 \, d c- d* \
if ($query->have_posts()) :
9 X5 [ Q8 _2 v. i l while ($query->have_posts()) : $query->the_post(); ?># [1 F) S* ~8 c- |- n+ D( T" q
<div class="notice">5 C. V3 m. X# |* b$ W
<h3><?php the_title(); ?></h3>' e; h7 O. t2 v' {4 }5 L4 n
<div class="notice-content"><?php the_content(); ?></div>
5 |8 \ N+ }( g5 l) } </div>4 E5 \2 Z) K! ?- k9 d
<?php endwhile;
" o- h- x; A2 S6 l3 Y wp_reset_postdata();
2 B" i/ U0 F3 u/ _' u endif; i6 e2 ^9 X/ \2 i1 ^: t6 d
```
. t1 u2 _6 H j0 N; r
" M$ F; k Q& ?7 g 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|