|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
5 ] W, a$ w" P0 H4 r: I' q# i# O1 Z# I6 W( ~
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
4 p+ U) C6 S/ k& ]) l
9 [6 z* Q9 V" v3 x以下是创建自定义插件的步骤:
) }9 C- {7 n$ v. K2 p2 i7 Z) c
p: o6 o) Y7 h* R1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
: g# ]6 r4 ]0 m7 n
- n, V3 _- m1 ~* j& e, g ```% x6 }" \$ e, F4 n" u+ B
<?php! U3 A0 o) @* E
/*
7 H$ e, Q* u* s( v7 G) E) X Plugin Name: Site Wide Notices Plugin+ Q5 V1 D. n$ @& d
Description: Adds a new custom post type for site-wide notices.
4 F+ G; |# _5 z. P' I: O, l) N Version: 1.0 G( }! X6 j; S# e* z) Q
Author: Your Name
" s0 C1 X3 m+ L Author URI: http://example.com
& C# k. S) W( {" m */' W- n3 q4 q5 _+ w) J9 c- o
' M( n2 P1 g$ n" h1 |( d // Add plugin code here...
6 } k& N; N0 O4 `$ n9 Y ```
! H; C) p# N- J, j
3 a9 ~7 [* j% b! S2 ] 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。- u' J6 k7 u* n d6 T$ \% _
( m, x2 M" y# i7 r9 M, h" b3 i7 ^$ }
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
5 B: M: i" |3 } ]) w
4 v- t7 ]$ Q/ _/ a g* f% w ```& X, ?; |, S- B% ^& D
add_action('init', 'create_custom_post_type');0 p% _6 N" z% i$ M# L0 Q) q
function create_custom_post_type() {9 Y/ y' R! V( S2 _& |# ?0 i2 w; J
$labels = array(
3 l0 `4 z m+ Z9 k$ V; E 'name' => 'Site Wide Notices',
7 {; o* G* n+ d1 Q& ]) J: q 'singular_name' => 'Site Wide Notice',. z5 v& t( k4 t. L
'add_new' => 'Add New',! R' \1 a) O8 _# B4 S7 t8 J
'add_new_item' => 'Add New Site Wide Notice',2 F3 ]: a; G; I+ H, x) i" y; d0 m
'edit_item' => 'Edit Site Wide Notice',9 u6 v% P5 x; I, ^4 Y5 {; B
'new_item' => 'New Site Wide Notice',
" z1 f$ n. s5 t 'view_item' => 'View Site Wide Notice',/ e* r. U5 X8 ]
'search_items' => 'Search Site Wide Notices',( A! _: w1 A7 _. b. ^5 L5 o, @
'not_found' => 'No site-wide notices found',
6 J- T$ ^3 f, t. F 'not_found_in_trash' => 'No site-wide notices found in trash'" s& f/ Z6 Z9 k( K/ c
);% M; V x+ Q# H$ @* Y0 z; ? L
( M1 d5 b3 T! [
$args = array(
Y0 I6 j" ^2 q8 }3 n+ q( j' F3 E 'labels' => $labels,4 [5 h4 l. y) a P- B: K
'public' => true,
! _& f" b+ U6 L5 f1 B! v 'has_archive' => true,! f1 K$ t2 Q2 \3 c: w7 k
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),1 F6 ?* Z4 E D1 X( W6 v9 X5 R
'taxonomies' => array('category', 'post_tag'),& I& F! g& w& i ^* Y P, B
'menu_icon' => 'dashicons-megaphone',
3 A# q+ A3 Y) C$ t+ e6 z! J 'menu_position' => 5,
$ b1 N! s+ {" M6 {5 R% i8 X 'rewrite' => array('slug' => 'site-wide-notices')
- l# b+ y9 G1 c" Z* ~% Z );0 N4 x2 M5 {, `1 L2 Y2 M' Z
3 @0 U1 |2 k( `3 M2 ? register_post_type('site-wide-notices', $args);
& P5 ]& F9 k+ e# b. e* \ _+ g$ G- F) | }
1 J, P, n- M0 W$ l5 B9 h& n ```
( P$ O' f# e' ?0 a0 l; s6 e# U* S* T; ~1 E. c
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
1 z2 Q/ A/ h% z
E$ J0 v; H; y5 @/ X$ x9 E3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:. ^/ F. Y) |: n" T
, g6 h* ]/ ^: O ```/ u8 ^* x4 W4 U- M" p! b, X5 g! L- t- _
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');) ]# D2 `7 a x% ^( R, ]0 y
function add_site_wide_notices_boxes() {
( ^4 T ~; Y$ V add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');; {6 m" _% i) T( r. h U- x
}. n3 [6 q h' [. o+ A7 a* ]
9 z. C% l/ i" v
function notice_details_meta_box($post) {# d9 H0 q. x, X4 b
wp_nonce_field(basename(__FILE__), 'notices_nonce');3 y2 c d! d7 P4 r$ w I
$notice_title = get_post_meta($post->ID, 'notice_title', true);
5 N4 ~1 ]" q$ h* B $notice_content = get_post_meta($post->ID, 'notice_content', true);
1 S. F& ?* \$ k' a" R" r3 Y ?>
) Z9 c! c7 U! z2 Y) S% `+ \ <p>4 w; [8 u( }- {, R! [
<label for="notice-title">Notice Title</label><br>
" S4 ]+ F% ^2 | j8 W; w <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">4 P9 c) O( ^3 n/ U) o/ M
</p>
+ C6 M# `" d& Q/ u% T" R <p>
0 `1 }) M& f% i# f, ]" q/ l <label for="notice-content">Notice Content</label><br>9 N/ e/ x m& q* J- A6 a+ n
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>3 _2 L( P: k4 B0 F" O
</p> \% Q3 B! d: _- v6 D0 u+ |0 E
<?php
' E% Z6 {+ Z$ l# t/ G } L1 I# b* m, K, ?. A; u
5 G2 D7 t. R B3 O. r X add_action('save_post', 'save_site_wide_notice_meta_box');
- f. w* j( g; Y6 N+ h; S function save_site_wide_notice_meta_box($post_id) {7 r+ T6 _- w9 ?& |% W
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
/ O8 P( Q/ H0 P& Y; x1 D( F return;
, V& X o- G( z R4 }* F: J" B& S9 S if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
! p0 I: V& x+ Z& J& P& i% a/ `" z8 a# f return;
( w* g9 k* E, t/ C" S0 @' ~* {1 L4 ^
if (isset($_POST['notice_title'])) {
) O6 U! _& v) d$ U* Z- U update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
/ }0 _" \' H3 D( i* t9 |: T }7 \0 }' l7 o+ S5 C! b
if (isset($_POST['notice_content'])) {8 u' j, ~( @ `; D- n0 r
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));' V2 M/ ], x, r9 g2 ]
}
' B5 f1 j0 A0 b8 ^& I& s6 F }
0 e4 I) ]/ d/ u' ~7 U, E ```
, Z) T$ l/ D' @5 y y) l9 N
& u$ l F: h. [) F) \ 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
6 [0 T9 G9 S6 ^5 x
# x6 [1 O, ^; v4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
7 {1 J) ?6 ^5 R4 C! Z7 r
* n; g K, d$ p7 C, M+ Y4 O* c ```
$ p' H& H# A+ ^7 p j; i $args = array(6 Y7 }4 f% p4 J! E7 B6 P& ^
'post_type' => 'site-wide-notices',) L1 M# p( V) m0 Z
'posts_per_page' => 3,
" A$ O o# d. b0 ]5 I 'order' => 'DESC',& W- X/ @0 C2 y0 L) t( [" X- r$ q4 A6 E
'orderby' => 'date'
+ o" p) r: G' Y, y );
- b0 y+ K$ T, e& g1 [ t $query = new WP_Query($args);
7 n* G3 {2 y; c3 P if ($query->have_posts()) :
g! D; i/ D: m7 }1 b while ($query->have_posts()) : $query->the_post(); ?>1 j" {' |& T$ p. Y
<div class="notice">! n& S% u$ } D
<h3><?php the_title(); ?></h3>+ k& }" b. u7 A5 x6 H- U
<div class="notice-content"><?php the_content(); ?></div>
" Y2 I, |+ i G9 f0 @ </div>4 z9 `: k8 E7 ]) F. Q& ]
<?php endwhile;
; f: H* i' @; Q. H6 y i wp_reset_postdata();
, r- U% A2 v& K. m( v( q endif;
' }% t$ V- ]6 i# H ```
2 Z3 }6 l, ]/ u& l4 B
5 x( s4 ^2 W0 g/ b 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|