|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?2 X7 S) f" u; Y/ [3 ?- D' c) \* u
U4 ?" Y2 e+ S. r q7 L
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。( M* n2 b3 H4 M+ N: g
2 }3 e. n7 _3 D$ W8 a1 g/ I& _以下是创建自定义插件的步骤:
; s: W4 c$ |3 E( ^, @) x$ k9 |
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
1 N" m! n: o5 u% M- w9 t! x! f! D! i u
```8 L0 ^; K/ |* m5 m$ T5 S
<?php
" I, m; Y3 S# G' m2 j3 m, s$ r /*
4 H. a5 n6 I# q Plugin Name: Site Wide Notices Plugin
1 C" ?" }0 ~1 H& p) b: e+ Z Description: Adds a new custom post type for site-wide notices.
0 @& `3 ~& W2 G1 Q# J. ~! ] Version: 1.0( i5 L- x9 r; c! o9 J! o
Author: Your Name, Z1 P; U7 ^4 U+ I% W. h
Author URI: http://example.com5 x- A$ [: Y: \+ P t" E9 v
*/1 m0 g! n5 O2 [ M
+ L" w+ _3 j4 w7 c; y b
// Add plugin code here...# ]# O" Z4 [, Z1 T
```' y. a# V9 A2 k9 o2 R+ r
& x+ D' \- P/ p8 m6 Z7 G 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
6 ^- O6 N" s' `8 ~( i0 }0 m5 c+ P0 g+ r k) A7 p- ~
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:- p& ^; `" ^. p6 z$ O: N9 c
+ C. H1 E$ B& T. N% B
```0 A6 j% ]5 k v% h) w0 q
add_action('init', 'create_custom_post_type');
3 M1 v" }/ N8 F7 s9 e1 ^2 J& d function create_custom_post_type() {
+ S% b% C* t7 t# f2 Y$ ], z, c, d8 ^ $labels = array(
+ e2 Q" S; \7 ` 'name' => 'Site Wide Notices', x! T" Z* K$ r7 h8 ]5 Y
'singular_name' => 'Site Wide Notice',
6 y. k Y& C2 ^. n4 X3 h, G 'add_new' => 'Add New',: }# Z0 N" L/ m3 E. _: J6 e
'add_new_item' => 'Add New Site Wide Notice',! i Q+ O3 ]; R6 B8 N; \
'edit_item' => 'Edit Site Wide Notice',
& P- U5 u6 G6 U. D7 Q5 {* p 'new_item' => 'New Site Wide Notice',8 S6 v) G: H9 W& ~
'view_item' => 'View Site Wide Notice',, Y' k. G7 k \* H$ ?# R- u
'search_items' => 'Search Site Wide Notices',
; z; C- ?4 S" I$ T; e7 W$ K- L7 d 'not_found' => 'No site-wide notices found',0 ~" I j: b5 r1 l) e
'not_found_in_trash' => 'No site-wide notices found in trash'
# |+ R. r* x* S% f/ j6 Z# Q8 U" i );
$ F3 H% b6 l7 O/ a2 Y$ s
! o. U: _1 q) { $args = array(
; u2 Z# q7 ]4 m$ q6 c& c/ `; B' ^ 'labels' => $labels,3 K9 x6 m; |: M' M; K! s# G
'public' => true,
4 v9 w$ A) j6 B1 S 'has_archive' => true,& x# h* `4 N: s0 r
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),7 A4 r" S9 i. Q5 I6 L' Q# ]( k
'taxonomies' => array('category', 'post_tag'),& P5 X/ h2 M1 ^! r, B# f
'menu_icon' => 'dashicons-megaphone',
2 q2 l- f8 g Y; Y1 ~ 'menu_position' => 5,
8 a6 T8 ]7 w1 `, I9 C. W 'rewrite' => array('slug' => 'site-wide-notices')( s/ C6 N! o( n: v, E, a
);
* m. ?- |2 R% E# B4 D7 A# r0 P- {( g: O/ ~4 }& P8 r
register_post_type('site-wide-notices', $args);) V! q) [! j- z$ C/ ~. e- I1 } d, N' i
}
! ~, D T% N2 b, e. `. y0 J5 W7 v: O ```5 K. L' a! y% T! l4 F+ @0 Q
" F6 X( `6 ~1 f: J( E2 N 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。% n# y; A* n% P
9 m3 a/ B. E! h* B- J; Y& w: p
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
* v) _( T D8 V- D
$ a; n( ?5 S2 l) Z$ u0 { ```
- z0 r- x# \' N+ E add_action('add_meta_boxes', 'add_site_wide_notices_boxes');% l/ E' p8 d* T1 ]( `, ?) B: }
function add_site_wide_notices_boxes() {# C z/ R# N0 x$ _
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
( Y A( i$ ~* G% L2 ? } R' c( B1 ^& B. c% N
. S0 ~) \3 Y" I2 c2 [: G! \' z3 y
function notice_details_meta_box($post) {
: R: b1 j" A8 ] M; O( M( r wp_nonce_field(basename(__FILE__), 'notices_nonce');( d; G5 ?( U$ }$ L7 F: N) O* ]
$notice_title = get_post_meta($post->ID, 'notice_title', true);1 s7 D9 X1 S! T: c9 O9 T) f- G# ]+ \
$notice_content = get_post_meta($post->ID, 'notice_content', true);) d4 `# L* O8 q7 N1 b( d8 ^3 M9 R+ l
?>
: d% ]( m, P5 ?2 R" o' @ <p> B# X- @: l+ { s0 N
<label for="notice-title">Notice Title</label><br>. T" _! _, p, O7 a
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">! r0 A% x o5 \. Z' d' g! D
</p> Z; i- S- \, Q' b6 r) a' d7 y
<p>
9 y9 _+ u0 p" }6 N/ n <label for="notice-content">Notice Content</label><br>) ^( ~- X# X6 _1 b
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
" I8 I7 k% o2 H8 F" k- d9 }1 Q8 n </p>
! r+ `, V0 `9 F! Q) j2 c& o5 j: { <?php# A1 f4 |6 @0 ]6 O( [0 o$ P$ @' F; e
}' Z4 d1 J. z, a9 N
2 q0 B! X: _& c7 t
add_action('save_post', 'save_site_wide_notice_meta_box');. a6 J" t: V8 e' a
function save_site_wide_notice_meta_box($post_id) {% @. A$ M' ]; M! u
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
5 F: i9 a# C3 | return;
6 f2 H" K, u, j if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
+ K2 j) p5 C+ H7 d return;
8 y7 w/ y' W$ j: V6 Q
/ i' i" ]4 X" x* ?; H& L G if (isset($_POST['notice_title'])) {$ L) Y6 M; ]/ y" L4 w# ?
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
: ^9 b& F3 w: I% `4 S! i0 n* d }" @5 M0 C' b0 A; X0 x2 f1 i* T3 |" f
if (isset($_POST['notice_content'])) {. ^# c* B" |! R* M
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
2 ^2 C% X) \2 {" ?6 S) } }
7 G( |7 l. ~; U* x }; G! \, A& q. y
```
0 Q, q q' O4 q; t: g$ P9 Y& Y. j$ g, U) Q4 d8 U/ q Q% Y
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
; ^+ t& v; L7 s& m F7 x0 o
: a4 c3 l0 v9 G3 s" J) m4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
7 w& t# B; S6 T% i( W
8 b& U' c/ a. x ```0 w& f( {2 A/ p
$args = array(. \1 j5 d! y! b, }5 d: r% f8 B6 S
'post_type' => 'site-wide-notices',
7 e+ M& N* m4 V: M* K* o$ W' R 'posts_per_page' => 3,( T7 P; y/ F, Y! w
'order' => 'DESC',
1 h' L2 f( s3 b" @' s5 L$ U 'orderby' => 'date'
P" C$ q) E2 R; { );) H/ v# P. A; b% d5 \( X) m
$query = new WP_Query($args);# J+ |* L/ m7 {! `7 A6 t! [3 X
if ($query->have_posts()) :
/ G' h- Y. Z% \+ E$ |0 o! M while ($query->have_posts()) : $query->the_post(); ?>5 x. E0 j* _- B7 N5 a
<div class="notice">9 _: ~ e) R4 _! s
<h3><?php the_title(); ?></h3>7 Q* Q/ R2 \/ e" w/ [( w* R$ D
<div class="notice-content"><?php the_content(); ?></div> ^% f% ]- z0 ]6 ]! B+ {
</div>0 G" h2 L9 ~1 I" r$ C* l/ |
<?php endwhile;
1 D- A$ L& E3 [( m: z1 H4 j7 R wp_reset_postdata();
% I7 @6 m2 a7 S( D. x endif;
) |8 w8 h9 M* R7 y* g ]+ q ```$ A o' A. c/ R$ F4 e% C: r
/ y1 p$ y; g/ e9 o( a7 ~
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|