|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?' E: U- f8 X2 P: [9 v
" E6 D; T( {' i# g; g
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
/ B- w( s' f# W7 T8 h/ H& i( e, Z' ~
以下是创建自定义插件的步骤:
9 Q& ~7 @, E6 M9 W/ `' F t( g" N# Q; [% N
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
% E. N5 G: W( l; G5 x2 k( k' I
1 q ?) q" ~. m# y ```( z) U% {$ @/ \3 g, T
<?php
_7 D s* I k& U: e! U /*
6 S! Y- G, X, w( I+ b Plugin Name: Site Wide Notices Plugin; ~5 l( m3 \* B$ o& a% S& h1 i
Description: Adds a new custom post type for site-wide notices.
0 R @- M5 I$ W! g, E% d Version: 1.0
0 _+ j1 S* j8 m* G8 Z2 ]6 y0 @ Author: Your Name
4 Z, h$ g& s- H6 _5 S Author URI: http://example.com) \: u9 e" H ?$ }6 M
*/
5 i8 J% q% h' K2 x' K
/ d( F7 T. k* `$ P4 L5 Z // Add plugin code here...1 V& p( \. k! @4 K; P
```* u& ] M: i& t
/ ` I% v& }' K6 j% { p( F4 I
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。- n8 B' N& \) z
! f( r2 i0 J, m; R/ ^
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
5 h. D4 J% i, f; m. u- H, V- j [
" a* f) T$ k8 f/ w5 K5 W ```; U$ `7 [* b' m8 I+ o1 N; i: I
add_action('init', 'create_custom_post_type');
! c' l) m9 L: v H3 f3 y4 h# \ function create_custom_post_type() {) D+ }8 l z0 G! F
$labels = array(! |. U; o: ^6 {- x. S K+ R4 D
'name' => 'Site Wide Notices',
2 b8 i/ B/ T' T1 O6 ] 'singular_name' => 'Site Wide Notice',
1 p5 F7 b# B) G' ?1 p 'add_new' => 'Add New',
2 V: }- ?/ n4 g 'add_new_item' => 'Add New Site Wide Notice',
6 M8 U* p5 n( Z4 } 'edit_item' => 'Edit Site Wide Notice'," ]! r6 e& H; t, f: O
'new_item' => 'New Site Wide Notice',
3 L1 J3 q, L) k1 C. g% @/ P 'view_item' => 'View Site Wide Notice',
( ~2 ]* U/ k, ^* Z0 l! {4 T3 ` 'search_items' => 'Search Site Wide Notices',
7 w" t3 u; \- `. x* s; q' @! w7 w6 C 'not_found' => 'No site-wide notices found',
; J4 V/ R- T, k3 { E9 n 'not_found_in_trash' => 'No site-wide notices found in trash'
: f% A9 ~+ T6 |/ o: L );4 x% c, T) y# t/ n4 X7 i
8 j, i' X( s3 z; A$ S: y% e% s! N $args = array(/ R" R# p2 ]7 u- B9 }; @) I
'labels' => $labels,
8 r) y# A! \5 B! O 'public' => true,* B5 U1 e' D% b0 t/ }+ B% e
'has_archive' => true,
6 [2 k) B$ I& M0 o 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),7 C6 m, `, j6 ^# I, p o* P/ K; E B
'taxonomies' => array('category', 'post_tag'),
% j3 ^) Z6 n/ Z/ f- q2 \( F 'menu_icon' => 'dashicons-megaphone',
: ]+ {, x3 E2 @8 F% O3 P 'menu_position' => 5,1 V8 C7 ^/ p3 H+ A- R, g
'rewrite' => array('slug' => 'site-wide-notices')
; P/ x/ ~- S7 z1 l2 R );
0 G1 h2 J7 _2 W- [; o) F& B. w7 w7 D! N6 f
register_post_type('site-wide-notices', $args);
* c2 D$ t$ D" L5 K2 p+ V* T. k }- d$ b9 l# T- K" N0 N% I8 x: W
```
' I; ]0 P! p# n# K- A; M% u! G' ^
: X+ }- U% A% N5 e# x4 a 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。# d! j& d4 h- L
2 i0 C& i& I9 {
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:/ ~% ?7 C2 A# Q7 E
. Q: P* a# H3 L0 [& |
```
6 l- F$ p! l9 M3 o7 s add_action('add_meta_boxes', 'add_site_wide_notices_boxes');) y& O! f2 B5 A3 j
function add_site_wide_notices_boxes() {) w- Q) O. n- H# G& p
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');& ?3 N1 q' ]" U( z U( Q& k
}
$ J$ e& @& ]9 X( O
3 B6 @! [, b8 c3 u- h function notice_details_meta_box($post) {7 i4 D8 z+ t' d7 s w
wp_nonce_field(basename(__FILE__), 'notices_nonce');$ e; {1 B2 T a! {$ u
$notice_title = get_post_meta($post->ID, 'notice_title', true);6 ?; U. ?4 j8 h8 X% |5 t$ ^; P
$notice_content = get_post_meta($post->ID, 'notice_content', true);
. E, w |5 \$ ?# t Z2 Y8 V4 W: J ?>
- I/ c4 i. g: G7 U* F9 D8 G5 | <p>' i- D" g" r7 G/ l! ?
<label for="notice-title">Notice Title</label><br>4 Z$ h" s! N0 t) w. P# B" q) P
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
( p: w2 x4 f; m( y% j& y </p>8 A; T" F: G+ F2 B" i) q
<p>
5 j/ q' V( ^, a; I <label for="notice-content">Notice Content</label><br>4 k( r4 J( T/ y* A8 [
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>9 C [0 m/ U; s7 L
</p>$ t6 O0 U) K+ ]- p; G) R( x" B7 k
<?php/ S: w* O% ]' p2 _8 S
}. C+ k' H+ J5 r
' ]* {: ?/ v* X/ r add_action('save_post', 'save_site_wide_notice_meta_box');
# @0 I4 B5 k6 X: K6 O; |7 Q9 B function save_site_wide_notice_meta_box($post_id) {
& ~6 p6 p% Y8 l' T if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))! i8 w. d! E0 V# f
return;
9 J+ Q4 E1 j; d% z if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
: F5 |& ]' U Y5 A" d5 \+ ~, {# V return;$ T7 O$ T/ Q+ q$ [3 i( t. L' _" Z
* O: r6 I; l p4 [1 r if (isset($_POST['notice_title'])) {
8 Z" Q0 Y, q; y; \/ b* V update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
+ v1 s2 ?/ D7 }8 ]: q# Q ~& Y }0 W3 l+ d- j0 |/ r! b/ q; q
if (isset($_POST['notice_content'])) {( |# W& j% W3 Z/ T
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
# s2 O9 S* ~# O% U7 s }
9 h$ a8 m2 v( S, ]; V; e0 d o }! {0 L3 I. U t. r1 P( y3 K4 V
```
/ x1 Q2 O$ @6 p) J0 j) U Y% b) r3 X! Z5 w1 [. y% H
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。4 c1 G# {! e, Z0 {4 T: K6 Z
/ D3 h1 f6 t' \
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
1 ]7 X" E" n8 D! z& t! U/ r- J `6 y/ u* ]0 G% ]; K
```( o8 x1 r! ^' m) s" \
$args = array(
3 Z- T& Q0 c5 m" m 'post_type' => 'site-wide-notices'," j" E: q8 X1 A( F$ D, f
'posts_per_page' => 3,! _8 B6 r$ s7 }# \( F
'order' => 'DESC',
- v% E* u) c3 e# _( F, M- x 'orderby' => 'date'
2 I7 \& M( z+ V) Y );
8 C' I v0 }' e/ C $query = new WP_Query($args);- d* M" d6 M5 S$ U2 b2 u$ `0 y
if ($query->have_posts()) :
. o7 b' i# `2 {" O( o |/ o while ($query->have_posts()) : $query->the_post(); ?>
! k5 W m5 T* ^$ u" X2 {( P <div class="notice">0 | L- D2 r0 k' J
<h3><?php the_title(); ?></h3>
, e) [! f( h% g5 ]/ |* { <div class="notice-content"><?php the_content(); ?></div>0 x3 _+ N, p* I# q# y
</div>
, v! ]& G l! \5 {' @, { <?php endwhile;
" @8 v: ?+ s2 m& w9 J& Z3 r wp_reset_postdata();
/ _* O' F! K c$ O, G, u endif;& [, \; z" r; s5 F1 V; E' p$ W: w
```; m' c3 v) ^- a
7 H0 ?/ s& [3 o1 } 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|