|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
6 L3 A+ W1 J W1 i7 l
* r; d8 a" K; \+ \如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。+ f4 d1 j$ O/ N& I* H
% `) o% b6 i. w- }7 H' M& A以下是创建自定义插件的步骤:
R0 r) s6 e. U; {5 s. {& |# a2 ?% h7 d
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:8 ^0 Y* {, E1 l# q4 Z% [
% \7 E; T+ L) v/ e2 V% O$ Y
```
0 S) u" h& h! o; q1 u6 K, h( f <?php! X2 l6 x# n5 @- Q) c6 U7 p. m
/*0 D% F Y* R* C
Plugin Name: Site Wide Notices Plugin
" T+ O0 C( r; L, i) \6 s R7 S Description: Adds a new custom post type for site-wide notices.
( h5 ?, E: C9 ~9 m2 Y Version: 1.0
1 C. ?+ Y D+ e6 S0 Y+ s) p( y4 V Author: Your Name
0 Y: n5 W0 u" i+ I& i& Z+ L0 w Author URI: http://example.com
# C1 {+ s% V$ L) P5 G( n5 f */) X9 k$ h1 u' {7 P. Y
3 w3 l- w; |0 R2 ]* H7 L: L0 i // Add plugin code here...
@0 e* l# ~ `7 X- s) x+ m/ M% s ```
: O' ?3 H; U0 v$ S: f" G" U; _' p3 D& E; z
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。( f( _2 {0 p* t
# x: A. y, X7 Q4 \2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
; C0 h2 P7 S4 H8 s4 L0 ]1 N( X3 j! y" a/ i! a$ Z! {2 A; g2 C+ h
```
# L* q5 O3 I/ P6 w add_action('init', 'create_custom_post_type');2 P+ z( Z. q) ]+ I# X7 H3 l5 ^, m/ e5 T
function create_custom_post_type() {
, ^: y0 V& x6 n $labels = array(
$ p' ]! p7 w, k) E: i) _ 'name' => 'Site Wide Notices',4 K! x* D! c2 g1 Z3 Y6 v
'singular_name' => 'Site Wide Notice',& B: {) @! O* `6 F6 F
'add_new' => 'Add New',
4 ]' W S" o' C, r+ R 'add_new_item' => 'Add New Site Wide Notice',
: b/ I( ?$ T4 R8 } 'edit_item' => 'Edit Site Wide Notice', P$ n F+ N7 f* i% @. _; ?! |
'new_item' => 'New Site Wide Notice',+ z. y' | r/ B) B
'view_item' => 'View Site Wide Notice',9 B* d! n6 a9 J6 S3 `' S
'search_items' => 'Search Site Wide Notices',
/ s- `5 J: ^& j9 z$ q 'not_found' => 'No site-wide notices found',
7 c+ Q8 K4 q _2 ]5 [+ [$ v2 g+ q) D 'not_found_in_trash' => 'No site-wide notices found in trash'
+ V1 K( w& j% W4 F) L3 e );: }' m" a$ X" y
, U' r# K! S X6 n% E$ k
$args = array(
6 h8 g: B+ Q8 G0 ~8 X; C 'labels' => $labels,
0 }$ y+ p3 J/ L# p; Y. j 'public' => true,
9 @, R$ [; T5 I. p9 } 'has_archive' => true,5 \- j6 F5 Z4 y6 b
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),4 T& z" ?& u% ~" J" k! Z
'taxonomies' => array('category', 'post_tag'),
) @. G3 i) U! p) ^& |3 S; Z 'menu_icon' => 'dashicons-megaphone',
" c. _( F; W; ^* {4 C. r) T 'menu_position' => 5,
5 h' m1 L6 E1 c5 [5 D, J) b, ^* o 'rewrite' => array('slug' => 'site-wide-notices')
* Z ~( Y. g, L4 }+ U );( [ ?% Z+ Q2 p6 r t6 B5 g
# f2 V) N% p- x1 |7 [; @ register_post_type('site-wide-notices', $args);' r6 Y/ Q. [+ V3 \, D8 V
}* A9 l# @, o+ z0 j& P
```
s. \2 v2 Y d8 W) `) [1 k' F5 B8 `1 ~' T
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。) x# ]+ w5 I8 ]0 l
) g" d' c! r4 I2 o0 @3 R4 Q- ^3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:* L( a Z+ O. f7 ?# n
- e5 e; i. B# J+ o7 T: i
```
% S& ^6 @+ R3 j) S" v" v add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
0 D: `$ e+ h+ I3 B9 A) e- m function add_site_wide_notices_boxes() {
. Z; u S4 w# ], V' i5 Q add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');, p# H% t. i v0 D" b
}0 W$ p( k. v& O# X% E- N
$ g/ Y0 ~/ N: w5 B0 K: O8 H function notice_details_meta_box($post) {/ a4 n0 I) L8 i( T8 e9 m) i
wp_nonce_field(basename(__FILE__), 'notices_nonce');) ?' ^ l0 n/ E: r
$notice_title = get_post_meta($post->ID, 'notice_title', true);1 b- a1 W) n# W7 K; @/ K
$notice_content = get_post_meta($post->ID, 'notice_content', true);5 ]2 l. p) |% |
?>
5 Q6 b: g( b' m3 \ <p># ~7 K2 O8 S% J/ k( V
<label for="notice-title">Notice Title</label><br>2 Q( S3 z, m2 P& V: h8 O+ f
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">) x5 U7 U8 o: [) K" G
</p>
2 P1 v2 \' @ U7 I3 A! L8 a( C6 c <p>- \4 j c5 l: A7 R. d
<label for="notice-content">Notice Content</label><br>1 D# b9 b j. }. G- u! Y" O
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?># h# r" g x' G' t! i
</p>- O9 K' d3 X- @% ^; U0 N! m4 C6 q
<?php
# Y# Q; N& a: W! S% H }
, ~) P- D9 z- l j& }/ `7 p/ O8 c9 O: q
add_action('save_post', 'save_site_wide_notice_meta_box');
& F+ z% G+ `8 @$ D% |' k function save_site_wide_notice_meta_box($post_id) {* h/ H4 c! s; S U
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
) d# L1 s' B5 b+ N0 b" M return;
- m/ N( J* P7 b1 ?! Y, r if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
& ?8 N; A7 G& w return;
" n; n" Y2 Y$ t; I: y( z- ?' w( f% r8 ~$ }% z' q6 t' [: W* X
if (isset($_POST['notice_title'])) {
0 O& ~" z* E5 K% c6 X1 }$ ] update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));4 g6 S; j9 D; B$ F& |
}
' |( j* R& D4 Y C3 H if (isset($_POST['notice_content'])) {
; p6 J, E- l+ E$ P+ j; F: F update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
) s2 l5 ?( f5 L4 f. G% S5 { }, i8 [3 Y3 Y. `0 l! d
}
' M+ U+ ]9 l0 r ```
7 T" k" J+ Z5 C0 e
' J0 u a' i- m [& a7 G+ T1 C 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。6 x! V$ F, Q* ~( Z7 a8 q
7 l# p! ^. k5 f8 W: v8 H4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:1 o: @$ H+ x( A4 _) n1 s0 B- o
* H K0 k4 }; V. p% e* M2 c; D ```
7 a( h. ?4 K7 P* @6 a3 ~ $args = array(4 U4 z6 @% E" e# c. W8 R* r
'post_type' => 'site-wide-notices',8 ^ Q# W2 u/ {+ \( p! v2 v0 }8 ~7 |
'posts_per_page' => 3,
! R" W$ Y: r, e, s 'order' => 'DESC',
$ f& T2 l2 ?; W+ L6 v 'orderby' => 'date': Y9 W0 d6 q- Q5 r) |# a
);1 I" P6 m1 ^: X9 i" s
$query = new WP_Query($args);
" `+ C) p D L' `- [2 c3 l5 @ if ($query->have_posts()) :& V' c1 g- f c1 S3 y. A9 A
while ($query->have_posts()) : $query->the_post(); ?>$ z; O, c: q% N' X% Y* d5 s" a& }
<div class="notice">
7 N4 y8 z- L( l <h3><?php the_title(); ?></h3>
2 N' t) k7 b" t8 w" ? <div class="notice-content"><?php the_content(); ?></div>
- R$ i) G8 c& r4 @$ } </div>
+ ^7 z; t& `' X ~* e; s3 T$ S$ b <?php endwhile;1 a0 F2 i; Z% }# U' V8 Q4 y! G, _( \
wp_reset_postdata();
! Z' W3 M- q4 }, l& X5 K2 W endif;3 f5 j( z) s4 C( x- J0 N* s
```7 i" X) a: k2 m
! R+ o- ^- {. Z& f
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|