|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?0 I3 F% Y. _: H% q% t
- w( K* I! l. R. Q$ S$ ]" f# M/ p如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。9 m/ S5 x* d. g
/ A7 b5 m @6 f7 W, a' D- r3 i
以下是创建自定义插件的步骤:$ g- P5 b, Q9 M' a) k# i
' S9 R/ V2 c8 I) m5 g; U
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:& r) n9 F& d0 r
: V2 M8 K/ s, @9 B" X- ?/ `
```
1 S% L! k2 F4 T7 \ <?php a7 \: X# B. S7 P8 q
/*' |; w Z: O( x
Plugin Name: Site Wide Notices Plugin
2 [1 s/ Q' V& P7 h, r/ o Description: Adds a new custom post type for site-wide notices.
* D0 }8 C" m# [# C6 ^0 v Version: 1.0
2 p! W" P# v$ ? Author: Your Name! R9 H# J4 e, B7 }' K
Author URI: http://example.com) C& R8 K* T" }, r3 b; F
*/, S$ F8 L1 H: q
4 M9 }$ ~6 J7 G% ~3 P // Add plugin code here...
& V8 m1 l. V/ g m' r# e ```- t! c. f* x+ }: O# [2 B
' a3 ]$ h h8 ~' K/ V 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
, H$ X! ~5 r/ ~7 k& ]" |* P7 ^$ A- r9 L$ `0 Y
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:* H$ U/ z/ v/ c9 L; R" `/ W+ R
1 y% \" f3 x; {
```
: B' y2 c1 q- o6 O5 \5 W add_action('init', 'create_custom_post_type');
2 g: N; e, r, j7 o' W! J& H. B# _ function create_custom_post_type() {
/ [, W) N. z5 x; t4 a0 c4 F $labels = array(* a* B8 }8 U8 a& `- v% S
'name' => 'Site Wide Notices',: Q" ?8 x; I( t# v% ?
'singular_name' => 'Site Wide Notice',; `: Q! P, `. N" `6 z/ t! e4 E
'add_new' => 'Add New',
! H3 `% T2 T8 z- x. E 'add_new_item' => 'Add New Site Wide Notice',' s- c3 [& n* B; F; T
'edit_item' => 'Edit Site Wide Notice',
) q/ }9 q( W L: A2 ^/ c& ~% y 'new_item' => 'New Site Wide Notice',7 z1 y$ O! V. r; Q5 y
'view_item' => 'View Site Wide Notice', m3 W9 p. ? i: [, j
'search_items' => 'Search Site Wide Notices',6 }" T9 |: v( b1 R' N2 J
'not_found' => 'No site-wide notices found',* j/ H4 u b& h: J; l! [
'not_found_in_trash' => 'No site-wide notices found in trash'
0 }3 X1 {" t K7 R$ y; [9 P );' l% F( q, v; R( q" c5 n& P0 S# H& j
8 W4 c) G7 W5 U0 s
$args = array(
" c I6 {1 L* n0 p 'labels' => $labels,
2 K/ O/ P- g! M9 |5 y4 m 'public' => true, i1 Y" _+ T9 T# L7 E
'has_archive' => true,+ Y; W# G6 P; U- D
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
' D# P. s0 r6 A$ t 'taxonomies' => array('category', 'post_tag'),
! z2 B1 t; P3 X/ H0 K 'menu_icon' => 'dashicons-megaphone',
$ x& N3 A# F1 D- _$ E! y 'menu_position' => 5,- F0 @0 ~1 k7 [4 d) k
'rewrite' => array('slug' => 'site-wide-notices')' D) j% m6 L% [( p! h
);
! w) X7 z/ ~8 ^( o% s/ |2 E r1 G+ M5 O% P! l1 h( D9 m
register_post_type('site-wide-notices', $args);
& n( S. U, F" H, H8 q9 M$ B }/ [/ g% G% @2 `8 d4 R
```
- { R. f( L U
+ V3 T3 n" i; N! X2 l 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
, W' ^4 a: D) v8 ]) m% R( K9 m0 p2 B% P& { j4 Q8 `, N$ `) D# [
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:- F, j+ a/ H! i$ u" X* m
+ y1 o- a0 q0 w+ K
```
" R( B4 k2 N+ {( m+ R; w) N8 g add_action('add_meta_boxes', 'add_site_wide_notices_boxes');) x' [8 }% y% R7 z$ T
function add_site_wide_notices_boxes() {0 p$ @& v# S J* e2 K+ D
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
' }: D6 f) L$ F j2 j }5 V- q% |; u+ R; L: ?: D/ M
% \+ J- P: u- z: ^( _
function notice_details_meta_box($post) {
9 x* S4 K X- e( K8 R2 j$ { wp_nonce_field(basename(__FILE__), 'notices_nonce');
* Q$ e: B" ? p' o7 G& |& @2 k, F$ x $notice_title = get_post_meta($post->ID, 'notice_title', true);/ r4 X0 T# k- {8 q4 x! y3 C
$notice_content = get_post_meta($post->ID, 'notice_content', true);
! M" \* l5 J) w$ h- F ?>
' `- P% D: U6 H- ^1 ] <p>
4 e6 G& X4 b( H- q <label for="notice-title">Notice Title</label><br>
; E0 [: F# y7 {+ H <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
6 O# h6 w- ^8 z: d5 B3 z+ r. d: @ </p>
8 R2 H8 ?. n* P5 T1 a0 t <p>
8 `# i/ a- S! z' ?' M: y; G <label for="notice-content">Notice Content</label><br>
* O7 J9 p+ p. b% R <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>, N4 E! J# e; H
</p>
7 K6 A6 Z( z& y5 P) E6 s <?php) u4 A, L4 J! F$ S+ n; |
}
8 Q/ Y, Y, b8 R
& x; J/ V- N% |$ l" E add_action('save_post', 'save_site_wide_notice_meta_box');
( {/ _' Y6 ?2 n/ t, I function save_site_wide_notice_meta_box($post_id) {
: R/ X/ j* G& I& v7 O, |# Q if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))9 u& l+ J! |( I
return;/ H9 j2 y/ {# q, `( l# Y
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
( m. s. x( ^# @/ V4 T return;! V& j3 s$ e- K9 E
( {( t3 w7 {$ ~: q% {$ `8 g" F, T# s2 w if (isset($_POST['notice_title'])) {0 y _; X; G& B6 k3 f( O: W
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));$ A" u6 u0 A* M! g
}
, T' M; _! y" Y' B h% k0 y0 L& e if (isset($_POST['notice_content'])) {9 i, h+ a, d* u
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
3 [9 u5 y- T# X' t8 a }+ C0 h* E9 _) |( L4 i5 d, h
}5 _. c+ V1 P; g! I* c9 }
```
/ y p3 _. K0 g1 v* j5 P$ N: l4 ~! R0 Q; R1 [$ J1 p
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
! v; k% R: t, e! Q# e
4 t4 o% u) I7 j4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:$ {: b. D0 T' L9 q- Z4 I- y8 ?
0 ^9 q W+ a5 l$ m4 k% L% R% U6 r | ```4 F3 h: D* s9 O" D% ?9 ^
$args = array(
/ K- ]( ?8 W- J6 n" z& w 'post_type' => 'site-wide-notices',! _( q) W2 M. X3 r
'posts_per_page' => 3,
1 f6 |) e& x, x `$ P; w: [ 'order' => 'DESC',
$ ` v, |. e2 c, n2 e. }% A: r 'orderby' => 'date', ?( u- j3 C& q6 L3 L
);
1 W; Z* f; I2 V2 G! h: B $query = new WP_Query($args);
, M/ I W" A6 S1 D if ($query->have_posts()) :
9 ]. ?. ]/ W5 u! j( ^& { while ($query->have_posts()) : $query->the_post(); ?>. D) d( R- v; D( E
<div class="notice">
3 p$ x+ e; U" A" {( Q# L- X' y <h3><?php the_title(); ?></h3>& t' T, r; k" @
<div class="notice-content"><?php the_content(); ?></div>
5 k, g" q: e3 B& R* \% L/ V </div>
# m4 ^8 u$ z; {( ]4 e, A7 Y <?php endwhile;2 H u& d$ Y. N) x4 C( |
wp_reset_postdata();, s+ Y1 z: `1 L s
endif;$ r9 o) H, t1 }
```
9 t8 O8 B7 k9 Q8 N# H3 X
& u, k& f" S; `) j& P' o. v6 C 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|