|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
# u; A$ D& J7 ?/ t( ] m8 W7 Q
5 t' X/ V& R7 ^/ r, ?如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
9 J/ Y2 l0 m# P; j' T3 |5 W: ]2 _8 E# k$ Y* O. ]
以下是创建自定义插件的步骤:
- y+ c `; B: W$ h7 E* R) r% w; X3 ]% `4 N1 I) U
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
1 b3 G8 ?' a1 I3 s8 Y C) ]3 L
7 V3 o: F$ Z$ U0 W ```! Y# h3 L8 \3 I
<?php& T ^9 m9 L/ I1 X3 x# M
/** ^) G! R. ]# D, ?$ o7 `' G' g
Plugin Name: Site Wide Notices Plugin
4 x" k# c- p5 j+ Q Description: Adds a new custom post type for site-wide notices.
$ P/ \, Z6 m5 o; e; O( | Version: 1.0* u5 R2 ~* s3 j7 z$ [* G* z
Author: Your Name3 M9 l2 ]9 b/ \
Author URI: http://example.com- U1 u3 x0 p) b$ K
*/
* ]% }/ J7 p' _+ L
0 e. {) |6 N6 A8 O7 I! i // Add plugin code here...
: F8 Y# b* S. Z" B) f* k. {# A$ L3 m ```
) K& a, I6 ~1 `% w
! ^" I2 W7 X+ w/ [; K 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
) ^' g! A6 j G( L8 ~
/ B# Q. {$ k: @/ [$ }2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
' {6 x+ M6 s r9 `+ h# G5 N# i
, r8 G* c; ]3 H8 \. t4 y2 x ```
( t. X7 P6 [$ i* G' | add_action('init', 'create_custom_post_type');7 ~5 u4 n S( _$ e' O8 G
function create_custom_post_type() {* y' `3 }% [' L4 v) f
$labels = array(
. H1 Z0 `/ i5 A8 k# n U3 D F. | 'name' => 'Site Wide Notices',' w J6 @2 q# o+ k
'singular_name' => 'Site Wide Notice',
; Q4 ?2 o$ V- y% \! j6 d 'add_new' => 'Add New',
+ _9 x0 t% z& h8 N1 @$ e4 E2 x2 f 'add_new_item' => 'Add New Site Wide Notice',9 a! Q; G. z- Z6 ]) Y! N/ D% u
'edit_item' => 'Edit Site Wide Notice',
* E4 t' }; l" [# b2 Q; T+ X+ {9 C 'new_item' => 'New Site Wide Notice',; L( P) m& J. N5 d1 m
'view_item' => 'View Site Wide Notice',
: p, g$ M: Q9 A; w" j' n6 r 'search_items' => 'Search Site Wide Notices',
- B* j: v0 O9 R- V3 @ 'not_found' => 'No site-wide notices found', \& r b1 e1 s' N8 u/ G$ E7 H0 X6 b
'not_found_in_trash' => 'No site-wide notices found in trash'/ B/ D% d7 b) e Q
);' [4 K" J: \( t+ J( C) U
9 ?$ g, g2 a8 g- h( l) T $args = array(
% N- L) q b1 L+ h# J 'labels' => $labels,
+ }$ @) w: T2 J" k8 o 'public' => true,
* H; d' z/ a) z3 V- H 'has_archive' => true,8 l; ?( B: `# ?' p, ^1 Q8 s
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
7 @0 I. L: A, p: X. { 'taxonomies' => array('category', 'post_tag'),, D4 d2 y; A" \
'menu_icon' => 'dashicons-megaphone',3 M" j8 g& F4 t8 a) V6 R2 H
'menu_position' => 5,
1 N* B' D* b0 k& i; K; y( O 'rewrite' => array('slug' => 'site-wide-notices')
# ~- W& ]! {) p$ J7 M );) x* t# a- V: P' P
' m: v" s+ O/ ]$ q: ?# V X register_post_type('site-wide-notices', $args);
+ O2 z" {$ ^! B4 L4 D' {+ j" _ }
) W5 x; S1 P; D' w* N4 D ```5 m3 x) ?. f" y/ v
/ Q2 D* t$ }/ {0 u, c 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。, U2 y5 y" `7 z9 \ _2 t ~0 I0 F
, a& L6 T) q/ u }7 V! \0 d
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
5 ^+ D* O; H9 E! `: E- o* o3 b- Y5 l1 J
```; H1 D2 C6 O% N8 @
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
" F# i6 T# V. t+ |4 p! N/ O function add_site_wide_notices_boxes() {
/ @6 g: m! F! B; J8 e add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
% o v" R6 ~1 a, U! R) T }# s* y( d7 r5 U8 B) y, ^# A
3 q5 S4 C9 ^8 H6 o5 P function notice_details_meta_box($post) {5 d" k! g; |/ Q, ?
wp_nonce_field(basename(__FILE__), 'notices_nonce');
! u- L" C& ]* j! o5 l2 [. Y. J $notice_title = get_post_meta($post->ID, 'notice_title', true);
) ]4 m: x; X$ G7 L9 S. o $notice_content = get_post_meta($post->ID, 'notice_content', true);
% v' f7 t n6 E. T ?>
1 k+ B" x3 U" k1 }: ~- H# i <p>- W( U) e$ E( O+ G. }
<label for="notice-title">Notice Title</label><br>
9 C% ~7 ~: q6 i <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">5 @% d( n/ y# S
</p>4 `) O& R& s8 S
<p>
x9 y* b' o7 a; k: u+ i <label for="notice-content">Notice Content</label><br>
/ F! U1 j$ o4 v: n5 e c/ U <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
; T5 z( I' p' i0 P- ` </p>: w. Q% @, _; F* L. I) k
<?php8 N$ Y7 K* o8 h$ `' [$ k
}) z- N' |' ?4 t! u2 Y/ l
e7 `. t3 x+ c
add_action('save_post', 'save_site_wide_notice_meta_box');
' f+ M4 S9 E. m% R function save_site_wide_notice_meta_box($post_id) {
* N, z0 C1 z& W- n! j if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))2 m1 y7 Y# U9 E4 c }& _
return;' j( u/ ^1 E; `' ~0 j+ h
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE): K3 B% r t6 s% U
return;
2 ~) m/ S* J/ i- J
3 i3 P8 _, B4 r; A if (isset($_POST['notice_title'])) {
; ?9 d k- G) F+ X2 s. v. q6 P/ K, I update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));" X5 E- W- t4 C
}- _% c$ k& L) P
if (isset($_POST['notice_content'])) {* V2 f8 Z' u h" W. U
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));9 S7 b, G# f" E7 F+ `
}1 `) z0 e# ^( y/ C: m, Q3 H4 Q& w
}, [( e }( J) W2 k( R/ I
```
/ @+ o# n* u9 f. \' p# j. W* M) `1 k' ?% B
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。4 a4 K! U( W5 [! F
- |; Z1 A! V6 J: ?' V
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:* _5 V0 O) P# n7 v) y) w
9 \% D6 s8 W( m6 v ```7 p6 E% K+ _% O) v' J$ H; R
$args = array(5 A% y' b( A' _6 U5 c" O
'post_type' => 'site-wide-notices',# x) I& q7 v0 P
'posts_per_page' => 3,
& D! @ e" t9 K2 F; r( A/ G 'order' => 'DESC', t& m# t- n) k" e; X$ w0 `8 E
'orderby' => 'date'; z; a& r0 X O! ]2 F, k. t
);
$ q2 G: O! T- c3 D; l1 t $query = new WP_Query($args);& T( k L* V4 g
if ($query->have_posts()) :7 c$ s& Z" L% C! m7 o& @8 Y
while ($query->have_posts()) : $query->the_post(); ?>
( P' Y4 V' Z# q4 T0 l& Y/ B <div class="notice">
. Q: [% u; g9 N; G J <h3><?php the_title(); ?></h3>
' M6 y/ w. ~" y) u <div class="notice-content"><?php the_content(); ?></div>
! b8 d% w' x0 S' U' c </div>% K3 T% G `. w% O
<?php endwhile;
7 A6 X' s |# c wp_reset_postdata();4 t, F) ], ]( g6 Y( H% b
endif;2 ]1 p2 @) m) ?/ z: j* z/ M1 Z
```' c6 ^9 S2 g8 o* @( g; Z9 g
; A7 g- d' @# V! I8 J 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|