|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
) L. e5 n# m2 }* L6 ]: c) R. W
7 c3 q, H) W- y2 y如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
' Y7 k+ ]( {6 T/ q! H2 i, e6 X. C- ?) G2 S
以下是创建自定义插件的步骤:& L0 t; K6 T9 |
- B6 Q( T7 Y* \- r: P3 u* t
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
: j* P1 ^& l% Y, J3 q" x7 O q
7 V5 E$ Z+ M" o' t4 ~# J ```
5 G( ^1 p8 Y, b& b6 {& {5 L' f" W/ b <?php' i+ V" a T3 P+ G' d9 n
/*
+ c4 g3 j* G6 x. J, s! h Plugin Name: Site Wide Notices Plugin
# L0 O! h3 n3 U5 l/ M Description: Adds a new custom post type for site-wide notices.6 N% S5 o, R* t. x i+ Q# Y
Version: 1.0
& l; I O, E- F2 Q& ? Author: Your Name/ l' Q6 V( E) ?. h$ u
Author URI: http://example.com* b/ C% c' j- }. e- ^7 Q0 e; @
*/
9 p6 K x$ P" _/ _
0 B6 G' F' W) o7 e8 v$ v* ] // Add plugin code here...
, T; R( S( o, y# { ```
* Q3 h) I& w6 e' t6 b
# [ w5 ^7 F0 c" m) X& Y 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
2 M. [$ h0 E$ L2 Q! o" x
M [- C# z. x1 z& S Z1 {. \& C) d2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:" M7 N% {9 B. n# j' r) }
6 b' c: k F" f' Z( X+ g' d9 j- O ```* L( W. L+ B1 u, F8 G. H9 a
add_action('init', 'create_custom_post_type');
2 }$ e3 x& ]7 y function create_custom_post_type() {2 q$ G/ F' L5 T/ u* t$ U& Z
$labels = array(
" v r" l; R( M# y) k& i 'name' => 'Site Wide Notices',
; s# g% n$ @$ g; M+ f, }, z 'singular_name' => 'Site Wide Notice',' `7 A2 f( i f- J% F* E
'add_new' => 'Add New',9 g9 @% L5 E9 J- {" y( s8 {- ~$ H
'add_new_item' => 'Add New Site Wide Notice', Z) H, Z8 o1 m' M& W
'edit_item' => 'Edit Site Wide Notice',
6 L; s# W( Q+ W$ ?. | 'new_item' => 'New Site Wide Notice',' i% w5 y% S9 x0 g/ {9 f
'view_item' => 'View Site Wide Notice',
& h; u( {9 D% @$ |1 | 'search_items' => 'Search Site Wide Notices',! p) M9 Q5 H* Q$ R5 L" J5 C4 q
'not_found' => 'No site-wide notices found',
+ ~1 D6 s! Z+ p0 u# d8 K& q9 u 'not_found_in_trash' => 'No site-wide notices found in trash'
! l7 L3 ]1 u- C );
5 ^; ^7 q" C1 a+ d8 U/ y$ e6 I$ V/ S [9 F$ q
$args = array() K8 R( j2 a4 ?3 Z, A% H9 f
'labels' => $labels,0 p. @, f; }% P0 S; q B3 w# T' q
'public' => true,
7 \- f# p# u0 L* i4 }8 ]+ ?! R! T 'has_archive' => true,6 Q2 w! i0 q* e! i8 l
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
- {. x3 C4 {+ N! a! u+ `% r0 P- ` 'taxonomies' => array('category', 'post_tag'),1 S1 R9 U4 C. F: d
'menu_icon' => 'dashicons-megaphone'," M9 q' x% j" D! i
'menu_position' => 5,
; H1 a6 B- N% A8 h" N' D% M1 ?2 f1 E 'rewrite' => array('slug' => 'site-wide-notices')
, n! ~9 ]. L+ K$ ` );& b7 b1 c. m) e/ K; u
O7 o' Y9 B; `' ~
register_post_type('site-wide-notices', $args);
3 v) a* }) {& Y2 b9 _- b }$ j& o1 I8 m! t
```
( Z- }" e0 o4 v$ ]0 [" e
4 R7 `( d* z& T. p/ C; P3 n( d 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
1 N7 F7 |8 `7 @( d- s
3 C% P' ~# r; ~: s0 }* O3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:* D m2 m# l% j
0 X# i q" V! u0 U; \! a2 ~
```
+ b3 O) ]4 W7 O3 |8 Q add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
5 k% _# e( h$ |% k9 v" @ function add_site_wide_notices_boxes() {- w' M( z& |4 g- k# o4 c
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
/ e5 e, `) S9 j. G }% |" L( F0 @7 I
4 u( g! y* ~+ d6 m# I' x function notice_details_meta_box($post) { r- t, s0 B; L/ p5 p
wp_nonce_field(basename(__FILE__), 'notices_nonce');
: `/ f) G6 Z. Y7 m5 m $notice_title = get_post_meta($post->ID, 'notice_title', true);
) ?( r6 i8 F4 I9 I# l& x $notice_content = get_post_meta($post->ID, 'notice_content', true);0 I! \1 V8 c* }" m, {) f# ^7 j2 C/ ^
?>/ S6 r& Q+ ~, {
<p>
' R( b" u" V1 e9 O <label for="notice-title">Notice Title</label><br>, g( {; @) d, j% W% [: ]/ U0 E5 r
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">+ e( P7 I! _6 J, m, | f
</p>
( A4 ^! L) g3 y <p>& |. ~ |" g$ P+ Y0 D; d
<label for="notice-content">Notice Content</label><br>( l- Z( q/ w! ~" U5 k
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
) L0 ~8 h V* _ </p>
7 M0 A: W$ L: @0 y5 q, U: f- t <?php
* Y. ?8 k1 e* k' Y; t. u! h4 }1 v }
8 K" V! X: u) t: G! i. @1 _- ?$ S) [
add_action('save_post', 'save_site_wide_notice_meta_box'); |9 Q0 _; i z! c+ R
function save_site_wide_notice_meta_box($post_id) {7 s% Q$ b. C3 t8 \4 x; \
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))& ]# [* m# T6 R7 b% ^
return;
- ~2 _; _" U1 l7 j% P( z if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)( t! a% p2 k4 t" f, o7 k
return;5 @# u. I5 U* s6 j+ I% o2 W
. R8 H1 b& {1 t. R if (isset($_POST['notice_title'])) {
5 P4 Q g& v% Q+ D: G2 Q: u3 f' P- l update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));0 g4 H* `# a. `+ L' E; t! ~
}
& ^2 `5 Q$ m8 Z% i if (isset($_POST['notice_content'])) {
# E$ d! \7 t M5 T update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));0 J1 y9 f. t/ p8 B6 i) w+ l! a
}6 h6 J- m1 x# c9 q' [- ^
}
! {8 Q$ v6 I; H+ N' d A ```: }; t( ~, d: U1 z) I
' Y; o1 T$ w/ ~$ u# f) D7 [
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。9 p$ s8 }: f4 C* o+ i
% J: v; b5 e6 h0 T/ B
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
* ]1 @6 _/ p: I, M- `( C- [
! s9 I$ U1 o$ V8 M5 S ```& I( p* R5 E9 K* S& ^
$args = array(- G: ]2 y8 o3 x' o
'post_type' => 'site-wide-notices',4 X% u9 [( s/ [7 t! |) v
'posts_per_page' => 3,
5 c2 t( y" l. N) A4 a( H 'order' => 'DESC',
/ m2 f: q0 R- v o' D: g 'orderby' => 'date'% F' V: i* t( [3 O& e: a% z
);
" Y+ T' L3 b) ] E $query = new WP_Query($args);
) `; g7 _, B9 { if ($query->have_posts()) :) @+ [+ h( D: q4 K/ r
while ($query->have_posts()) : $query->the_post(); ?>
) b0 I3 b: p4 r9 P5 S9 I } <div class="notice">* F3 g3 w' a7 u! K+ c* Z, }
<h3><?php the_title(); ?></h3>
6 G# v2 u& r& ] O( S <div class="notice-content"><?php the_content(); ?></div>
5 Y# p4 B' Q0 K </div>
* u* E$ u [. q6 F <?php endwhile;
9 b* s6 q/ g: ]* m h0 f' R wp_reset_postdata();" q8 f3 ^) t6 o }
endif;
$ c2 z8 d' d% {' e ```! D% }1 g% T; t2 h$ c! F8 H
5 L* @0 @. `" N0 k; g
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|