|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
& Y* L+ Q' n. b# n# T1 c- [1 q' {% s( @$ h# T6 x: z4 _6 h
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。: M! \$ _( ?( V/ }3 z+ i7 l
* G3 X/ E1 r7 o% I9 K以下是创建自定义插件的步骤:' e3 |4 D5 d1 p5 K$ U! T8 U2 a. X
( I/ b7 U9 k3 n- C) k1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:% O+ V" T5 Q, [% g5 y
4 I( J; W( W6 k
```$ l, X5 \7 ]. L5 L1 {! U$ Q% v
<?php0 }* y, v+ `" L/ k( ]/ j4 i \
/*! m" p2 D' S: a
Plugin Name: Site Wide Notices Plugin
# _8 v8 _( I6 W( U u1 r( ?) v Description: Adds a new custom post type for site-wide notices.
; c) Q0 ?+ w, }: H' E0 k Version: 1.0+ g% ? k& F& ~
Author: Your Name8 d, K7 n9 F7 n/ g# n' H8 U# L
Author URI: http://example.com; O0 ^1 A l9 v% t
*/
a+ D) l: A; j
$ Y" g% Z; Y9 b0 r7 j // Add plugin code here... z9 C+ w4 @: s l7 N8 k* d$ ~4 a
```
" n) |: B# D3 U' ~! b
5 \) I ]2 `9 ?# g 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。0 r9 A1 r6 z/ d5 L6 ~# i" A, I& @0 F
5 h" u3 l |. i0 Z# b1 U
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
2 u: G3 T3 [0 K( E% u( }
{0 @5 j' V6 u9 L( f# a! j ```
6 C% j4 y3 N: C* v$ j' x, i add_action('init', 'create_custom_post_type');
s+ f! v) g% f5 @6 z function create_custom_post_type() {
/ o- w* I& `0 D" U, i $labels = array(
' B5 x6 v' [6 ^+ O$ n7 K8 W 'name' => 'Site Wide Notices',
; e6 |( K5 z, p" A 'singular_name' => 'Site Wide Notice',
) Q7 b" e& q# ?5 ~ 'add_new' => 'Add New',( U) k! K& D& {2 h: c6 i4 h* d1 q
'add_new_item' => 'Add New Site Wide Notice',! w) X3 H. ^1 l, T' l q
'edit_item' => 'Edit Site Wide Notice',
4 B* W( m5 c- g2 m- ? 'new_item' => 'New Site Wide Notice',' U* J' c/ ]5 r* \
'view_item' => 'View Site Wide Notice',) o$ q, O, s8 [7 Z4 V- m- ~
'search_items' => 'Search Site Wide Notices',
7 p- [# H. D2 t5 i" y# j4 i8 U/ K% |5 K 'not_found' => 'No site-wide notices found',- @& @5 T9 @9 @' y' U9 w8 l; t
'not_found_in_trash' => 'No site-wide notices found in trash'1 \0 z1 c) S# r4 D) A* y+ k
);
: R6 [% l* [* ^5 P) {
& G! R+ l+ _, c# L $args = array(
3 [& G9 g6 g' c7 _ 'labels' => $labels,2 _3 v5 { w9 I& T
'public' => true,+ [% E3 r, C4 I4 ~' ?/ M {" M
'has_archive' => true, J2 D8 U% I5 A7 H2 r
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),( q" M9 ^: b! \/ L& k [2 Z9 w
'taxonomies' => array('category', 'post_tag'),1 Q, j' ^7 ~' T' I; A# j6 @
'menu_icon' => 'dashicons-megaphone',( s& u% }7 |& N0 E$ D
'menu_position' => 5," F. s J8 x; t( [
'rewrite' => array('slug' => 'site-wide-notices')
2 ~* p9 C1 O J );+ s5 A& S& z$ X
# m E* c; X8 w$ {8 L1 B
register_post_type('site-wide-notices', $args);: W3 g) Z& o' y# f
}, U5 e# t# Q# _- }( w A
```, x( A, Y: l$ `; X1 y% t
4 Q/ T" {$ W% w" V 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。; ~& c) x* q9 Y1 d
# S2 U" O K6 G; B4 T) h" N* ?8 |3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
+ B7 Z$ c* H9 j5 p. f9 j6 U! q/ t
```2 T! D& R) @+ |) R3 X+ N2 `
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');8 x6 V$ Y6 r% x. [ a
function add_site_wide_notices_boxes() {
5 m2 y0 K0 F1 d; @0 o) t+ j add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');8 D, Q4 D" Q8 e: ?1 Y
}
. C* I/ l: \! k0 m* Z/ l
( i; s: h/ I3 ?5 T5 e! ~ function notice_details_meta_box($post) {6 P$ d8 F) Y; H- J8 E& N
wp_nonce_field(basename(__FILE__), 'notices_nonce');
$ a. f7 o; Q9 `0 i! m3 l( I $notice_title = get_post_meta($post->ID, 'notice_title', true);% w( T- B4 z" q4 O
$notice_content = get_post_meta($post->ID, 'notice_content', true);. Q) h* F+ U: i) B3 b: o1 R4 n& ]
?>
: g. u/ Z" Z$ f <p>
; i: j! ?0 E+ @7 i& G% F) N8 p <label for="notice-title">Notice Title</label><br>/ g' y4 z( h0 n( p
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
& K# o# H# e: @+ Y% L </p>
- @% a5 A$ `9 \ <p>
% w: s- [0 |: O5 N' B7 f <label for="notice-content">Notice Content</label><br>
; l2 S2 c' v7 |5 C) O <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>" J8 U ~- L& M& r! [
</p>! C+ m7 a% d$ l2 x9 C
<?php9 O' Y4 v8 @( X2 n6 v- ]* B
}% X3 s0 N# T, ?1 s3 L
& T, s' t1 l, e/ F* b5 A' _ add_action('save_post', 'save_site_wide_notice_meta_box');) ?6 _4 O: f E
function save_site_wide_notice_meta_box($post_id) {* p5 I+ z# H. R% n) H
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
9 T4 ~/ u5 g' z& l1 Z9 t return;
0 r! \5 ?, H' V. R3 m* y if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
# c3 K1 U6 J7 ^1 x4 S& l return;
( ?& {; G% e4 t! D! R* V: R# N6 l2 h3 v9 I
if (isset($_POST['notice_title'])) {. p- M S; `# X- g, T4 u; N
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
" F2 O3 J0 O$ H8 b9 f2 I }+ D& K+ I. f! U9 e( F
if (isset($_POST['notice_content'])) {
2 ^ ^+ N7 Q d# |( ^5 Q update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));; R, y$ u9 @7 L" S* ]# X8 o
}# [3 s5 i0 @* q& J
}
9 I6 M% b, J0 ]! k: W' f ```
/ @- O% `& ?$ Q' W& Q: q( P# L* \/ E$ Z: b+ a: b
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
; l" e% m! X& k+ ~4 L5 F! a8 d1 K9 r) D+ D5 I3 q
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
+ y! L2 l9 B. N( z3 V+ N. r; A0 V- m- t: V
```
3 \) b) d* Q4 S1 z" m) { $args = array($ ~' Y" T5 V2 ^1 r" T- j
'post_type' => 'site-wide-notices',
; d! ?7 Z9 X: ?7 e$ e! u 'posts_per_page' => 3,
) E& _: l9 b: z# P2 |. b 'order' => 'DESC',
+ G. x* _: P) h* q$ v: N! I4 a3 I( F 'orderby' => 'date'
9 R! V1 r8 U( p );
/ o6 b" Z5 l5 l9 {, ^6 k( |4 A; s! P $query = new WP_Query($args);
1 j8 {! ?8 r1 _- V9 L( A9 a" { if ($query->have_posts()) :5 `& j$ p8 N# y; P- |
while ($query->have_posts()) : $query->the_post(); ?>
. N2 x `+ D* |$ }: ?* R7 ]" o <div class="notice"> @2 }: O7 @: H
<h3><?php the_title(); ?></h3># B& P; i# ?$ M$ \# H
<div class="notice-content"><?php the_content(); ?></div>
. O% T% `% c' Z0 u. u. l5 i </div>
* o% a4 N& [6 N; Y <?php endwhile; K( _ S% ^! B# b+ _& ]. L- j
wp_reset_postdata();
# p# c, I% G* i. s+ @) N$ G; l endif;5 j$ B: c' t- G8 c4 z
```, b) P6 c0 X0 n2 W
2 U0 D3 A& g0 i/ Y' `& [4 J 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|