|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
, e r! P: Q9 O0 X+ @, n- j( U6 A9 S% Q, u" s7 i5 S
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。/ u8 [$ Q* u9 |3 n/ V" Y0 @% {
2 z) v. H; `/ u( z
以下是创建自定义插件的步骤:8 Y8 j3 Z; y/ T. J M
8 w1 G" Z9 X. J1 |% T
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:1 B0 y: V2 f1 C9 U% d" I0 Q- u# s
! T! K# P0 k# h. ~
```* I/ \) g) i2 q1 s9 ]
<?php
g# W7 ^5 T+ U2 y1 t! ]3 N /*
( p+ k g6 S& k7 Y2 g3 o Plugin Name: Site Wide Notices Plugin& s' i4 F+ m; n" J; V
Description: Adds a new custom post type for site-wide notices." ~( r. ?/ n; V a: E; D3 c' P
Version: 1.0( h$ j/ J) I7 g2 B& E e% p
Author: Your Name5 \8 }- g' N5 T
Author URI: http://example.com3 |6 ^. o0 G- `& B- }- C) ~
*/
% U/ i' X" u0 L& i0 R' u, @8 U% V( c. K2 J* M; M/ ?$ T; j5 h
// Add plugin code here...0 Y4 t# h" n/ E! r) P! ~" B( K
``` B8 l9 {3 ]1 _- ]' C
. o: ]) X6 h. K! P5 C0 I) u: \ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。9 Z. p) Q1 H. f
* K+ ]6 F/ N' I1 ^0 p8 H; x2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:' Y4 }( {% Q) m5 I) c
) r2 D1 T) K* L$ h8 J' Q ```
( U$ I1 F @: k5 W5 e( w! k! q add_action('init', 'create_custom_post_type');
% }$ J6 R4 ~* }0 Y3 N" x function create_custom_post_type() {
% X6 J5 H$ w$ x4 \0 @1 V0 |, @ $labels = array(
7 V2 x% Y2 D2 B f! F( q 'name' => 'Site Wide Notices',7 u7 [7 M4 @: A5 c# p* H" R$ _- f
'singular_name' => 'Site Wide Notice',
# \7 T8 {1 {5 q; H" x4 C 'add_new' => 'Add New',
e) e3 E# n/ n$ n/ e& r2 \9 d 'add_new_item' => 'Add New Site Wide Notice',
) _% K/ @7 `" V) D! q7 r$ U 'edit_item' => 'Edit Site Wide Notice',
# O$ |1 J$ V. p# M, | 'new_item' => 'New Site Wide Notice',
p& [+ i) O H: p* D) } 'view_item' => 'View Site Wide Notice',! o& v+ ^: ~) G9 X* Z1 O3 {, V& i
'search_items' => 'Search Site Wide Notices',& E6 u" m+ }6 `1 y! W
'not_found' => 'No site-wide notices found',
& K. l. s, R4 Z q: N 'not_found_in_trash' => 'No site-wide notices found in trash'; H$ h) \# e# E2 I' ?5 o
); ^% L( ^9 S3 z% K/ g, I' R
- r, }" T( z+ h# x; x
$args = array(0 {6 s( K5 `8 j, c& S
'labels' => $labels,
w( B; x. Y, A/ V( v% [$ d 'public' => true,
3 v/ Z5 o* U' h4 G4 {1 F6 B 'has_archive' => true,
+ M, y4 B& [' j) e/ i0 ] 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),. ~, {7 b% i2 H B8 u% c+ I2 @
'taxonomies' => array('category', 'post_tag'),
+ M9 Y" D% k. ^0 O$ C6 G# n 'menu_icon' => 'dashicons-megaphone',! B1 F* j {) c1 l* M+ [( `
'menu_position' => 5,( m! B) A( n: l
'rewrite' => array('slug' => 'site-wide-notices')
5 s* a8 R1 S1 }$ n! I: d( g );
5 M5 _* ^$ O1 ^- R& x& G$ ^5 g# u9 m) s+ H0 l ]* R4 }
register_post_type('site-wide-notices', $args);' e5 k3 b5 \9 ~# p! f2 d
}# D: ?% n. F" o1 ^4 y
```
' e" e9 c& @! z
' V) B2 i, a2 n0 D- X 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。4 h' s$ |# ^/ U1 o# v
/ n e# h% T; q n! t3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:* S- B1 H% t0 v/ s
4 `) \3 h* t, U% N, y: V( c: _; {4 w ```- U7 z& X+ d7 t) g
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');6 {3 w" }2 m n" X$ \0 a/ u j: ]7 b
function add_site_wide_notices_boxes() {
! x5 r( G' V+ ^7 C add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
% Z2 \6 J' G6 J }
/ z4 M8 }+ B+ b, X) G. Y' x# o7 X. q5 a% s9 k1 k2 Z" a
function notice_details_meta_box($post) {; s; [4 ?( W. M2 x' p4 f+ M
wp_nonce_field(basename(__FILE__), 'notices_nonce');
9 x+ ?" a2 N+ O% k; N) M. [7 t $notice_title = get_post_meta($post->ID, 'notice_title', true);4 m5 z# i' }6 d, i; O% Z
$notice_content = get_post_meta($post->ID, 'notice_content', true);
; A# K" T, U Q0 U7 @/ v! Q; X ?>
* N4 I7 d* t6 t <p>
8 s6 O! Y* a0 q/ F+ H; | <label for="notice-title">Notice Title</label><br>
* x/ Z/ K# M3 f( d <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">& H9 Y+ Z1 P; C' Q. a7 W
</p>
0 J4 T% V( J1 `' r4 R <p>! j+ G4 T! B" O* [6 H
<label for="notice-content">Notice Content</label><br>. o2 J; J- w( d
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>. u/ V$ s+ B" J, v3 ~5 A1 V' n U
</p>
( O4 y8 `% ?8 o0 i' P <?php
6 R4 C, {' S( V. Y }9 o, c/ \" F8 q! F
* b% ]- j4 @( L1 n9 f! x, O3 T0 T
add_action('save_post', 'save_site_wide_notice_meta_box');
0 @ B3 P3 C9 E- ?, Y) [ function save_site_wide_notice_meta_box($post_id) {/ X) u* E1 K: `9 E2 R5 Y
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))). \* e, s7 F. n4 G8 B4 a* |
return;
- c7 ?& x7 c" V9 U! B) L$ s if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE): @2 j9 v M c, h
return;& G K, u ?0 p2 F
- y" v7 }8 d$ d if (isset($_POST['notice_title'])) {% Y+ P) e7 m/ |) y6 r4 u8 A
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
5 B( t" Z! I9 ?- b, O }' u! a4 A y1 i1 W0 Y O0 u* |
if (isset($_POST['notice_content'])) {) |, t& U9 M" d
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));: y9 P" J% c0 G$ K2 k B
}/ T3 j' U" D. ]
}% W k2 L% `% r) y
```
. Q4 [( S8 C& ?/ L2 L6 C) j% ?9 `7 W! r" H& r5 F* _3 j8 v/ o
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
5 F2 I% e, [1 t u4 d0 Y
/ ^2 R3 p5 ?+ p5 t4 O: P) ]3 R" x4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
/ U6 U& q3 ~7 ?& Z$ i" d& a8 ~, [( x* W) I
```1 K4 U9 R. c3 n& U* Z1 V/ D
$args = array(! A J; F K, E. k
'post_type' => 'site-wide-notices',
% l3 ^6 a; |8 I X; ^ 'posts_per_page' => 3,) ]1 R) R2 D3 q! H
'order' => 'DESC',5 z# e) Y i: l0 c
'orderby' => 'date'
5 Q% M# f# q, J% U) Q: Q/ e- X2 r. ~ );
/ [6 V4 p' K2 O9 N6 `, o, D $query = new WP_Query($args);
+ X% Y! _2 O& Z) j: _* S1 L5 I if ($query->have_posts()) :
- c) J2 c3 ?2 t. j while ($query->have_posts()) : $query->the_post(); ?>
" ]' X( ?9 z- q* l <div class="notice">
& w* t" n$ s" R N5 N# h <h3><?php the_title(); ?></h3>
/ q7 n$ g J1 o7 B+ L1 q' f' W <div class="notice-content"><?php the_content(); ?></div>! p( L/ }1 _1 K/ F6 ^
</div>
4 T" s0 s- |- d$ ~7 S <?php endwhile;8 b, P# j1 [- a! }( Y
wp_reset_postdata();- Z3 y, d* G! y/ B& u6 y! ^
endif;
* w9 Y8 d' O: U [2 E( x ```
, {! x/ S I, _% v
h9 c, }' K8 l 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|