|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
3 P/ D7 o# W$ z/ C
( t& F! O: a% ~3 m如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
9 G- I5 E% L7 W9 o4 j
* w" k- S1 q* z% M以下是创建自定义插件的步骤:
+ |, I5 b) z1 V3 s8 B5 O! u* q
9 }3 `" A- [( @' \7 `' d& V4 N* B1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:5 K$ P" v/ c5 e# P
0 J$ U, b. a& Y% J
```
0 |3 w# F! U" v4 t$ M6 z: h) A! } <?php
Z$ x: ^$ P2 c* ]( d, u9 a /*7 \7 H" `5 O9 J; s2 }. K6 b
Plugin Name: Site Wide Notices Plugin
% B# X8 U3 a$ N# ^. z# n! {1 y9 x# r Description: Adds a new custom post type for site-wide notices.
9 I; n3 G( d. J7 w' o3 b Version: 1.0
- p& V0 n# n7 S K/ |. z) F0 _1 f) R6 @$ ] Author: Your Name8 \8 [! |8 {. A$ \( B' l& t2 h
Author URI: http://example.com [8 J- f3 z. Y" X: b& _5 S' L
*/" X; J6 l1 ?6 @' L! M- L
4 {; K" v% j8 Q4 \" p // Add plugin code here...
, T) i) P" y8 P" b! k) d ```
, x9 W7 C1 I1 j7 G' f) W5 |& y" x- ]: v3 a6 Z- {. j8 h2 t' a
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。; f7 d5 S& G7 G& h
1 M& [) |; Z/ }. F- q
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
1 W2 L4 X4 a9 i! s9 D3 {* @4 k
7 t3 F/ q* m. `% M, k ```
% a# ~6 a0 @- d! B: w add_action('init', 'create_custom_post_type');3 }' Y G6 o& j$ k
function create_custom_post_type() {0 R) e, P1 v/ p
$labels = array(
6 o" r ^/ t/ A4 O) j7 v 'name' => 'Site Wide Notices',
6 }% r* D- @/ y$ i& Q, x1 Q7 Y3 _7 b 'singular_name' => 'Site Wide Notice',
$ `- e2 r2 C- l& K+ Q 'add_new' => 'Add New',
J: T5 s' ~$ o8 z; ?/ y0 R 'add_new_item' => 'Add New Site Wide Notice',
3 Z+ v2 ?8 u! e- X# Q 'edit_item' => 'Edit Site Wide Notice',
0 B4 E0 v% w$ S0 u 'new_item' => 'New Site Wide Notice',
( d( @2 D8 M! B+ q: H, H+ A' M7 O 'view_item' => 'View Site Wide Notice',
5 I; k, d$ |9 z6 t 'search_items' => 'Search Site Wide Notices',
7 I+ N) O% e5 c a" r 'not_found' => 'No site-wide notices found',
) g, j% C" j5 C% T 'not_found_in_trash' => 'No site-wide notices found in trash'4 e2 \+ i1 B9 c
);" W6 M2 |2 A' Y# N$ c+ O
1 A* h/ M( {" T5 j% M
$args = array(0 x/ `7 B$ Y( |! L; B
'labels' => $labels,9 N( L# k" k8 N5 ~$ i
'public' => true,3 f+ z8 D$ M! z5 ?( S
'has_archive' => true,* U' T/ L) Q {5 ^+ x9 G3 B- L8 z
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
* t. p, Z' a) X" [ e 'taxonomies' => array('category', 'post_tag'),
& C T( B0 ?- Y& p+ t 'menu_icon' => 'dashicons-megaphone',' h6 O1 K, s* I% @
'menu_position' => 5,0 |. R- ~9 E/ k4 N4 c
'rewrite' => array('slug' => 'site-wide-notices')
* `) [7 e4 v$ K# ^ );
, M; o* n9 n4 k ?! @/ m* l
8 v; ?4 G( |5 Y, J8 D3 z register_post_type('site-wide-notices', $args);5 c2 E$ F7 a0 y' s' {: V
}
, M# C. M7 S3 g6 X- P7 ^ ```1 ]8 o9 d* I. R. r6 r* g* \
/ i2 i7 H1 @" I4 S% ^. k 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
8 b* F4 r. O& l* U6 M, J" R& `/ Q' n) U5 P: L* ^
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
% x/ r0 ~- M. o+ p6 n4 e+ y1 H8 \
* @ d& Q& D0 O5 \! l% r# b+ k ```' k8 |5 u+ Y2 h6 S
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
; |/ V/ ~$ U$ H' ]9 x0 K: q& e& O function add_site_wide_notices_boxes() {2 Z9 K; m, C3 R7 }' A
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');$ c# Q: N8 \# T" x9 `( Q2 u4 G
}
+ F: u8 N& w* `
, J! J$ d& d8 V0 z0 [) z function notice_details_meta_box($post) {) u& P+ _' v2 x
wp_nonce_field(basename(__FILE__), 'notices_nonce'); U! P6 q7 {! Y
$notice_title = get_post_meta($post->ID, 'notice_title', true);
1 D0 | \5 y) I $notice_content = get_post_meta($post->ID, 'notice_content', true);7 W8 f, `9 Y. A+ D
?>
! G( r {0 Z! i& ?7 s <p>
( M6 A! a- w: R7 L ~, v <label for="notice-title">Notice Title</label><br>; u5 d& f4 x+ @9 j5 \
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">" l% v2 y8 P$ {9 j- E/ e
</p>( C) |" G3 [* L' i! Z
<p>
/ Q/ G+ s. i3 X! `% b ]) f <label for="notice-content">Notice Content</label><br>
P4 \; M! `0 H: S+ } <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
% n! @7 i; g/ \6 ^2 _4 | </p>7 I }5 @% Y" G. { O+ Y
<?php$ \0 J0 M. G/ x, `0 d& E" h
}+ l9 X/ }3 }) f M A
& q0 {' l2 |! K. o( v% @3 B
add_action('save_post', 'save_site_wide_notice_meta_box');
0 B/ K3 q. R6 I6 M$ T( u( c% Q function save_site_wide_notice_meta_box($post_id) {
3 s" |6 F; a5 Q/ v if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))& H$ U3 w; i% o# l- N- N
return;
" S" Y* _+ [/ t7 @; E9 g! Z" j if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)$ {+ x9 R* ? z: L7 L6 D. o
return;" A* F! L: `2 @8 c$ r/ e6 |
. o( `5 \' m7 L" F# U. q' k if (isset($_POST['notice_title'])) {3 i; F" e% c! b) m
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
/ J% b% \5 L# T0 B }
- S3 L2 p$ i( E* j if (isset($_POST['notice_content'])) {
( d( C* e" e, }7 V update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));" z. Z4 l9 ^4 ]7 j a6 W
}% C0 A- t' p- s1 N# U1 H C( `3 J
}
B. K, f: a# [1 J6 N9 ` ```. P. x. U% e, v- `# a2 Q
- N& Q7 _2 u! ~1 d* G( I% G 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
( q0 _ n' \. k' O* D6 E
0 X( t7 C) f% b. V8 W, b b4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:( m7 e6 k! o3 n# N k& n# Q' X
* s8 J, [* W" h' c* R4 b ```* h3 `( I7 A. \6 P8 T( Z
$args = array(" Y: u$ F/ F/ @$ Q |. a
'post_type' => 'site-wide-notices',
2 P7 b% n% O5 S5 P7 R 'posts_per_page' => 3,
# p0 c2 g/ R6 P" t 'order' => 'DESC',
6 e- A2 ]2 D1 i9 C* F 'orderby' => 'date'4 ?; H# p& P6 w) g0 s
);
4 {! Z' n) m; I" R $query = new WP_Query($args);
5 A/ W7 F+ I& G, c: R8 @ if ($query->have_posts()) :& t* m) o& c: T) m
while ($query->have_posts()) : $query->the_post(); ?>/ }% b9 J; Z q- M
<div class="notice">) Y1 N- N9 {* Y8 [) [# {+ m( J. k
<h3><?php the_title(); ?></h3>/ ] ] c: c) g8 g; e
<div class="notice-content"><?php the_content(); ?></div>
7 M3 G$ m( Z4 M3 }0 N! L. h7 B2 C </div>
5 O5 [/ d4 a+ k <?php endwhile;
- `. Q1 R* }# j wp_reset_postdata();
7 M# a @) j/ ^5 I* j& w- } endif;
( Z5 s9 ~6 M$ L& s! q ```4 k: y+ Q. J4 v' Q8 B
; v5 t; `- J- x4 h! k
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|