|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?* Q7 x/ y# @0 ? _: _/ ~8 M' z
! s9 p, Y* x2 f如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。( n) k5 ]6 x: {2 G1 H: [' K1 m2 h
- I. W- Q! t7 g7 m! f$ u& Z以下是创建自定义插件的步骤:( r0 E! J* y1 i( Y. d( ?) i
; R+ n/ b9 m! E1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
2 e9 v6 p) v' c0 Z4 a8 V# k7 m% C1 q0 F& ~' N
```
+ Z u- [! ~! t' B <?php
" I$ G5 D- \. Z4 d& p7 \ /*
1 Z& g- _* z) ] x G Plugin Name: Site Wide Notices Plugin
7 r# {6 h, |2 W' V Description: Adds a new custom post type for site-wide notices.# J( ^' v) z$ H- m5 q& M% J" h! I
Version: 1.04 T5 k3 N) p* o3 }8 _/ b4 x, a
Author: Your Name& U. t) v' g2 U; W3 y* i; c: A) n
Author URI: http://example.com
& M& [$ r" q- i: U" A! N G */" }8 e( D( ^2 J9 }
' ^9 w, G8 H1 [ // Add plugin code here...
" E5 }# Z! d D: c ```
1 F( e+ A2 J3 |7 H1 I1 o: B
/ g, |& `9 o) V; R2 b( E 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。0 w6 N' O% Y! ]+ l3 p/ y! a/ A
; j& R4 _- E3 c( Z" L2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
) j+ P4 o. G; j- r! W$ k& a
$ o( i7 Q5 U+ J: k ```& b8 u, k* \' h; r4 o! i8 p! ]
add_action('init', 'create_custom_post_type');3 B; m( l2 q7 z; I
function create_custom_post_type() {
5 C, E# R/ C, K2 ] $labels = array(
' r; o1 d j# d3 |8 d* I 'name' => 'Site Wide Notices',
' l5 g) w' j; z 'singular_name' => 'Site Wide Notice',
. `7 F+ R5 D* j6 |8 r) @1 [2 Y 'add_new' => 'Add New',- v; ]( H; S% i( P* d! e' p& N2 m
'add_new_item' => 'Add New Site Wide Notice',
$ J7 \9 g5 B% g1 v+ g, Q 'edit_item' => 'Edit Site Wide Notice',
7 c! L4 V4 h" f/ N& ]7 I) o( u 'new_item' => 'New Site Wide Notice',3 ?* ]* [% ^# C6 z7 W: a
'view_item' => 'View Site Wide Notice',# Z3 N. V4 O L/ j" R% G+ i6 Q+ `
'search_items' => 'Search Site Wide Notices',- w4 r( C0 n3 Y( E+ |6 i
'not_found' => 'No site-wide notices found',! [' O p2 w6 ~6 @8 s
'not_found_in_trash' => 'No site-wide notices found in trash': ~( y# H& L4 O3 I1 G% k3 s
);7 Y c \; _; J9 Q, [: X a7 q
) A. V& g$ \: V4 k% a- s $args = array(
( X/ ^3 a/ H) H( `$ O6 H e 'labels' => $labels,
o; A1 @' D0 V$ [ 'public' => true,
& w4 G0 g' q/ _/ X+ t: ` 'has_archive' => true,
6 x) @/ }- ~2 J& R4 Q0 ~# V$ D 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
6 _' W- }% f* P0 F9 P 'taxonomies' => array('category', 'post_tag'),! `1 J: r; m! _* B
'menu_icon' => 'dashicons-megaphone',
7 M( N7 G0 ^) O5 E! |( E* O 'menu_position' => 5,
; B' @/ t g* r! I/ V 'rewrite' => array('slug' => 'site-wide-notices')
" a* K6 W6 M- S1 Z' w8 Y( H );
0 @8 q0 y% [6 {( F$ X# W* N, o0 p/ w5 K! ~" v
register_post_type('site-wide-notices', $args);
/ W4 [% w, `) k& y }2 _" f0 @: c7 I$ l! e5 t
```
7 D- [1 T) R& e V" d3 R! Q
6 w8 \7 q: x+ H" P 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。- U) E/ Q; X$ |, U9 D
7 ]0 S) |; _+ r- i2 g$ ?' @1 K
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:( {; L0 t$ U, e8 v4 u5 Y# W
4 O- [$ C- p$ N0 r/ _4 e ```
/ s) K) I2 D0 }. z add_action('add_meta_boxes', 'add_site_wide_notices_boxes');5 ]9 j7 T1 b9 Y. E# [7 b
function add_site_wide_notices_boxes() {" \1 a! m7 {/ M6 ?6 i2 v
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
: B$ D6 _% o% ~% S }
. r- O# v, R% l' M
/ R- X/ w* L# I5 D3 }* Y function notice_details_meta_box($post) {
$ Q; v* q2 l, E- }- Y6 `1 {* K wp_nonce_field(basename(__FILE__), 'notices_nonce');
. _" o j, I7 d8 X1 G# \' v $notice_title = get_post_meta($post->ID, 'notice_title', true);; E* Y! `# i4 f- c9 z
$notice_content = get_post_meta($post->ID, 'notice_content', true);
8 e. y7 ^1 K/ R9 }! Z6 C ?>" z9 ^+ Z% R1 k c0 K0 [
<p>
* t4 o' q8 {; x( K* }* r <label for="notice-title">Notice Title</label><br>- }: ^: r n$ q4 g) |0 @ p
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">; ^. j5 J( n( s1 b
</p>
! }( A3 w9 t" W* ^ <p>2 Q- ?* i6 N' H/ O# G$ H
<label for="notice-content">Notice Content</label><br>% P: R" g ?7 n2 K' W c
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
7 d5 }8 J$ w$ \! A; }! H </p>/ b b: t: h- s" F7 ?
<?php
4 [, ]; c* U. ^ }% H' E0 @" w, y; X& A) A7 A
. j. |) U8 z7 R1 d8 x
add_action('save_post', 'save_site_wide_notice_meta_box');- l( }* H/ K. N( G+ \
function save_site_wide_notice_meta_box($post_id) {4 r+ E6 [8 g/ G3 U* ^# S$ v
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
1 F# N4 K3 |1 g* e) E return;
+ {7 c$ b5 X; {' q) v, U if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)! \: i. ~0 Z0 t' e3 n% R* d& h6 k
return;
M s9 T" n) y7 E7 O
7 D4 |) U* U# T& W if (isset($_POST['notice_title'])) {
/ r! [3 _& @" B* U. e update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));$ V+ x5 B3 \6 H4 {0 i" W& Z
}( @$ l* w# M0 Q, ^6 S8 o: G
if (isset($_POST['notice_content'])) {
( r4 m+ o( l: H* O# Y update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
" o" e0 q6 O( A6 n8 A5 g5 ^ }
& m& `$ w+ Q% h3 \, A& u }
) b/ K. M& c0 B* f ```
% L! k9 u9 G$ E0 ?8 J- _$ m# n( _# d( f+ a0 s+ q2 r) U S* g
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。# v* f* Q; m) y a7 O* C9 l4 ^+ I
2 S* z* _$ I' C% ^9 W4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:- B# o+ f% n' O0 r& \* k. ?, Q5 Z
3 d$ X- G- ]0 C$ k5 }7 s% J6 D ```" A. P# l8 v4 C2 } Y
$args = array(
8 k9 E; o- h3 P, P' w 'post_type' => 'site-wide-notices'," F* S, G! i& d. U1 ?% \% p
'posts_per_page' => 3,
1 t1 c/ [# e# K: N/ P# M, H 'order' => 'DESC',
8 w5 ^/ ]9 \0 c" F. D; i- q 'orderby' => 'date'
" x' F+ y: x/ M' V" W );
6 ^2 v$ R# z6 \0 e5 y( N( J4 Y $query = new WP_Query($args);' U* J& ?3 D6 E% |5 |. t
if ($query->have_posts()) :
: B' P8 A, A( @3 z c. L; D while ($query->have_posts()) : $query->the_post(); ?>
1 ~& W9 Q: R# Y/ c' p. @7 ^0 g& N <div class="notice">
1 ]% ]1 b5 i2 ]' c/ e; c <h3><?php the_title(); ?></h3># ~9 P8 @5 I5 V) d
<div class="notice-content"><?php the_content(); ?></div>
5 d2 h( t$ ?. ]6 Q- u </div>
) [1 N1 k& ?% y5 A& s <?php endwhile;& j4 }% s( }* z8 Q
wp_reset_postdata();
3 L( y+ F; O/ D5 p, x endif;' A/ K; I; [' ~# d2 ~2 L
```% H0 D0 U; ?: s9 ?5 k+ d, b
. v$ W& `( W6 W8 O/ D9 e+ u% ?
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|