|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?6 E: ^- s; `1 p5 Q d+ V, k1 W- r
% Q& C, U8 \& |9 o+ s如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
# w8 e! [4 E0 E
3 f: R0 I5 A8 o+ e4 {4 s% I以下是创建自定义插件的步骤:% F5 T* \5 }8 ?5 r. Q6 [
$ I6 T( J" d1 D1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:0 h8 |% m: E& L" h
! s$ x; N. M# F# K9 N
```
# `6 w9 Y$ B! L% N0 V1 B, O <?php
4 S* W" T" ~$ d) b4 B /*
5 o; g9 b6 H' v) M# P- V4 {# L Plugin Name: Site Wide Notices Plugin; W) b0 E4 ^- }' [! B4 O. L( \
Description: Adds a new custom post type for site-wide notices.! d% c6 q4 L! b9 v
Version: 1.0
2 v I- D1 v/ X% r Author: Your Name
' u1 I" W- P- d# P; M Author URI: http://example.com
: \2 k& k2 B% e/ l */! c/ @* c8 W( V7 Q% C1 P2 g
/ n, h3 j! _$ ]& w/ \) ? // Add plugin code here...& L0 _/ U7 D; o0 E! K
```
; v. w& B( `) T) j/ j h% ^+ p$ [# `9 s, `! j; m
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。# O$ c. O4 Y' [' A Z: u
8 h0 Z9 r; T3 n. b; \
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:" w: ~6 J9 h% l1 @- I* G+ j
8 i& [; V, \4 X I ```1 T2 W( }1 f* M! y
add_action('init', 'create_custom_post_type');' @- Q* v! t' O& [
function create_custom_post_type() {! W, s. |9 B* e4 y/ D, ?; v
$labels = array( r- [: I9 C8 g! w2 `" [( ~
'name' => 'Site Wide Notices',* }' G# c+ U0 K
'singular_name' => 'Site Wide Notice'," K% X: D) }1 T9 n5 m. b
'add_new' => 'Add New',/ J) `6 F- A6 O1 b; b, e
'add_new_item' => 'Add New Site Wide Notice',
/ }% n6 ^. J) S2 @) L7 f7 S 'edit_item' => 'Edit Site Wide Notice',* l3 ^" ]. ~: p
'new_item' => 'New Site Wide Notice',& t: e; Q+ D7 b
'view_item' => 'View Site Wide Notice',# Y1 ?- C) ? ^' X5 z7 _. j
'search_items' => 'Search Site Wide Notices',: e$ q! ~5 d+ o+ h
'not_found' => 'No site-wide notices found', ^$ e2 N) \, i9 V0 X& w w
'not_found_in_trash' => 'No site-wide notices found in trash'5 g- ~+ Z+ M+ I# A) y h
);2 Y) A' c' H6 y2 w
" f9 ?' l8 I& W8 v $args = array(
6 a1 Q' n$ p1 @+ E: p$ B+ @; g 'labels' => $labels,
# [/ C9 @* V4 i2 M$ i" i: o, q 'public' => true,
" s1 |1 f0 W4 k4 h 'has_archive' => true,
( c( l. p# ]7 T* G* ^2 i, n 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),( X5 `2 e$ A% i* \
'taxonomies' => array('category', 'post_tag'),3 H: R) I6 d/ c. b ]2 A
'menu_icon' => 'dashicons-megaphone',
6 l/ F1 w# ?* n# C1 v3 h( I 'menu_position' => 5,9 p. V) S; l+ E: O: `- @, u
'rewrite' => array('slug' => 'site-wide-notices')
6 g9 [' S( n' m5 I2 Z8 C3 J );
! `) n+ |$ I7 x+ E4 Y6 E6 F+ v! Z& E( A" x1 s0 f
register_post_type('site-wide-notices', $args);0 ~3 ?; z* t9 P3 I
}
- S) I! l5 e) H! Z8 X& B ```
) Z; q2 n, o. m& V! f- M' H. b, u5 J3 m( N2 q5 L" N
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。- l2 C' e. K. k' c6 C
4 ?8 ^, [4 @: K/ p" V/ y) k# r
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
9 _! U* G9 y+ u, |& d9 o( W( Y% Z" U8 t; H
```
# T! {, s: D6 V( ` add_action('add_meta_boxes', 'add_site_wide_notices_boxes');! J- X: E, R# v1 i8 e/ r% [. @* w
function add_site_wide_notices_boxes() {
% ]/ z& Z5 z# I* @8 j0 g add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');7 `' Z7 O: }7 O* U
}
( T2 T7 }: R% {3 _, R" R8 z
( G1 R# A1 R* }/ f/ X+ M function notice_details_meta_box($post) {% e f3 u- {4 w& r9 U1 E1 v
wp_nonce_field(basename(__FILE__), 'notices_nonce');
7 A4 L1 d2 @& v7 r1 `+ D$ H$ t $notice_title = get_post_meta($post->ID, 'notice_title', true);# d( z* c$ c( P0 H! e
$notice_content = get_post_meta($post->ID, 'notice_content', true);
; Y- g; t, O, o3 P ?>
* i6 i) R6 O* \; a) @ <p>
( H5 O1 m8 H. S# H' R$ Y6 v <label for="notice-title">Notice Title</label><br>
3 [- u3 V% {! i4 n; z& B1 x N# }5 @. a <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
* t5 p0 [! j- L) s2 u; H0 ] </p>
@1 f/ s' l( m2 c! L' p6 F <p># R8 [! v+ \& p* H0 T0 `
<label for="notice-content">Notice Content</label><br>" f7 q2 u# c: V! i# Z& o
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>/ g8 k4 n6 w% u: M3 A g/ `
</p>
8 X% D8 p: p* X7 o5 d) B; A4 V <?php
4 e4 ?4 v4 C- A }) V# P5 N4 Y" S A
/ z# w) f+ c, d' n6 u$ y( G
add_action('save_post', 'save_site_wide_notice_meta_box');
- t- V& J$ M# x5 V5 \& l! [& N function save_site_wide_notice_meta_box($post_id) {
0 m7 x* p/ A0 B3 v b if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))); o& F. y/ J1 l+ O2 o$ O) H
return;
( c" n, S7 r" l' C+ @* L if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
4 ]' ]( X8 q/ |2 T1 _+ ? return;% f4 j7 [! b, x; r
, \/ d+ g( w/ I$ n3 B; q) j& d4 q. w
if (isset($_POST['notice_title'])) {
7 s4 V1 h1 J# H5 X8 h update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));2 \" \, C, X- ~8 ^, e+ {
}/ G8 v) W7 U5 \8 N- Q% r% W
if (isset($_POST['notice_content'])) {6 e r: U @3 ~& V
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
" _/ `0 ?4 A3 B. i( I. D5 Y }
# f4 u+ {6 b8 n: H: W }% K5 {% H1 l. ]# q$ l$ ^: g
```
( u. t' l( x. k
$ @8 w6 M) z" o8 z3 v 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
5 ^! K( d' d8 P% z9 Q9 t) i a4 I& H% X7 w( J! a1 ?8 i
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
+ e2 `5 u% x5 | d0 [$ K* Z5 I; Z+ X1 N4 P. Q4 v( ]# {$ _, `
```
! ]! C3 Z5 H, v+ a0 R $args = array(
% n% D( u6 l" B+ U* A# N 'post_type' => 'site-wide-notices',
4 H2 i! X `( r+ l' X2 @6 }9 x, f 'posts_per_page' => 3,; f3 A% M Y0 }5 G3 k; \ ^
'order' => 'DESC',
% a& T+ O2 i1 \' N }1 P 'orderby' => 'date', ` O+ b H$ t
);6 r8 J4 ?/ M g% i/ R
$query = new WP_Query($args);
- o: I. g+ k! i6 c if ($query->have_posts()) :& @5 A0 V4 e, U% H9 ?
while ($query->have_posts()) : $query->the_post(); ?>) i% a0 h: Q, O% {
<div class="notice">
7 |2 z9 U$ p) T0 a0 j9 F <h3><?php the_title(); ?></h3>
4 [( m' z1 L" Q7 A9 N <div class="notice-content"><?php the_content(); ?></div>6 `0 w8 G( v4 [. C( J, c
</div>, } ~1 Z e* b# A4 \7 A
<?php endwhile;9 M1 W! z( ]% h- M+ q" ]+ K
wp_reset_postdata();7 D# ?; U9 H. N$ O( c
endif;
/ X$ ^$ h! P+ V" I; m: k9 e ```1 k' W) d- C) `$ ]4 W" _' g0 y1 C
6 _. t4 K c, B- d 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|