|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?& x: o1 [ S6 \) ]' A5 z
! ?" h% S q! i: U) T如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。, w7 j" n! Q: U' ~+ c
& a, q* V+ p7 z$ X: w$ `以下是创建自定义插件的步骤:
2 L, r4 X: b o( {- _1 }5 a' d- l+ d% h0 |! A$ u- k
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
' E$ m' P0 o5 V9 t5 H' L6 Q% ?/ r C8 W: h9 h3 t
```- k' X1 W# V; T1 P9 x- T7 K
<?php; D- B, p, @# k/ m% K7 C+ R% C/ q
/*
: \! H$ r" T2 n& _) P7 N Plugin Name: Site Wide Notices Plugin
3 _, f' R8 D. m6 B: [9 a, x Description: Adds a new custom post type for site-wide notices.
1 A7 l$ k+ a K/ L2 H$ B4 d- K Version: 1.0
$ e. k7 p1 x5 X E r, u: N Author: Your Name
7 a9 O8 B% l f+ L$ ^1 _4 j Author URI: http://example.com# ]/ t' M% G4 D: \
*/# Q' T; W O9 s4 ~+ g4 }
4 j; G$ U* q/ k // Add plugin code here...0 i" N" d6 a2 e4 U X
```' F6 l* U" @- [# l
7 K, ^7 i- _' U; ~1 [. z+ ^3 U 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。8 A, C# p; ?+ y" c
( \8 K- y9 d" q! k% `" h9 d' i
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:* Y0 J$ L3 d& V9 N/ A
/ X4 h0 _ ~. J7 G7 R ```- A& ~- Z% Z3 V5 {
add_action('init', 'create_custom_post_type');% ~, ~5 Z- N3 f* r }& P
function create_custom_post_type() {
% r# Z6 a- I/ l0 x $labels = array(
' ]- {% G( s3 ?, e: @( p* c: E 'name' => 'Site Wide Notices',) O7 ?" z L7 G7 A
'singular_name' => 'Site Wide Notice',3 X! [: Y; u7 c/ i- O J/ G- F9 S
'add_new' => 'Add New',8 d, {3 O5 y0 z% z% H2 `
'add_new_item' => 'Add New Site Wide Notice',2 Y+ H9 K: U5 o8 ^ T# s
'edit_item' => 'Edit Site Wide Notice',
- p5 o7 I3 W- D3 J# k7 P3 z 'new_item' => 'New Site Wide Notice',
1 L: `# ]8 P' n6 s7 w8 e 'view_item' => 'View Site Wide Notice',
_. t4 x% t' j5 J7 e! c 'search_items' => 'Search Site Wide Notices',7 T0 {6 x6 g; ~, a. c( w& i1 [7 y2 f
'not_found' => 'No site-wide notices found',
, J1 j x) g1 W; ]3 l% l) a" M 'not_found_in_trash' => 'No site-wide notices found in trash'9 g1 T* V& B# @2 t: E: q; u
);
# z3 K5 |1 N* k4 @. M; Z: P& Z, i" o, O6 ?" b0 C4 |
$args = array(
* j$ d/ N6 \- k5 H6 ~ 'labels' => $labels,
5 m. r& { G2 l1 O; \ 'public' => true,
8 _: h) s7 g8 L 'has_archive' => true,0 ~7 Y) T% r9 h: D3 F
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
: U- L1 _3 N0 X# h 'taxonomies' => array('category', 'post_tag'),: ~6 X0 U( q2 f: f
'menu_icon' => 'dashicons-megaphone',) @% Z* |( G' f3 r
'menu_position' => 5,2 [( X* r$ m4 i$ \$ t
'rewrite' => array('slug' => 'site-wide-notices')8 w& d3 R: R! M( X% H2 L9 k
);2 P4 k! a. E7 a6 w$ @/ ^
2 ~8 G& F$ b! ~. ?2 M0 P+ ^4 n
register_post_type('site-wide-notices', $args);5 V- F [( x5 w. y2 H
} |) y* f8 r( ?2 v- e
```
) C3 `8 I3 ?" u5 s* K
+ {1 c7 a" |& l 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
/ Q; a: s$ S+ O1 H( N
' Q- I1 j" [$ T3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
: z! h" X5 x1 V' F8 l* o9 J( a0 e2 z( g& T
```
# i: q; G' i& M( q4 a8 } add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
; m0 S" _% N& k' C6 M/ c+ t function add_site_wide_notices_boxes() {1 Y5 B7 X M# L s0 t
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
& r3 h- F2 a O) M }
! L4 Y+ {; f u; h% y$ C" D
3 l, m/ P3 M' j function notice_details_meta_box($post) {
$ B/ h' P5 D# x9 g wp_nonce_field(basename(__FILE__), 'notices_nonce');' Y1 e: k9 M6 L F' n7 }
$notice_title = get_post_meta($post->ID, 'notice_title', true);% w9 u5 S; Q3 p1 m* h! c d5 s0 `
$notice_content = get_post_meta($post->ID, 'notice_content', true);" c$ R! n; h. ~1 `- x& r
?>7 R( C0 [ _4 ~; { c- \
<p>
$ I& j( A) m7 J9 g <label for="notice-title">Notice Title</label><br>& P9 r. l R: R, g
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">( i. q- p8 G) l6 V5 ~
</p>
9 |6 U6 K8 u" A4 [9 F <p>" t7 `& f& \) k# l
<label for="notice-content">Notice Content</label><br>( C. {4 {; {& F0 V3 H4 e# D
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>7 }( J* ^% n4 n6 H
</p>
A, k9 \5 {1 t0 e- g <?php1 |1 @/ [ j {8 w
}: s5 G/ r+ B) Q1 ]
% H! v% |2 C/ D add_action('save_post', 'save_site_wide_notice_meta_box');
3 ]8 R) J% A# i% t; ?! m" b function save_site_wide_notice_meta_box($post_id) {' \9 ^) `. [' B4 ^
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
+ C, q8 Y5 F- H/ _3 L return;
: U, Y2 y# T' W0 m! q4 @ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)% c! e/ D' D: s( k. x
return;
, l" M# \0 g4 |# @. U2 t8 |0 G
% d y/ S$ V4 R- L; H! I% } if (isset($_POST['notice_title'])) {
2 c' E7 S* O. P6 \- m update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
" }- a' ]' ]/ n }
* Y5 v$ ]& m! F6 V. M; @ if (isset($_POST['notice_content'])) {
" k* _' Z! Q& D1 U update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
; I% R+ u0 s, h } \2 s" |) O1 X6 f( O2 Z
}2 m1 ~: \0 D% T; r
```% g) K1 U' M; u! u/ I9 P! _3 Q
2 j# L v$ h8 a ]
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。6 a# A" b2 ]2 G" l! T7 y" s0 w
, p, L+ D& r0 T" U/ [
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
% c F9 }3 X/ W9 B# Y( u6 N" j' M7 W- z- Y4 X% F) }
```
0 A, C; q' Z. Z. J% E. z$ Y3 D $args = array(
: H: A+ M% Q4 Q# T, z: e4 s1 w6 y 'post_type' => 'site-wide-notices',
3 p/ T2 P' u+ A* m. q2 q 'posts_per_page' => 3,
) Q. z2 Y% N' ?: o" }3 }4 S 'order' => 'DESC',# [( H$ f2 _( S U" ?* [+ E
'orderby' => 'date'7 r( w+ S$ R0 Z+ F
);
9 P( I# c H* S# g $query = new WP_Query($args);
& z8 w% W6 w9 V& @* C if ($query->have_posts()) :1 `. q0 N8 C8 k: N% S" [- o; C+ Z2 A; i
while ($query->have_posts()) : $query->the_post(); ?>
' A; K% W% J+ a* Y <div class="notice">- v) T P) O( y! _9 {' w& e
<h3><?php the_title(); ?></h3>" [! }% {/ L ^+ v: E1 f8 n1 r
<div class="notice-content"><?php the_content(); ?></div>
8 C# F, C/ h/ M l </div> A! B Q& l% e- p* _3 N
<?php endwhile;% n5 \. s: O1 _+ E/ s8 e
wp_reset_postdata();
4 F6 Q8 E( u+ |" D* } I1 Z7 p endif;
5 I' o4 {4 U+ i; {! E: p6 A ```
: \2 {. q+ p7 _3 Z2 P4 c$ D8 y* r) R! I" i. ]
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|