|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
: t. _& f5 E. v' J
) O1 K% w! ~1 j+ [' h如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。+ X# Q ^: m5 L! Z8 w+ z
2 R& J# r2 q3 `3 I/ Y: O! E以下是创建自定义插件的步骤:
6 [4 c& d3 { |% d" N9 h2 f' W. \9 ^3 L+ M. t* d4 `5 x3 ?
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:+ \/ `0 k' V" B) l. n5 Z
$ D2 U) p# p# l5 V+ l& F2 r6 P
```- W" G# h- { v) E* {
<?php: L/ u. [7 c* d+ M m
/*: Q. L6 t$ k! h% K3 ]& R/ D
Plugin Name: Site Wide Notices Plugin0 c% A! ?+ h. q% I/ j
Description: Adds a new custom post type for site-wide notices.# L; p( d7 n( v* K$ w& u/ y
Version: 1.0
& l5 w5 Z! l# f5 l/ { Author: Your Name
# |. {" h) K2 o. L Author URI: http://example.com
; k$ D9 U/ C/ Q, E9 j f' F */* C* Y. i6 G5 ?
, {0 `/ ]# ?$ [% C, @
// Add plugin code here.... L3 ~# h" x- ?- o3 s: F9 \/ U
```
5 ?: m0 P9 E3 e. d8 S& g. P9 B3 \/ T, I' K% W$ P
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
1 k- Q" s$ b% M) D
) B6 ]5 _5 k5 t; p. N2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
, `# `9 u, X( |" G
! O" S }2 Q L) P/ M1 K ```& o, s! _: k. t- d9 A0 S
add_action('init', 'create_custom_post_type');/ t! _3 q! ^( n& x( b4 \1 x( L! }+ q
function create_custom_post_type() {( ` e' L+ Y/ M- ^: s i* f
$labels = array(" i: R" T8 O7 k* N" Q
'name' => 'Site Wide Notices',
. }2 h. E7 p8 F6 i* q 'singular_name' => 'Site Wide Notice',
/ u, d4 ~% K) T- S! y2 g& L* E( a7 W 'add_new' => 'Add New',
6 }3 s+ d7 R, P 'add_new_item' => 'Add New Site Wide Notice',$ q% p2 `& b, k3 ^( ^5 n
'edit_item' => 'Edit Site Wide Notice',3 K2 S0 m; v/ f
'new_item' => 'New Site Wide Notice',
! }# b8 x% G) f2 C) R6 ^5 R- q# m 'view_item' => 'View Site Wide Notice', d+ ^ r! X7 R/ b! A
'search_items' => 'Search Site Wide Notices',! [: T5 W% k' `- z# ]2 Z D2 n. n; W
'not_found' => 'No site-wide notices found',- G6 p q6 \ m& b) n: R8 W
'not_found_in_trash' => 'No site-wide notices found in trash'6 W+ S5 y, M7 F$ O. \& ^
);1 R# Q9 x8 t j7 T
, i4 t4 q. r2 l1 C1 _0 j
$args = array(
! r9 x: E" H* p+ X 'labels' => $labels,
+ e" \6 S @4 [- F/ {. t2 }- O' }$ l } 'public' => true,
7 ?6 e+ v4 [. K" t! I$ `$ q7 O 'has_archive' => true,
- C2 u+ N8 \* K3 ]2 ~+ m+ e 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),% M+ R& K' s7 W P; x$ z
'taxonomies' => array('category', 'post_tag'), ]7 a" \9 F- p" V
'menu_icon' => 'dashicons-megaphone',
5 I1 @) ]2 [, w3 [ 'menu_position' => 5,- u- a/ H. U; }) G
'rewrite' => array('slug' => 'site-wide-notices')
1 _- z. |( s2 O5 k4 a );
1 H# K% j2 i- N0 ]) |$ w6 a1 l. r8 Q$ w1 Y. K( ~$ @, D [9 z
register_post_type('site-wide-notices', $args);
# h0 u4 w, \+ v$ u }/ S! x6 @1 o9 t
```
T5 }) u. S- ?5 Q* o# m% Z& O1 ?( v4 b
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。8 C i, V) j- W: | d- g
9 S r6 x: @- `, u
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
. U/ n0 n2 }8 D9 C# f7 w# h% _; ]6 v# H. ]$ B
``` L0 [$ r6 ^4 @8 Z
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');4 K" F6 k1 k+ h- s6 n* u+ i
function add_site_wide_notices_boxes() {6 Z7 Y2 W1 T) U% g
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
: e8 F" c! E9 i- z. L }
: H% V' O- ?% f/ D2 s2 [3 ?8 ~
9 \# }, k5 p. e/ f! n* L5 T function notice_details_meta_box($post) {
1 ~8 H# @ }' X# o wp_nonce_field(basename(__FILE__), 'notices_nonce');
: g0 T+ A! ?6 a $notice_title = get_post_meta($post->ID, 'notice_title', true);
, B R, ^$ N1 g H7 c3 N5 \5 E $notice_content = get_post_meta($post->ID, 'notice_content', true);
1 b6 E* K- X* M( z ?>
: k0 a5 [& Q7 s/ X" Y <p>' s! Z4 @ d- L) Y* j
<label for="notice-title">Notice Title</label><br>* D' K8 t& y$ T6 m d, [& a& f
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">5 n! N6 E/ |; j* {3 ^3 @! l: Z
</p>
( c- Q5 `" ^. ~4 U, Q% ~5 X9 T& \ <p>
# ^& G. _9 n A* x+ q <label for="notice-content">Notice Content</label><br>
. l3 c% m( K' |+ V <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
1 R4 v1 x, B( ^+ _- Y% y </p>
4 r! Y, R) z o6 J4 K& b <?php7 Y9 f# U( M0 N4 G$ @, U: O
}
' s1 T% R7 ~; N6 R q4 c8 c+ \6 j: u$ n7 i' f; X
add_action('save_post', 'save_site_wide_notice_meta_box');# s2 ]4 J: `( g( P% ]
function save_site_wide_notice_meta_box($post_id) {2 e% k+ u1 i* Q, ~, a
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))1 C! Y' M& ]% @) f7 H
return;5 }) K5 R7 z/ A$ \" s. ]
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)$ s; @; a! D( q
return;
) o" T. K* p1 a0 s( g: h3 z/ E8 |' z7 y; C# F
if (isset($_POST['notice_title'])) {0 Q( O& [ V. _
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));4 o2 I4 D5 H6 i- T5 ^/ Q
}
8 k3 r) x5 Y `) E6 k8 N+ H3 W8 C if (isset($_POST['notice_content'])) {( T& |/ V- e1 e
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
2 H0 P3 v$ [! J% ? }
) q h. L! w! S: J% W }
5 Q+ ~: A) U& Z8 A t7 O ```& a. S; z, t3 F w
+ s8 L4 K9 A) Q. z% L7 `$ P
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。! X' @: i2 m) E" D
* @) g. o: E9 z/ t3 T, A4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:+ c& _4 K; L- I
J( j6 P2 k& M$ Y6 p& T% X0 w ```
5 @8 Y/ f: a# K! q7 l $args = array(
9 `. |" U5 o! j4 J$ @4 L 'post_type' => 'site-wide-notices',$ b1 ~) ` e7 B
'posts_per_page' => 3,
7 l! j" P0 D9 T/ c 'order' => 'DESC',
$ n# @9 ?% K+ F 'orderby' => 'date'
0 m9 j! H- J5 f );
0 s4 D% W8 q0 F4 L* q+ G/ @ $query = new WP_Query($args);* |6 |& ^9 R( O- F7 [
if ($query->have_posts()) :
( `! f Q( f5 U1 z: J while ($query->have_posts()) : $query->the_post(); ?>
) ~$ n% ]1 i3 T l' P+ p: f <div class="notice">3 e; i' B. ^+ b- M N5 H
<h3><?php the_title(); ?></h3>- C2 t" o" e' g8 S1 z% n& r
<div class="notice-content"><?php the_content(); ?></div>) b/ d. ~0 Q- {
</div>
& M T9 c' }! u2 _; _9 k <?php endwhile;4 B1 {# x V2 S5 _: s! u3 w
wp_reset_postdata();' L# n$ T$ T$ k$ ]# L+ e$ p# q
endif;
. x r! X% g: i ```
: t6 w; ~' u) { Q
8 g# { ^- {0 O& W! g6 n1 } 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|