|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?! r( o1 K5 p }2 G, L S" Y3 D: D, f
7 i6 G7 m& A$ I2 z+ \( a8 H如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。" Q; r8 a# G3 A6 U! e; D
! H" q$ ?. Z/ O# D& X
以下是创建自定义插件的步骤:
% q8 j( A- C: d% ]0 T+ T3 i1 u& @% |( s& d
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
* f% |3 U2 j& u& @0 _* v1 B9 N' U" H8 Z* s# G, B- C& r
```
1 s. l) Z' D( Y( m* ~* m8 B <?php
4 J. ^! S6 _7 o0 D& r) s3 u$ D /*9 y: U: x8 V V# j$ @& E
Plugin Name: Site Wide Notices Plugin" N8 c H$ @8 a) ?) K: ?# f
Description: Adds a new custom post type for site-wide notices.
; Q- a0 [* v7 @' Z Version: 1.0
$ M" o' ]7 w1 t; L Author: Your Name
. ]. c6 m$ a* [! u/ t4 [) X* ? Author URI: http://example.com
' F: V( M% ]9 E& e1 v+ P */, P% Y& @! C' ?, u, X# J
/ C, T2 i) ^1 F$ q1 H9 J( A* z0 q1 D // Add plugin code here...+ }. _0 ?2 R3 j; A0 ]
```
0 M% @2 V9 f. K/ z& t' `/ f
; o3 l. M6 E: d2 i 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
% {0 b0 h# e9 \% j i5 c
' D# U, Y5 s& w+ \, @( h' B1 C2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
, z* x% K/ S$ h5 y, Q( L. d7 d8 c+ m3 k2 g8 ]0 E. r7 r
```
& |, u) j5 t- q. C$ u7 v+ C add_action('init', 'create_custom_post_type');" W, H7 u& t4 w7 k S ?5 j9 y
function create_custom_post_type() {1 p I( w0 g3 t: M. Y; ?
$labels = array(
/ {" G. z2 B# f K 'name' => 'Site Wide Notices',
1 n |4 V) G6 }% U# ~. k/ S v 'singular_name' => 'Site Wide Notice',5 |9 q; Z0 k; F2 U9 d D
'add_new' => 'Add New',
+ ~% g" o1 |2 @5 @+ I0 i1 o 'add_new_item' => 'Add New Site Wide Notice',
" W m: q: T+ X 'edit_item' => 'Edit Site Wide Notice',
3 N' {3 Y8 }- y& F8 ~! J0 I9 d 'new_item' => 'New Site Wide Notice',
1 @# ~( N9 W2 s( R( ^. C) H 'view_item' => 'View Site Wide Notice',0 s6 M5 |& l- ?6 U
'search_items' => 'Search Site Wide Notices',
: O) m/ M* _" y* n# c: t! V 'not_found' => 'No site-wide notices found',- h& ?7 }0 n6 T+ F
'not_found_in_trash' => 'No site-wide notices found in trash'
: Q F8 K* R2 B. I0 w ); W( z1 b5 h. h7 N- `( E. S
- \" E: z4 z( q2 d: w
$args = array(
; |+ k. ^3 K9 H 'labels' => $labels,4 |3 h# P& v$ Y; ^& i4 D2 d* e
'public' => true,
3 C. Q2 J3 c1 }. H( q( a1 R 'has_archive' => true,/ r5 B$ N! @9 i- A j
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
4 f" K& c4 B/ {# O0 K! M+ f 'taxonomies' => array('category', 'post_tag'),
5 v. S- q+ m9 }9 F$ R4 _ 'menu_icon' => 'dashicons-megaphone', \: G7 W, Y0 ^: t6 d
'menu_position' => 5,5 [+ S6 U8 V3 A. V: s$ N
'rewrite' => array('slug' => 'site-wide-notices'). x( t/ n6 a5 ^" \; M8 T( H
);) H4 C) G7 [; {9 D
4 n1 c+ b% _- e! c+ _# T register_post_type('site-wide-notices', $args);" H5 V" a7 |5 z; l
}
7 K& z( \" o; C- F4 \3 j6 a- [ ```# S h0 p: L2 D& p
6 q; a" b0 r$ k4 E 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。! z- b0 E n5 P
; P" N" n% d f+ M! n) s, b
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:# N1 y" U% X2 f5 G
$ p% Z" x, [; Z0 x" J, ?
```% w: O T( p/ F/ s9 _" }4 @
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');5 z; h) C- {( ?9 \6 {
function add_site_wide_notices_boxes() {
( P. k& w: B5 _3 \* C add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
) D. r& h$ w2 z- k9 K }6 i* H: D4 L' \; \
% W+ Q1 ^* N# y; T# T; b function notice_details_meta_box($post) {& z+ A& H- z& v2 Q8 C: h
wp_nonce_field(basename(__FILE__), 'notices_nonce');
7 Y/ \) B _% v# D+ W! f $notice_title = get_post_meta($post->ID, 'notice_title', true);/ S ~" k: V G& d0 C* J
$notice_content = get_post_meta($post->ID, 'notice_content', true);
% X/ h1 Q' g3 g- |, D8 C! A ~ ?>: {% z8 v$ I7 D6 V
<p>
* q( U7 [% t7 `2 s) y <label for="notice-title">Notice Title</label><br>
4 L8 @3 [( ]0 f; d4 M$ U <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">: ]/ m5 ?% {0 a' T P! k# m& N0 X
</p>
# T* f9 Y. J2 t1 e0 [ <p>1 C6 ]" ?4 v0 C
<label for="notice-content">Notice Content</label><br>
* G/ O8 v! n' y0 B <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>$ c! c1 l R- I7 O
</p>8 Q$ d; j! u! `- @
<?php+ N1 i; p. J! c! e" k
}
l7 v4 d2 T! Y6 G3 o9 T# j- Q( K. T5 j# Z. U6 d
add_action('save_post', 'save_site_wide_notice_meta_box');
8 N3 z. w# B: g( z/ {/ M7 U function save_site_wide_notice_meta_box($post_id) {7 V6 l- L5 i) x2 n7 s$ p7 x! A
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
7 f9 n5 H, A; T" \2 L return;
; {2 Z& ?' G3 T* y+ Q if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)/ ]! C2 K( b" g$ m* [$ b
return;
Q* D7 G+ q. d( V, `
7 g' L9 U( `4 d# s9 i if (isset($_POST['notice_title'])) {6 D& f( K2 ]' _/ Y
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));) p6 J+ O3 ]" K A' f! y' i
}% t. s! \3 `% k/ h% n
if (isset($_POST['notice_content'])) {
- n# k2 ?4 R- J) m% m. j( m update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
% o1 ?; D7 C1 k }0 v5 j) u* Z7 {
}
: \" {( ?" S( W& m8 K, u ```- a" k3 l8 [- p2 ^4 O6 ]
2 K! O/ q: ^% V1 K2 y
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。! d, o, x' X+ ^% b: i
- p; T. T `$ N4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:: a7 w! V! P; f4 M3 b+ ^, M N
! R; [' s/ X" B" V/ M
```
( g7 q0 x3 i! O; c# w $args = array(
2 [3 a2 f- l6 d( P+ S; _- B" n { 'post_type' => 'site-wide-notices',
2 v6 P0 c0 N7 n' ^4 c* D W 'posts_per_page' => 3,. j: m: G; z) k/ p1 ?! I; N+ R
'order' => 'DESC',& \, Z. j* I* ~8 J8 ]
'orderby' => 'date'
$ ?3 e; K3 x, u% O2 u; a+ ] );! V6 v4 X- p9 P. P4 g! X( w2 j- i( ], G
$query = new WP_Query($args);
! Q/ [( w1 }: A2 ~* z if ($query->have_posts()) :9 ^1 m& o; O$ Z u- x( Z ?% A
while ($query->have_posts()) : $query->the_post(); ?>
6 x6 x; Z7 ]2 [; N3 B <div class="notice">5 b) E4 l, R# y! D7 X
<h3><?php the_title(); ?></h3>2 I1 \2 W( N/ z3 Q
<div class="notice-content"><?php the_content(); ?></div>
2 p/ i" `" g3 ?+ R8 E. Z </div> Z# ^" ?" N1 a
<?php endwhile;& |$ [/ g0 d P; Y6 T
wp_reset_postdata();9 O. B7 ?. j) L1 s1 G( J
endif;
2 t* e$ b* Q# X ```
( r+ i! m' Z# L9 k, F3 a7 v2 p3 \/ J/ R5 j* E9 q6 L; w0 O
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|