|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?- X2 m4 t3 V! z+ @/ g, _
" `; t6 k3 L" j3 o$ Q2 j: E如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。" s( ~/ h P) s& e
3 B, u7 x3 n8 v
以下是创建自定义插件的步骤:
9 x7 C8 b+ V7 s X% s- k( O& R# |' ~% x, J' Z, D
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:) ~) h* b( `& D9 G- U
9 e2 @; W. S* E$ y& w% u$ p ```; O9 F+ @9 V( q2 ]4 e) c
<?php, I2 e1 A8 J- ]
/*6 Z& H" A5 `0 ?9 E% @- O- \7 ?$ l
Plugin Name: Site Wide Notices Plugin
/ g% o9 q. {* ?+ b5 S2 Q& b Description: Adds a new custom post type for site-wide notices.
3 s6 D" o* D( ~: s. p Version: 1.0, ?0 Z: Q6 F+ _, i* V2 G+ R e1 |
Author: Your Name/ l! ?/ T4 S8 O9 C: s) k
Author URI: http://example.com& r+ p% W+ O; S- p
*/% t* l% e W& T
! G H( q: u! p' g& ^ // Add plugin code here...
: m {# F2 z7 o2 _; ~) Q8 q4 q7 v ``` x+ {) _0 H* ]. K; f* ^/ B3 J
# c0 U: s! m% {+ {0 ]; N& E
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。+ K7 i3 k1 v3 E) c+ J3 Y
& X# ]8 F& N0 ~. D, n2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
: Q& F) D/ r+ L3 m! @1 r! S: l% i5 V" L$ o& z6 [: m
```
$ a8 L8 @- @- S, j" z- X' n add_action('init', 'create_custom_post_type');
# m" x/ @0 |$ |8 \. v7 ^$ z7 I. ?( l function create_custom_post_type() {
5 I& v4 t" \& b* _, v, p $labels = array(2 e+ R( d& q6 B' y! ~; ]
'name' => 'Site Wide Notices',! a% ~$ w7 F) T% O! {1 G
'singular_name' => 'Site Wide Notice'," V/ Z6 C1 u. ^% Y
'add_new' => 'Add New',9 b9 {" x: U1 U" ?% m
'add_new_item' => 'Add New Site Wide Notice'," l1 m$ N) `8 d/ G6 A
'edit_item' => 'Edit Site Wide Notice',5 ^' C# } R+ o) c( a8 G9 e# g
'new_item' => 'New Site Wide Notice',
# _ B2 C, i: H" A 'view_item' => 'View Site Wide Notice',
( H S, P: H' f2 x1 N 'search_items' => 'Search Site Wide Notices',
9 y4 ]9 D. ^. E, ]$ n% J1 h 'not_found' => 'No site-wide notices found',
" J# f) a8 F0 w" q0 O) w7 N* h' a) z 'not_found_in_trash' => 'No site-wide notices found in trash'
; K y7 r( b) _- u8 U );
$ X9 A+ O [9 N( [2 {; v8 L8 s
1 @- |2 {# W' y& I* u0 W/ @ $args = array(
! M! Q9 g( J; n% E3 \( m 'labels' => $labels,
3 M. m6 K: ^5 n0 [ 'public' => true,
. V- i; P' b# i 'has_archive' => true,
% p: h# E, l x' @ 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
* F. H+ {2 z( K- {- } 'taxonomies' => array('category', 'post_tag'),
, Z. K; }( b: k( |/ q 'menu_icon' => 'dashicons-megaphone',
2 f# T3 |" p, _ 'menu_position' => 5,- E6 g2 g- P- y5 i
'rewrite' => array('slug' => 'site-wide-notices')) N, _3 X/ {# j9 R! U' V
);# S; z. D1 A9 _$ v" g
' ^( U3 C7 a# r/ p3 B register_post_type('site-wide-notices', $args);8 w4 J- F: D3 F9 u! `
}5 Z+ L5 q* ~2 n. U
```
8 j. O) ]) N: `& n3 R6 [7 V6 G, Y$ N6 K1 R$ |# C- C
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。 Z( d# e: Q8 ]( {% Y. W; Z
( A0 s, |2 g- T5 [6 e
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
- M: y+ g$ ^8 l6 `
9 |5 b) W- Q( K9 |# \& D+ K! `( | ```6 v9 Z1 o, f+ M. W
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
, K; f1 B2 z8 s# y8 ^& o$ z function add_site_wide_notices_boxes() {; Y `- k: c$ S1 ]9 L5 ^' ^
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');& H o( F( e" ~. b
}& X2 v5 T/ x; E# b, W2 ?
1 }: Y: m% j( d5 W
function notice_details_meta_box($post) {
6 c3 r8 \+ z, p. [5 @ wp_nonce_field(basename(__FILE__), 'notices_nonce');5 j$ g: i5 H9 Y5 k& D: b
$notice_title = get_post_meta($post->ID, 'notice_title', true);
& z* R5 s1 O/ p4 y Q3 l5 P $notice_content = get_post_meta($post->ID, 'notice_content', true);7 _( M" ]+ A: ?8 _- O! o
?>; F; h: h0 t$ g4 d6 u! W- O: {
<p>
9 [ W* J& e' d6 O <label for="notice-title">Notice Title</label><br>, M/ d# U" ]0 g' R) ^
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">& ~+ e$ j. F$ }2 C
</p>2 w4 Q9 i, n8 s% h
<p>( Z9 U& O( f, ~
<label for="notice-content">Notice Content</label><br>0 q+ J# z4 p: g
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
) D( S3 a" F5 @0 q2 j </p>; g3 f6 F. T! Y& _& I" S5 V
<?php7 D& i! A$ ]- D& W2 b$ x: [+ Z6 P
}
. M5 g. t8 \& n) s( g# v
* m) l5 c; \5 k5 {! T- U9 ? add_action('save_post', 'save_site_wide_notice_meta_box');" L' s) Q; S' N/ G% f
function save_site_wide_notice_meta_box($post_id) {
' i W: s7 L4 j" ~" P- [: N( z if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
, s6 G4 Q# z1 h return;; U7 [4 b! I2 T4 \/ a4 j9 Z# I9 ]+ ]
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)' `* V6 d. r, W. l; K- Q3 Z
return;) n& J! p8 T! Z. L1 N3 w7 p
3 h" t4 Y p, ^ if (isset($_POST['notice_title'])) {
2 C8 ^" l B5 a3 [6 s update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));, l# C1 u3 R9 R0 H- i+ P
}
3 V" S; y8 C6 m0 z6 F: h$ i if (isset($_POST['notice_content'])) {
( Z& D; ~: X4 O update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
2 {, p, k. ~' q }
. y# n( T% B, d: \ }
; y2 k; k' L6 g ```
, {$ Z6 M0 J- h9 e2 }* e
" B) S( C# n% F6 c# r 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
/ O/ |) l- ~( @. v' E" s( H& t$ A+ A( z! o4 o
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:" B( n" G8 U# d. f! C5 }
8 o2 W2 {; U# ?" c6 d ```4 B+ U/ U3 a6 S/ N4 {' _
$args = array(
; c6 s' c3 a- ?! T ? H 'post_type' => 'site-wide-notices',- ~) l- K2 c& E
'posts_per_page' => 3,4 R# C1 @( t" n) a9 V
'order' => 'DESC',4 \ K$ h( u$ e
'orderby' => 'date', C+ f; _4 Z: }6 H
);. t/ G x) G# J
$query = new WP_Query($args);
4 E7 Z# Z8 f% }' l if ($query->have_posts()) :
+ B, [* i1 [( K2 I while ($query->have_posts()) : $query->the_post(); ?>0 @! `; r+ o) o8 ]4 F# N9 z
<div class="notice">
4 [# m" _5 D) j2 {4 l+ B <h3><?php the_title(); ?></h3>
: i2 ^3 x; ]" n t4 l <div class="notice-content"><?php the_content(); ?></div>
9 X. t& a0 Y5 a Q </div>
( [4 A3 z( V+ \6 ?" s <?php endwhile;. |/ T, f, }9 C. ^7 J
wp_reset_postdata();
) \' j# K$ T; H9 c endif;
4 k! ~7 n+ L" w B; R ```
- ]& l3 V8 ]* `: h/ e, y2 I
! V) r) l% [2 \- ~+ j 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|