|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
6 C$ t, S4 ]+ l1 r% p1 Z! k$ |/ D2 M: T9 T
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。; o" n: Y- z! w7 r
% e# e) ~9 f, E1 f" y以下是创建自定义插件的步骤:
6 c% u9 N: x1 t+ O2 D2 m# ]
- g# q. S0 J2 P' S( x7 P0 h1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:6 k* J% ?9 ?) E9 v/ |+ b0 s8 u7 a
; x' ?, X( W" J8 w# a1 w
```. x. e3 ?) o, G8 h" b
<?php% K* M, y- i4 {% {6 B2 L; u" g7 p: C9 ~
/*7 H k: h2 ~3 C, H9 t
Plugin Name: Site Wide Notices Plugin
) c( u, U/ z7 v- A( E9 \ Description: Adds a new custom post type for site-wide notices.
/ O3 b8 |9 _- Z4 t- } Version: 1.0* e; B0 f6 e3 A7 t, T
Author: Your Name' i' I: M1 n p T4 Z6 x
Author URI: http://example.com' ]- _6 ~0 M- m1 [% P n
*/2 c, j. J. k; v. L8 T7 C% O& |
! B# Q. Z( |2 b5 ? // Add plugin code here..." ~& y t& {' d6 K7 J2 \! y
```
$ B3 `2 F- U- B s3 b3 E* h- U7 S# ]+ e
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。# t$ J! S2 z& {1 y( s, [
0 ]$ ~8 H3 c7 z8 i9 }4 B; Q
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:0 k: V; z9 I9 Q
4 S$ O: }" {; r6 H
```
) k$ h( a# D8 d, a% M add_action('init', 'create_custom_post_type');
8 c7 H& i( Q& A6 s function create_custom_post_type() {( V5 t F+ W2 E# F; k
$labels = array(
4 _! Z! d, Y9 V; a& w. t, j0 F7 e 'name' => 'Site Wide Notices',& f9 z* s% ]9 u3 C/ L( @
'singular_name' => 'Site Wide Notice',
* [" u) t+ @! P+ e- \7 f 'add_new' => 'Add New',% w. B& H4 z, x7 X4 u' `* C
'add_new_item' => 'Add New Site Wide Notice',, X& z7 P* O3 B3 n( H
'edit_item' => 'Edit Site Wide Notice',
" O C! F& A' @4 o 'new_item' => 'New Site Wide Notice',
; K9 [4 E3 V4 y% C4 _ 'view_item' => 'View Site Wide Notice',1 b( X9 r8 P# t; _( e) @
'search_items' => 'Search Site Wide Notices',: v8 g0 g, b$ j1 Q
'not_found' => 'No site-wide notices found',
% G# S) h7 {2 n 'not_found_in_trash' => 'No site-wide notices found in trash'4 R+ ?7 N9 C7 C8 ?3 y5 Q
);
7 o6 t6 f' n) W9 j+ C; q. b" V
! ?( w6 O: W1 x$ p' L1 ]) U! R, N $args = array(( N. m! o* t; v4 V# Q
'labels' => $labels,6 c/ j6 n) Z. }4 u0 [) w. a
'public' => true,5 Y9 q* m) }8 B7 w) ?
'has_archive' => true," T! t! o9 p/ n0 ^; S0 X, c
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'), ]0 Y2 z: P& S1 ]* q( T; m
'taxonomies' => array('category', 'post_tag'),, \+ F9 D0 z0 M1 p. y. O
'menu_icon' => 'dashicons-megaphone',
: s0 c7 r/ ~/ | 'menu_position' => 5, a0 j0 G8 D3 h0 O* S6 w B8 Q
'rewrite' => array('slug' => 'site-wide-notices')( B* V" n/ s6 Q6 }7 g7 R
);8 D: l8 _: o: m, K
0 H7 p& @1 f/ d! ` register_post_type('site-wide-notices', $args);
1 ^% S- u- s& q$ |# E( ^ }4 b- b3 {% H/ P1 j# m8 O1 ?1 z
```
" _+ ?& U0 p& s5 w2 L8 Q" _* ]# w' i% F- H+ Q; V& m- J5 Y
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
. d9 Z, X! r' _% L `2 M
( b+ a4 ?0 N% S3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
- f! f' ]4 N7 ^9 k3 B$ o% X$ K. A; f+ h- e% t6 k
```
9 W6 j) g8 I/ W% i6 i" b( { add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
& E. \, \4 }+ C; M function add_site_wide_notices_boxes() {
6 a- W+ u) _0 Z; z. T( U* O) r add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');7 O/ T/ R$ u4 S
}" @! i2 m N+ n- s0 N2 P/ @! I
: {5 r8 B" _2 O- Q function notice_details_meta_box($post) {, D7 ]3 ?' L8 I4 s6 a( b( ~
wp_nonce_field(basename(__FILE__), 'notices_nonce');
8 C, U; T h2 `( t $notice_title = get_post_meta($post->ID, 'notice_title', true);
4 S% l4 p, I& \0 j $notice_content = get_post_meta($post->ID, 'notice_content', true);
% }9 \" `( R: C! r p4 E7 m ?>
3 u! S% f, u, p+ b <p>
6 F, D+ p& a* u% A) l <label for="notice-title">Notice Title</label><br>) k$ A6 b: }! c; \+ d% B+ |; r
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>"> F6 L$ j" u% o- w6 k& T' l
</p>
C1 q1 [6 s, t, o0 m# U <p>$ d% d& a0 A8 m! v# C3 R8 j% F) B
<label for="notice-content">Notice Content</label><br>
% I' s8 E' c+ q @5 _* T <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>$ W2 T/ H0 ^2 j+ H* h; W
</p>
$ K% G% \! c7 C7 _: Y L8 | <?php
; ?9 y! [) W' b9 T }
, l5 k8 a/ M w0 s7 v, z6 `
) M5 W1 U9 w$ ]6 @& w add_action('save_post', 'save_site_wide_notice_meta_box');: [* y; O3 s' b) h* e& m0 F
function save_site_wide_notice_meta_box($post_id) {9 A' k( g" ^6 N! |
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))* i8 P5 w/ ?* k; `4 l; J& W( ~/ h
return;" d7 g! j5 t" U+ l1 o- I" ^" @
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)4 g) J* S a% r0 A
return;2 M. A9 G) [! n# O! [) F
$ I5 |0 I' k* J( c4 C; F
if (isset($_POST['notice_title'])) {0 j& {( H0 I7 o& A: J9 U
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
- |! b4 g& z. a, D' ]" y; H) ? }
* ^) ^( m1 D. E if (isset($_POST['notice_content'])) {3 r) D' |9 y( B6 |
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
6 B- O& Y- T$ S- H$ ]7 k }8 U6 P* B+ H. U0 T, t) p$ M
}% j3 O9 L$ F5 {; H+ N! {6 a0 f
```
$ t7 \& u6 ^) d7 D% B; {5 M" Q8 |0 Z3 }
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
) z& C* R& U. C$ G- c% M( A/ R: P; M% ? L+ A! ^0 d' Y
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
) `& s, v0 w* |5 U, e; I) M0 x' e. k; A( k; q3 q
```4 F: ]7 N% |$ s+ J. |& G: a
$args = array(: Y# @4 K- C' P- ]- w: L0 v
'post_type' => 'site-wide-notices',; l. B& U0 m% Q* Q( g8 Q
'posts_per_page' => 3,' ]& I+ C- o$ E
'order' => 'DESC',: I- r9 x. ?" A& y6 v1 O' n
'orderby' => 'date'" s) J- o9 u9 f3 J( w& ~7 V8 X
);
' O4 @; [4 K: q $query = new WP_Query($args);
! P7 Z$ x" x& y7 U! n4 U if ($query->have_posts()) :
5 x6 y' m% `, o6 z G while ($query->have_posts()) : $query->the_post(); ?>
9 q' W4 B2 C! m <div class="notice">
9 s! s7 {/ D: i <h3><?php the_title(); ?></h3>
* a A5 S6 g2 ~3 i0 F" { <div class="notice-content"><?php the_content(); ?></div>
9 K' q6 X* A# l7 A </div>8 h# s+ b* l, |" X' Q) b$ x! r
<?php endwhile;
8 C1 I' I6 k1 z( G& S wp_reset_postdata();
: \. V6 ^9 h i3 f5 z. l endif;
, D0 s5 [3 c+ u9 B; s) |9 Q ```3 T4 p% P. r% f/ |5 x6 ]
1 t2 r0 B9 N6 [
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|