|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
" }! I U& Y$ |; v2 M1 D1 \0 ?5 S% t3 y4 \5 Y- s H
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
' \2 q4 h) N" Y% ~6 G6 S7 u& @" N$ @2 o5 B9 q
以下是创建自定义插件的步骤:
1 y# g5 m$ m& E& f! t8 t. E+ O; b6 _% C( t J# X
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:/ {- Y5 Y. \* [8 {6 Q
" ]- l0 E& t2 b0 R" V2 n8 j6 R& d ```3 l2 z3 O% j9 _+ q! X0 Z$ `5 d% F
<?php) ]8 V! {0 {5 V; d
/*
4 l/ a9 v9 R8 }- n* q* r Plugin Name: Site Wide Notices Plugin
( S2 t# i* W% u" |1 b/ s J Description: Adds a new custom post type for site-wide notices.8 h6 ` Z( U: C3 V) u% Z+ c
Version: 1.0
& B; `1 L8 D9 x3 k# r3 S Author: Your Name
) L4 I1 S+ j; [- B. w Author URI: http://example.com
2 U y5 m/ I9 D */& j) e5 |8 U9 b- @# \4 d
: q. b8 j$ W, U // Add plugin code here...2 X# r/ J7 l( H- @: [1 ?
```7 ~2 |4 E+ f" a3 K; }; S2 R7 L$ M
" p, t% [# K' Z/ B* _& B/ q0 L 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
7 B! @/ z: x" o- `' m1 ?
7 {8 W I( ]$ v$ C2 {8 l2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
; E _- E/ a* z) `6 x6 y1 o! e4 \" g) U# P$ Y
```& K/ u+ B! [8 w4 h/ A7 t% H5 z$ c
add_action('init', 'create_custom_post_type'); J5 C# ?8 E/ l' t
function create_custom_post_type() {& O! S% `' ~& q( U9 Q( K0 T
$labels = array(. V. V0 e8 F7 d
'name' => 'Site Wide Notices',
: P8 p* D% P, v7 ] 'singular_name' => 'Site Wide Notice',* d9 G l* g: d5 b7 z% {$ H4 a
'add_new' => 'Add New',& y9 O/ ]: x- W$ i2 Z, V* ~
'add_new_item' => 'Add New Site Wide Notice',; y, i* g) z3 |& x
'edit_item' => 'Edit Site Wide Notice',
2 e2 Q$ e2 k% S- w 'new_item' => 'New Site Wide Notice',; v$ m; t3 M3 S4 H& G
'view_item' => 'View Site Wide Notice',
! X, u0 l( E2 z. e; u5 p2 n" f5 T& p 'search_items' => 'Search Site Wide Notices',7 h* i- ~/ s/ v Y+ z( w" s( Q5 ~
'not_found' => 'No site-wide notices found',4 R; l$ [/ J; ^( ^4 l: f' l
'not_found_in_trash' => 'No site-wide notices found in trash'
7 a8 z1 i7 h f4 G8 U. k. I: L );
" a9 U' W! B' O, o6 ~3 n) J2 R5 v$ H& P- d
$args = array(& O' S7 ~4 N( J# Z0 w
'labels' => $labels,0 w# ~- x1 U! r6 e
'public' => true,7 @# o! b* L; e- _) \
'has_archive' => true,8 Y6 S& z2 E0 O5 a7 B( Y
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),* V6 d1 V2 p0 b6 j: d# U
'taxonomies' => array('category', 'post_tag'),
. [4 y" @. _) B3 j 'menu_icon' => 'dashicons-megaphone',
' C% L1 D9 z/ T0 N+ H; Y) s 'menu_position' => 5,& x7 X; d5 R: e) Z6 ^' q
'rewrite' => array('slug' => 'site-wide-notices')
* i) h: T- s. S ~$ G3 r! j( n );
& ^+ p" g( v/ m% K& f* x! G! t( M
( H+ a' ]2 d/ y; d# L2 m8 H5 v o register_post_type('site-wide-notices', $args);$ L, a9 i. F. @
}
* [' @- O; C( s+ B, V2 C ```
# y0 G8 Q+ R* G* ~8 T8 _
. F h, o8 Z# k7 u6 K 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
8 O5 Q; a! G% _0 E% i1 Q! t) G/ i/ x5 Y$ K% j5 \, Z% Y# `+ Y
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
% \1 R6 b5 ]$ M7 o" Y2 }) ?+ N) M' q% X- [3 k, d, O2 O2 F
```: @6 z1 q2 J0 c( {
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');4 g$ S W! W5 ], x& T* ?8 p# u
function add_site_wide_notices_boxes() {" n( E) F7 s4 |9 K
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
" x/ A3 i. Q& c: P, c }
2 C3 `( t; e/ k5 q ^
+ }! t. d: s+ I7 ~3 V- N) G5 C function notice_details_meta_box($post) {# Q1 M3 B N) p3 W5 A3 y
wp_nonce_field(basename(__FILE__), 'notices_nonce');( I+ f! h- w2 M
$notice_title = get_post_meta($post->ID, 'notice_title', true);6 f: }! _0 j" x, P2 [, e9 u0 h0 S, A
$notice_content = get_post_meta($post->ID, 'notice_content', true);
7 w* f( ~% j* w3 d2 | ?>+ r# Y7 C1 k, _& `
<p>
1 y* z6 m0 H- U1 J0 Y: y d9 V* [' E <label for="notice-title">Notice Title</label><br>, y$ i8 e! K1 o+ H. W
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">! [$ [& F9 p0 g. N+ Q
</p>4 f2 i u! i3 w+ e m3 |, P* d
<p>
7 @- G4 Z" t; S) H% V& q; ? <label for="notice-content">Notice Content</label><br>' x5 o: _( P- o- t. p- S9 `
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
6 g; U8 K8 p3 ]: ] </p>
/ l" q! K7 n) Q. F4 r <?php
8 u Y& j. k. o- e. X9 p }
( h3 K2 E, E% e5 X7 f( h
6 J$ e: Q8 d. P J/ E! W" M7 l add_action('save_post', 'save_site_wide_notice_meta_box');
# M, e; M! q, M- o: h2 p# n function save_site_wide_notice_meta_box($post_id) {
: ]# v3 \4 { \3 e4 X8 \ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
% V+ p" b7 f) L9 ~+ G return;6 F* k0 J$ B9 G2 G* A
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)) f4 ]) u4 W" E) C, i% Y; {( }
return;* Z* A, F0 b! d+ D. B' n, e) ?
% C! ?- }+ J0 y5 r; X& `* z7 G6 R if (isset($_POST['notice_title'])) {
2 Y/ D, W: `) E2 q, O C& v( n" }( V update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));; s5 X- B+ `2 S/ ]7 S
}
9 m i( n1 w4 P& r: F) i if (isset($_POST['notice_content'])) {
. `" ?. R* V$ h4 b- U# y update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));9 K2 N8 D+ [0 S0 X% J- L
}4 _5 R7 f. ~. A$ j3 k2 Q! g- ]; i
}
4 h Q7 R5 e" b ^+ ^ ```$ ^1 ^% l8 B. i4 \7 S0 ^
: A& ?/ v7 ]' e z" f 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。0 ?2 g$ Z8 f; |7 ?* [( S
, ]2 \7 z8 e+ L
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:; \0 M- Z4 @5 S% @. q
2 | E) d$ G0 X7 ~6 X! U' R& l ```) Z. t2 n9 U1 e! d* T3 a: }6 v: K* ~
$args = array(
+ e7 o% {# f+ L 'post_type' => 'site-wide-notices',0 Z2 h+ a/ X7 [, B3 e a% K1 n+ T9 ^' s
'posts_per_page' => 3,
: h* o4 S( t1 `( o0 ^" u/ i 'order' => 'DESC',$ r% T6 r( o; u* j6 S: g% Y
'orderby' => 'date'" P1 b% j+ \ ^$ I5 B1 Q! i t3 `
);
0 ^' Q# N- {4 V/ R $query = new WP_Query($args);
0 H+ l" V" S* {6 |2 q: K L- G& ~ if ($query->have_posts()) :7 m3 s. B) c6 Z% g6 L
while ($query->have_posts()) : $query->the_post(); ?>( \( v# a: \0 ~5 h# \
<div class="notice">
! v- }6 I- _5 z( X <h3><?php the_title(); ?></h3>, y4 S) O$ l3 O* R3 s8 K |1 I
<div class="notice-content"><?php the_content(); ?></div>5 j1 s: b/ Q" i S
</div>- B/ C1 b2 r' m$ v/ O9 O
<?php endwhile;
0 v4 H* y* H# u! p3 l0 y, H" Q wp_reset_postdata();$ g5 k$ c) k( i) r3 {" A" u
endif;
' X( f; ?$ ?8 ]: W# G' p ```" @9 Y5 v0 r6 j0 ?- y1 g
+ x8 T* J3 Z: [' n9 v
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|