|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?5 N, T% P) ~: x. D+ S. ]
7 r* g4 T4 w+ S
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。; t& I4 `4 ?5 |2 z) k/ U
- F, B# N& w u7 f. i4 |6 P以下是创建自定义插件的步骤:
$ ~9 Y& B# n; d8 O2 e6 N
: ]5 w8 T5 I6 m- M. m2 t( R/ C1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:# O* \& L8 x& B) x
4 y* h p8 r$ ?4 ?% Q' y" ^7 K+ G
```6 _- o/ O! S9 w% x4 O% j( ]. @" h
<?php
1 c+ z( b( t5 K) w9 C1 ]4 P /*
. X3 F+ |3 x; h9 c( z Plugin Name: Site Wide Notices Plugin$ ?* M9 n& ^" p; X1 X/ e
Description: Adds a new custom post type for site-wide notices.$ L3 y6 K. j: d8 }+ C% M* J
Version: 1.0
6 F5 Z" {# b2 u9 G( P/ | Author: Your Name- Z p: l$ @4 K) P1 L- S
Author URI: http://example.com
f7 f# f, A7 y6 W1 |% v2 w *// y0 t5 @7 I( X! p% ~4 h# b+ Z
1 q" _' A0 ^+ ~- |' v8 [ // Add plugin code here...
0 h: @$ A/ m. a3 I4 W4 { ```% {% ^8 ?" A& }6 d0 J
' `2 r* x! \5 D3 ~& \ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。, d( E- L) T N: z0 l i9 w+ O
6 q0 t5 R+ X5 _2 O
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:+ X7 x0 H2 ]2 {$ O2 J$ a
) X; v9 J! n% y) w/ L7 ~3 h( J
```- |" x) D r# ~$ P2 J9 ?1 c
add_action('init', 'create_custom_post_type');
% \ e* ~. {3 ^7 L$ @+ Q) ?' o; i function create_custom_post_type() {+ a8 \$ e" C- C. M, l$ u5 T5 g
$labels = array(2 g$ V+ ]. V9 `9 [
'name' => 'Site Wide Notices',
# k4 D4 i8 V1 d8 ` 'singular_name' => 'Site Wide Notice',
( X0 `5 P8 U, x 'add_new' => 'Add New',
8 W. F/ n# [! I4 g, _' u" v+ z 'add_new_item' => 'Add New Site Wide Notice',
2 K1 m* k% R/ N* S& s/ V4 H) J 'edit_item' => 'Edit Site Wide Notice',
) ?; V' ?: l' o' a& ^: N; m' p 'new_item' => 'New Site Wide Notice',4 F: x. n; { d+ o- Q/ f
'view_item' => 'View Site Wide Notice',4 s" S2 `& T) a) ]$ v1 _
'search_items' => 'Search Site Wide Notices',: x, I; ?- ~0 `$ j! ^) L) @% q5 V( O
'not_found' => 'No site-wide notices found',
( W; L0 V; M. N1 P, s, L( ~! N 'not_found_in_trash' => 'No site-wide notices found in trash'
( t0 d8 H6 T& \3 b: N );
l _0 c3 {2 @) x% l1 _9 t5 X% s+ U1 `% d
$args = array(* D. @4 n- x* k+ g: q9 c$ P
'labels' => $labels,5 x6 o4 S) l. R- ]
'public' => true,
$ X4 ~" T- ]8 m% }' R" H3 b 'has_archive' => true,
" B# W" F* X, `2 c3 G$ h 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),# Z; c& k* s4 x: N5 y
'taxonomies' => array('category', 'post_tag'),
1 [! }$ ?2 c7 Z! G } 'menu_icon' => 'dashicons-megaphone',
2 g) s: t. h2 {, {7 x8 s 'menu_position' => 5,
: w2 p( |, X( u9 R f; {4 C 'rewrite' => array('slug' => 'site-wide-notices')
# S, t* K; p/ b, e, N );: x) e) r; F8 [9 ] _. ?
4 E" ` e! b8 Z6 \: `/ ^
register_post_type('site-wide-notices', $args);
+ t0 U* Y$ Z% B. b# ~ q5 p2 q3 G+ w }
7 M3 n" O! M% i4 p* a ```
( f+ Q) X7 s+ t6 o: S2 ^, f, F" i/ V- O* X' P \$ [
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
) n# \; J% G3 e4 l. x1 s
0 i' i) ^8 f4 I v [+ }3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:' P5 j" j/ `* J& o- r# R3 H
- A4 n: {2 Y( I& P0 E6 b/ m ```
* c9 J0 q: P4 w, T add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
: l; J5 L B" y1 T' v% M1 X# n( A9 [( q function add_site_wide_notices_boxes() {
" x& U2 V0 d2 i, h) @ add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');& T# a, t9 z6 _# X7 d' G( v
}
2 v' ]. [8 \, T+ i
: M" E/ P$ s# q9 _0 A& ]# c function notice_details_meta_box($post) {
( T3 j. T/ Y, a6 c/ E wp_nonce_field(basename(__FILE__), 'notices_nonce');4 Q g, l# S, y" |1 {1 ]/ a
$notice_title = get_post_meta($post->ID, 'notice_title', true);
* ?( R3 s7 T4 B6 M, `, c0 m$ _% R $notice_content = get_post_meta($post->ID, 'notice_content', true);
+ C- f6 S D/ | F- S* L' l/ p ?>2 D" c, \) C. A( l
<p>5 d- Z1 Y! G9 e4 w: F( l4 H
<label for="notice-title">Notice Title</label><br>) H1 X' G" k7 [- P( f2 z! o* ~$ a
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>"># N7 A' b: U2 q9 O; i! ?
</p>
6 V a9 m8 @: @ <p>3 |6 u. P5 H6 {- `& M) r0 D
<label for="notice-content">Notice Content</label><br>
2 c' i# o4 O5 ?/ x3 c <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>% F5 q4 }7 L7 f! U. N
</p>
: n9 V& u; T: L l <?php: _, A+ \+ q" v q6 H& z
}' ~$ ^+ e0 v5 I) f
3 D1 {7 j3 P! F4 D
add_action('save_post', 'save_site_wide_notice_meta_box'); z L) A9 B3 n' B7 v: O8 C7 t& }
function save_site_wide_notice_meta_box($post_id) {$ q( L1 b3 D! r: j/ ^# t, S
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))4 f5 ]/ v# k' l+ s& U3 L2 A9 v) O
return;
0 r0 b8 J2 t1 A8 B6 n2 r if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)% A3 K5 R5 \$ Z& o/ i- |
return;
$ K/ l& i$ r5 D% x8 x
, X! m4 X$ j! q if (isset($_POST['notice_title'])) {
Y7 B6 w; a6 } }/ q7 W update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
3 ~1 B" L, X. C9 c! {* n2 e2 ] }
% Q' t1 I4 l5 b+ L: }9 S if (isset($_POST['notice_content'])) { I8 N+ U. v. g$ r8 n
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
8 Z/ R$ c; f W8 U0 R7 F }6 F, ^- z$ q% ~
}( P2 P5 p8 f Y# c& o9 V1 b. {1 g$ r
```
) c, {3 a1 Q1 i9 }7 e* Z
" ?3 z# a9 ^% U* D g+ [3 [ 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。, C/ I# `* R" b6 i% l7 l
# ?. s7 [* t9 Q4 T s3 k) E
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:: i& j. ?/ e& t: W
' C2 m. k) _5 l0 s$ _& O ```0 k" ^/ `1 \+ L+ y$ w) F4 C+ H
$args = array(. w, T( k2 ~% d
'post_type' => 'site-wide-notices',5 D. r( P) r) O- A6 W5 Q' ~
'posts_per_page' => 3,
! r1 Q6 }4 w$ r4 x* K0 ` 'order' => 'DESC',
# W# z" S( N2 y/ ~1 r3 u* A 'orderby' => 'date'
- c0 A, G0 g- H8 \8 c, Y5 { ); e* x2 H3 e" K; i. r9 K7 U) d5 M
$query = new WP_Query($args);; G! D! `0 _; Y h
if ($query->have_posts()) :6 [" D5 d% A8 i$ N$ R
while ($query->have_posts()) : $query->the_post(); ?>+ ]; E# ~" W6 T* f$ s0 P
<div class="notice">' X( i! n$ R' k) g+ S+ I, r
<h3><?php the_title(); ?></h3>
' I+ e9 U r% i8 a2 t4 `& k) w# E <div class="notice-content"><?php the_content(); ?></div>" u7 c$ s. \$ h; w! u
</div>; b1 S: _2 v- C' t1 Q! \
<?php endwhile;9 I( j3 {; Q( n; j9 i
wp_reset_postdata();
/ T+ W) m+ Y; G; k9 X+ Z endif;* [0 W3 o0 d+ _) p
```
- V1 z, c5 b/ M+ J0 H% a
8 Y: v n% m9 x) F9 H3 Y 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|