|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?" s. v8 A k% f2 n/ @
5 o; b. d& ?; e+ T, R g2 T J如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。- j- b4 t( h0 n) v7 O7 ^
; ]4 p) J9 G1 h! f }以下是创建自定义插件的步骤:
% q6 E1 ^8 Y7 b% f
: l# ^# l% U K1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
) V+ \2 g1 |7 w" J! q$ Z
- |: x% p* y4 E$ |# m5 ]+ g) u ```0 e/ G8 o6 W" \& W
<?php
# }: @6 Z; \: ]1 U6 L /* e. \; ]4 s! [8 r% u* Q* A
Plugin Name: Site Wide Notices Plugin/ R4 M" y+ v, f1 D: r
Description: Adds a new custom post type for site-wide notices.
% E! `% I, U" _+ E Version: 1.05 F" d; X( t& g4 m* Z
Author: Your Name1 Q4 _1 i( z- Q
Author URI: http://example.com1 T q1 R4 Z- I$ Z6 s
*/
) W# k& V4 G4 i6 s: S
4 j0 \( q J. J# e% i // Add plugin code here... V0 n% T0 U. j) y
```% d- q% Y% \5 L* s5 P
6 K+ _9 S( x- v J, i/ ]- T3 x, ^) B
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。( b) V& @- a9 S5 \0 y& h! C
' i+ N$ @, S( {2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
" I% a- B7 X8 ?# [, ~5 b' S% p/ W" d6 y. B$ i3 O' X+ [* C
```4 ^5 D$ h/ N* `, }% F! K8 R
add_action('init', 'create_custom_post_type');
! U) M+ X( I; V/ Z% N function create_custom_post_type() {4 r3 q6 t- ~! F. Q2 Z6 j7 v+ l$ g" k
$labels = array(
- J X) Q" H9 N$ e 'name' => 'Site Wide Notices',
( K7 J3 O% Z4 I2 a6 \) B! g S0 ? 'singular_name' => 'Site Wide Notice',* B6 z5 @2 F7 U( P
'add_new' => 'Add New',
* c4 I" J& A, F) T 'add_new_item' => 'Add New Site Wide Notice',& j$ x: E! y! v$ L; O
'edit_item' => 'Edit Site Wide Notice',
# ]1 Z P, ?8 K; t2 F4 U5 x6 y* M 'new_item' => 'New Site Wide Notice',! f2 |2 T5 x# `6 f& o H
'view_item' => 'View Site Wide Notice',
1 ~: z1 d6 N* G; s' N 'search_items' => 'Search Site Wide Notices',
& E+ ]) A o2 k) L9 u' ~ 'not_found' => 'No site-wide notices found',
+ I& P/ M G1 n6 G2 y 'not_found_in_trash' => 'No site-wide notices found in trash'9 b8 I. _) Y5 z& B2 ^4 w
);3 e1 S( R# z4 U9 N" G( E
9 a9 D$ j* ~( Z, l& Y* e" j: L& \+ q $args = array(
) g! ]: C3 j4 L 'labels' => $labels,/ o1 T2 g7 S# D8 K* a' {
'public' => true,& `0 F* ~. q' o W) `2 z: A" o
'has_archive' => true,8 n6 `% ]3 I+ z' |6 P
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),8 p3 G! N* P, h" h! U
'taxonomies' => array('category', 'post_tag'),
% `# ~/ ?* W6 m4 D0 k" @5 i" B$ Z# i 'menu_icon' => 'dashicons-megaphone',
) c8 a/ w$ s+ `9 g* I7 T$ i% E) T 'menu_position' => 5,
9 \" K1 Y2 U1 I* I 'rewrite' => array('slug' => 'site-wide-notices')- f% G! G2 m2 f
);& k/ Z1 p( E* Z( c9 Z% j, N, j
6 A0 E1 f- Y- l2 f @! p1 J
register_post_type('site-wide-notices', $args);( ?2 E5 O! u& u
}+ |. N9 Z' n( V8 e3 W2 p& R
```0 X/ y) W+ x6 g6 y! p0 c2 w
, l+ q$ _3 F/ I) Y @
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。5 d# s+ H8 h' M3 ^& [
: N! S5 W# j3 R, j# w. x/ E3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
5 f0 d+ U, D! q+ t/ U$ Y' U- G8 W# s' v( x2 }$ q5 U; q' \2 b
```
5 y8 V+ y9 H/ ~ N add_action('add_meta_boxes', 'add_site_wide_notices_boxes');- O( e* y- X8 `# e4 ^, y5 ?( z! ?
function add_site_wide_notices_boxes() {7 t' K9 L( M$ a+ Y' b3 ]
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
8 S# `% T% T0 {& s' U! l; }8 | }
) Q* J6 x# W. A$ f3 N f" k
3 Y/ C3 D% y) `* X8 B* x( _# o function notice_details_meta_box($post) {
: l9 i" |+ ]; J wp_nonce_field(basename(__FILE__), 'notices_nonce');
3 k8 ^ h/ Q, z8 @1 c9 c $notice_title = get_post_meta($post->ID, 'notice_title', true);
8 X+ h' {7 E( w/ D9 F. f3 D $notice_content = get_post_meta($post->ID, 'notice_content', true);
- `4 r4 |% s/ W3 M0 o6 m. E ?>& m6 S, u, W, h8 u0 B1 r
<p>7 @5 I, j' b0 X f
<label for="notice-title">Notice Title</label><br>
& L( n7 B7 h" H$ g" E* ^5 |9 y <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
) j0 O: j! w& [+ f3 f </p>& q/ n7 K0 [+ @& L- r1 ^
<p>
' m0 a5 v4 x( t2 f5 d6 [1 s <label for="notice-content">Notice Content</label><br>
2 ^% O0 M" Y g# x# h <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>0 N% d% o0 u( Y" ]3 a: R4 w5 D% G% u- K
</p>+ Z5 ~+ B3 R2 j6 g' g# C) _( D
<?php
5 [% t- b8 A3 p0 \5 W) f }3 X! ? T* D$ x; E
! u" c' `- T5 J2 E
add_action('save_post', 'save_site_wide_notice_meta_box');
. \4 G* r' R7 F, a% A function save_site_wide_notice_meta_box($post_id) {6 B$ I$ U3 y0 I4 Q$ r3 w4 i5 {' {9 x
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
% D5 {* E, a* d6 ]3 O return;% R7 Y, p7 U$ K J9 \" h8 c
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)" g7 e. I+ {1 e/ o- e
return;" S1 ?5 w+ w/ v9 @+ j( b$ r
' }) D( \: J* ?4 g& k
if (isset($_POST['notice_title'])) {
8 e2 ]( f2 T" O6 b update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));( `/ n. B2 B, ?3 [1 ]
}
2 _- _# s7 `# E8 q; H! X if (isset($_POST['notice_content'])) {
$ F# S: W, |1 @% P update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
4 Z* B* Q. H: V- O8 D }/ K3 Y* R4 q# B8 |* [( D
}+ m# _6 F+ S0 E" M
```" r/ s! }9 {; n6 L0 R
1 O; ]& x. u9 W2 c' D# a 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
, Z U o2 E; g" O. j: P# v, [) T2 J! `4 ?/ j1 `! |
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
$ [: O. b) o; b
7 |. [9 z. }$ @- B+ V3 N ```& o$ V: k( O, V+ q o
$args = array(0 w; \/ _& N" k/ n# x3 }
'post_type' => 'site-wide-notices',
2 ~* H9 {; s- B* f9 t O 'posts_per_page' => 3,
2 S1 `7 {) }/ R0 @: j) ] 'order' => 'DESC',5 @& ]% o2 r; U B
'orderby' => 'date'
/ d& [9 w% ]9 Z2 _# y/ O0 A );: @# C! l R3 G* q% z" I
$query = new WP_Query($args);, O5 c1 O" y) M7 T' [
if ($query->have_posts()) :
) v& ^& n t9 L while ($query->have_posts()) : $query->the_post(); ?>9 p7 ^+ F+ v9 T g6 O
<div class="notice">7 J4 W' \/ S2 o) d+ n0 h @5 Z& K
<h3><?php the_title(); ?></h3>
" o6 ?2 L3 n( J- c$ Y0 |1 u <div class="notice-content"><?php the_content(); ?></div>
0 L8 y: I$ b2 h* b7 u- }% c </div>9 B5 j1 K, j" x/ W( C Z
<?php endwhile;
: o9 c) b, a% M$ j9 N wp_reset_postdata();
- ` u# e1 d) M* ~7 d endif;
% i8 j9 v6 ]. }3 l1 z8 @3 E9 q0 F6 i ```) E" y& N8 c% x8 Y' _' v/ {) w
( b. G" O/ t) U2 V7 z1 [9 o 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|