|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?5 o/ l0 W- h- P8 {8 V* j4 ?3 T
5 J3 S" {+ Q! N/ N如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
6 u/ ~; o, ~ p, Q
9 o) f; U; |% j* `2 u, i5 k以下是创建自定义插件的步骤:
3 {) {2 G5 i- f" P, }1 X6 m
* U6 ~8 }! M" a" [( M- m2 h" D+ J1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
7 P) C0 z1 c) t% h/ D5 v9 o0 N+ k0 O! u: x- ~; y; E
```! i/ M3 u" T6 `" x, Z+ Q$ ~
<?php4 R9 w8 r! D0 U) {# G5 J3 z/ ]
/*8 w w2 C% d7 R, S
Plugin Name: Site Wide Notices Plugin; C* [) O, _) E& M6 F
Description: Adds a new custom post type for site-wide notices.
; ?6 t* t+ H$ o o, _ Version: 1.0) A4 i& n5 k4 }, z, q
Author: Your Name w3 K7 I0 s" s% O+ q/ t' r9 c: O
Author URI: http://example.com
0 f/ N. I7 Y+ D3 Y5 x */' R9 m+ Z3 h% e2 H- ] W9 y4 g9 R: b
$ H0 y" y% u: E+ d/ C
// Add plugin code here...4 M& u5 [, j1 D! A
```% t1 m& s8 m7 X4 Q& @& r) K
5 W0 E. D' W& U6 F! ]) ~; _9 Z 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
/ m' R. O+ E" [; M3 @4 A
0 @ a# ~9 q- R0 ~. I/ |! o2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
6 o) q$ [# Z$ r( H
8 g+ N' g- z0 w- |/ Q% G+ Q _/ L ```; u& K1 E* h8 x, I! K
add_action('init', 'create_custom_post_type');$ }0 a( l9 Q; r: [2 P. h I
function create_custom_post_type() {6 B7 y! x( @7 i9 U& V
$labels = array(
' ]1 @ G/ D w7 p3 e O5 e1 l! G 'name' => 'Site Wide Notices',& f4 M" Z' g- D
'singular_name' => 'Site Wide Notice',# @6 i; T1 ~- A- O7 k1 B
'add_new' => 'Add New',
7 Z' i# `0 `# k* i7 i1 b 'add_new_item' => 'Add New Site Wide Notice'," ]/ d7 n! o# q6 Y$ y1 w
'edit_item' => 'Edit Site Wide Notice',1 D2 b& c3 [# k
'new_item' => 'New Site Wide Notice',' i- e9 B \& W" `% j- Q
'view_item' => 'View Site Wide Notice'," E: l0 t& G& r* n! [# m# O
'search_items' => 'Search Site Wide Notices',, f- I2 ?- ]& y6 m. z/ u
'not_found' => 'No site-wide notices found',2 ]2 K% Y1 g' {, c
'not_found_in_trash' => 'No site-wide notices found in trash'
* T+ m* l, L- i! m# j7 | );1 p0 R2 L$ y, { g
; G5 r; M4 M( j/ U$ Q8 |! I
$args = array( U4 Q1 v9 A. w! f6 f" _7 Y
'labels' => $labels,, x5 L, Z# U0 o: S$ R* c- I
'public' => true,
/ F! X/ u6 L& O& B8 g# D E. _ 'has_archive' => true,
4 C* k0 E& i& O% w9 Z1 T 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
1 K1 I0 P C" x2 |* b 'taxonomies' => array('category', 'post_tag'),
/ D: Y* u. [2 I) F8 `! T 'menu_icon' => 'dashicons-megaphone',
/ b. U; N) q& z% n" @0 G 'menu_position' => 5,
9 ^# @3 @: W H( j- S 'rewrite' => array('slug' => 'site-wide-notices')9 w' v T2 r* p
);
1 t! V7 @7 f* P$ K9 g) `5 D- G+ t/ `9 U q
register_post_type('site-wide-notices', $args);
; [7 G# G, P- }' G" H4 U+ ~& a }1 t X4 A) d" p
```
" W; q1 C9 K2 |4 `. O7 K6 q* T+ v+ o* t4 N) J
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。" W7 a! }# ]; K! T0 O1 O: H
* G; j7 C" L5 K
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:5 Q& a% o3 ]9 c9 Z& I+ @; J3 T# b/ j
; Y" h$ }% M2 p# |) I ```
, L) d' p; ?# F% k! f* j add_action('add_meta_boxes', 'add_site_wide_notices_boxes');& V: b3 H9 T8 w$ t7 ^6 V
function add_site_wide_notices_boxes() {
3 S* D( j) S1 M+ a5 C( S0 s add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
. v: {' O1 E$ Q" k }! \: ~; }% T! s* _
9 S ?; ]5 g3 V" g6 w8 ~: V3 R
function notice_details_meta_box($post) {/ ~* a( B* t: ]9 J1 o+ B2 X
wp_nonce_field(basename(__FILE__), 'notices_nonce');
1 J6 I7 t Y: R$ I+ f4 ` $notice_title = get_post_meta($post->ID, 'notice_title', true);
4 b/ ]3 F* }/ t$ C, w4 l' _% a z U $notice_content = get_post_meta($post->ID, 'notice_content', true);8 e# Z+ |: F+ V* ]
?>
! Q' e. d: ?7 o$ A <p>
}+ A# c3 f) t: y4 H- U <label for="notice-title">Notice Title</label><br>, i0 x4 _$ f; R) o) P
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">" K$ o+ p; \6 D2 }. m
</p>5 k) B7 Z6 h/ [. m0 U) g; G! H
<p>
# N6 p; C: I# H# F5 ]) i <label for="notice-content">Notice Content</label><br>
4 B$ O& ?3 X1 ^% H2 d <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>2 A/ i* ^: R/ V
</p>$ r) o ~9 b1 `& ^6 a. W4 e
<?php# b+ D" a+ |) R; _. _' _
}; ?( {7 Q7 r L" Q7 M9 H+ T1 C
4 U5 G! G. [$ @2 G add_action('save_post', 'save_site_wide_notice_meta_box');$ t: U5 P7 T9 Z! O% S. T0 L$ r
function save_site_wide_notice_meta_box($post_id) {
% g5 H: t2 g% P+ w7 Y if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))): [0 K2 A2 `, V
return;
' S/ @4 l$ U& {; n if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
" R0 i- g! C% A/ b return;
& R% b$ ~& k( x* |5 L1 Y4 k
5 T& X, |- j& N; h if (isset($_POST['notice_title'])) {5 _8 T; x1 R# l! H, H1 _+ S8 w
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));+ z$ [' Y9 Q/ I( v3 H
}3 r' j" Y+ \3 K1 c% F' y* a! j
if (isset($_POST['notice_content'])) {
& X" ~+ B) l5 j, z; { update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));4 @* ]! o4 |. i0 l4 e0 \ M
}
1 t, p" z D1 i! } }
6 E s) y- K" f+ T. v! Y+ x: X ```& {5 R; P% e1 I. X- s
, u4 M0 a; g4 |& H0 R 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
- m. j* R" E0 q" n; |/ F/ z# Z7 Y" l3 N1 n* y: V9 b# ?
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:) R% E: F# f9 g! u/ m# X. z. T5 Z! p
' W4 V( ?3 h) ?" s0 y
```
3 k1 ?: _% ?$ v. X $args = array(
2 Y2 W% _/ M, O2 G# I, ^3 f 'post_type' => 'site-wide-notices',7 M6 J) S8 [6 P
'posts_per_page' => 3,9 ^2 o, y2 H1 L/ l' T' C. c6 T
'order' => 'DESC',( |0 S8 \; I& i; o
'orderby' => 'date'
. N0 a7 m1 ~/ b! b' Q; a );
7 G6 v: \( o* X* } $query = new WP_Query($args);
; n3 V0 C, K: {, w' q if ($query->have_posts()) :9 |8 z* A' X: `0 L4 _! ^
while ($query->have_posts()) : $query->the_post(); ?>
4 }' g# n& T2 \! H' n <div class="notice">& ? {$ K5 H9 l( S$ v' K' v
<h3><?php the_title(); ?></h3>
3 T8 p& V. H5 q1 } {7 Y <div class="notice-content"><?php the_content(); ?></div>/ }. ?6 ]1 `1 H9 l
</div>
# M( B c2 C5 }$ f, l; c/ ?% h <?php endwhile;, ^, z B0 |5 G
wp_reset_postdata();
' a, Z0 C: M N, b/ ? endif;
0 h4 r2 o/ p; ]/ @0 @. a' f( o ```
; w' [% w9 D* e" ]2 V
: U" Y& o; [0 \, H* T, @# Y7 q$ X 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|