|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
' C! q* |/ v' `' p: j4 v
+ c, e* q2 u" B如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
; x9 |/ \+ x4 D1 n4 e) c5 P1 o
: A9 B W. a0 u% o% n以下是创建自定义插件的步骤:
9 Y/ _; X3 l! Z3 l: f' h& r5 P d) ^4 R* o1 ?; O( b
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:; ^" p" }5 ?+ w7 l) f
0 j* s* L7 X& g7 L$ w) N- w
```
: }5 B" w$ \$ D' d# w5 p/ j <?php
0 X0 s: k2 z; v" [ /*0 p2 {0 w! a) a! G1 o# {$ l
Plugin Name: Site Wide Notices Plugin
: s. Q9 g# [+ i. a' U* d Description: Adds a new custom post type for site-wide notices.
* D+ j x) z7 d8 \ Version: 1.0) |6 c* N$ S) H
Author: Your Name
5 K! H) I+ J$ b* n0 I Author URI: http://example.com
; l- d1 r& f t */; \! G7 G. r9 Y" |" R. g
* Z0 y, L9 x, H
// Add plugin code here...& P) P# m1 ?6 u/ w# s
```7 \% I6 Q; `" h3 H6 K
4 A7 B% @3 Q0 a+ z x$ @! r 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
+ {4 Z( }$ P& [2 z, K6 l# X1 V
" d: O$ j$ `0 P2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:0 L4 r' e: w; T
& i1 m5 {' Y9 ~* o% C7 T5 ` ```
- z& Q$ E0 b6 A" L5 J& X Z add_action('init', 'create_custom_post_type');
4 @ F0 _; [) W' D function create_custom_post_type() {3 m* u. O2 d. V5 Y F
$labels = array(
1 |& W$ n" v7 u8 B( D! i 'name' => 'Site Wide Notices',6 H6 ^; u B7 s$ r9 q. @8 s# \
'singular_name' => 'Site Wide Notice',7 x5 g0 }2 I) H' D& v4 {
'add_new' => 'Add New',
( }9 @- _/ e# J Z 'add_new_item' => 'Add New Site Wide Notice',. s r+ W) H6 ^, f, ^: M6 n3 n2 Y
'edit_item' => 'Edit Site Wide Notice',
1 m5 o6 h+ H, ~' q) X' T1 \' c 'new_item' => 'New Site Wide Notice',
4 U3 x- a- x( V! T2 H* _! V 'view_item' => 'View Site Wide Notice',5 |# w0 Q/ V* p/ V7 D( b4 K
'search_items' => 'Search Site Wide Notices'," {; d+ W4 b; @6 x/ D
'not_found' => 'No site-wide notices found',) Q% i' ~4 Y8 C3 }$ r0 Q
'not_found_in_trash' => 'No site-wide notices found in trash') M1 f, q/ G4 }. P" {3 I' H$ i
);+ L w( Z0 m k
: @9 Y/ ]$ U9 J$ O' u' g0 E5 B
$args = array(3 }6 l" x7 _0 ]8 Q8 x0 [* R+ `
'labels' => $labels,8 l; A2 g( ?( [8 h0 B" J
'public' => true,
7 F- C3 b" F. F 'has_archive' => true,- i1 V2 t! E# L* \; {) ^. D* U
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),( n. Z( w1 `8 |4 Z' x
'taxonomies' => array('category', 'post_tag'),8 y! r: s+ g& P6 S
'menu_icon' => 'dashicons-megaphone',
/ `6 R D! R% m7 y) \$ o# Z9 ~ 'menu_position' => 5,
& n" L( m& ]& D) i: S5 ]" } 'rewrite' => array('slug' => 'site-wide-notices')
% N! c% \7 T6 v o/ n );$ Z, Z! t$ n3 V% Z" q, V: z
1 |5 C' X! w% v8 c$ B
register_post_type('site-wide-notices', $args);
, g, a# F6 z9 J' j- u& u& M1 ~ }; _! Z x' i, g5 O9 y+ k
```& B3 n7 b1 ~3 ]+ {* B- x8 t5 ^
1 U; x6 ~) d0 g1 }& x' h* O+ s
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。4 G5 ^; g* v/ O: O: r& u1 P7 F1 [) f
9 Y+ |; g0 N) P* S' }: j3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
6 u5 P1 W% \- h5 g) ^% m" f4 ]( P' `, U9 f" H6 j9 i' c
```8 h% k# S% N6 n2 `
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');/ x9 ^* d7 z/ V) _3 }
function add_site_wide_notices_boxes() {
) G+ ?* }5 S: V/ V" T4 x add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');/ N2 T) B1 n) _. J5 V2 t3 E& M) c
}
% t c' h1 T: \/ _ D% i6 b4 S
% \3 I p! y! U- i function notice_details_meta_box($post) {6 b" M M6 V9 y, p: z
wp_nonce_field(basename(__FILE__), 'notices_nonce');. Z y# w( @2 f& W- ^/ g
$notice_title = get_post_meta($post->ID, 'notice_title', true);
8 C' k p) d: \! Y- q2 I6 o: y $notice_content = get_post_meta($post->ID, 'notice_content', true);% G) _5 Q4 u, l" v% N8 g
?>8 J6 q4 Y; g# M' C
<p>9 [, @ b8 E4 \
<label for="notice-title">Notice Title</label><br>
' M* ?9 n& m' D" h. c <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">6 m$ W* l- u! P# c% L$ e8 T
</p>& E: d! ]1 t$ T+ o5 F8 `- A. i
<p>
' T1 v# E. X( h <label for="notice-content">Notice Content</label><br>9 c6 c, @# @2 g0 V# p
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>7 }- o7 @4 t* `; Q6 z
</p>: X" z8 L9 t2 g
<?php
. t8 P5 s* C" ]9 |6 A }
+ F7 E; J) K5 ~
# o+ P9 H2 {! w9 a& B5 c1 ` add_action('save_post', 'save_site_wide_notice_meta_box');
4 E% K1 Y- o, H% m! O2 F4 I8 s/ g function save_site_wide_notice_meta_box($post_id) {
! @" Q* T# k6 f6 l5 W if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
1 b! z, c! ~) ^5 l0 I return;( V- z) x k# v0 _1 U' e- J
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
% [% q2 B# f/ a return;4 i8 w0 ~: N( M8 S, ]
1 Q7 A+ M! W* ^; S
if (isset($_POST['notice_title'])) {
! p) q% y, m3 ]- P$ M p update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));: q- V0 Y0 u# [6 q5 W4 u2 E, {
}
& K- \$ p. L" W8 Q. i if (isset($_POST['notice_content'])) {6 j* h( A4 J J! y1 u% r$ X3 Y
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
/ `2 m+ K$ ^% m, R3 |! p }4 A! S5 j6 D; l* L$ y
}% `; V' v& C6 O" j
```9 V8 W/ ?6 Q% B0 Z# u8 x$ |
" ^! z! v# D- ~/ t
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
1 T0 M5 C, G0 j/ |* ?$ C q, o7 K2 x
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
- H7 H% H1 O5 q1 e* l9 d2 i: A+ t: s% B. g
```
# F' i8 M" m4 i" l9 F8 N $args = array(5 d9 x+ v+ `7 K9 e
'post_type' => 'site-wide-notices',) t% C; g3 W' p9 U" Q
'posts_per_page' => 3,
! G& E0 T3 e6 h4 j 'order' => 'DESC',9 \6 f0 U- u% \1 e; O* {. z! p
'orderby' => 'date'" i; T; k* ^6 u5 @" g6 g
);
$ V' s& U* P" j8 T% [8 H& d Y# O $query = new WP_Query($args);
) {& J" m& x0 \8 q3 F" k- f/ _ if ($query->have_posts()) :9 ?9 u' `% A3 h$ t$ U( [
while ($query->have_posts()) : $query->the_post(); ?>% @: A( N7 d9 W! }, X
<div class="notice">
1 j7 o' _7 ~) u7 I6 a <h3><?php the_title(); ?></h3>
7 H* m& O6 R+ b/ [ <div class="notice-content"><?php the_content(); ?></div>) O: b/ v w Q1 }: t9 o7 r6 d
</div>) A& | V, f7 ?$ _- [
<?php endwhile;: M7 i0 z: ^; }$ z6 v
wp_reset_postdata();
+ u, j# v/ ?9 X endif;" T' F R' U; r N# K( v1 T* t
```/ `6 s+ v, j7 N4 P
& c8 s$ m8 P! w
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|