|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
+ T) I& k4 Z6 P& Y
7 ]/ i. C* E* A' R5 G6 f& f0 N6 A如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
7 l& P! c3 ^1 F+ Q& V3 V, o+ f2 ?! i# l
以下是创建自定义插件的步骤:
5 Q/ Q1 ^$ u/ w& m2 d
2 V+ q; c, _: x8 Y3 T. l2 a6 M1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:9 w# y! P: O# _: c: D+ ~
5 v f5 ?- I8 {: n ```
9 s- X: F/ \8 _3 T! ? <?php
& Q ]$ w, u/ t |' S) q6 y /*
0 D4 ~5 n* j0 v; H. z Plugin Name: Site Wide Notices Plugin
7 |1 J9 A) [7 C2 L U% D0 a" N" g Description: Adds a new custom post type for site-wide notices.- E) M# v- Q9 E
Version: 1.0
4 I' H& {! X+ i3 [3 b) M1 U% | Author: Your Name9 e s6 ]. p- w, Q4 C
Author URI: http://example.com- O4 \- f' u! z) r/ G
*/
( \/ D! g8 P/ H" K" u
- S5 w; d- ]" d/ V // Add plugin code here...
. F2 q: ]' n, `1 L' D ```
9 t8 u Y U6 L7 P$ n
3 h2 Q) [9 }. M" m0 {) O 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。* J$ C- j5 V" P3 b9 _& D
; V2 l! B: \6 x7 u# y2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:; E4 G& N8 \9 c( {' e2 n$ ^( ]
% z5 V) N: Z( W4 M2 S2 Z
```
. g1 Q% U! C) @ ?' n. ] add_action('init', 'create_custom_post_type');
$ d) K9 h6 }6 v0 P- ^/ K8 H9 B. t function create_custom_post_type() {
1 ?. K; I' o# i, m& w* Z $labels = array(6 [1 }5 @0 h" K+ H
'name' => 'Site Wide Notices'," ~3 w& s- z9 F/ I& `
'singular_name' => 'Site Wide Notice',
( \; O! N* k* L: b: d 'add_new' => 'Add New',5 J/ `$ x+ F3 X
'add_new_item' => 'Add New Site Wide Notice',# a- P0 D- {7 ^; w9 y D+ t: u
'edit_item' => 'Edit Site Wide Notice',
$ j* [2 g* [$ @, p( Z. ~0 {) h 'new_item' => 'New Site Wide Notice',
" X7 i2 Z) R! M7 x3 ] 'view_item' => 'View Site Wide Notice',' z' ~& X( o; D1 _4 g, Z
'search_items' => 'Search Site Wide Notices',3 _! R$ a9 a7 Y9 k
'not_found' => 'No site-wide notices found',. h0 u6 k: g- C. s3 y& _# U; y
'not_found_in_trash' => 'No site-wide notices found in trash'
p4 G; k4 A; m7 F; U' L, \! L* x );
! Q7 |7 O0 z8 p' k$ D
7 Q* [6 s+ ~' z. D7 P $args = array(
/ p( {) R) ^5 g( J8 M 'labels' => $labels,
2 E7 t8 I0 I( e 'public' => true,; _! |$ M3 x: e; Z( t$ x! U; y% `
'has_archive' => true,
, A/ V. ~: w! a$ k% n6 P 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
}( N% L0 ~9 p9 C. F' N 'taxonomies' => array('category', 'post_tag'),
, n1 D$ x" n/ ], P9 R 'menu_icon' => 'dashicons-megaphone'," }+ j# i4 P0 S3 T0 A5 F2 S
'menu_position' => 5,
* L9 M$ |" ], _$ y 'rewrite' => array('slug' => 'site-wide-notices')% g! J4 u& H8 z* J- b4 y- Z& H1 b6 G
);
! ^9 Y( V- b. @" Q' ?1 L* } I- H* N1 x) r+ x! e+ b6 F" l8 B
register_post_type('site-wide-notices', $args);7 ^2 c" q V. ~% h, \5 [0 K; f$ f
}& I. C/ l$ n* Y% G6 z5 x7 x# G
``` C/ j9 P! J- a7 P
! }+ ?, ^* [4 g ~. T: b$ ^/ v8 f
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
: {) E! C" @5 {7 A1 V6 O0 L2 m/ @& m3 x/ V5 Z- E& |
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
. z& f3 \# m- Y1 W* Y2 |. N0 |: ~$ h t. T/ s7 b
```* [. D" x7 Y5 P# i& h4 k. R; \
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
: H; w+ Y" l! T. e function add_site_wide_notices_boxes() {
3 W7 E! S$ v# J4 _' y( U add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
' i3 j2 f# c/ J8 O. [- u/ t& W }1 w: H3 Z! |& s7 \
6 t/ i/ k0 p1 r7 p6 h b: c% o function notice_details_meta_box($post) {
" l5 H0 L* W" U wp_nonce_field(basename(__FILE__), 'notices_nonce');8 V. T- @: M# J
$notice_title = get_post_meta($post->ID, 'notice_title', true);2 L/ t5 T8 i% f1 e5 I9 Q+ T1 k9 L
$notice_content = get_post_meta($post->ID, 'notice_content', true);
1 g3 ^0 v3 a" d; z- J2 K/ @0 G ?>2 t$ L* U }9 D
<p>
( t& C. x+ n" e& G# `! W4 M <label for="notice-title">Notice Title</label><br>
5 A- x" K- @* x <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
( {2 \& ~7 Q4 n1 C </p>, Y* Y: c( f+ Q( }2 ~" C ~
<p>- Y6 n, H& j' z, j! P
<label for="notice-content">Notice Content</label><br>* V* Z6 r0 _+ R G; {5 q
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
% I. V' r( i! r </p>4 t7 Y5 T1 K9 \) l5 C
<?php
4 c) ?1 `( L! o1 o; l* m/ j) ` }% W" r! A( S& X2 [! g/ j6 S
l0 T, C3 F" F
add_action('save_post', 'save_site_wide_notice_meta_box');
" o) w) g* o% z6 l( K function save_site_wide_notice_meta_box($post_id) {4 }: K. D' H& a7 M4 e4 y- d
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))* T" A" Z8 X2 Q# z
return;/ c" u! l7 r5 L2 L. o# d
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)6 |% b( |" ?) p; A
return;0 V: ~6 N+ u8 e% N
9 r1 H% W/ [1 G
if (isset($_POST['notice_title'])) {
* l7 N) V. s5 n/ y update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
8 _ p6 _" @. c% W! R: w }. Q, `) Y% d2 ~, a/ K
if (isset($_POST['notice_content'])) {
" n& k4 p" D+ i x( b; g Y% c update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));! v& W) q, F, x7 r3 J4 p0 M
}
9 P8 F3 q4 G4 }( C }1 ^ N3 ]4 e3 B$ A5 f
```$ U4 q9 w4 |8 M+ E* G; ~! I: e
2 ], b$ e% l; A1 k5 r1 a! d
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
, q3 v) t( c6 ^' _6 n0 b# }! }8 X0 `& k5 x8 S. H" M) s) p
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:5 M. I) y3 e: P- R
! l) n; u7 A0 O( P5 z
```
! k( K" @7 H; f5 T $args = array( b3 k5 n* v6 s! O
'post_type' => 'site-wide-notices',$ k$ l' m; I4 o9 k# m e
'posts_per_page' => 3,+ G% c0 p& V* U3 R* j
'order' => 'DESC',
% R8 F+ ^6 i: n: P9 O" k 'orderby' => 'date'
8 Z5 T7 |1 M0 i* z: D# k% ~ );* K2 r! u+ s+ B( t3 d: m
$query = new WP_Query($args);
+ z) t, W- B+ L) M$ ?6 b$ g" O if ($query->have_posts()) :
1 q( e' ]$ W2 w% z$ T I& E! y while ($query->have_posts()) : $query->the_post(); ?>' _, s+ Z3 v; u
<div class="notice">
# F1 y; \7 }1 f- ~9 i <h3><?php the_title(); ?></h3>
: }% N# i4 c; R; _ <div class="notice-content"><?php the_content(); ?></div>& F' S1 i' t. r0 w
</div>) c+ q* \) K% J/ a
<?php endwhile;
; t4 Y( A0 o; ^; |% M. P# X0 W wp_reset_postdata();7 c9 @) O) k( I* | c0 N3 r3 D. @
endif;5 m* m( q9 m: ]5 Q9 u& F
``` z O; E+ S! s
/ d+ `9 N: n& {4 ~ 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|