|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
6 O3 R, c# Q B) O, ~
X: n3 J/ E9 W/ H9 d% H! c2 V如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
8 S K! P: a; n
) g& M4 \, j5 a0 |; G9 O, j$ |以下是创建自定义插件的步骤:
1 T6 y$ J0 I% ?8 a9 Z
7 q% A+ ~% l% x& Q4 p' C( ^, m1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
2 n2 w: z+ ^. h/ {( s) D9 l
2 G% Q# a) X# t) M8 h) V ```
$ A: V: y. I, l7 k. L <?php
" s+ H3 P4 T9 t6 P /*
: Y& K* d7 P3 c* w Plugin Name: Site Wide Notices Plugin
+ }8 }- c/ @* \. C8 {$ v5 Z Description: Adds a new custom post type for site-wide notices.4 O# C' H' G& R; A9 } m0 N
Version: 1.0
3 U' F2 _0 R: K j Author: Your Name* v6 O0 j# W r
Author URI: http://example.com
" q* `5 L- A% n6 A* e* H */
, m2 D/ ~5 N; Z$ ^4 Z3 p, T, I" a* J; S* G$ k! w
// Add plugin code here..." ~4 O1 X! O! d( J
```
4 k h: @. d* I$ {. j
1 \! @6 \* q6 s% y9 S5 {" c# y2 j% B 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
( D6 ^& ?" _' Z. u
* K2 b( ~8 [4 X$ l J2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
1 w; m6 V) R: c+ N' q, P r* |& b; [1 w. A$ v u' O( Q+ R: X
```5 L8 R, N) u$ K' q V0 D
add_action('init', 'create_custom_post_type');
. t- D( A! f% { ~& e function create_custom_post_type() {
" W: j! x0 `; a0 _7 O $labels = array(
* }& v3 j9 @7 H* B" ^ 'name' => 'Site Wide Notices',
! k0 ~6 F$ z8 C9 l n4 u/ c 'singular_name' => 'Site Wide Notice',
+ |& t( M+ K: g( E" |+ g3 p( R 'add_new' => 'Add New',# T3 T5 _) `6 W. i( \; X
'add_new_item' => 'Add New Site Wide Notice',
7 b( t& I: t$ I 'edit_item' => 'Edit Site Wide Notice',' k) N% \3 J! H( j4 o
'new_item' => 'New Site Wide Notice',$ O3 q8 G! r4 X& f6 ~ H
'view_item' => 'View Site Wide Notice',0 S5 D$ S7 E% |! B- ~ X, Y. k& r
'search_items' => 'Search Site Wide Notices',
/ T' [( C5 d: V8 j. P2 v! y 'not_found' => 'No site-wide notices found',% M+ k/ k& F) ^. L7 A$ i3 P
'not_found_in_trash' => 'No site-wide notices found in trash'
4 T: W. s5 n3 r: O3 K );1 L1 Z3 s- B7 e: B' `8 _
# S% W2 X/ h$ Y$ U4 t
$args = array(
1 [- }$ {5 T( J D- n" a 'labels' => $labels,
) s$ \& |" M& W: G5 | 'public' => true,0 {# |6 y* c/ T. X& P
'has_archive' => true,
+ N# F0 @8 {' Z2 w5 P# x 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),1 X: h* y: d! j1 r2 }
'taxonomies' => array('category', 'post_tag'),
1 b' C" C" {4 O8 c; u3 i 'menu_icon' => 'dashicons-megaphone',9 J* v% g7 B2 a
'menu_position' => 5,' k' Y0 H! a; o% t1 v
'rewrite' => array('slug' => 'site-wide-notices')
I( {) w" h9 E _3 [ );: o, y2 |! Z! T, ^
8 U0 ^/ i! P, |; V& l register_post_type('site-wide-notices', $args);
% q/ ]( x# n9 c0 v% O }& R; C1 P. A# ^; A \4 E) i
```/ x: f" @. B- _: E* R& a
0 ?0 f5 c/ a+ [ O, T 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。$ M! A. ]6 u$ Z1 r, e& l
# e9 t/ X2 q, L8 f( W
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:1 O& S. i* C* G0 X
# j# E5 o1 I" R6 Q4 r: K3 p) Z ```. L; g5 j$ G% k: u! f3 ?
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');: X! h5 A9 q6 {. K) _5 I* \1 a
function add_site_wide_notices_boxes() {
* l6 l5 k# h( g7 Y add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
+ R% ?6 w* R/ K. ~6 [+ C }
! S e- |9 G2 E( S3 U) h7 M+ j1 i v
function notice_details_meta_box($post) {
1 ^! ^' x! r4 [8 d) P/ A) U; f' B wp_nonce_field(basename(__FILE__), 'notices_nonce');
8 H/ ~$ Q5 [9 W, O $notice_title = get_post_meta($post->ID, 'notice_title', true);
. q3 I6 y' y" b: e- k8 ?! l' ~0 c $notice_content = get_post_meta($post->ID, 'notice_content', true);
. B) m) _$ l* w/ k: M e ?>0 d* N; s, Q1 H1 _* c
<p>
6 B3 }. t% C( F. e <label for="notice-title">Notice Title</label><br> S/ p) _) n8 t) R8 G1 O" a
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
+ [' u* U9 b% q1 U3 }/ ^ </p>% U9 U+ j! a5 b4 }
<p>8 n, s9 I; ?5 c( d* g7 o6 D/ P
<label for="notice-content">Notice Content</label><br>
5 f' d9 g0 ~+ g/ C <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
. `( h# @% D* x </p>4 w4 O I( I5 m x7 M
<?php I& T7 L/ G" o. ^! B) Q n2 g
}5 D) N8 C0 E% L( v7 z, `" ]& ?2 o
* E) U, f$ [) k% K. j, B5 e
add_action('save_post', 'save_site_wide_notice_meta_box');$ L) y6 G7 C/ L# s" J
function save_site_wide_notice_meta_box($post_id) {
6 J9 {0 K/ {8 z if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))5 }! S/ G3 x; L1 _* t
return;
! {0 u. J( G8 D( f if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
! n" C! G2 o: ^8 @7 N8 ~6 h return;
0 S2 O, L# ^8 l& b9 m/ Y
/ h$ u1 o6 d1 |4 Y& Z& O if (isset($_POST['notice_title'])) {6 N y; F7 [- q6 ]
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));- E& f8 q# @6 y' w! d; b6 A* h
}
- E1 H' B" q# F% e$ g( r, d if (isset($_POST['notice_content'])) {
m0 F8 S* n6 l) F4 X1 G5 B. r update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
' c$ Z% e4 m$ @1 t, e }
6 j4 Q1 o) L$ { h }
H9 q# \+ ~* X, A8 ]2 [ ```" P- c1 C, j+ O* C2 k$ r
& u: ]/ d; z; o' j) M; Y
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
5 T1 b0 c' n7 I3 J) X
- f3 @7 [: c: u$ k6 E4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:9 o0 n3 V9 \& L& n
. H c% _- \: N* p1 g# h
```; V$ s' T* e' w7 ]% S. ^
$args = array(
, {( y+ q f* P* |0 Z0 r* y) s4 n 'post_type' => 'site-wide-notices',& ?! W0 h* {/ N" |2 ^( J& t6 x
'posts_per_page' => 3,- N: F6 m$ \! T7 R v9 A1 N
'order' => 'DESC',
& \4 j& G; ^( t, x [" ~ 'orderby' => 'date'
$ T9 G4 h" M( h4 n O4 \+ H );
L6 N8 n5 y- Z" b $query = new WP_Query($args);' a; T3 Z6 i0 N, k$ w3 X& _- x
if ($query->have_posts()) :
% o+ ?6 Q$ H% P: I3 V7 C: I6 K9 v while ($query->have_posts()) : $query->the_post(); ?>
( y7 ?# Q/ _2 V: |5 q, A <div class="notice">: G7 K+ P5 A* ~9 F- C' f. U9 I
<h3><?php the_title(); ?></h3>
( S- e# { E3 G8 G1 v, f& E <div class="notice-content"><?php the_content(); ?></div>
* k* N. ~5 t. w+ K9 N </div>. g) J& P" `, k/ ?# \" g4 k& D8 ?
<?php endwhile;6 _% J6 D& @) Q; w$ z: D
wp_reset_postdata();
b: }* ]& }) N. H8 U0 m v" m endif;
8 `7 ]0 r3 K4 \4 @+ [% l4 k ```, D9 {' u4 Z; W2 [9 P# E
9 j" |: {" o7 _" j: P 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|