|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
$ n- r/ w6 I2 Z- T" d# n& ^, Y9 ~, S6 J' A4 P( T8 f% s/ s9 N
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。; q- b! k8 Z6 H: Q$ }1 y
E, z7 K# B- q" n
以下是创建自定义插件的步骤:8 _+ w( K: t! {) x9 Z, F' W$ E
0 m2 G: }3 _! Q5 k c0 h6 ~8 m" `1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
* ]+ {9 r( S, N. g+ `+ g# r. O
9 G2 L2 m- k( A& m2 _6 D4 D ```; Q2 u+ G! ^, s0 T
<?php
- }& J5 k4 U, ` /*3 I9 @, G4 W1 L: k, k
Plugin Name: Site Wide Notices Plugin1 {# C [+ n2 y* u
Description: Adds a new custom post type for site-wide notices.2 r& h1 c- }# b' V, v7 G1 t
Version: 1.0
- A; W5 J( p/ c# b Author: Your Name2 M, P4 J4 q: `5 i6 w
Author URI: http://example.com W6 s* e# F# w; ]
*/
6 B; f- I1 m9 t
& \5 s% h$ y7 c& f4 c* d& ~ // Add plugin code here...
3 h+ F( _$ g w7 O) i9 s3 ` ```6 s, [) b. H3 \# \ P8 J
& C+ x% B6 Z2 T3 d) R e0 C0 p4 o 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
# [6 f$ S6 j7 C8 |. X5 y4 U' b" k2 h- v1 E; L8 S; R6 Q
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:6 J$ {! A$ C! I. Y/ K
0 @# B% U1 e' I/ ]/ D) F: T, v ```; l& ~3 T8 j+ H# ?
add_action('init', 'create_custom_post_type');
+ N: D+ d% @! C# g& i function create_custom_post_type() {; P d- c/ Y+ `9 m2 N
$labels = array(8 Y; W. W8 A+ _; h
'name' => 'Site Wide Notices',5 Y/ \& b/ L! a& ?& y( i( v# P* l
'singular_name' => 'Site Wide Notice',
, X1 |3 [ P9 c! a$ M( [8 _0 g4 _ 'add_new' => 'Add New',
9 C. W/ U* ~. H: m/ P 'add_new_item' => 'Add New Site Wide Notice',% z: U# v' k) V1 G& l
'edit_item' => 'Edit Site Wide Notice',1 F" W* T) \# u" s7 A! D, E
'new_item' => 'New Site Wide Notice',
; }2 ]4 n5 z7 O! s9 ~2 |. e 'view_item' => 'View Site Wide Notice',' j) [0 o- j3 H- ]& v5 L5 @
'search_items' => 'Search Site Wide Notices',
% o( k$ g; D6 Z* k/ T$ E. q 'not_found' => 'No site-wide notices found',
3 z( F- g' w+ z' S# z& C0 c 'not_found_in_trash' => 'No site-wide notices found in trash'0 ^7 O8 A8 \4 a( g
);
: L0 H6 m: U7 c( b
; s4 i. f& O# M9 [ $args = array(
9 w1 z. B% o8 X$ `8 U1 u7 C# |' m 'labels' => $labels,) n' w0 i! Z7 x2 P) c
'public' => true,
0 E! O5 K. w0 P' ^6 a6 ` 'has_archive' => true,
/ }( u7 q: D0 W% a$ E 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
5 ~" [+ v* n% I6 t; z% U# y 'taxonomies' => array('category', 'post_tag'),
0 W( }* a6 L2 X/ ^( g 'menu_icon' => 'dashicons-megaphone',. b7 n8 f0 ~/ o1 s2 t( f$ ^2 ~; u7 d4 S% Z
'menu_position' => 5,, u+ f% q( J4 j
'rewrite' => array('slug' => 'site-wide-notices')$ K4 N0 W% G v' S' l& V9 V9 W. w
);) B$ U( P& X0 B q4 ?% ^
8 |+ c3 o" N- ^$ a! }' }& R
register_post_type('site-wide-notices', $args);
9 U% \+ s3 T4 s$ p3 E, v. _9 w: e8 @, L/ L$ h }0 O& `/ w3 ^+ X# E
```
2 w9 K$ W: m A! A& R" t" Y9 l) |" S
- V. p* Q+ M% t) W: Z; @ 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
& V9 _: N9 U6 T& g: f2 y
8 N: |) { S; T7 x2 j3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:8 z, p" ?) [' X) ]/ E
e% i" C& x: A: e
```
& l) t1 G/ s' w, c3 @# M% { ^ add_action('add_meta_boxes', 'add_site_wide_notices_boxes');4 y8 i, a0 x) C, B/ b, E- Q
function add_site_wide_notices_boxes() {
* @/ S& h8 W/ k8 Z9 ^4 k: ?: j' x; N add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');6 w1 X4 P+ _7 ~; Y
}
* Q# m6 k: z0 S' d% J! N+ l' [' `8 r+ j0 _
function notice_details_meta_box($post) { l3 s/ f# N# i6 z5 u
wp_nonce_field(basename(__FILE__), 'notices_nonce');
4 I( M! Q% k0 y c0 Z $notice_title = get_post_meta($post->ID, 'notice_title', true);
* [- E0 v/ D% O( L3 d: I! x $notice_content = get_post_meta($post->ID, 'notice_content', true);
9 n* M# A+ \' d8 m ?>4 } l% S: x3 a
<p>
4 s0 V) h/ l9 `8 k, n3 U: B0 j <label for="notice-title">Notice Title</label><br>9 x% r* H1 |* O) }
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">5 ]1 v- {. p% U: L
</p>
9 Q0 P' G, o8 J3 n. [. y <p>& n% k5 v! Q: x7 t$ }% }8 z
<label for="notice-content">Notice Content</label><br>
3 G6 _1 c+ J, O! [6 C* k <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
% w# o& A$ ?5 y3 L6 r" R# @ </p>
) [. D- ?3 ]: L( m% I7 X# t <?php+ v! T7 ~1 b2 W
}3 A9 T8 O j7 u
" Q# w$ \2 R3 N' q6 r$ w+ T add_action('save_post', 'save_site_wide_notice_meta_box');: z/ B4 b/ a* C8 M5 b g2 \
function save_site_wide_notice_meta_box($post_id) {
4 ~. F2 v' t4 a6 ] if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))7 d/ P; o: k& U1 V, D" y3 g' E- a1 U
return;7 N8 Y1 J G# t: j ?; L; t: D
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
& n& M+ ~0 l! A% Y9 @, a return;
+ N5 r8 G. t- V( B" g' o j& X) f* ]) B
if (isset($_POST['notice_title'])) {- H! G% P, x) b. O, }' L) U
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));2 u% q# \& c3 U O2 p
}8 p( M! l6 H9 y7 R
if (isset($_POST['notice_content'])) {! e- A3 L, t) h0 N
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));+ V8 P& @% ]) v+ P9 `3 q1 ?
}
( W1 ]: E$ B# c$ K+ c1 S' k( Q& U }) F$ ~, Q- K7 ~, Q' @' g& r
```2 ] b6 u, W$ L; Q8 f: o( @5 B, f' B
# D: X4 G& _) M1 |9 J
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
2 y/ r" P1 j/ {# F% B! d: C- w! w+ a" J3 P0 t: g) y( r* c
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
4 G/ b% X/ O5 D% ~# u/ O& m) U8 j3 [4 Y0 K& X% u
```
7 \& j: J$ i: M0 z" Y# u $args = array(
7 j$ H' w( B* p2 H8 u1 U 'post_type' => 'site-wide-notices',
; N) a/ i) m" a) p7 z 'posts_per_page' => 3,% [6 |- n8 Z r3 ~$ C8 g& X( h9 r
'order' => 'DESC',
/ [% r4 ]7 R. h& q8 f 'orderby' => 'date'7 F9 J# t9 `8 ]+ p; T
);
& k; g( L1 O" Y( R $query = new WP_Query($args);$ m6 @; k; Z9 N, Q
if ($query->have_posts()) :+ T; b( ~1 i! h+ Z$ @
while ($query->have_posts()) : $query->the_post(); ?>
) {( b: p1 k, C, @0 h( R( z. A <div class="notice">
- x1 V3 A9 A; q <h3><?php the_title(); ?></h3>
$ m- T1 z, {' e( E! x, a$ ^* u <div class="notice-content"><?php the_content(); ?></div>9 {$ I" |# }5 m9 S, K
</div>
6 u9 b s" K* y6 y) d W <?php endwhile;
7 d% S) m( p# b. h$ | wp_reset_postdata();
: u, v: O: s! R) Z endif;
: k N. x, ?$ _- Q! @$ U: o* k$ { ```
) ]: T t7 x) U5 }
2 b i2 p- O6 n# |* z& z+ r 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|