|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?7 p7 n+ e: R# m) b$ b* W5 r
p$ K3 P1 k9 f. S9 ~/ M
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
/ \. n$ a& O+ Q0 \# }0 K
; I6 Q, N7 S2 _& F( F. ~以下是创建自定义插件的步骤:
; V; w5 Y. \6 ?$ }- _& ]: U& G4 O
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:. t% f! {6 f! V% c1 P
1 e. N- q9 }2 a: h. e9 p
```4 d5 t3 T5 r/ f8 @4 Q( K& Y1 ?
<?php
& c! w) I2 t2 h: J' `. m% j /*
6 g% M0 H* }7 {9 a% x Plugin Name: Site Wide Notices Plugin
/ U+ i. R( b, G m" X( } Description: Adds a new custom post type for site-wide notices.
0 c4 a* q% U p8 a* r" W" T- a Version: 1.03 k" N7 N/ _4 T
Author: Your Name# U7 Z- z9 v# F; _) S6 q2 t( R
Author URI: http://example.com
" x% J2 S6 R! b' b; o; N! N, l */
+ z; J. d! b3 [( a$ l3 A8 o, g6 E* g- r( j' e! P4 X# [. d* h
// Add plugin code here...
8 O6 h- w$ Z- B3 C- Z" A6 ? ```4 v3 Z" H% w2 m" j, [
4 T$ \2 Z6 M$ b$ a* x; o
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
% Y( p9 j+ S3 i& [4 A! C4 T. l: c; h {* }% A& h' r
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:" W8 c: J; J O" h+ n" q
' [- s* S1 h& ]+ e0 t ```( c+ ? j: B" Z" \
add_action('init', 'create_custom_post_type');) r) S |7 V; i V3 C% M ]- ^
function create_custom_post_type() {# b. P/ q9 O$ j J9 y) [
$labels = array(% A2 P" {: @" E. D- P
'name' => 'Site Wide Notices',
+ D7 g' l4 Q1 {0 i. _5 S 'singular_name' => 'Site Wide Notice',
! J; s" f0 W8 g. N, T 'add_new' => 'Add New',
( r L$ S3 c/ S, M5 Q 'add_new_item' => 'Add New Site Wide Notice',% R# R$ V% }1 D5 Z R; D+ J" e
'edit_item' => 'Edit Site Wide Notice',8 [9 Y/ @8 r* `% U1 G: F% H4 }
'new_item' => 'New Site Wide Notice',+ y7 [6 g2 [# H* X7 F$ L
'view_item' => 'View Site Wide Notice',
: {! c! F& G. u6 E1 Q$ [ 'search_items' => 'Search Site Wide Notices',
" \. b0 T% d* X2 h2 n 'not_found' => 'No site-wide notices found',
; Y. ^" l4 l A5 f3 f( ?3 L8 t 'not_found_in_trash' => 'No site-wide notices found in trash'
k/ P9 ?; y: b );
( K6 i6 E5 S* D) k9 D7 K
) c6 W7 c) r9 }7 h4 Z $args = array(
+ T4 F" y$ v+ r( m. D2 K 'labels' => $labels,
7 s/ m, Z# l R6 O5 o 'public' => true,
2 @, K2 f% w& p8 y) o$ L7 l: o) } 'has_archive' => true,( w4 E$ c- o7 S! |; `7 i! k
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
) V* I2 R! C$ m# U 'taxonomies' => array('category', 'post_tag'),
" R7 t" D, o! W h, C# j$ G 'menu_icon' => 'dashicons-megaphone',
2 R' L# r( t0 E 'menu_position' => 5,
- ~% @6 h- j6 h' F9 U X% G6 ~# W, a" b 'rewrite' => array('slug' => 'site-wide-notices')
# }4 t. f/ d3 L );8 W) P! ?+ s5 s1 j
# c8 ~- N0 |% I, \2 o B; Z" B5 k register_post_type('site-wide-notices', $args);
( Q6 t, |( N7 F4 P8 V K }
$ U0 f# Y- ]! T5 l( j ```
( m. i1 D) l5 g# X% z# H4 i
5 ^/ r) P4 a7 f. X6 @. v" f 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。( O6 L0 H6 }+ L' E3 ^8 ^
7 B; F2 O' B; s. h, }3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
/ e* b* T6 u* K2 r: [+ u% B
. W" Z7 @6 Q+ U; p1 { ```
) x- { \) V0 g add_action('add_meta_boxes', 'add_site_wide_notices_boxes');2 V: c. [& t5 E4 A% n$ p
function add_site_wide_notices_boxes() {* p/ ?4 `* P; {0 K
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');8 F" k8 @4 q& n9 [" w7 Y4 e4 q: Y$ ?
}
& n& R. N# A( m( d
+ D/ b; j$ k) F4 Y function notice_details_meta_box($post) {$ B4 L$ [! T$ w. _: R$ C; T; x& b
wp_nonce_field(basename(__FILE__), 'notices_nonce');
2 y9 F7 P8 N% S' v! ?6 I $notice_title = get_post_meta($post->ID, 'notice_title', true);- \, f8 U$ f' G' |$ ^/ e- v8 M
$notice_content = get_post_meta($post->ID, 'notice_content', true);( o5 a: m, ?5 ?0 S
?>
* T0 X o/ w# P2 Y- _( R <p>) R" a- _. c% S4 O' T
<label for="notice-title">Notice Title</label><br>+ T m; B3 k2 I ^$ o L: e
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>"># ~, _, y( r$ J- s' Y
</p>
$ Y0 R, E2 K2 o/ \ <p>0 l: x3 r! S9 \4 q0 ]
<label for="notice-content">Notice Content</label><br>0 }$ [3 M8 Q+ v3 j0 S6 u
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
9 I9 k: R) v- x% [* J1 X+ k, x </p>( Z9 [- k8 F# l* J d$ B& U7 v
<?php" q& n/ N5 S; F, K& A0 e
}
) L$ J3 O+ m v% K
! A* `8 M% `8 T1 E4 T; i# N; s add_action('save_post', 'save_site_wide_notice_meta_box');1 ^6 U6 q+ G5 L+ s3 L: E" I
function save_site_wide_notice_meta_box($post_id) {: N' O6 `: d$ l* e- m ?% z
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))$ N: B; H4 P6 J# L2 O5 P
return;
5 X( T% L; J6 ?; m2 T if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
, o0 u- S% D9 O return;% e: \& q1 u7 [( g- D
7 C u% k# I, F if (isset($_POST['notice_title'])) {
4 C: W; F! {2 l5 w$ V( Y/ _+ H update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));6 C3 j X9 _6 h+ D3 N
}
/ E5 A8 i$ K6 ~: {& ^# ]2 p if (isset($_POST['notice_content'])) {4 L- ]7 k8 u. `3 X7 [6 v
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
+ y0 C8 [' S2 v8 k* Q8 U! g) r }
/ r3 I. z4 A# p% `4 q4 J# [$ |% G }
# n9 n! A, G0 W( _$ W* J+ B; q ```; V+ Q+ G; Z8 s3 H* x9 u
! E; \. p4 _- x' t, b! W
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。3 W3 J! c+ c/ O H9 Z1 i( O
7 o& V9 E) Z% X' a3 p5 b
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
8 A) h& o! Q w7 E0 m/ ?9 ^! l0 Z: t7 p" l4 w
```
! c8 Z; U7 h3 E+ U $args = array(5 c& _1 f- i: F3 a& f( h
'post_type' => 'site-wide-notices',
0 [ d$ F! t5 s' T 'posts_per_page' => 3,; T3 r3 W# W9 K; u6 v
'order' => 'DESC',; P2 A9 G: ~" U. a/ K) k5 B
'orderby' => 'date'5 ]' {$ F3 G2 J5 I5 y+ i& k
);
( F# r1 q! J6 O $query = new WP_Query($args);7 Q, j( L c9 r$ h" ~5 T
if ($query->have_posts()) :
6 y/ T+ G: p N3 a$ s! C1 R while ($query->have_posts()) : $query->the_post(); ?>
7 J4 F4 W) o6 T: a <div class="notice">
2 h* S# X$ l) B8 E: l <h3><?php the_title(); ?></h3>; S& x/ c( G/ k( M# ^
<div class="notice-content"><?php the_content(); ?></div>
6 r, a0 Y1 {% o8 r; e7 X9 Q, s; Q& { </div>
, L/ t, D3 s7 h) p <?php endwhile;
1 C& ^- `) M4 L& ~$ Z wp_reset_postdata();
w! i9 ~& X5 q endif;: t/ B: p n0 g1 s7 {
```
! p7 h; I0 l# x2 f3 J c: h5 k: }' D7 ~$ I# H5 M- o
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|