|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
& p6 y: k: P+ U5 z) r( i# q1 ?: M* p) O7 ]% E3 i
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
3 L7 ?8 p' u# N7 ?
; ~, \- z2 t8 g9 c" T* o" c以下是创建自定义插件的步骤:
4 k" b: a) p- |7 M7 i( b0 ]/ B( X$ f# T K- C; {& P
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:' J3 o$ _' ~" x, r( A
! c# s7 l, S6 R& q ```! k8 z+ D5 j/ J6 ^
<?php
) F4 m4 k3 O7 a /*$ Z2 d6 I9 u3 s: s o
Plugin Name: Site Wide Notices Plugin
3 |3 W/ f- Z; S( z" o. u4 l! t- w7 z Description: Adds a new custom post type for site-wide notices.
" W* F4 ]" a, A& S5 p Version: 1.0/ D% H8 l, ^! ?# V+ Y6 {
Author: Your Name7 f8 A- W) J6 O+ A" T
Author URI: http://example.com, @8 k ?7 W6 h, N8 t8 s3 \3 X
*/! R& q- i& h+ X) J/ C8 H# ~
+ H: L4 \' n& y' E8 C
// Add plugin code here...
* c! W0 p6 k, E" R! i" p ```6 Q( a' ]: l0 t5 G, \7 t3 U% C
9 u7 w! T2 ?! K& z3 c! z6 G: z5 G! [ J 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
0 A% ^1 U3 t& W5 e% t0 C: w, o
; U' h# R% I D4 o$ n6 ^$ \9 u2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
: P! t( X1 s; ~( m' k( K
0 ^; N) x! Z9 v9 B# Y6 r3 l ```% ]# |7 Z6 ]7 @9 U5 o+ V% n- p% J
add_action('init', 'create_custom_post_type');
c2 a+ k: n/ i, S/ t function create_custom_post_type() {
5 x8 x0 P. M: T3 [2 s $labels = array(
K" ~$ P% ~* N2 o 'name' => 'Site Wide Notices',& h: H2 O/ d! ]6 Z' p! {, a
'singular_name' => 'Site Wide Notice',# T* N: J) q' J1 q5 K* W
'add_new' => 'Add New',$ `. W4 ` S8 s# G. H4 A' M7 _
'add_new_item' => 'Add New Site Wide Notice',
- H: p- c. \4 g% g5 E; H 'edit_item' => 'Edit Site Wide Notice',! c4 I+ j: R! H w1 T6 k/ A
'new_item' => 'New Site Wide Notice',, m! F; N) J# a! {9 l$ o8 v8 w* I
'view_item' => 'View Site Wide Notice',
$ M/ w1 _( |& Q% b 'search_items' => 'Search Site Wide Notices',6 P$ R1 ` D2 t
'not_found' => 'No site-wide notices found',
' V. S2 u" ?5 H8 j" S4 Q 'not_found_in_trash' => 'No site-wide notices found in trash'" Q5 c3 W7 X! ?/ z
);
# ~- Q# n9 X/ N8 o: H2 m
# Y" f8 h8 ]0 A3 A $args = array(( N2 o0 h: x. c& t/ N0 a. a- _. Z
'labels' => $labels,! ?" t. P. }* @/ {
'public' => true,( d9 P5 q) {3 Q
'has_archive' => true,
; h0 f& v/ `" {( Y 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),6 T. N8 q) d8 ]
'taxonomies' => array('category', 'post_tag'),2 |2 ^+ x& J& d6 C6 S5 W: \9 b) l
'menu_icon' => 'dashicons-megaphone',% G1 Z! L S. ~ ^
'menu_position' => 5,
0 Q, b/ O, v: I( s 'rewrite' => array('slug' => 'site-wide-notices')" z4 @" z% @! L+ p' o5 G+ c7 ]
);
: n/ g8 W: h) t1 X. R1 Y; v" P2 _ X# ~2 A! Z
register_post_type('site-wide-notices', $args);/ p h2 C* V/ _" |$ {& ?1 q
}
; r# n& j' I, w( F3 Z+ | ```1 j( I# g' `: p. M6 ^
- [$ }: c9 r1 X# e) c' O 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。, m: L" z; L- G) H
( @; I, t- d7 \9 b3 S5 r2 J: u
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
3 q/ w( P- h1 t* b2 c5 I0 d- ^# Z- V% A: J/ Y5 B: K8 |
```
5 l; W; ?+ {5 p K$ y add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
1 ?- ~% f% p& s8 Z2 a# M, { function add_site_wide_notices_boxes() {' L% b+ m X+ e! e' g/ Z; G& b
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
& B/ T6 b! I; D+ m4 r }* d- S c/ y1 r( z6 `( C
' ^$ m; k4 V& W! s& \' {3 h
function notice_details_meta_box($post) {8 G3 B' J) r3 D3 Y
wp_nonce_field(basename(__FILE__), 'notices_nonce');3 l; v1 {8 a- X* y d" L
$notice_title = get_post_meta($post->ID, 'notice_title', true);
- ]" l4 ?5 {, h0 m# o0 M0 y $notice_content = get_post_meta($post->ID, 'notice_content', true);
! h! V& o: {; U B' L( @ ?>( q( y8 y' k1 C
<p>: E: [ A" i% w5 F6 o" ?% G
<label for="notice-title">Notice Title</label><br>
! T0 v2 g1 T! x+ |% q: r <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
0 J+ I! a- @/ m5 p* O </p>
9 ~1 [. Y% R' t( }2 |* ~, J <p>! V. C% t6 H7 ^- E% x% \6 Q
<label for="notice-content">Notice Content</label><br>3 \4 j" n. J% |. g
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>2 K! l' D8 @6 e. u
</p>; Z, l7 { U8 S, r
<?php* J/ p9 M+ U I
} m! Q; w E7 v- Q1 t
( o1 t) {( I0 E9 ~) r! U# \
add_action('save_post', 'save_site_wide_notice_meta_box'); X0 ?" H l* ~* Q( G# W) }
function save_site_wide_notice_meta_box($post_id) {
1 h1 {/ ?- K& x6 x if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))): j, T+ c, k1 c% |, }/ {" Y* L
return;
, ?% G6 ^% h2 m! N, p if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)4 W3 x0 y# }9 m6 f* P) D0 q
return;
* x# k7 @ n9 L# J$ T! n" D
6 x) ]4 c* g. O% @ if (isset($_POST['notice_title'])) {' w* `2 z: n2 `! j! [8 g
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));, `/ B, U; z d9 E$ v+ G/ L0 Z
}
0 S* ]6 s, Q; D5 M" p% {" C, F) y7 X if (isset($_POST['notice_content'])) {
6 {" \. `) n2 d% M( ` update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
2 j" g1 x0 W* W, A `/ a& l }' Y; A5 s; l: {: G1 }1 M
}
1 K: P# \ S- B$ u* C ```
1 j# G) X v0 ?6 E. q/ v5 t4 _$ Z6 P4 D& U4 c4 g+ o
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。9 L7 |3 }# w. J3 V/ R
/ \* _8 {5 A) Z+ m5 P7 Y$ m9 L4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
0 [0 r9 b& W+ l) f0 f, o0 S
! H, B# ^" J4 z4 o& a ```# t0 y/ d* n% l' [
$args = array(
8 ^* Z! B) q9 {" A% Q& W 'post_type' => 'site-wide-notices',
" w7 }; y% y' Y 'posts_per_page' => 3,
8 N) H& m$ Q9 G1 h& E# F5 W0 t P 'order' => 'DESC',
; ]4 j. ~2 X; f# C3 k, U r 'orderby' => 'date'8 J/ J; D% K! t4 A) D' j& V
);
8 n5 j j6 ~% a5 B5 I' e, { $query = new WP_Query($args); x7 j1 t1 k" z W
if ($query->have_posts()) :
/ k3 i- J4 {: O/ O$ D while ($query->have_posts()) : $query->the_post(); ?>
5 G5 O" Q& L% S, o" e& q4 c <div class="notice">
7 }& t# d1 c( f% [4 `7 V- ] <h3><?php the_title(); ?></h3>
. y% F, v2 ~) r3 J <div class="notice-content"><?php the_content(); ?></div># J2 Y+ e3 P9 C
</div>) `& l* I U8 L
<?php endwhile;
% ?" m n; T0 x, `9 p wp_reset_postdata();
: y: j0 Z% w# S( D8 x8 p endif;* n) H1 W5 P7 ]5 |7 q: H
```( ~! |& T# Y" I- c
8 W- C) r8 o6 @4 @; N 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|