|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?- c$ `9 U) g: J0 M" k; v' K1 d
6 ~! l; A1 q6 d, b: V }! J$ w
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。1 X3 ~) x( G, m+ G5 L0 P9 i. {3 ?
2 u5 j( L0 f# ^/ P以下是创建自定义插件的步骤:5 p4 U( R# b: z, F) ^$ P# @: ~
* r& k& r* q# b! \: E B r
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
9 K: ~+ h& k/ a3 f- f2 j# C" _) b2 q
```; j1 E9 N m1 k, Z8 h
<?php' E% l$ O; q) V; b# i
/*9 {2 O! @. x% b
Plugin Name: Site Wide Notices Plugin
+ R8 d* ?/ b9 G, G3 f Description: Adds a new custom post type for site-wide notices.
; J# c8 S% t) S) ?8 O Version: 1.0: H# T0 I* H* J; o! b
Author: Your Name
) k" G8 ^# f3 } Author URI: http://example.com. u* J5 `# A7 r& V2 N
*/8 L2 T2 |5 T" x! x2 p# y2 q# t. [
+ e+ E6 \( [$ Z3 X
// Add plugin code here...6 U" T! N# T' a5 `' A) z6 w
```
d+ o) u: W: O
6 t; x7 Q% { U4 B/ ^3 i' }5 @ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。4 X# y0 L" x0 {" v
1 r3 c- U7 J( B; A
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:2 C% G# k6 a6 a9 W
; L* l$ O5 O! x+ o1 p/ ^# [
```, N4 x1 u w/ B% m
add_action('init', 'create_custom_post_type');
! U0 Q R7 ?, p function create_custom_post_type() {
6 X* @" Z B) n6 d7 e $labels = array(
. \, `2 h7 Z/ G7 V2 }1 N 'name' => 'Site Wide Notices',1 r, L" r! y* b. T' s t
'singular_name' => 'Site Wide Notice',7 h! ~) b+ ~4 t3 v* |& g+ I
'add_new' => 'Add New',
2 F3 D/ j3 ~) ~# I 'add_new_item' => 'Add New Site Wide Notice',0 n9 O. O) _) h3 y0 i, c) A
'edit_item' => 'Edit Site Wide Notice',
$ J% [* ]" d# b; t. @2 Q4 k2 M 'new_item' => 'New Site Wide Notice',' h" f* ^0 Y& A B! `% D2 n2 q
'view_item' => 'View Site Wide Notice',! H( r) _, L+ V0 O! K5 I
'search_items' => 'Search Site Wide Notices',
?) a5 L2 f% O7 d* S 'not_found' => 'No site-wide notices found',, k' U" m) e9 p% ~
'not_found_in_trash' => 'No site-wide notices found in trash'' X1 M( P K6 U9 v w9 b
);
% a! p" U+ c" X6 F8 m4 n
6 d" s' b# g' d4 B $args = array(. v! f3 o% g& m
'labels' => $labels,
$ U/ w5 C: [# {" l, ^7 g/ c 'public' => true,
, n5 h M9 |3 f' L$ } 'has_archive' => true,
- r( b: E @6 X 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),: S* s0 G8 Y- s/ N3 Y( M: c+ _9 Y
'taxonomies' => array('category', 'post_tag'),* |& G3 X, d6 _1 A; ^
'menu_icon' => 'dashicons-megaphone',0 q" C) H8 M1 J$ g0 q% _
'menu_position' => 5,: i! j7 D1 q: v, P
'rewrite' => array('slug' => 'site-wide-notices')- [* }8 m- l6 d1 D6 z5 X
); X) a( ]" B# o+ |( _
% N. g: ?* ?8 w: X( F2 u A
register_post_type('site-wide-notices', $args);9 h, g7 }( T9 d& K3 B. n* ~
}
3 L7 V x; n7 P1 e, r2 T, u. V ```! |$ I( ]" r8 {
[' `' N; p/ v7 s6 z8 v, D* L+ P+ g 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
% T6 u! R3 G, y
$ G+ k* v1 E" U0 t& N1 `# F3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
k' A1 P/ q5 Z! r0 a& u8 K; C7 d0 t; s \/ X$ u+ T
```7 O4 h/ F7 H$ y( U- Q6 ~
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');, X2 X! G8 {) A9 k/ H9 c/ A$ ~1 k$ @
function add_site_wide_notices_boxes() {
& r" ]5 g, g- V add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');' \5 L( X! P1 J7 z" s& \9 L
}
& e0 }& M2 t; [9 K1 O& r8 I' x. v0 ]& u: f# D- F6 m" K
function notice_details_meta_box($post) {
+ p& s$ q% M' b% } wp_nonce_field(basename(__FILE__), 'notices_nonce');
5 C# G/ s! x# G3 x# N1 j, ]; R" w6 Q $notice_title = get_post_meta($post->ID, 'notice_title', true);7 i/ x% o) h& u7 J- e. W! l' M
$notice_content = get_post_meta($post->ID, 'notice_content', true);; |) u; U) a/ m/ w
?>
- ~) F( r( [+ s6 \" D: H0 ^6 n6 {7 E <p>1 ]& ?" k! f" E2 T6 i0 W( N7 ~
<label for="notice-title">Notice Title</label><br>: `1 J, E9 L3 U( j6 h* Y @0 U
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>"> U& F8 j8 j$ m, _! ~3 N$ D$ x9 Z
</p>9 R; `5 `8 {% V3 H: ?. `
<p>4 e6 J( z" {3 \( a- D0 ~
<label for="notice-content">Notice Content</label><br>
* x4 T1 o8 J6 L) H& f <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
; E0 C/ r' b; n' j3 s </p>
; N& D1 L9 f7 b* H% z <?php# E0 j* P+ b! j- @& o& T
}. `: Y8 a) \. e+ R6 u3 O
0 Y* P; i, Z% e! X9 I, ^ add_action('save_post', 'save_site_wide_notice_meta_box');) J: x0 |$ u4 [( M; A6 B1 H: j3 j
function save_site_wide_notice_meta_box($post_id) {
2 n7 M' I5 D- k: p' f a if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
5 f. c* |5 n. v/ J: Z) w9 ] return;
6 s9 O( L/ x& x2 \- H' t& x- v. z if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
3 v6 l3 Q7 l* J2 i7 b return;& B0 \# t) P0 ], B
, E$ O& t* c; P. L$ p if (isset($_POST['notice_title'])) {6 k8 K9 y5 t) ?) z3 I4 q
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));! y8 l& n- I) b; ^) s; @! F; f
}
) L" Z% O% ^/ u if (isset($_POST['notice_content'])) {1 h C' K9 T; i1 m
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));- y0 H% p% m7 `& q, K
}! r+ J- [ j3 s& Y, r$ |9 {
}
$ M" Z3 {" N- J; l ```
* R7 c5 O f( ` i* f, @6 B" x) ~' D* i2 {$ @; M0 I, A
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
* D5 `6 {% K8 I2 T- t4 ^% S6 {9 S h% \5 s5 l6 d* x& t' d5 J; B! r
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
; _# I! j, Y- C! \) E6 Q4 `: X# j( j- j+ r6 \+ y( m1 n* P
```- I" a2 i' k) ?4 m8 F; o
$args = array(
+ k5 O$ e; q/ J, q# i' j) l# F 'post_type' => 'site-wide-notices',
+ y) ]) E+ {8 i' ~; D+ | 'posts_per_page' => 3,
! u' @+ H. S. }" K# T# i& V 'order' => 'DESC',
+ o! X6 v7 g" P; x! S) t8 T 'orderby' => 'date'
8 {" L. B# t4 a6 l, H: V1 O4 L );' K; D$ a/ C0 h
$query = new WP_Query($args);
3 t8 q2 `$ s6 F7 ^1 _+ K b& @( v if ($query->have_posts()) :
# w* N I5 F% q) ? while ($query->have_posts()) : $query->the_post(); ?>
}/ r. L% G( ] <div class="notice">( D" F" r+ D+ p% W/ m+ Y+ g
<h3><?php the_title(); ?></h3>4 }8 ~6 P% b2 U `% L
<div class="notice-content"><?php the_content(); ?></div>4 W5 x" b, u6 Z
</div>
& U( G' c4 [4 v$ g <?php endwhile;* k/ C5 q7 F. H
wp_reset_postdata();( ^9 m3 H' l" _- X8 `* l- h
endif;4 T: x, G1 |# B* w7 {- }
```/ ?; k3 m# Z3 b' R6 T
, ~$ c$ Z) @. m: J! T 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|