|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
% C) c% N; F% b$ \$ \8 X$ z" f, Z. [& \8 P2 J% L' \
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
# ^) |# G) j1 t: U) f, y) b/ a
; O9 ^+ g9 o2 _. }, X8 [) i. y$ @以下是创建自定义插件的步骤:; Z& i$ F. b" z' h9 d
) m2 S& h! F y4 n% w! j) t1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
7 H" P' }- r" K8 G1 F
# }& Z* W2 o6 s2 c ```
: X+ Z7 q) t2 o4 j& }9 y8 ? <?php
1 d9 e ]# J: W8 j& T6 w+ h" c /*
$ f6 ^- [2 U" @1 |* |5 n# O Plugin Name: Site Wide Notices Plugin' Z3 M! |) d* g0 ^, B: w
Description: Adds a new custom post type for site-wide notices.7 f& Y( }) J- O2 o B
Version: 1.04 x- H9 _# S, l+ W5 p4 u
Author: Your Name9 G0 S# ^. z7 D* w W* F
Author URI: http://example.com
5 ^$ U( a, Q' i: G6 f/ w */* x+ k( o8 z l$ }7 \" V
8 Q* S& V8 q$ I. E // Add plugin code here...
, `, m2 ]7 C2 w( d. [ ```
4 D! Z' @6 s2 C3 s7 g0 X6 N3 M
5 U& w- s: q8 e7 G 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
, o9 P% x/ Z/ c+ n; E% ]$ R p6 U: F1 s1 @, x. ?% B6 U9 h; b! r
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:& R- }$ j/ [2 B0 {2 a% L$ P/ i
& x5 @- U9 N8 P: [ ```
8 J. t* N% c a% h add_action('init', 'create_custom_post_type');
6 }7 {; u7 @: }/ w5 D% `; o8 } function create_custom_post_type() {, X o% W. R: Q: q
$labels = array($ F& Q3 a) s# o1 t
'name' => 'Site Wide Notices',; \8 j& _ L3 W ~+ ~6 i
'singular_name' => 'Site Wide Notice',
9 K* K* E9 ^7 z' X6 L! h M5 n+ } 'add_new' => 'Add New',: o- |+ k3 a+ i) P
'add_new_item' => 'Add New Site Wide Notice',
; j+ ~3 w1 r3 M% p 'edit_item' => 'Edit Site Wide Notice',2 p+ k+ A( Z; G
'new_item' => 'New Site Wide Notice',
3 x5 W) }2 j/ k- Y1 s 'view_item' => 'View Site Wide Notice',
7 W0 B3 @0 d: x2 } 'search_items' => 'Search Site Wide Notices',) P! {$ K7 a* t# u7 ^, A: @
'not_found' => 'No site-wide notices found',
7 l' ~$ D3 k( e7 N( _+ X3 j 'not_found_in_trash' => 'No site-wide notices found in trash'
5 |2 `5 A4 b' e0 w$ N );: K3 q8 ~. w* h9 Y
$ C8 D1 H: C* M
$args = array(7 l5 _% z8 L9 V9 Y) m
'labels' => $labels,( Q" y6 |* |& C/ I5 @ t
'public' => true,
. J" I9 q- D4 t: E 'has_archive' => true,
' \; d) L) c6 {8 T- e, w0 I 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),) `/ y6 v- R) S) W
'taxonomies' => array('category', 'post_tag'),
/ `) `& k1 O3 K8 ^4 @ 'menu_icon' => 'dashicons-megaphone',
+ ^" z& h# N9 i- V 'menu_position' => 5,7 l& L; @6 S$ e2 f6 U$ }; g% S: n
'rewrite' => array('slug' => 'site-wide-notices')7 I" } D! M! u+ u; c ?$ e
);
& U# O! e) c& _- P, d" I& }# t( g/ W& f% j
register_post_type('site-wide-notices', $args);
}3 x1 s' q; z4 y$ c% C' [1 h b }4 Y; c* h7 J. o0 j* }. c
```
R; L3 g# O7 F1 M' j% Q: g7 |
( M6 z' k5 e; e" g( N) `7 a 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。5 C) K+ p) ] v
P l( N- h) y/ b
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
* a& q# ]3 I! C# h3 q. l
$ ]' x! Z9 [2 M- x7 ]& V @+ p ```; Y9 H* V! V- b) ` h
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
: ^# S' m7 O- R' B% @) N" P2 X" j function add_site_wide_notices_boxes() {
0 e A8 k7 X. d add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
- o7 n& O# `4 H- L/ j }
! _5 N- n4 n' s9 Y8 ~1 t+ E) v- M" w0 d
function notice_details_meta_box($post) {6 y, g4 W' i3 K4 @
wp_nonce_field(basename(__FILE__), 'notices_nonce');2 B9 E- L# T5 w9 {- b! G
$notice_title = get_post_meta($post->ID, 'notice_title', true);+ S1 ~1 k; B# Q$ O% y
$notice_content = get_post_meta($post->ID, 'notice_content', true);
) b; s$ V' ^, S5 p; D, X: L ?>
3 C1 C+ K* M* r <p>6 l/ c6 ?% B$ \3 }8 F
<label for="notice-title">Notice Title</label><br>
/ \, `' V$ @& @6 G. i& {: y <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
* s( ^6 W) a' v7 V- V/ \ </p>
2 ^; N$ s, d9 s3 `0 K <p>
8 d( G! F1 z7 C' L) d <label for="notice-content">Notice Content</label><br>1 T/ A6 }, `- B+ A$ Q; O
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
# t. W4 M* [+ h% F- ~ </p>
7 |) Y- Q5 X7 k6 V K- T <?php
8 u; ^; j8 ]& n! m$ N t6 D }1 [% `4 R# m- F7 m7 g- x
% f( ]+ z% i: b- n3 y add_action('save_post', 'save_site_wide_notice_meta_box');
, Z3 R" x" F L# W* h* h function save_site_wide_notice_meta_box($post_id) {8 A5 H% c1 I- ?! l6 P
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))0 S+ k& r' Y5 A
return;: e$ _% j& M% X4 A
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)# @6 {4 Q3 F1 ?3 {' S P
return;
. a" a! W. O* m4 ?! b$ _4 O9 U- S% X: l
if (isset($_POST['notice_title'])) {. C1 d) f, ~+ }# w4 [& H
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));3 h5 c1 ?0 d0 x9 q
}
- q/ m5 [9 U& F1 l0 Z0 v if (isset($_POST['notice_content'])) {1 Y1 d! e9 V1 R7 z: Q- o8 h [9 L
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
# }9 {- p- N4 I; _, O1 d3 d; a0 w+ X }5 r Y" i; c: w3 m. s
}
9 g2 Q3 a$ C' ^6 o, G% a ```
/ Q' A9 o: O- v5 T: W7 Z. h+ b: J4 w0 w6 e8 X6 F
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。) j" v4 w) M7 S; m( n2 I
' c1 C; s' D% E: ?; [4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
$ B8 Q" x/ @2 u* }$ ~. m' I3 ^ s. A2 H, N! d$ u
```
( l: W+ o4 }7 ]- G! K% z $args = array(
5 C% J# f; R" }5 K& r 'post_type' => 'site-wide-notices',
6 k& n0 X" c/ p6 a 'posts_per_page' => 3,
8 Q v% G" K( N* U N' j0 w 'order' => 'DESC',1 l8 J1 U+ i" H& v
'orderby' => 'date'8 V! I- q' ~3 K4 ?( L% ^% z6 J
);4 M( P5 C" `6 W7 E
$query = new WP_Query($args);
. D' i2 C/ ~- e# [: z6 { if ($query->have_posts()) :
5 b$ a% u% l* ]% b, U: c while ($query->have_posts()) : $query->the_post(); ?>3 @9 |+ C( i- D8 |1 |6 w; T( J
<div class="notice">; E% J& X- h9 ]7 i: J
<h3><?php the_title(); ?></h3>
' P& S5 Q a9 t7 s1 L <div class="notice-content"><?php the_content(); ?></div>* p( D0 X# c+ F4 n5 C3 Z \
</div>
5 n" f2 K/ H7 F8 j <?php endwhile;
9 G4 A+ i2 K: R0 {, v! a% I wp_reset_postdata();
7 i' [3 |( H5 ~6 K endif;2 j% @$ P! ^8 R0 ?) u$ E( q* Y4 f+ d
```, i; f) z- ^) L3 F
/ B0 I1 n6 Q! L 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|