|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?# T6 b3 v3 v- E' l4 c9 M
) x% d9 D) q+ R1 S! B
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
% n( n- U; s2 {: g3 h0 {5 m7 i5 x7 m# ^! e
以下是创建自定义插件的步骤:9 p% `" h3 z6 } Z# N3 r
! \' P2 U3 k6 u# o1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:; L9 p! S y4 z" Q
7 U3 K$ x0 I" n j9 L7 S ```. Y) C* ?5 G, X5 d
<?php
' A3 T: ~( X8 [$ Z$ @+ f /*
/ |; O3 K; H6 d& S! I9 X1 G2 O1 e Plugin Name: Site Wide Notices Plugin3 k! H" _4 C# ^$ h0 Y6 n4 i
Description: Adds a new custom post type for site-wide notices.
0 {7 s3 ~$ i. z; G( ]) j' y, @ Version: 1.0$ Y1 t# v4 b% D7 `
Author: Your Name4 {) t( S+ o7 Y4 P$ U: z% w3 w
Author URI: http://example.com w0 H0 I6 k1 ?0 x q0 v6 \
*/
# R, O. E5 s# ^ `2 w5 @
! ]( T8 W; E# F+ { // Add plugin code here...* Y$ ?, F4 A$ {8 f- b! E! @
``` Q" O8 @# |2 m9 z0 A1 H
( q4 D7 e+ A! h! I" ?9 L: \ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
# Y* Z3 J! L3 c+ |" \
" c" E+ b8 A2 O: k2 R) B0 E9 \! p2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:$ Y0 M; `+ F7 @0 c6 x( w9 ^
2 G! h2 {8 V, t; U3 q( O) l; ] ```: B; ?# h. T. Y: `8 M
add_action('init', 'create_custom_post_type');
+ b+ F/ s" ]1 F, Q/ p- ^: T function create_custom_post_type() {
1 x9 f; o/ R5 I $labels = array(
7 a1 Z+ B% x/ x# Z8 U 'name' => 'Site Wide Notices',) s4 Y3 l0 j6 K. y9 S6 }2 B4 ^
'singular_name' => 'Site Wide Notice',5 j% x+ X) ]/ `. T0 n. m% t8 Y/ y
'add_new' => 'Add New',
+ J; A# K2 f+ t5 r7 j# N 'add_new_item' => 'Add New Site Wide Notice',. `0 w. k9 U* q' H% c: l
'edit_item' => 'Edit Site Wide Notice',6 @9 b1 B4 M* \6 e. V$ C' V: V7 O
'new_item' => 'New Site Wide Notice',$ }7 e2 C& b/ F/ b
'view_item' => 'View Site Wide Notice'," h+ s! S4 F, R" f8 M
'search_items' => 'Search Site Wide Notices',/ _. T4 @: G; A$ t+ x: u
'not_found' => 'No site-wide notices found',
- ?( D+ ?; s0 F" Z0 v0 @ 'not_found_in_trash' => 'No site-wide notices found in trash'
8 C/ k" }- v$ w Q );
1 b- b' E5 y$ U- Z$ B: H% F
. T5 I7 t! I2 z5 G+ n7 s; P $args = array(! r* W+ q8 }0 Q! y* p4 T
'labels' => $labels,
8 L, [$ T9 C6 ]( ] 'public' => true,( [- W: i$ M; A
'has_archive' => true,; m: O/ i0 M" a1 j, `! x' y' R
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),$ ~5 E6 b& W9 h* z6 C5 e
'taxonomies' => array('category', 'post_tag'),- O: k1 Q2 y# |: z- T' g6 d0 |
'menu_icon' => 'dashicons-megaphone',4 h; Z/ R- h& ^, L( n- j
'menu_position' => 5,
9 Y- b: X J! b! M 'rewrite' => array('slug' => 'site-wide-notices')" ^3 E* O, D, T' a
);
# e+ d* y/ Y! {1 Y" K" W) y$ s, [$ o
: x1 o/ [7 j8 K* h register_post_type('site-wide-notices', $args);
& w v( c6 z0 { }; h8 F' z& r* V( Y; a8 U; c; b
```
9 h f6 q) y, w6 q7 H
; G0 E f* e* u/ r+ W 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
6 L- ?; ` a& i9 T6 U2 D5 o9 b9 Z6 C1 I- H- n/ _) h
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
. e" x; B+ e. ^+ i' d6 \& h% m; g5 K, S+ n" s; p5 A
```
% a8 L& }2 W4 P" c2 u6 D2 q9 o8 t4 ^5 [ add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
% v$ S1 e/ Y. I' T. H* P f function add_site_wide_notices_boxes() {9 W. b2 C$ a! s3 m: Y* P2 n
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');4 X% k8 D- u; W, B1 _% h3 f: ^
}
7 Q% l4 ~( c3 C! u# ?0 e/ o! Y7 J! E! |
function notice_details_meta_box($post) {
- S% r4 }3 w9 E wp_nonce_field(basename(__FILE__), 'notices_nonce');3 x6 f7 V$ O1 d P* t
$notice_title = get_post_meta($post->ID, 'notice_title', true);& S! K- _' u3 f1 S% X# z4 I- y
$notice_content = get_post_meta($post->ID, 'notice_content', true);4 c: W5 p, R! D) V0 S- I# {% k
?>/ I: ]- e7 P/ n8 G6 t
<p># g8 f3 v" {# Z S j
<label for="notice-title">Notice Title</label><br>
% w7 Z/ }' a0 S$ k1 I <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
. e4 g7 k+ Y' v" a# m6 o. p </p>
; k/ R2 ^' ?7 D, L) U6 g! y <p>% [1 ], D4 _0 O( e+ w1 {. |8 r, v
<label for="notice-content">Notice Content</label><br>
/ T. f2 n9 i4 C) m <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>2 ~# H) Z) F9 Y' l. I9 W+ c+ [
</p>
1 a! K7 Y4 f2 f- d8 [ <?php
- c' }; X$ J7 X }
# U* P+ C5 P* H9 Y& d _/ N
; R: N& X% A, ~0 W: M) _ add_action('save_post', 'save_site_wide_notice_meta_box');5 j6 r: `& x% g6 g5 V y: r
function save_site_wide_notice_meta_box($post_id) {: ~( ^3 `6 f5 d+ `, x
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))! o N0 V+ Y+ e" n
return;1 Y6 @' a, E" t! Y% N" Q: q3 C
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)% b1 E" a& F. @3 b$ A
return;
: L( O5 N6 F1 {) q2 @( v% S2 X% z& n
if (isset($_POST['notice_title'])) {
% [9 K9 k( f8 B' B) I1 z. l0 J update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
3 O" J) M# `7 ]/ x) X" c5 L }
, `7 Y7 _: f6 B if (isset($_POST['notice_content'])) {
4 W; b2 E. g* [2 H8 \: P! i update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));1 m! ]0 @9 p7 @; o4 {3 \
}
+ n' G0 {" D+ r2 C) \# \, n9 L" i; h }9 x: j7 g0 e2 t3 H( D
```, T+ B8 t+ c0 w, v9 w0 U7 L
0 ?( L; z$ Z+ y7 J+ _- v
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。- g( M; l2 ?/ o3 l. R
' q, C, p" t2 I3 _4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:5 W4 S1 Z8 t2 e4 S
1 {+ g& p ^7 A$ `9 Z1 C
```# N/ `$ K1 Z4 g' w8 T9 @
$args = array(
- O# ~9 p" A; n 'post_type' => 'site-wide-notices',
1 E$ o- k' ?' \& y 'posts_per_page' => 3,
. C4 E/ A1 s* L* A. h* V 'order' => 'DESC',
+ J/ `; |2 Z9 ~( |9 r 'orderby' => 'date'
. x& p. n8 i4 S9 X( K- g1 R7 A );
4 W) E3 [1 q. H& [* E% L" T $query = new WP_Query($args);
7 `! t5 i# b' J0 b% \, `. p! I9 w, x if ($query->have_posts()) :9 F3 |4 _. y# c$ B1 P
while ($query->have_posts()) : $query->the_post(); ?>
. u% m% C( Z0 J <div class="notice">
- [4 s! ^2 N5 B; B <h3><?php the_title(); ?></h3>
" c5 S6 z0 g) N& L8 R <div class="notice-content"><?php the_content(); ?></div>, \4 C9 d; Q$ M# g7 V
</div>! E7 ?, A& n: G6 `$ @7 ]( X
<?php endwhile;1 O! Q. u! l5 s6 }% o
wp_reset_postdata();8 V3 y- l9 Q6 z& a
endif;. f4 R T1 D3 P
```
6 l& V6 R7 V5 J3 ^+ t9 \, A
: @. o! O$ s6 T G" h ?* @+ Z 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|