|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗? J8 P$ E6 d& b2 R
2 G# x% H" z9 f6 C如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。7 b, M+ \2 Z( q k3 n
" ^1 ~* C# i9 {' ~) k以下是创建自定义插件的步骤:
7 R" K( k* x9 B3 V9 J. q G" o1 p7 C
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:( @1 b) k% ?$ c7 M$ [5 P" ?( h
* s [* V; j" X$ ~, Y
```5 i' l- n8 ^2 S
<?php
( U2 P! V% ~/ ~' v /*
! F: z; C; m) q5 M+ t2 T' h Plugin Name: Site Wide Notices Plugin! h$ G$ @9 D, _' j, t
Description: Adds a new custom post type for site-wide notices.( E9 [: U7 _. @8 c
Version: 1.0: ?. S$ l0 `# u2 _6 l7 \+ C7 E
Author: Your Name
* F; w/ n' x/ _6 j, {: y- i Author URI: http://example.com& Y! X: G) @/ v- r- j. I
*/
; j$ w; k3 c! A$ ^+ U1 O! j
' N7 |7 v9 z. y( ] // Add plugin code here...
9 }4 m) V6 t# R* R1 {' H ```. y% y- I9 M( Q# t! l, ?3 E
; k- t0 z$ w0 g+ ?) ?, F2 O' I 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。4 D( S7 j4 k' b9 m
" r! C8 s- K$ Z1 ?* u" n. v/ L" o2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
2 {: V, a8 I _0 V4 r. ~+ m7 _9 ]1 z& Z1 w0 S
```
$ s. _ v& M0 h# ~% X add_action('init', 'create_custom_post_type');+ i2 H4 l. G, I1 o5 Q. o- v
function create_custom_post_type() {8 B! U, R' F/ G
$labels = array($ E" a& z7 X, u
'name' => 'Site Wide Notices',
: O" T+ r* p# k1 ? 'singular_name' => 'Site Wide Notice',
E# Z. R& @: `) m9 @4 [ 'add_new' => 'Add New',
8 i2 t$ a: l7 ^$ z% g: G 'add_new_item' => 'Add New Site Wide Notice',# _3 T3 g+ }9 W8 j/ H! z
'edit_item' => 'Edit Site Wide Notice',
* h3 m8 S- D7 v8 W7 m* G- m 'new_item' => 'New Site Wide Notice',3 X8 S+ L f1 A8 g, R7 t
'view_item' => 'View Site Wide Notice',
8 E% E/ @8 t% g! d% C 'search_items' => 'Search Site Wide Notices',
, u% ]: g0 `# M9 J& D 'not_found' => 'No site-wide notices found',+ \* h" Y6 _# s: N
'not_found_in_trash' => 'No site-wide notices found in trash'
' ]4 O2 C/ q7 a/ I: S: G7 W );
# u. L; h8 v( Z& B* o; b7 A, i2 |9 z* _ T a
$args = array(
$ Q* B6 E: W+ p 'labels' => $labels,& B6 ?) M: |8 R$ ^4 k
'public' => true,. N! R& _7 Y7 Z- q7 e$ v8 b0 J4 s
'has_archive' => true,
: I6 s: h+ T- ?: c 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),; Z- F) @4 N6 l( z" b( Y
'taxonomies' => array('category', 'post_tag'),3 T/ f( O- ~9 g" [) \
'menu_icon' => 'dashicons-megaphone',8 D6 W) M. o. W, f. [
'menu_position' => 5,
8 b; o. `" W& L: G% {7 Q8 @3 ~8 Z+ h 'rewrite' => array('slug' => 'site-wide-notices')
0 x) y! ?, F0 f" ?( T1 V );; q8 X8 T" Q/ U) K6 ~: l
+ S8 B' B0 M* n( Z- r3 h" q& ]
register_post_type('site-wide-notices', $args);, O$ Q& a4 f( j
}
6 [) Z, X# k+ ~8 r5 Y, U ```$ k5 W& ~$ E* ?/ R9 s
9 Z3 j) h$ @: }0 e4 [# x- T/ U- ]; ^ 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。, H! @' ]0 f G" e
3 d) o* o7 k9 p: q8 n: C. X, y3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
$ I& o: \3 F5 z2 |. i' x( i# {
7 l% O% C7 y9 `2 K5 v ```# s" P. W( s3 _: f6 O9 l
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');6 k; F, |, N- q
function add_site_wide_notices_boxes() {0 @ q" b3 p* @5 C# P% |5 L! i
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');* v/ T* x4 \/ f! q
}. L6 E( v, s, B" y3 y6 ?
2 _! L0 ^, k9 i* @, o& M. b* f
function notice_details_meta_box($post) {' a q# n: H& b/ q# r `0 t
wp_nonce_field(basename(__FILE__), 'notices_nonce');
2 }6 m7 j4 x$ [9 J2 N; S $notice_title = get_post_meta($post->ID, 'notice_title', true);
- X/ q& x, ?) H) I( V" B $notice_content = get_post_meta($post->ID, 'notice_content', true);# k" g/ u6 N0 d6 a! L
?>
8 n1 t' ?" J5 u# e" Z4 F7 e, g <p>
* ~- s$ v0 B! O8 r* ^& u# Y- t" m <label for="notice-title">Notice Title</label><br>4 u! |& _) }- D
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
+ }4 r$ o$ |8 g </p>( I$ S7 Y' t F3 V/ O, x
<p>
" }2 h3 z6 F% U <label for="notice-content">Notice Content</label><br>- v6 c! Z! z1 F$ @/ i
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
8 f+ C" R8 M( X, W3 Q </p>1 W# j3 b4 n) ~& K
<?php
8 h) _$ R: q9 o! S, o. m }
7 [" h: w! n2 M1 @" o& k% n9 d4 Y/ }8 l$ R
add_action('save_post', 'save_site_wide_notice_meta_box');
8 C) j6 w5 `( r$ M* G2 D L1 b function save_site_wide_notice_meta_box($post_id) {* I V0 h& H! k# d; S5 Y
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))1 ?. w% ~7 \0 B8 N; Q
return;+ o# R0 a R+ w
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)& H; L; a$ C" `' N4 k7 ~
return;7 b9 f: v8 P, L! l W4 B3 `
1 q2 \( D4 k# O& }" N9 }! h0 V if (isset($_POST['notice_title'])) {
# k* c* q4 z; I0 _; s update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
* }& R$ R) P) C! H# x$ g$ r }$ a9 X5 u" w2 @- r8 m; q$ ~
if (isset($_POST['notice_content'])) {8 b. t8 K0 v% P" X# r8 O# R
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
1 S% @" F/ Q: n6 _2 Y( Q }
6 t- n; Y3 E( w" X: p }& w9 I3 E* ^9 {$ I/ e
```
" y# ~* Q6 R# {/ v! j0 s0 W8 L. G5 y" {+ J5 I
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
& R3 p# H/ V k
$ B' d; `& T" ]4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:# ~0 ^$ S& F# A1 C
- H% p4 D5 u$ m, x0 o
```
: u, }: D; L1 b5 }" u# P A $args = array(
1 q* W; O1 y& V) [* ]- b$ T# Q 'post_type' => 'site-wide-notices',5 U2 w2 H" g; B3 b5 g
'posts_per_page' => 3,
7 ]) S8 |4 a* F) Z- M2 r' I 'order' => 'DESC',( F! B8 }* m1 C/ U+ Y D3 ?& R
'orderby' => 'date'
+ v% ^0 G/ q2 [ );- O. u/ l0 B+ N7 ?. _
$query = new WP_Query($args);
. ^3 v: y9 k6 b0 n' ~* N if ($query->have_posts()) :+ H1 A. H/ c6 f @' Q7 K5 }
while ($query->have_posts()) : $query->the_post(); ?>8 l( g- m% t1 r) A5 [6 Q
<div class="notice">
6 ^2 e; Y. R/ b' u8 E <h3><?php the_title(); ?></h3>4 [6 M+ b3 C' C
<div class="notice-content"><?php the_content(); ?></div>
5 J& I( y8 L/ E* f' c+ ]3 J" C </div>; v) n$ D6 h* O6 A/ X! v) s
<?php endwhile;
5 n7 W& I B* N+ O: R& v" W4 @3 { wp_reset_postdata();
; V% E& m' u, Y: L; F! X endif;
% X* d: m( {8 e3 L ```
1 S4 y9 V' K7 \/ l: p: J# W* L* H7 |' P7 h
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|