|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
4 b2 `8 Q' _ j! y) m7 n( P1 {; R+ \( R1 t
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。3 \) z$ l+ U k* O" o
/ }% f( t5 ~$ {9 ?3 g& L# i
以下是创建自定义插件的步骤:
& @- g! `* g$ T
?0 W1 }6 c4 x1 _( u1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:' _1 j' l: _8 `
4 r$ w1 U/ X$ a" V. p2 |& R8 k, J+ m ```
+ P" G! f7 I3 l, M7 m9 n9 T( k <?php
" C$ H! X! v2 o/ @+ r2 J, }( \* `9 D /*. R& C5 [, @6 x5 A3 P: q3 l
Plugin Name: Site Wide Notices Plugin% Z9 o* K- K% F- i& E% z7 I
Description: Adds a new custom post type for site-wide notices.
" W, @' x' ^4 k$ G/ Q Version: 1.0& [" e& ~7 _% N5 S% Q
Author: Your Name, k/ s! y& N& V0 p# k5 c
Author URI: http://example.com, z* U7 E% f* q- |+ v/ s: ]
*/$ a* v& W# I/ R( A0 E7 E" u3 ]
/ _" H: d$ t6 u/ o8 ^: M7 U // Add plugin code here...
# y* w: U* E. c. o! Y ```
* C+ R& q" l" O' X& @9 _, M5 f$ w$ I+ B. r0 E) v
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
$ y6 y* e; U. c
3 T, F1 l9 t6 i/ ?! V. e# ]( O2 E2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:, m1 `9 G: S1 S; w5 b
% Q3 }; l7 y! f/ ]1 T
```
/ J5 S8 R, d- I' T6 g5 E% \ add_action('init', 'create_custom_post_type');
/ J6 I" d ]& N5 z$ L: F6 B function create_custom_post_type() {
$ [9 D4 Y C$ ~- H+ } $labels = array(; I8 `! {0 D/ u1 @
'name' => 'Site Wide Notices',5 b5 r$ [; N) z) _) d2 x8 H
'singular_name' => 'Site Wide Notice',8 B1 W7 s! S! m3 w2 T% T( M( u
'add_new' => 'Add New',/ W, ]: m( K- B2 J1 t. j
'add_new_item' => 'Add New Site Wide Notice',
0 o) e6 r& p4 x' R5 D$ s9 o 'edit_item' => 'Edit Site Wide Notice',
/ K2 t+ |! H' r/ q! K/ C 'new_item' => 'New Site Wide Notice',
7 n0 K3 {# c& p, z; t4 @* K6 i9 e 'view_item' => 'View Site Wide Notice',9 l* c/ ]( L5 s/ b0 e
'search_items' => 'Search Site Wide Notices',9 R; R7 R% z7 V' W
'not_found' => 'No site-wide notices found',
7 g x8 d8 @) ?8 r0 ? 'not_found_in_trash' => 'No site-wide notices found in trash'
% p9 F& N: o9 O! H );
8 i D1 H1 b+ Y% `4 C
6 K# W" f6 v$ M1 F" G/ v5 r $args = array(1 ], x) T V4 H: n! v1 D
'labels' => $labels, r6 G2 \; `# {% ], Y, a
'public' => true,' L% e3 R) y& `8 P+ d; u, B
'has_archive' => true,8 G8 D. N- L7 z6 c% w& J
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),4 D1 o0 I9 W+ k0 e0 H _
'taxonomies' => array('category', 'post_tag'),
w1 V. } R7 _& o, M 'menu_icon' => 'dashicons-megaphone',
- d4 ^0 I; _: c& ]2 B0 g) L 'menu_position' => 5,* i' t y9 [# v# Q% k& d
'rewrite' => array('slug' => 'site-wide-notices')
' ?' x8 d3 i: C8 k, v; n. A );/ T' D0 e( M6 f0 a
1 t5 P' u i# \& s- J7 V3 ? register_post_type('site-wide-notices', $args);8 [" ` c$ |, Q5 w5 \9 K$ n
}
6 E+ C w. L( ]+ `; T/ e ```
_! G* Q3 F& o# N5 u
' s$ j& `! }+ ? 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
, r2 ~2 h! T/ N' U( K7 @- C, _& e% \# |6 t3 d8 H# U
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:/ i7 x# _5 b! U6 y* f: X
8 k5 E" ]) D! n/ B: v0 U7 L% V. P+ ^ ```9 ` i, W8 d' N
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
7 K2 b% S; ]" A7 T function add_site_wide_notices_boxes() {
. c' d N- |( k+ X4 ]2 f" H4 S add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');& r3 Z( G0 P1 Q3 ^+ c
}
) l2 c5 \- F) O
" E4 H. I9 u D" X9 k function notice_details_meta_box($post) {
+ F4 c5 {# ]6 z/ \5 d wp_nonce_field(basename(__FILE__), 'notices_nonce');
# G( S* d4 @" h# Y3 ^, X $notice_title = get_post_meta($post->ID, 'notice_title', true);
, \0 H: J& r- ?2 z, j& d/ \. p $notice_content = get_post_meta($post->ID, 'notice_content', true);
5 k& \& E" U/ u ?>
. t, Z( u" K) u: x+ i <p>
6 A8 \0 q5 G2 o <label for="notice-title">Notice Title</label><br>+ T' `' q7 i" ~: o. n& Y& a
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>"># j/ L" M0 ?9 Y$ o5 z8 l/ v
</p>7 p3 |( T4 c9 B, J1 ]- V+ D
<p>
! q& a n1 T3 K/ q5 b$ X. X <label for="notice-content">Notice Content</label><br>. M+ C0 R( O1 L% d! H) [0 H
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>6 @; T2 b& |$ |& u& V+ l" o
</p>6 k5 [# n. V- k" {1 J% L
<?php% z7 M; c8 p$ { |" k
}
; b1 T, w" d/ _% j, I, Z3 u L* n$ U6 v4 C" [
add_action('save_post', 'save_site_wide_notice_meta_box');; ?% a0 W' X/ Y3 Y
function save_site_wide_notice_meta_box($post_id) {2 j" _! @) \5 B: p/ h" `' R( E7 h
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))4 W4 P$ @* F/ N3 d- `$ k
return;6 Q* U# e+ B+ X% [7 Y) S; i4 _
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)7 Q& u; Q) U' `! B
return;$ L% p. @2 u2 A
0 ]0 M5 g7 G7 k& l0 V8 P# } if (isset($_POST['notice_title'])) {; H" N5 H2 p6 g- s
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
6 p9 Q3 A) [2 s, C( g8 t }7 M; u: J$ D% i/ x5 h- k
if (isset($_POST['notice_content'])) {/ m& l G& f$ b+ T6 b" t
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));1 x5 K1 l1 ]7 l/ b5 c7 O1 `
}4 l2 A K$ Y3 W" P* s
}( r" g- R( h" R& L3 c7 y
```9 ?* m& X* L: y, w; b6 Y0 [& c; p
+ n& A3 i! Z- C+ F; `8 f
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。 \- r" ^! L3 t5 @
3 v6 n4 @7 f" G3 r4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:: J; d0 u/ Z( { a4 J+ ]% k
6 g) x/ W g. D! V
```
8 l) N3 Z" M n $args = array(4 c) I6 ?4 j# g! H/ j9 M5 M: w
'post_type' => 'site-wide-notices',: U5 e3 g8 j9 t! E8 H- `5 T
'posts_per_page' => 3,
) p4 X+ b( g3 J6 i T 'order' => 'DESC',4 A2 j. U* Q& l* A4 X
'orderby' => 'date'
% |7 x7 ^/ `# S! V: W );6 c8 u8 J9 T @
$query = new WP_Query($args);
5 D2 \3 n" e: g- U if ($query->have_posts()) :+ W+ g! h8 D7 [; y; ?$ B+ a' M
while ($query->have_posts()) : $query->the_post(); ?>
5 @6 M( `4 r a" J2 A# c <div class="notice">3 g, g4 m' N7 q" v# x
<h3><?php the_title(); ?></h3>
& h* |0 o% g* h' a: h3 q <div class="notice-content"><?php the_content(); ?></div>
. x7 ~* T5 U8 V4 Q6 @# U4 X </div>
+ f1 P! F6 l. t) _9 @) f <?php endwhile;' V6 b: k: C: Z- U+ E
wp_reset_postdata();
4 j' r- V1 @) _/ S+ u# y- e endif;
( H: F# I4 J* r3 l* X1 `9 r4 _ ```5 d9 E5 t9 z; v: t
6 i' a- w/ h% V# v/ r* O F
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|