|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
, x i1 \+ c$ E/ G1 T
5 M4 P, n7 V1 ]7 g+ J7 S如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
1 w. A+ M9 B: j* M3 E8 W* d8 N% W* t" I! y4 Y2 K0 M
以下是创建自定义插件的步骤:. S4 j/ w( B6 T) c3 j w7 D
; M6 d2 {# j# |
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:1 v% ?" `" ~3 ?. i- m4 K
- K! X! b) G/ C5 A
```
& D5 S7 n' U5 Y8 }) k <?php
' g( _1 I4 M( i: i* i /*9 _( o" [+ f7 K1 s( m, V
Plugin Name: Site Wide Notices Plugin
! ]' e7 P; c" z% _ Description: Adds a new custom post type for site-wide notices.
0 s1 A9 C1 S) r% E! D, R! r; G9 {: z Version: 1.08 Q3 d, P9 [; Z2 n2 H: V2 M4 V
Author: Your Name/ [3 j8 g! ^6 N8 i
Author URI: http://example.com1 @, o8 r7 f, [
*/1 f7 S7 z7 y3 X9 a
, T8 a9 {: _7 u! h {* V
// Add plugin code here...0 G9 y/ n) Q. h8 O
```# ^, p; z1 S& A$ a& t: b0 \
; Q+ R- H! T, F7 q, L* q0 v
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
' Q1 e9 O0 m) N5 j& g/ l! I1 Q6 k- l* Y: h( ?( u
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
4 _1 G+ X! t1 [% f# ?+ Z; A
5 p" ~+ p) }/ q, s ```
( f( f( B! r* ]. o V" c7 `! [1 | add_action('init', 'create_custom_post_type');
1 t2 ]3 H+ Q5 K8 Q function create_custom_post_type() { U% X, N. K+ `6 J
$labels = array(
" R |3 I+ e3 H" X) T 'name' => 'Site Wide Notices',
. ?- ^4 ~5 x% Y3 _+ g 'singular_name' => 'Site Wide Notice',
. W6 U: R# {+ U+ ^8 ?; [ 'add_new' => 'Add New',
, d. Q; |) ~$ J9 \& r9 O 'add_new_item' => 'Add New Site Wide Notice',# ^! C# f, J1 Q8 e/ n' @/ c% w
'edit_item' => 'Edit Site Wide Notice',
! ~( t! D: x( |- x& h8 b 'new_item' => 'New Site Wide Notice',
/ q# I4 V. x% l) U% z, P+ E 'view_item' => 'View Site Wide Notice',% P o6 \. j$ C
'search_items' => 'Search Site Wide Notices',9 k: x& l) {! C! t
'not_found' => 'No site-wide notices found',+ E/ p/ ~ k" f
'not_found_in_trash' => 'No site-wide notices found in trash'4 s& [. I3 ? L! W8 c
);8 B# {6 u$ G, Q' x6 f2 }
6 i! D$ V }' t* N& v $args = array(
# n! K5 ?8 N% K T* ~" K 'labels' => $labels,
$ ?6 ^# D9 Z/ }- X 'public' => true,. L: z3 x0 ~; z4 A
'has_archive' => true,
8 A+ j* y2 }# C& F. O 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),3 P5 d2 e; a( O: B {& _
'taxonomies' => array('category', 'post_tag'),
; v+ y* ^( ~" `! R5 L 'menu_icon' => 'dashicons-megaphone',
+ L! l0 W% P7 _% R4 c" K 'menu_position' => 5,6 o! u* y8 n( ^9 }' u* b
'rewrite' => array('slug' => 'site-wide-notices')
5 g8 x& u ~+ Q2 ^$ T );4 k& ?# ?: n. \0 J" M6 H
9 b3 N7 Z+ d$ J0 x( H& J register_post_type('site-wide-notices', $args);
3 C3 S* L5 Y) G1 O9 s; Q( a* x }
3 y" k _- h0 R% i& N5 S2 l" ~ ```
$ q# `( m) r- P2 E- g$ L& I% y
( q4 U& x$ J( }+ i2 ~ 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。# u9 [) T! u/ k+ e
" j6 x/ `& w* P: ]9 x4 @5 }+ b3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:2 F7 ]/ r* i* h5 ~, H( @, `& f
; s8 k4 s* r% C ```
. p7 @: z# |+ d8 s$ { add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
% x; o5 o7 F! u6 a( ^ function add_site_wide_notices_boxes() {8 k# E7 `' o2 c/ p# T% }. S4 U3 ?
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
- K: Y) t3 o! p3 P0 Y- V6 e }
0 k7 [5 B3 c* a- G: [; ^7 T" w( U; T# D) z q8 B4 z
function notice_details_meta_box($post) {6 X$ [% C w1 X3 U {) Q
wp_nonce_field(basename(__FILE__), 'notices_nonce');2 D0 n* G5 L2 f- `/ L
$notice_title = get_post_meta($post->ID, 'notice_title', true);; t+ L" g K; M( Q) f- j
$notice_content = get_post_meta($post->ID, 'notice_content', true);
- ?. [; K3 R& m3 S1 G2 X9 r: X ?>
4 k: n, Y& z) X" w0 J" R <p>
* Y8 q$ k( L' P2 F9 H$ i <label for="notice-title">Notice Title</label><br>5 ^7 e( r- A$ ^9 ~0 f8 g# K
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
& K# }% T# \5 A6 w7 { </p>7 b, |6 D# A% T: v0 d
<p>. ^8 ]1 i5 Q% {8 [$ c% O
<label for="notice-content">Notice Content</label><br>
( S, ~- C3 q( y6 t" `0 D <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
: I% X& n9 N( @ </p>
" R; {' ?: P* t- F% n) v9 s! {! Z <?php
; G# t3 D9 K. [/ u { }
7 d$ f2 H/ N) j1 u1 L
% Z3 j& E. u! ?) y/ i6 S( {6 Y) ~. L add_action('save_post', 'save_site_wide_notice_meta_box');+ H3 t7 s# ] W* i# ^0 l; _7 w9 N
function save_site_wide_notice_meta_box($post_id) {& m! e$ @' m- D" M
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
1 Q7 r9 J! A3 M% W2 C& N return;
` k2 A# A" p: b. c7 t if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
) [1 R2 m9 z4 a% F return;" k& y( c" H, r/ U( `, ^5 |7 }
% P6 y5 G& p/ U I1 \4 ]1 \
if (isset($_POST['notice_title'])) {1 W( S9 M" l3 U3 D4 z/ n
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
8 u$ S8 m1 a* S" b I }' F# {% O% z/ Z; E
if (isset($_POST['notice_content'])) {) p' F) C* ?5 r2 i) e
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
6 Q& i5 L6 O) J }3 @5 A% U8 p8 R: R! H" ?1 R0 s
}
; g+ ?7 n! D9 D& J8 M8 L2 T' Q ```7 n( y& d- c. @$ g7 }
+ S5 a; |9 x, S: o, o" m, R 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
y" W% l* K$ v
/ A% @ N8 G- }) U! m3 t4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
: v" z. ~: o# u7 V! r9 Z3 I8 s y
! l: O: P1 `$ d3 M ```: ^* B5 a% m- T8 R# I. b2 h3 L2 x& c% E
$args = array(! h9 o8 R! J/ }7 o0 x( q
'post_type' => 'site-wide-notices',
7 F2 C/ D0 a; A( c& P 'posts_per_page' => 3,
$ m! |$ [) x$ D* n. A4 @$ K 'order' => 'DESC',
) F6 h4 M+ Q/ p9 s* c# T: o* u 'orderby' => 'date'
% y. x) s1 c. ?* [# h% j; o );5 w* w% B: E5 ?9 f
$query = new WP_Query($args);
" Y- l, F% t( Y- n Y if ($query->have_posts()) :- V5 }* s8 Y. O& }
while ($query->have_posts()) : $query->the_post(); ?>
& G7 a# l: ]( S/ G <div class="notice">0 V6 b& y7 @; K6 n1 p9 b8 e
<h3><?php the_title(); ?></h3>2 Q( D8 `: @3 u9 m4 X1 i; e$ m* u1 F
<div class="notice-content"><?php the_content(); ?></div>
% L) s, ^( Y r% U- g </div>5 ^% g+ x9 G4 ]2 ], h8 X) e6 I
<?php endwhile;( m* o3 Q8 [+ h% X# Z
wp_reset_postdata();
, G$ e1 h( @/ ^0 j8 Z endif;6 d2 Y; M, v4 V7 Z8 y
```
8 b9 f1 _& ]5 J. H U# u! L" F3 R+ e7 B* |0 Y' c# [; B8 V E
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|