|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?1 k U# t5 \8 X- q
2 g7 z! n6 [6 ~/ ~: N4 o& l如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
0 l2 G- l6 p! S. u
1 E2 h7 Y4 E. G. L5 D2 e0 D9 P以下是创建自定义插件的步骤:5 C% O3 b$ h) b
& _8 [$ t, e3 ]5 a1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
( D3 u6 L; G$ i* ?, B5 h3 N
* ~0 n/ p. ^ r% n0 M2 e ```7 o# `% s: ?+ x3 G+ o8 ]
<?php) y0 a/ ^& r# n6 {
/*% d' S* y! W. o
Plugin Name: Site Wide Notices Plugin% O H+ c5 j% Y) h" V+ p% a8 w+ Z6 F
Description: Adds a new custom post type for site-wide notices.9 _1 l4 O5 R& q1 j; h
Version: 1.0, e+ T* E5 k9 r+ j: }7 G; c
Author: Your Name
+ y! [/ n* I3 k% m Author URI: http://example.com
/ V! j, B# n) j' e, g+ ?# i7 Z$ ^ */0 a, {) X6 e7 R& X5 Z' l& I' ~
9 Q9 ?8 y3 v9 C Z. X2 @
// Add plugin code here...
`+ Z" E/ e* R& ^0 W ```
% |4 H9 Q; Z2 V; L: r7 k7 d% h
8 i# y; w: ~- I$ Y$ J) Q" _# p 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。- z% r/ y; E% i2 A& v' a) m/ ]
* }- t- a9 ~4 r9 L- C2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
/ I% J8 B6 ~2 A8 \% X6 `+ n- m7 z, @- ~. P; \8 ]
```, T% w0 [' d. H! T/ S, \' s3 [6 g
add_action('init', 'create_custom_post_type');
% p$ S8 k+ m6 H function create_custom_post_type() {! `6 ?7 P3 Z |; Y5 Y
$labels = array(
7 @7 G# L; ~) \. i1 F' q: J* R 'name' => 'Site Wide Notices',
/ [8 u1 W% K( |' f" k4 [1 T 'singular_name' => 'Site Wide Notice',9 ^5 {1 h/ z' J8 A4 i7 X: b
'add_new' => 'Add New',
5 c6 ]) H' P' e' I1 l 'add_new_item' => 'Add New Site Wide Notice',
6 s9 F6 P9 R- P9 H; |4 ] 'edit_item' => 'Edit Site Wide Notice',3 p$ K3 q1 s8 f: J+ p# {
'new_item' => 'New Site Wide Notice',
* I1 J1 j) Z5 x8 J( Y 'view_item' => 'View Site Wide Notice',( U, T5 ~: I s6 [0 l A9 m8 s
'search_items' => 'Search Site Wide Notices',
( @ h1 K; t1 b& L/ l9 f/ S 'not_found' => 'No site-wide notices found',
) f; r. J9 R, ^% e 'not_found_in_trash' => 'No site-wide notices found in trash'
1 i7 ]+ G0 o* T6 z) ?& u );0 \8 E& k9 N+ w4 G2 i( J1 }, q: r
/ T- I: _, \/ P- ^ $args = array(
8 I) q ]6 t- [ 'labels' => $labels,
: l Z1 N. S9 F& S9 e# q; x! @ 'public' => true,5 a! o' j8 ]$ G! x: H
'has_archive' => true,+ o7 H# U; @, g* f
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
( U" e/ N& d0 V7 [# O2 a1 \ 'taxonomies' => array('category', 'post_tag'),
) \2 S5 ]; B, i8 P0 P& K5 [$ c" \ 'menu_icon' => 'dashicons-megaphone',
: C- c4 P( N ^+ L( x; N 'menu_position' => 5,0 Y+ h5 k; m ^" c7 [: C7 L Y
'rewrite' => array('slug' => 'site-wide-notices')9 o+ ^6 f" H I4 @1 l' O' S
);
& `: Y4 a# y3 D" |* t& f2 s4 E3 Y9 ]6 N) x( ~
register_post_type('site-wide-notices', $args);
6 i! y* {! W6 B0 q1 s' E! J( x }
6 u8 f( y, [( T+ n ```9 [) L- I# v+ {+ y' ~
! Z( v6 Z7 t0 ]. E; i* z- x& }
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
8 H* f# I! U8 g& g2 i# V+ d' j- I
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:+ W. U% Q2 @ f
3 R9 J/ O7 y3 {+ ~/ L( b
```
" e& Y' H7 i. n' {0 B add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
0 O6 I* P$ `; j0 C% M" b) F function add_site_wide_notices_boxes() {5 s, h% r2 `( C1 y
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
; a9 J( m5 c- z' Y8 I# F4 D }. o, v8 l6 I' u: i
7 v4 Q: x7 d! f: p4 e* d2 U# G function notice_details_meta_box($post) {
( N- y1 I: L0 F+ ?0 i wp_nonce_field(basename(__FILE__), 'notices_nonce');8 H/ u) A4 k1 u) ^
$notice_title = get_post_meta($post->ID, 'notice_title', true);, u* z- b" w+ c; g
$notice_content = get_post_meta($post->ID, 'notice_content', true);
& Z" J# U/ p! Z A5 {- `% U+ Q- x ?>
/ T% F, P4 i, \9 X4 y$ Q3 f7 ` <p>
( j! ?8 l1 B2 k( r6 X <label for="notice-title">Notice Title</label><br>7 }0 d, G ~) V ]
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
$ v `: l4 q' l8 {# e! s </p>
, A+ V0 p' \5 l2 a a+ H <p>
0 w G8 u& |& C3 } <label for="notice-content">Notice Content</label><br>
, W$ t9 C, I4 x+ b% k+ I <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
U6 { X$ Y8 l </p>) H- u$ J: y2 j5 B
<?php4 ]5 v# J& H6 G2 H8 F
}
7 W) Z9 X; h. n B/ B0 ?4 z( r# R4 U9 w& ]3 t! L9 z
add_action('save_post', 'save_site_wide_notice_meta_box');
9 ~% h) h) x$ P& _0 {) K( \ function save_site_wide_notice_meta_box($post_id) {: O. ~) _& B9 b6 J. O+ G4 D3 Y
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
8 v& K2 k7 N" O/ A5 h- k( l" K return; A' o4 p2 O8 d" O
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
9 D2 y/ ^0 f* C6 q, r: t, V/ A4 @ return;
5 y$ B2 p. u! e! t! N4 u. w: ~1 a, f/ p' W( z' p" t4 n
if (isset($_POST['notice_title'])) {9 e0 C$ q6 I% I
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
7 O" R) f$ H! \# K }
; [0 w/ R6 ? r if (isset($_POST['notice_content'])) {
! W: l, @; O% A j6 J" O update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));- J: c2 w, u9 [) J3 U6 M4 j
}
3 l$ M8 W* Z) B/ m }
" X! E, J; S& q- t3 B; Q& { ```1 ~6 t- M' ?* i1 F1 i8 K
?5 b$ G k, a8 z2 ~: I4 b ? 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
- A! A# R- g' @( @& G/ u: C1 H
: A, t3 H4 V Z) \" \. ?4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
; }% y. R, w$ I+ ?7 L3 o1 E( M( u" X# Z+ T" r2 V
```8 j7 e. S# A5 m# Y0 u$ p6 |, [
$args = array(
; t2 y& Y- E8 S# W I 'post_type' => 'site-wide-notices',% g: C( A) x- ]2 \6 h0 @/ H
'posts_per_page' => 3,/ h9 f# a0 C: W* } K3 Z1 E8 d6 O# ?
'order' => 'DESC',1 T X0 ?" Y6 i8 U8 N# Y
'orderby' => 'date', [3 X+ w9 U! U# { R3 j: [" b; y
);
& M1 y/ H& Y! j $query = new WP_Query($args);* b5 F; P3 t x2 e' q8 [
if ($query->have_posts()) :$ L5 C7 v% M; r& n7 Z* d( _7 c
while ($query->have_posts()) : $query->the_post(); ?>
" A* j( p# X8 u$ h <div class="notice">
" L6 w; b5 H- d4 S9 \9 ^( J, v <h3><?php the_title(); ?></h3>& i6 S& s* }1 t" @. _* z
<div class="notice-content"><?php the_content(); ?></div>
0 c% n" O: z8 ^- }7 L& I% G </div>
" M1 q8 M, d, F+ F0 i( D1 e <?php endwhile;
0 P/ e. [1 @* i i( M wp_reset_postdata();2 Y. R- o6 X6 }, {: Q2 c0 B X: O; \' \
endif;5 D% @% _8 M* l, D
```2 I1 C3 r) _' E& w
1 M) [1 _0 r. w. q
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|