|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
9 K1 m: V4 r! W4 H- U
: p& L4 Y) h8 B9 ?4 S如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
+ A- J. S4 `4 m9 z. o8 Y0 f7 s j. n2 V
以下是创建自定义插件的步骤:7 f9 c* v( p1 x+ b4 I
- q( w& R. F; g+ l
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
3 T! e$ q0 d- i% g; ]) P0 z: S5 _9 w6 [4 D+ j
```8 T5 L# d% ?# }/ c# B7 W( d
<?php
b) h& f P# u) k /*8 F& ]' }0 x+ e S% l$ r5 ^ Y8 H
Plugin Name: Site Wide Notices Plugin
3 f3 Z$ _: i. y# z1 M Description: Adds a new custom post type for site-wide notices.
6 m4 ]9 s, \) s0 I4 B Version: 1.0
& d% E' Z3 y3 e8 ]/ F* N Author: Your Name4 Q' _# Z0 J% E* L4 ?2 K
Author URI: http://example.com( g) x- V9 J: x# N1 n" r. d3 Y
*/
8 k9 z. E6 t- ^. C! Y
, l: V8 p7 W! `" ~/ U2 v // Add plugin code here...
$ @' M/ Z' M5 P7 B$ A, P ```
. f7 V9 Z7 x& z1 u/ z! m' S
4 ^3 e1 U/ n( E! i2 C 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
# |% [" ?0 J, D' \# I
, t5 j; H7 S. m! b+ t0 w; Y" }" w2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
! W3 g4 e( n8 R$ t) t3 f( u) c8 n& B
```
$ W) m: g l f A add_action('init', 'create_custom_post_type');. |8 B- r% v- K) B* |
function create_custom_post_type() {/ |' }* `) m- s9 ^. E5 n$ w
$labels = array(
; G, f) ?3 s2 I7 m% @) Q 'name' => 'Site Wide Notices',
' f2 v, {2 I+ ?) @ V! H. ] 'singular_name' => 'Site Wide Notice',# b3 Y/ w# t' K) m" d: y
'add_new' => 'Add New',9 P2 Z9 l4 k, ]2 `3 M, `! v, x; e
'add_new_item' => 'Add New Site Wide Notice',
/ q& M7 v! o0 Z6 P 'edit_item' => 'Edit Site Wide Notice',# j" p6 k; | e3 X+ w
'new_item' => 'New Site Wide Notice', Y2 h8 X2 b. g2 T( H8 h8 U, w
'view_item' => 'View Site Wide Notice',1 R$ e$ j& m& k
'search_items' => 'Search Site Wide Notices',
, L: N" C$ {; K0 R+ P0 a/ c 'not_found' => 'No site-wide notices found',
- x2 S8 ]& A5 V2 _ 'not_found_in_trash' => 'No site-wide notices found in trash', v! o9 j3 A8 |: P; q
);, ~% ` v. a$ W( j0 w% D
X* B4 J5 s! n. a% F/ ~ $args = array(
0 a o# O) ^" \; f 'labels' => $labels,# S( ]" k$ S l5 y- w
'public' => true,5 A+ \/ U) I; |2 W) F
'has_archive' => true,
n' l0 W6 O5 i# p# [1 R0 \ 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),+ ]: F' I* n- ~" I
'taxonomies' => array('category', 'post_tag')," N$ T" |) G3 Z2 @# O
'menu_icon' => 'dashicons-megaphone',7 m& b! j9 A' y' M3 z& H9 A a
'menu_position' => 5,
2 U6 A @3 M3 @8 k/ Z 'rewrite' => array('slug' => 'site-wide-notices')
3 O" T1 ~$ e7 C- T9 i& r );9 s1 g+ {- c4 a8 _3 r6 z
h4 n' a q7 Z2 r) S# j! p
register_post_type('site-wide-notices', $args);
! M, k8 z& F9 `: V* a# m; g }6 a3 h$ \/ e/ g/ |
```1 M# V; O1 R- n7 y) P, t: w2 l
# q0 g9 q. {6 Q8 `2 h! d7 }2 S/ r 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。5 G$ f6 B3 N, t* p
) ]' P: r. \' r8 N! F1 R* P3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:, G7 n* x! E4 [0 O# L, x7 k
" d' N1 s- i1 Z5 N0 I6 |9 C+ t1 m ```' Q6 H5 c" t$ C e& P9 s
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
# {0 k. g" ]0 d" e1 i function add_site_wide_notices_boxes() {. F K' |8 g; K' T# W8 ?: N
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high'); q7 r% a" Y$ m: x6 b% y
}
- m$ |( x; b: @* z0 m- D4 A0 p: ?* d) c
function notice_details_meta_box($post) {
! C: L0 s) f1 M9 K$ _ wp_nonce_field(basename(__FILE__), 'notices_nonce');
Z, n) k ~/ _% K $notice_title = get_post_meta($post->ID, 'notice_title', true);
3 E9 `; J0 F* T7 ^ $notice_content = get_post_meta($post->ID, 'notice_content', true);
! h$ S& S$ j$ Y f ?>3 P8 G) ?7 w& @1 M* N. @4 H
<p>
" ^0 u4 v2 B+ f( U8 [$ r6 u <label for="notice-title">Notice Title</label><br>' K6 _8 A. U) w6 w! a1 u) ^& s
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">2 W& M7 C! |" u" P8 W7 i5 q! P
</p>
" q9 o- O+ T# k, ~/ ?9 ^ <p>* n/ n) f$ V" `% \
<label for="notice-content">Notice Content</label><br>/ r) r4 |- V- ^3 T0 a
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
1 f$ q+ p$ _! ~* G </p>
5 z; o, V9 b! L* N: ~: B6 \& o <?php
3 K( K* ?; N9 u0 v; }9 H' P }
+ u& ?: M/ R$ \9 }4 E3 B( v
' J& v# Y; d3 ^* c add_action('save_post', 'save_site_wide_notice_meta_box');
4 ^" @+ X* |8 e4 I function save_site_wide_notice_meta_box($post_id) {
1 L8 H- M/ B. k! w if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
$ n3 a# a8 l* y7 ^ E! d, w" u8 p) T return;' s) P" X. U0 ]; P, ~) W
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)! w" c6 p9 m- k2 C! w& x
return;
# E4 \# r* |6 V
* ?8 a" q; ?: @6 a4 z if (isset($_POST['notice_title'])) {
: v3 ?4 O2 |. c# X d. M$ [) X update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
7 k% E: P0 r3 R }
: B; f. Q& t1 @! V if (isset($_POST['notice_content'])) {
+ `' _0 l0 j0 W9 a update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
3 I; p$ L2 a3 W" C2 b+ ^ }
3 t! W. o: M; Z: L7 Y8 l5 K }
: T: Q0 q% O+ j5 Z- i ```0 h0 i6 }) Y1 ^: Q
& G& ^ w" p4 m/ W' J* E! h5 ^ 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
$ p( d( Y% A2 y D! T& D/ U: M7 D$ e( }0 E' d
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
! D* U4 J6 c4 p' m- ~
2 h2 t* A; _6 o9 R. f ```# V# p3 m. G$ v8 c6 o) S
$args = array(
' ^& ]2 C" J# Q6 p6 t+ Z: m) k) |# B 'post_type' => 'site-wide-notices',3 V5 P9 v9 E" o7 X) G
'posts_per_page' => 3,
5 V2 x( M* p$ N W 'order' => 'DESC',
& {/ J x. ^& M3 A& c* k/ {' {( } 'orderby' => 'date'
* E! H$ K/ M: u" g! L5 r2 V );4 u2 W8 V$ l$ V5 X" K$ w" n: k7 A
$query = new WP_Query($args);
, `8 P8 {& W# N9 z! W if ($query->have_posts()) :
& G8 k7 H4 w* K* s* H- F: ]0 A6 w while ($query->have_posts()) : $query->the_post(); ?>. _( ?$ B, B8 S% ?# ^
<div class="notice">
( N3 K1 a7 y) u; j# X; R <h3><?php the_title(); ?></h3>! S7 s6 b: w% u9 n3 K
<div class="notice-content"><?php the_content(); ?></div>: A1 y2 n: t$ f- ?, t) u5 Q
</div>
\3 J- U a4 Y7 f, k <?php endwhile;! y$ r2 @$ m1 i+ K: B
wp_reset_postdata();
3 R0 }8 j9 e. f7 g' J endif;/ \- c! N% o/ ~$ |& W( u2 @
```- C. ?. ~& D2 M5 {2 u) q- ~- r
" w. q) a E& v( g# a5 x4 r 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|