|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
4 N/ @' H) K8 d3 K1 i9 Q( n
6 d* K$ B9 F6 ]如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
8 h/ ]$ G: S* Z1 R& U" e Q, A$ i! K3 k& d
以下是创建自定义插件的步骤:
+ s" R5 i8 c% D) _$ i" Z8 |0 x+ E/ Y4 V2 w9 k$ m9 w
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
9 }$ k7 M) y$ P5 c) b
& v& O! K s) W# ^! p6 N7 R ```; \! e5 ` b4 V% T+ D+ Z
<?php7 R' T h/ D" |# K4 e4 q d# R& b
/*( A/ A2 v* i2 g1 V$ i
Plugin Name: Site Wide Notices Plugin, n) s+ N9 |7 ?& l4 Q
Description: Adds a new custom post type for site-wide notices.
) e) s6 P: d6 Q- r& {+ X Version: 1.0
$ J- F! {1 r( w6 f Author: Your Name
" n* B* Z: S& G. { Author URI: http://example.com& z7 A) l4 H. b* U! D! f6 T' t
*/
3 M- U- B0 X( d& [( _$ @
; N# W9 }! Z9 }2 d3 { // Add plugin code here...
" e( L9 M$ U: o! N& k ```* j8 u: f. D U+ G
, [( j% b& `+ @+ d- n I
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。, L2 A, S2 T$ |3 ~; }8 y3 c1 ?
+ @3 @$ z. ]' s3 v ~) {* k- Z
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
$ Q' J2 e- v. B* f9 Y& X9 z% }8 O! S2 c# M: P
```
4 ~, `. D- s& |+ E# J( x1 ~4 ~* z add_action('init', 'create_custom_post_type');3 P" l0 |2 P$ e e6 G. s8 Z$ y
function create_custom_post_type() {
! h& q7 K" @" u' P/ s5 b% F $labels = array(( Z5 M; C! U2 a- F3 ]; ]) S
'name' => 'Site Wide Notices',' Z m/ S" G7 C0 ?) t# ?% R! s
'singular_name' => 'Site Wide Notice',* {6 _4 H7 C$ v
'add_new' => 'Add New',2 s' Q3 h6 `2 a# j# R
'add_new_item' => 'Add New Site Wide Notice',
+ }% V# E3 B' z/ |1 X0 k! y! i4 E 'edit_item' => 'Edit Site Wide Notice',+ Q$ y* u8 q4 K7 t+ R$ x! |1 ?
'new_item' => 'New Site Wide Notice',
$ @7 N6 o3 G8 X6 i5 e3 M. p 'view_item' => 'View Site Wide Notice',' n- b( f$ p) ^* c+ K- i
'search_items' => 'Search Site Wide Notices',& a/ v' Y4 I1 B2 \* x6 A/ v% g
'not_found' => 'No site-wide notices found',( q/ ~' |4 V' ]0 @& b/ G
'not_found_in_trash' => 'No site-wide notices found in trash'
* J! g9 R# d% G/ x( _ );! g: Z, l Q) h# ^0 q! u0 e v: X' K
: q! u' ?' s' d" X+ O( C $args = array(+ I) _1 d6 i7 S4 Z& i2 S3 j+ R2 W& U
'labels' => $labels,) U8 G! {+ v# e7 C! a# G8 T+ ?# z4 {
'public' => true,& d* A" D( W8 W# K/ }3 g
'has_archive' => true,7 U) {0 w! f: s: _) V* G9 {9 @5 U
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
$ p: ^, H5 v9 A. _$ w9 W 'taxonomies' => array('category', 'post_tag'),
# x$ h: l) k& Y. J! Q( P 'menu_icon' => 'dashicons-megaphone',6 e: R& ^0 ]! V) R
'menu_position' => 5,
% k8 w/ t4 S5 S/ P# W+ |. P 'rewrite' => array('slug' => 'site-wide-notices')
8 E T# P# V3 j( C- V% V9 H2 @& G );
# a# U3 Z5 i9 N0 n7 Q; @
8 k/ ~9 l4 P" s2 c4 l register_post_type('site-wide-notices', $args);
6 |0 K: X# X/ b7 ~# y7 S4 v m }
& |& e! Z! ~3 i2 {, n$ j3 K ```
& l- c( i) l- C& Z6 X* x7 R9 Z0 h8 u( I- a- [2 U
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。9 z2 }+ } b, \# n7 ?
( x4 K! K4 [% B0 m4 E. Y3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
# f- [- b& e1 S2 \* y [3 J5 B) V( R6 V/ a
```, Z! K+ `% w0 J
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
, G% I: i* q. I: y function add_site_wide_notices_boxes() {
. B# D' J! r$ x1 K add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
; K, Q" x/ K* n5 Q }
a1 l6 C9 e* Z" \" d" D) ]
`) W& K6 ~4 {" K& y! z function notice_details_meta_box($post) {" l/ c3 \7 h1 ?
wp_nonce_field(basename(__FILE__), 'notices_nonce');3 L/ x5 y# \2 X2 Q; w
$notice_title = get_post_meta($post->ID, 'notice_title', true);; T- k0 ]3 ~8 x- z+ z3 J
$notice_content = get_post_meta($post->ID, 'notice_content', true);; w$ l2 [, R" i' Z6 `. x
?>; A& B/ R1 x) ]. ~& ~- Y4 [
<p>
/ o' U9 t0 t# P; ?! s& N <label for="notice-title">Notice Title</label><br>
4 P& R- y: c' _5 |/ H <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">- P; w7 d2 A( j# Y( t
</p>. o: q% L5 R3 r9 g k
<p>, R7 [2 P) k: f" Y$ T9 y9 [
<label for="notice-content">Notice Content</label><br>8 ~ v5 x2 k2 f% Y E
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
' |7 B: D" _ z </p>* H' n. ` j6 O) ~. C
<?php7 d7 d" R# W j& k
}8 y' D! A; u% N N. ?
3 z1 [4 N/ z1 N. w5 ^* p
add_action('save_post', 'save_site_wide_notice_meta_box');
: t$ Y7 U# s/ {' \& @ function save_site_wide_notice_meta_box($post_id) {
4 L3 W' ^( b: r8 v$ C5 W. M+ V if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
2 ^6 B- L) ~) g* T) _3 K4 w' n return;
5 X1 E8 Y8 Z% I# [ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE). n1 ]9 f/ Z3 X+ O! l* ?, q: G
return;& ~1 Z* L8 X# J7 T
' k* n) z7 \) P
if (isset($_POST['notice_title'])) {# ]; ~: V& e% @; J0 F* v
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title'])); a5 k, [) E4 r8 A7 l: m7 S5 W
} O/ Q; i# a! m! J# d
if (isset($_POST['notice_content'])) {
' L3 N' B1 c, E8 h. o8 L8 h2 ^+ j update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
) s) O/ l. S1 ^% t% v* S }/ J. `3 I x( a7 i! K
}- L3 ?, q: h3 b
```
9 b) ]; u* }, J) S( M
' Y* [8 ?) |/ r" s 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。* U! h2 z* r# @. U# T" o6 w' J
, i0 U. N7 K" v: S1 `$ s8 @3 M5 J
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
/ d% G) @( S2 Z5 e! A
# f4 T& @4 b' |3 | ```% ^+ Y5 `6 R+ w) A W7 A; f
$args = array(
* N$ n, \1 ]7 a% ~' u2 q. i 'post_type' => 'site-wide-notices',
) \) ~/ x) X1 M" t 'posts_per_page' => 3,
% F" }/ Q6 y1 ?+ l 'order' => 'DESC',5 u6 b( S% X# |
'orderby' => 'date'. \/ o; _. p( C2 I% D# `& ?. c: O* q
);
7 x( D! U+ n3 F% E9 q $query = new WP_Query($args);
: H. }7 p8 S4 o$ H3 Y1 { if ($query->have_posts()) :
" b( Q* E' _$ z S- I5 Q while ($query->have_posts()) : $query->the_post(); ?>6 B) C- Y2 g1 M% K' ]
<div class="notice">4 P3 A( L" h s7 |7 {% X
<h3><?php the_title(); ?></h3>5 Y* i8 h g1 ]5 }
<div class="notice-content"><?php the_content(); ?></div>
$ M# ^ p: D/ [/ l9 h </div>
/ D3 T# m" ~+ p% q( t <?php endwhile;# ~' L, y' v5 ^1 g" r" J3 C
wp_reset_postdata();. Y, h7 g* `8 m& Z/ T* y
endif;
) @% E; o t4 @! I ```* O: h2 R( G/ i5 n2 L$ `! t( w
0 d" n" Y3 W: {: m P 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|