|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?2 P3 |# ^* p- v* b. X
6 D5 v* e4 c, r9 n3 C
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。' ?9 @+ V) z! H! ?
' Q$ W* f. f1 A- s& f4 e* A7 j
以下是创建自定义插件的步骤:
' I0 G E: `; }) K1 C' \* W1 w; e) u# Y) q' K5 U& F% d
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:5 G. ~/ Q9 N* E8 N6 f" W) u. V
) }( }, l. {. W ```: M7 _9 ~; d4 k1 c' n" R. B) }4 A& q
<?php
( T5 r! g8 o, `9 r /*7 U8 S& S# _0 J# Y
Plugin Name: Site Wide Notices Plugin6 a- V* M7 }5 R
Description: Adds a new custom post type for site-wide notices.$ U7 m* H% _. i: ~/ i# b: h
Version: 1.0
3 X$ Y+ F2 A7 h6 ?5 K$ v. ]: b Author: Your Name
3 u2 C, d X3 X. t% o( X6 F" u) B Author URI: http://example.com
* ~" y+ V. c/ k6 b */
: m/ L; E) j+ K7 [4 C. L y2 D9 S0 I- v/ u* x
// Add plugin code here...5 @$ J+ w, Z9 v9 u3 N% ]; W& G6 s
```
9 q9 C& j' ?- v- |
/ u( C! ~7 e3 X6 O X 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。* h. b3 _ I+ {0 z6 U9 m
; ?4 N7 G% B4 Q) X
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:9 s! Y o. [" |# w
. F+ y/ B# H0 N; i2 k ```% Q, l) k: T4 O' a) _/ g
add_action('init', 'create_custom_post_type');
; D3 E: S6 c% v4 \ function create_custom_post_type() {; N7 r; E* _! Z. U
$labels = array(
) [( ]; \$ |$ {7 N3 q 'name' => 'Site Wide Notices',
J- ]2 L+ X2 R1 n4 v( Y# }: L 'singular_name' => 'Site Wide Notice',! A& t2 K5 b# b
'add_new' => 'Add New',# R6 S) {. a$ d
'add_new_item' => 'Add New Site Wide Notice',! ]* Q9 ^" s- ?! k2 S; I) m
'edit_item' => 'Edit Site Wide Notice',8 k3 E1 d& D0 N: W8 u! C: g, z" I
'new_item' => 'New Site Wide Notice',
5 o8 @! N5 Z M% o+ m" l 'view_item' => 'View Site Wide Notice',8 W5 h% Z5 u9 Q) {! F$ |
'search_items' => 'Search Site Wide Notices',- ]6 _1 h8 c! E# }; g
'not_found' => 'No site-wide notices found',
* ~2 H8 M; {: a' i9 _6 Q 'not_found_in_trash' => 'No site-wide notices found in trash'; G7 m0 E, }) |9 @- ?" n9 t
);
. O1 s6 \6 Z9 c3 z; N7 \* V' v) N* G0 ~8 v4 t7 l* H
$args = array(1 z- F% t8 B6 o" F S
'labels' => $labels,
% P' \( J' G7 Q 'public' => true,
8 }" g, o$ ]$ U! { Q 'has_archive' => true,7 W) P" `4 w/ J) r. ?9 a
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),$ |! }4 e; Y0 A9 u3 S
'taxonomies' => array('category', 'post_tag'),+ T9 I# e7 Y7 y! Y. ?) I
'menu_icon' => 'dashicons-megaphone',
1 K: W6 O4 m1 n4 t/ B6 \ q) u 'menu_position' => 5,, y5 U2 j% F9 w) e) r
'rewrite' => array('slug' => 'site-wide-notices')
4 V2 {+ _% `, n" n! r );
L- m% }. \. N" w9 |, K1 w# c, D: r* G$ C! M
register_post_type('site-wide-notices', $args);6 n$ V! A w$ [* ?0 u3 A
}
6 C8 ~7 m8 P. C6 p ```
* ~ D' f2 @. Y) G: v0 G) v2 f6 L
3 Y& q' J/ S' X9 d, h 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
0 G3 Q/ r, {8 P" w/ |% S1 r% C% T( n
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:! `5 R. X7 b. R+ S8 s7 h
1 t- u! ]6 _" @1 X
```
/ ?9 _* U E; @6 ^! v add_action('add_meta_boxes', 'add_site_wide_notices_boxes');) ` f1 Q3 X4 q; }* f1 Z- j2 a* N% I
function add_site_wide_notices_boxes() {/ O: Q/ y7 J; x
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
9 y3 v' J5 f9 E9 b- K9 k }$ o$ ~/ c) H3 m9 g. y% ~
! f1 P. S5 t$ l$ J4 I
function notice_details_meta_box($post) {- E1 A& ?7 H, h+ @
wp_nonce_field(basename(__FILE__), 'notices_nonce');/ H% x/ l7 l8 P
$notice_title = get_post_meta($post->ID, 'notice_title', true);
W m. q7 w3 Y$ h# f& q! x $notice_content = get_post_meta($post->ID, 'notice_content', true);2 M% j U: F+ ?% M
?>
, S+ [; }; J8 { <p>
9 ~4 [* D; e+ h <label for="notice-title">Notice Title</label><br>
6 m k; T! c) H+ k8 s* j- t7 b, k <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
6 h' Z5 d5 j+ }/ D2 K$ q; V </p>
% X8 h L/ d* K0 m% `! p <p>- m! \8 }1 y2 c7 t% S( k
<label for="notice-content">Notice Content</label><br>
9 d: M7 j0 S5 @& x% `3 Y$ f# C <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>, _/ t9 Y9 U5 y
</p>3 v: N; y6 l, A$ i: J. f
<?php9 M. x. L D+ A- F* w* V3 N
}
$ J+ K& _* A0 ~0 `# F! y/ w
[, A4 W, j* q/ O add_action('save_post', 'save_site_wide_notice_meta_box');
! j% ?* f. _$ b$ o function save_site_wide_notice_meta_box($post_id) {
! c" F+ e$ e( j7 g if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
& q- Y2 [, U# i! Z, j& j return;& b$ k& K8 w/ V5 ?- u
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
/ H1 H, A: E6 u- V8 n% g return;
) w# |7 G& N2 L, ? k6 h( n2 _. R+ H% x
if (isset($_POST['notice_title'])) {
. f: T0 f7 C% L: ]; {4 z8 K update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
$ f9 K# ^% Q; n4 Y7 z4 O+ C }8 U4 q2 q& Y$ i6 o( F$ T
if (isset($_POST['notice_content'])) {
6 q" _5 m4 a: G1 p0 |8 Q7 O' j, C1 ] update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));" t9 x/ \& z7 H* R( {$ c: o3 r, U* w
}/ s ~8 b4 {; V. E6 l1 t8 ~/ s; j
}- ?. A; G C( ^6 c( ?2 ?
```
8 C# h) [7 Q4 O. n
d1 Q* T( A3 T9 A0 q% l 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
+ M4 s- k$ X; |0 N; n* `/ g7 |# a5 E1 S: m3 d
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
2 P6 T& h- z+ [ `3 F; _! I$ W* o L3 `+ j7 J- O
```
% d9 X" f% a. U $args = array(2 y S1 k5 D& c: H. W+ R
'post_type' => 'site-wide-notices',
7 I9 ]- u" c+ O9 v 'posts_per_page' => 3,
/ t3 E% P6 k5 u9 u 'order' => 'DESC',6 @, }1 N) v5 v6 B
'orderby' => 'date'
3 a. F( O$ N7 P1 C$ w. ?3 U );
, E9 E4 g/ B; z% u/ @ $query = new WP_Query($args);! e& Z% m9 z# Z
if ($query->have_posts()) :8 Q+ G L: v) h3 [5 ^+ N. t( n0 F
while ($query->have_posts()) : $query->the_post(); ?>
" a- T/ G. ?8 ^0 _% J, K |- ?: r+ A <div class="notice">
9 a+ l/ N( `! [# O <h3><?php the_title(); ?></h3>8 r* L0 D' J1 g' g: T: e% y
<div class="notice-content"><?php the_content(); ?></div>
i y `; n5 B </div>
5 g% v, E* n/ `5 M <?php endwhile;' J1 r2 g, y$ z# n& p" y4 L" X/ b2 p
wp_reset_postdata();
9 l2 ` A# v! P0 A% A endif;
/ J$ `4 j1 ]! ^' [ ```
$ f( E( R; {, I
# ] L& x9 a( _! \$ I 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|