|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?6 }/ k5 ?1 _+ _- Y
$ e. G' M, V' H$ z$ H- U如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
! N, P- I: M: F' J( F4 J& y0 r: U% o
以下是创建自定义插件的步骤:
' [, t& l9 f: D7 [8 H9 h) A( G
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:! j8 k& e; O$ o5 Y7 o& D7 V
, @* e/ S; _' H) c6 Z. v( s- q: W) A
```
5 Y/ s* h" r3 e) F <?php
* T6 p$ v N, H: J /*5 V: [* V4 C! ?+ K
Plugin Name: Site Wide Notices Plugin
1 M& H9 r# N# u; f4 T( E0 D/ C Description: Adds a new custom post type for site-wide notices.
( \. o5 e7 z% [ Version: 1.0; A% V2 ]' [" K8 m. h
Author: Your Name8 c/ [; U8 G) Q- D
Author URI: http://example.com
0 x$ g+ j, R" d& S( o */
1 |* w1 z# l2 R3 X/ p7 t" t) E* |$ @! U- X
// Add plugin code here...9 I2 r( Z/ S! [* f0 S! T, C2 c
```
: ]/ @9 e0 U# Y# l" D& i1 y
! }6 _% ^, B' a- \8 @ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。" K1 K$ T8 a0 K3 P( }
) I3 f I3 w+ D% v- A6 Q) B- ?0 i
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
, n+ ?$ I# `& h+ H! _
. n) ~; E) Y% k8 x+ M1 T2 j ```! a8 a3 m3 q; T2 A, |9 S2 n" w
add_action('init', 'create_custom_post_type');
; c9 `% p" f! L1 G! M1 Y function create_custom_post_type() {3 P0 _0 S: d, q
$labels = array(
( e4 F/ e% n: v+ Q 'name' => 'Site Wide Notices',
. H7 e) v8 \+ A5 w( C 'singular_name' => 'Site Wide Notice',/ u) d$ l; F- a5 Z3 |
'add_new' => 'Add New',9 W; z! f9 ^$ g) Q
'add_new_item' => 'Add New Site Wide Notice',
/ G& h( z% F! J" e B) P7 A( J, r 'edit_item' => 'Edit Site Wide Notice',
* y1 R! y" a+ M) r6 t0 s+ V2 T8 C 'new_item' => 'New Site Wide Notice',
" |+ x- R- l2 x. [ 'view_item' => 'View Site Wide Notice',: ~: E+ [/ V% K3 [
'search_items' => 'Search Site Wide Notices',3 n' t1 S! U3 p6 C# [' Y8 I
'not_found' => 'No site-wide notices found',0 d; `, |/ m& J5 N7 f- P
'not_found_in_trash' => 'No site-wide notices found in trash'9 H8 A/ l; A* ?+ J7 C
);
" ~8 |% Q9 X' r" p" m, c5 k7 b( N! z4 h/ A5 \: a
$args = array(4 }: b! s/ X# B1 Q/ Q% C
'labels' => $labels,/ \/ [% m# F7 {
'public' => true,
4 \" B8 |7 w) Y; B r; M c 'has_archive' => true,9 {. h& P1 R/ t/ Q: d2 ^3 b4 x( g
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
# o$ q' s9 C0 H9 @" ?; \, Q: m 'taxonomies' => array('category', 'post_tag'),
' L; b! G) n; F1 |: P8 s, |. y% \* e4 s 'menu_icon' => 'dashicons-megaphone',
- S* Y9 g9 c* h 'menu_position' => 5,
, M# [3 `" ?$ O0 i5 K 'rewrite' => array('slug' => 'site-wide-notices')* Z7 x2 s" m. d' c& v
);
! G! o& N. l; \# p( Q: ~1 B0 \0 i3 R
register_post_type('site-wide-notices', $args);
0 f8 Z+ M: H) o2 P6 B: S& B' x/ J }
7 h9 {4 V5 Z/ ?& t" s, r/ D ```
' {$ x% u' T8 b# s& B& t7 [# P6 Y1 o* z! ] u# `
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。 {6 P9 L4 p6 ]1 \
# ^9 }! `, J/ n8 s: ^
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
$ p* ] B4 P% w( L5 `9 s5 u1 c6 G/ o8 i% N
```
$ ~4 f: c c: k7 P( p7 Y add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
3 E$ W9 U9 ^. G2 Q: h+ }4 u- i function add_site_wide_notices_boxes() {5 R' N0 ~( ]- E7 y
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
0 i( Q) [- N, { }
5 S' ]7 G7 p; u0 v U: R
' U: \. o: t- t; L function notice_details_meta_box($post) {2 p0 B7 f* A! c; G* w
wp_nonce_field(basename(__FILE__), 'notices_nonce');
& H; v1 @ |2 `5 ^/ D1 v+ \ $notice_title = get_post_meta($post->ID, 'notice_title', true);# u+ U$ e8 T8 y2 T; X& u3 j0 a9 E
$notice_content = get_post_meta($post->ID, 'notice_content', true);
' j8 L* y- v$ S: ?5 C9 Z ?>8 Z, C# E* G& y/ d/ E; ~
<p>) l: m; I- R& Q9 x& G" X' D* a+ J! w
<label for="notice-title">Notice Title</label><br>
$ `: S$ o6 C( m, V <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
" Z; R j" W% L4 I w </p>- v. s- i3 Z9 m* h6 _* s8 p
<p>4 }0 W6 [7 N! c+ }
<label for="notice-content">Notice Content</label><br>1 v! W6 w/ r( p, }) ] V5 S2 E
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>. T. d& B: h+ h2 r: i
</p>
" J) E2 O+ B0 K6 ~' ]) G <?php
8 x+ T$ g, I) q8 u Y. G }+ A& a1 f! U3 m
3 ^+ C+ e( F' L! U) t1 X w/ D add_action('save_post', 'save_site_wide_notice_meta_box'); H* G3 O" M, ^# J
function save_site_wide_notice_meta_box($post_id) {5 N* Z# Q2 s( Z' U4 K" Q
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
( u+ Y$ m* S# w% T9 d- x. {9 Z2 _ return;
: C: g& O7 [0 N5 c if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)- q% M* J) S0 a9 l- B
return;
6 [; Z$ }" t1 Q5 C! S6 L- T# a+ b$ W$ x4 _" K; \
if (isset($_POST['notice_title'])) {
6 x3 ^# t9 Y; N0 J7 E update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));8 m1 c5 u( X5 Z* \' l' U
}: r$ p" Z5 `; w3 ~& _5 f( G
if (isset($_POST['notice_content'])) {
, p* `% E! x$ I) N/ i! J update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
0 @% D8 q. r* ?% _- _ }
% |( J# L: G: {; t3 ?( c4 K/ u }( X( k( @4 m7 ^9 p, G
```
4 J! x' x, Z) b! \1 z) x: d
. X' X. K3 v$ v8 ?$ D 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
* n1 j4 J; h6 X0 N& O9 E% O
, x2 c) d2 f/ g3 V6 `# D4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:( O. v2 v& M/ c! v. P7 K1 I# {7 q# j
8 @! w" K: h8 b. ]
```
. `+ T2 f- ^- T: J2 |# m $args = array(2 q* @, C; N0 n) j( I# d" S
'post_type' => 'site-wide-notices',$ D( y$ U8 w" p0 V) r _
'posts_per_page' => 3,% p9 P4 N y/ S2 W. J, i" P
'order' => 'DESC',
1 \5 O$ W+ A3 _3 I" Z6 z 'orderby' => 'date'
2 [ n; c' F% p/ \ );7 k6 u% y+ g' u) \% e" h
$query = new WP_Query($args);
- k+ c; ?5 a/ b6 i! z if ($query->have_posts()) :
: W/ ]% J& [2 w) X while ($query->have_posts()) : $query->the_post(); ?>
$ Q/ R! {8 }1 T0 _ <div class="notice">
$ l' ~* n+ f+ K: c/ Y% a7 C: O1 } <h3><?php the_title(); ?></h3>
. K+ }$ [* `0 j6 p( Z2 W0 j <div class="notice-content"><?php the_content(); ?></div>
. Q# Q" ~' h, n* V2 y4 V! N </div>/ K0 k& E. `/ U" z; F
<?php endwhile;
' l# F( ~( I& } wp_reset_postdata();
( Q! N4 U3 w2 c' r, k endif;
4 O( c$ _' [% `. I ```. _! k0 b" o/ ?% W H0 E2 u9 f- |
/ g9 M) t, {; D' P3 z 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|