|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
% j: g& x+ d% E: a* G! u' S- v; t6 j; o/ r6 h/ C% n9 A- @
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
# B5 n( f) e7 M7 O- d
" e/ E9 }0 V3 @以下是创建自定义插件的步骤:
) R5 V7 t) C+ g' m: _
# X, [3 `* S9 q) U# L1 _1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:4 \8 R7 G3 g3 a9 b8 [6 H# L
4 C8 o, j% i: F. y" _% ^ ```; j. u" S0 [8 d. T6 A+ B6 t
<?php$ Y& L, ^/ x& y0 V
/*3 o ]0 I: z+ s
Plugin Name: Site Wide Notices Plugin) K7 l& ?. R; o
Description: Adds a new custom post type for site-wide notices.% H5 }, e7 r( c, w7 a Q3 X
Version: 1.0
) s D2 s7 F2 X ` Author: Your Name% Y' {7 [* P) `! Q) \
Author URI: http://example.com3 }4 U# R2 B# e4 [ m' `1 [
*/
. {# W, f: Y, k/ j+ m0 h5 g& q/ w# L2 ]4 O
// Add plugin code here...5 d# O E# d' [. s
```4 x* ?; N$ m6 N( L, R2 L
0 |3 @. G4 A2 b0 \4 D0 H 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。6 x1 X' I; R% P" p7 G' ~8 m; G
4 v$ s* j3 V5 U% P2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
2 b9 }0 O- d+ k( X: j; o
; C' B5 U) X$ @ ```! P( L; ]1 P! Y8 L _" f) {
add_action('init', 'create_custom_post_type');5 i6 e# q) B5 T# C% Y
function create_custom_post_type() {
* D5 v' U. n- U# ^! v' X+ \+ o $labels = array(
$ ]. n( d P- ] 'name' => 'Site Wide Notices',* B4 `3 ^3 R+ r; t4 g8 k
'singular_name' => 'Site Wide Notice',
( @: l/ B2 w) J% k1 ~' X& y- k 'add_new' => 'Add New',9 x( _/ M6 _, u& A" r9 p
'add_new_item' => 'Add New Site Wide Notice',1 H V# x5 Z/ w" @) U
'edit_item' => 'Edit Site Wide Notice',' c% b# f0 s8 {2 b
'new_item' => 'New Site Wide Notice',
4 G/ K: L; J! U- M1 P: z1 q6 R V 'view_item' => 'View Site Wide Notice',3 }, x+ i7 {2 R
'search_items' => 'Search Site Wide Notices',
+ m0 J$ l( T4 P$ e$ H2 \ 'not_found' => 'No site-wide notices found',
0 F7 {* l' h" k1 z# u' ^/ I: ^1 S 'not_found_in_trash' => 'No site-wide notices found in trash'2 V% [4 C0 P L$ W ^; |% X6 ?3 c# x5 ^
);
" N1 D$ x& Q* W# W# L
4 l$ X4 [/ I; s5 m) t4 d) ]0 s $args = array(: a: t& T8 t0 |5 b
'labels' => $labels,2 r6 o8 w; |$ D
'public' => true,: K$ _. Q9 a9 E
'has_archive' => true,
' w5 B ?) B7 q$ O 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
; u# _# y# U; k. K; f Q 'taxonomies' => array('category', 'post_tag'),) ^, S7 y) T; i2 S# w. o
'menu_icon' => 'dashicons-megaphone',
3 _4 M1 k- E4 I% f' K8 | 'menu_position' => 5,
, h+ m; }( d, m# C9 N! I4 u 'rewrite' => array('slug' => 'site-wide-notices')
& W' A+ ]( W% \8 d. F2 S! f, b );
# A& a5 I% E6 o- p3 m
" e. q# B7 d1 f! r6 c4 p! _* I w register_post_type('site-wide-notices', $args);: p- {/ Z. r* W2 V
}( g/ l7 g' `. p4 B" i" d# N) l
```1 i: Z# P9 L4 x9 o) ?
- ~" u4 @5 J' R {& l2 Z 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。. Z. Q+ q: w: a4 J8 j
4 k! v1 W7 X$ a3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
) y0 C& R$ |* d% v; C4 `/ [! {
8 k# J1 p, Z( ^4 Q6 k ```
! z- \% o; c* b$ C! }& u; C add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
+ f2 m/ ?. q0 e K M7 @! E function add_site_wide_notices_boxes() {
. n5 u& I* M, R/ H0 P; N! O& P3 Q: [ add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
; O6 d4 I% w7 n: G( ^0 h }
) S# D$ ]0 a+ a: ~. j0 N3 R$ V# z
$ Z. N- D& c! d X3 K/ n6 c: H function notice_details_meta_box($post) {/ j4 h6 S: t8 F' e/ f
wp_nonce_field(basename(__FILE__), 'notices_nonce');
) q+ ?1 g Q4 Y) ]3 ^ $notice_title = get_post_meta($post->ID, 'notice_title', true);3 ~% p" D, I3 R+ Y
$notice_content = get_post_meta($post->ID, 'notice_content', true);
6 J3 {* g/ @9 M5 g1 { ?>) i9 L: K& k% W* J. R% ]6 G! V2 o
<p>
1 C/ m$ Q) f% P/ n3 m <label for="notice-title">Notice Title</label><br>
+ x/ e! d8 J; o7 M! K# U <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">6 Q% w' a6 J$ L- l3 G# F& b; I
</p>: L! `6 M8 m# @5 S/ m' L2 |; q4 e
<p>( ]. P& `8 K; s' x6 x
<label for="notice-content">Notice Content</label><br>
% u/ y `/ f3 R9 l& H <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
1 q6 b, E6 o; ~& |' G ~; S </p>
! E8 R) s& L9 L! E: l# e7 \% f <?php
7 ~. u* _+ y- Q- _, s2 K }
+ @$ o" g# Q6 ~) X3 ^: [
& @$ F" v; {+ a/ l& U$ E( ` add_action('save_post', 'save_site_wide_notice_meta_box');
4 ]" f$ W7 V0 q! |9 { function save_site_wide_notice_meta_box($post_id) {
o5 Z: x2 v2 t if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
/ i. r- O! c7 M3 _1 P) K return;
6 S8 Y# s+ t) B6 V if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE): H; s7 A( ^) n: q5 G
return;
& _ _% l' `; J$ N0 S. R5 m6 L& r; h# e- v: X
if (isset($_POST['notice_title'])) {
. u" Z8 M, o% m7 g update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
% |- j# ?8 y! V' H( R x; p }) x% K1 W5 b4 h% r
if (isset($_POST['notice_content'])) {5 I2 R7 [. V% F8 S7 `/ `. M
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));. q) O2 J/ g3 e; Z
}
6 r9 R- R+ P, K" T* ^ }$ E1 H9 `4 r) m% {
```+ E; y( E6 b9 q/ M. z' V1 b7 q
6 f4 i; u- g2 h 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
y1 d1 G! B# a, e5 Z! u
+ D" K) q$ `1 V* T F6 e% P; z4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
9 ~$ i6 @1 W W, h4 Z
. x- s) H: j6 e; Y) P! t3 X ```- x) p& |3 g; o# D$ j8 |
$args = array(5 p; D& s2 H3 `* M# v2 m/ X8 X
'post_type' => 'site-wide-notices',
9 D4 [ }$ B9 U7 n. ` 'posts_per_page' => 3,
5 f$ k% K3 d' E! ~2 e% a 'order' => 'DESC',% J3 r! m1 C7 r$ q ]
'orderby' => 'date'
, Y$ M u6 k& H6 q( o+ A! t );; w5 ?/ ?( v5 Z8 K
$query = new WP_Query($args);
- m/ Y; M3 N/ \6 V: n H& l) v: b if ($query->have_posts()) :
2 b0 ?; S0 Y. ?4 T; S2 | while ($query->have_posts()) : $query->the_post(); ?>+ h5 A% {. R7 ]1 ^0 G, }$ }! s& _
<div class="notice">
9 Y& W' K: r; y, `' N: h <h3><?php the_title(); ?></h3>% [. T8 N4 V2 E w: o
<div class="notice-content"><?php the_content(); ?></div>
; z8 ^6 ^4 D2 M8 g2 x+ Q+ M </div>& c. _, I0 [( x
<?php endwhile;
7 w3 W# P& H& }- F* j" d wp_reset_postdata();
) G% C! F3 P+ S }* l endif;
& H; Z4 o6 y& u1 }0 o, W ``` I, [9 ^1 }+ z6 M4 K
4 c. ~8 P1 C0 x0 B5 W4 V, a
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|