|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?5 G2 v# S4 E, q& p
" b+ g' P3 Z6 [5 q8 o- z# H5 a# d3 e
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。! [7 o/ H( n0 `+ W2 n4 F% r
* Y) R, m( H, q8 v
以下是创建自定义插件的步骤:
6 T! a( f7 n4 b9 u
5 C# I6 P; L- I! v S# d$ p1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:$ R# t6 j: \2 X! }1 i# @
* B* N. S& p, S9 @' n. Z4 C
```
* |) w5 X7 A' D9 K G) k/ l <?php
$ [! o7 E! o7 ? /*
3 N# p- n3 s. @/ E" U* U Plugin Name: Site Wide Notices Plugin1 R/ H4 Y9 Z$ Q3 T$ H. a- g
Description: Adds a new custom post type for site-wide notices.
+ o! d) q, ]1 T% @( ~2 p Version: 1.0: M) d9 d0 A) S- N T6 _. G
Author: Your Name( a+ n4 _" Z, _# ^
Author URI: http://example.com! b5 |9 i2 ~; O7 Z+ O8 R% D
*/
9 n) ~/ ` \( r4 r1 n
: b: [0 z$ r6 j. A // Add plugin code here..." E) V$ |( R- m% k( l- H
```6 R7 N0 f& N# \9 ^! d6 o: q
, m+ J+ _ V) j1 {+ Z' { G 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。% z* R8 Y: S- E! c3 i
# {* G0 ` D1 r8 F/ |6 \" X2 J2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:4 e$ V. B( V W& l% z1 F
$ r( y; L2 C" Z* I1 I ```& R) H( D& l$ ~5 \- ^
add_action('init', 'create_custom_post_type');
! X) G( i8 A! P function create_custom_post_type() {% ^ R4 O) P: C @5 D) g: K' H' r
$labels = array(: W) ?" l; R% O' q8 T1 g! ^$ M" j0 n
'name' => 'Site Wide Notices',
, \" t; a( M2 q/ t _8 R) ` 'singular_name' => 'Site Wide Notice',
* \0 W" d8 m' C 'add_new' => 'Add New',6 A- p8 E' k( d
'add_new_item' => 'Add New Site Wide Notice',
7 U4 W: f" T5 p" U- j 'edit_item' => 'Edit Site Wide Notice',
; `" o5 x, z0 P, @" f# [ 'new_item' => 'New Site Wide Notice',
0 X2 T8 R- i/ l7 K0 P* R 'view_item' => 'View Site Wide Notice',
! S- T3 L4 r- s* L) K 'search_items' => 'Search Site Wide Notices',8 g6 l& c( i l5 k6 A
'not_found' => 'No site-wide notices found',
. A D9 C) i* C$ y5 J6 i 'not_found_in_trash' => 'No site-wide notices found in trash'
9 H. J( v V; D4 b u. E" o );- N) d S0 C' j! K7 |1 w
6 [. A# h d' l9 c4 t $args = array(
+ F7 I4 W4 \8 ]. D+ O. R+ W 'labels' => $labels,1 L/ r2 u5 @; l1 p: N
'public' => true,1 q4 d$ c7 | w8 l
'has_archive' => true,; g! A/ N E. ?- K. ]3 T3 z
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),* \: a1 U# i4 e B
'taxonomies' => array('category', 'post_tag'),5 M3 F" g- {0 _
'menu_icon' => 'dashicons-megaphone'," A) V6 W0 R8 c% b) J' m5 l* R
'menu_position' => 5,+ ~; E$ M: l9 j5 N! a$ G- O
'rewrite' => array('slug' => 'site-wide-notices')
6 u/ X3 V; k+ P );4 Z" l0 i- K: b8 {; e
% U0 O6 ^) `3 }- o9 F; K8 }3 p
register_post_type('site-wide-notices', $args);8 e9 L. S5 p) F8 s) g6 x9 Z7 d6 A
}, n. a4 c6 w0 P3 M) v, j
```3 u* S6 e. k* x2 w3 F5 f
2 _9 s) ]- D' a$ J& m6 U& `/ C0 U; P
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。- e8 S% O" l1 Z
% n, k! K9 \, N! [
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:* L7 K% }( V. N1 |* U1 U
% X! C& ^- z$ a, h: ^ ```
# w% c+ v5 ?: S; F" x) f3 M add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
: P c: A6 m- N6 z function add_site_wide_notices_boxes() {
+ p# ?+ q2 ]7 s" s) [' b# D add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');/ y2 C5 N4 d1 F/ g
}
; j4 z5 S. S9 X4 u) P. X, f. V, v# l8 {
function notice_details_meta_box($post) {! I }6 Q3 y; w9 ?: X* | ?
wp_nonce_field(basename(__FILE__), 'notices_nonce');% x/ C3 ^" N6 T' [( Y3 V1 l6 l+ l
$notice_title = get_post_meta($post->ID, 'notice_title', true);" @7 `9 i; N- \" H! P
$notice_content = get_post_meta($post->ID, 'notice_content', true);
. I$ A/ f- ?! P+ z9 D% _ ?>
% D: q6 F& H5 f" T9 w <p>
' I% l4 ^9 z/ G' V4 b7 E <label for="notice-title">Notice Title</label><br>- f& c; H: i" C! D" Q2 F% n
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">7 e2 t5 T% W' D% k9 T6 w8 b4 w
</p>
; Z9 Z( V9 d: I# ~- X <p>* A% Y- R# O1 D# L8 b8 H1 a4 e3 c
<label for="notice-content">Notice Content</label><br>
0 ~! l) b" R% s; O; k3 N <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>+ w- q2 n% o; A% I# Z: T5 \
</p>) r7 b/ K9 ?- }6 v0 _- Y6 \
<?php
0 A2 x R3 Y* T R: L6 J }
" G; ]( l4 X- e, l$ n
; Q4 z9 n' O1 K: _; \, w add_action('save_post', 'save_site_wide_notice_meta_box');! e _2 b) F8 w( L' m
function save_site_wide_notice_meta_box($post_id) {5 R& U9 R b- Y n: K0 b1 L
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))# R5 `0 v, T! b8 |! _+ B
return;6 L* n# L3 ^9 k( Y# {* q
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)' X3 n2 b0 S% q7 b9 o- q5 X) Q) l
return;( C" f! }5 X) d9 S0 @
; c, S0 y7 C7 I. n if (isset($_POST['notice_title'])) {! [/ R+ u( ]/ v$ U1 H3 d! v1 m
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
1 O+ n1 b! ~7 b- Z. ~( t: E4 S }8 ^1 Q2 B* ]& `2 P
if (isset($_POST['notice_content'])) {
7 h* d+ n; j( l- Y update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));/ B+ q: x, K! r E1 G4 O
}% }1 v6 |! \& J0 F# Q) Z- A. H
}- J0 r3 d' W6 e8 X" h) q5 T
```
. `/ I. }: i7 I2 {7 k$ x. Q, b& g! b4 M, Q" @* |: `- m- N
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。. @# [6 P% D' k1 y' l
6 ^0 @: L5 H, n9 n d
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
7 V. r, y1 a5 c9 P" V' Q' E4 Y: B/ }: \
```3 L* i }0 u- Z6 l
$args = array(
! W* u& I5 g. \/ @" p! u 'post_type' => 'site-wide-notices',& s4 Q- j2 m& l+ \; u4 D% f+ x) x
'posts_per_page' => 3,
* M* f- S- h) P6 {$ d0 k 'order' => 'DESC',9 S; b% Y- ~: P, m- L% U( ^
'orderby' => 'date'8 q9 U5 d# a$ O
);! {) N5 f% u+ p
$query = new WP_Query($args);
" b8 d, c# ?; w( Z7 Y if ($query->have_posts()) :1 F) F8 ]- \# z" C
while ($query->have_posts()) : $query->the_post(); ?>
7 U3 F' H+ _+ x* P& M5 @ <div class="notice">1 G7 d( Z' W" f+ @4 N% b
<h3><?php the_title(); ?></h3>
% p9 p( J; ^, n <div class="notice-content"><?php the_content(); ?></div>& ^6 ~3 P4 G* N
</div>
) a% F- |2 \5 i) X+ `) X5 E7 `9 j <?php endwhile;. D) P' _+ [& ?1 v% i) V% u
wp_reset_postdata();
# ]! p7 G% x/ o) G5 Y endif;
4 s( F+ t. l1 z ```4 O8 m5 {6 D3 j
+ _* }7 Y+ {) L* j; M 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|