|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?4 h6 O- d! S5 B! i5 f! _
! J9 \* u3 d4 n) E2 Q C9 i
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
+ d. H& U* V0 A6 F& f1 U; B7 E* w/ l$ v5 S
以下是创建自定义插件的步骤:
+ O! ~/ R: n. t6 v" K. b( V& a
9 T* _: k/ ]. ~0 W& @7 ^/ s% v1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
& k) ^! [1 m$ j$ u4 M( f9 y5 x% i2 A N" w, U
```
% s0 a4 ?! y7 ^; Z$ l7 g( L <?php
. t" ]* r. r# k, ^; }# ]7 ?% P, O /*
5 z- t5 \; }( A+ N Plugin Name: Site Wide Notices Plugin
3 d/ d9 J% t5 g/ u# t, C k& ]% v Description: Adds a new custom post type for site-wide notices.( h" {( |( b! C: m% `# l* j
Version: 1.0
, Y, ]( J# j' @+ t- J4 r2 x9 W Author: Your Name
5 N0 \3 n" Y. ~. N- q Author URI: http://example.com
0 k% a# ?# d5 ^- S3 @ */
/ K9 u M9 _- y0 W
( T$ N6 o# m5 \" B( u // Add plugin code here...6 I; n$ t- Q8 G( N. ]4 C) N
```
' @' ]5 W' D; K* U! U) \% u* ~5 A6 e- s) s! c
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
, W; M) l, r5 I8 }6 r" y1 W% `8 G' k9 v. N# X6 \! ^
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
$ W: s: d& L1 s1 B0 e
( Q" y. N \/ j ```
& o& o6 d* ]: _' ?2 R add_action('init', 'create_custom_post_type');5 d8 x% V9 v5 ?! q. g) c0 y- R( r
function create_custom_post_type() {7 j5 H. B: ]1 s8 z, H
$labels = array(% W6 v* j2 w S; [6 R
'name' => 'Site Wide Notices',
6 R9 t* x/ B8 Z4 Q. S6 D' D 'singular_name' => 'Site Wide Notice', b y$ g8 Z! K. ]; R) r
'add_new' => 'Add New',9 h, e3 _: D/ `4 Q) ^5 g: x# T. G2 j& U, y
'add_new_item' => 'Add New Site Wide Notice',
% }9 v- E9 D+ ]2 B3 u 'edit_item' => 'Edit Site Wide Notice', x: h, G) ~+ q( {& y; u
'new_item' => 'New Site Wide Notice',$ R' l# C/ T S$ z6 y2 s7 T
'view_item' => 'View Site Wide Notice',
) u' ~" I, M1 `( Y. ^ 'search_items' => 'Search Site Wide Notices',
8 B E( ?) v0 }! G Z* O 'not_found' => 'No site-wide notices found',
) s$ w) e- B# J. d- z4 D7 \0 ?. X6 K 'not_found_in_trash' => 'No site-wide notices found in trash'
8 Z/ h! }5 e- q/ _+ o );
" q" j+ ]# q# \$ d) C7 v, w5 k, C, R% y8 C0 N/ j* R4 k# \
$args = array(
* q& z4 u/ |4 @) d4 P) K- w" N5 P) D 'labels' => $labels,
) V% e* Z$ j) O8 Y/ a2 U 'public' => true,' }0 N' [9 Z/ U( }0 ]0 u' ~
'has_archive' => true,7 {' U; t* S7 c1 e. [+ k/ l* Y- @7 E
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
& _9 N- k& R. ~* b1 T7 e 'taxonomies' => array('category', 'post_tag'),, O; H6 L0 w2 b- K
'menu_icon' => 'dashicons-megaphone',* C8 M. ^. a, a
'menu_position' => 5,
& r- V- \8 d) {3 V9 g 'rewrite' => array('slug' => 'site-wide-notices')! `7 z3 M, M5 E6 v5 j* g
);3 G2 a" u& p* y/ M3 m
2 V/ w! X" ^% w2 a( o. R/ P& | register_post_type('site-wide-notices', $args);2 U4 Z5 E; k. I! @* h
}1 V9 g6 E% M# {. @* s
```
: T6 O, I8 Y$ z9 W, b0 C6 Q; z! E! z7 Q9 G# A
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。! B% v8 e2 g, J- P3 B! S
; T5 d8 c. y3 `! R! U+ B; U! ^3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:& p N1 x* J4 [3 \ _# x: b
3 W. k; W# H2 m1 i% H+ ~) @) \ ```
. I( w2 C7 h3 T" v' f add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
' |: k* P/ Z' j; Z- x function add_site_wide_notices_boxes() {
+ }% t6 o& K. z8 t, N* [ add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
9 T( Y& y+ o' C% M( S }# D& ?" k3 l1 F2 u$ ]
% f* f0 H5 j/ V0 N/ {$ ?
function notice_details_meta_box($post) {* Q R$ c! s! B8 Q1 ^& O' {
wp_nonce_field(basename(__FILE__), 'notices_nonce');. u, y; u& E6 _" }8 Y1 u
$notice_title = get_post_meta($post->ID, 'notice_title', true);
* N t; K( K' H( `! [ $notice_content = get_post_meta($post->ID, 'notice_content', true);8 a& t1 O7 l5 E |* C" \
?>
1 q* i1 Y/ U6 a, \ <p>
+ D1 ]9 d) w, L <label for="notice-title">Notice Title</label><br>
3 m9 ?7 }+ K8 e) B3 c/ C4 f <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
9 d- G9 ]# Q9 ^* R; X2 u0 P </p># `: s# y% B9 P0 L
<p>
3 p3 a; J4 Q( B3 v <label for="notice-content">Notice Content</label><br>, Z0 }, G! b. H9 n3 ~$ X
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
3 z' B' a# Y9 o( Q </p>
' I# j# M2 Z& d; b* V, @ <?php$ r' E! ?0 c7 Y# f4 {7 W/ Y
}. r8 ?8 Y+ I( @$ o9 `
4 M( t, w) `+ ]% q add_action('save_post', 'save_site_wide_notice_meta_box');+ }4 f( J( C6 L8 S% C( f
function save_site_wide_notice_meta_box($post_id) {! h( @0 s) H5 @4 g7 o' r4 N
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))$ ]( S7 [9 n# \2 \' _' l0 _4 K
return;! p+ f9 r- b* S
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)6 F; ?0 i" A R5 b0 Y# K" I
return;
+ B; K q! F8 `0 X" n) N& u8 T" z* {9 T7 \
if (isset($_POST['notice_title'])) {
" n! \ J; W8 ]; w# @ update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));' \- Y" r+ U+ b3 _' [$ B
}
2 E A) Y5 u1 U; t1 V }, U if (isset($_POST['notice_content'])) {
& J; _1 \8 _( w update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
5 |0 p8 R ~2 }- a% V }
4 {7 q, H- B. J) j: e3 g$ Z }
% P( V3 M& \- p& `8 I& g ```
+ F; t! G6 Z; F K
/ y( z) v. N, ] 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
5 z$ E5 g9 z& b, `) }6 Q8 H5 C
A0 A# F7 q% v0 q4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:8 B- D+ a+ Q* O2 k& X
: W' z+ g! r# z# }- V! K4 N' {
```
* b2 P4 \9 U6 |" p $args = array(4 }. J7 l2 A* U
'post_type' => 'site-wide-notices',
$ @. I2 q7 C$ C O, X# T* [ 'posts_per_page' => 3,2 t/ ~% Y' N1 S; m; H) G
'order' => 'DESC',: S2 t/ F x: l4 a I) ?
'orderby' => 'date'
9 I, G. D% U, i3 u3 ^4 }' P );: p/ e# f1 L& I2 V, b* b9 @3 r
$query = new WP_Query($args);5 t% [' j9 w' q7 v' u3 ?! J
if ($query->have_posts()) :
3 K! r/ W2 b8 v- \# W9 A5 Z while ($query->have_posts()) : $query->the_post(); ?>
& C8 a r4 Y7 J! F <div class="notice">
% `2 b% S# I/ V+ r+ |% O# ?( f+ l7 c <h3><?php the_title(); ?></h3>
/ t6 S2 T4 }+ y: _7 ~; o. L! B <div class="notice-content"><?php the_content(); ?></div>
- z- i( `5 g/ @) \ a </div>
6 H r7 c6 k9 q* |$ C7 m <?php endwhile;
8 x6 B( h8 G: d; ]7 O0 d( D wp_reset_postdata();: d! H v& n- ^" N. O& R9 B
endif;* J, I8 \% V6 B3 G: ]0 e8 W
```: m: n% P' h+ p7 X/ ~5 k8 X, f
# ^9 l4 \* W) @3 D
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|