|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?( {* T# O. Z$ m
0 ]0 C7 }. ?/ H' H
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
9 J9 A" t5 e/ u' L& C
0 E3 y7 |+ H- w! w1 ]7 o+ @! }! {以下是创建自定义插件的步骤:
( j* m8 n! y x+ i) ~+ S" j
W7 c1 Y) Z8 C2 f3 D& ~1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
# k2 R- z) x1 B& V& s( ^1 P9 P3 p: B
```
3 Z- R K$ P! n4 x7 X <?php/ h2 {; L- X- f4 C; A
/*
7 w; O, p3 m5 C# M, E3 V- | Plugin Name: Site Wide Notices Plugin! c$ N2 }3 ?/ V' H0 A- H
Description: Adds a new custom post type for site-wide notices.9 X# z" r7 v, j& m5 k- M
Version: 1.0$ N% R- @3 F6 s; b* Y! w3 J. Q# p- Y
Author: Your Name8 A3 N, R) a$ n. \- l* u% k- ?
Author URI: http://example.com
6 E: S" g1 J5 h& c4 w2 I */
6 v: f# O3 G4 G8 r3 A8 a" ~/ p* s/ ~
// Add plugin code here...! v# e; y" s/ f4 e, ~
```1 s5 ]/ b6 W0 J4 v v/ E$ \- _
* ~ P+ y9 ?; U; J1 n$ W
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。' `& W! h$ L/ s* t3 M* O
+ o3 d, s* E7 b
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
6 i9 v$ \ U# J8 Z, ~2 x; \. d s
```6 F0 H D# \" y" G4 e- W5 @! z
add_action('init', 'create_custom_post_type');
. L' @- T) w) {: I1 p function create_custom_post_type() {
! w; i" e1 {% ], h$ z5 {2 x $labels = array(* R6 w2 V* ]+ X5 S+ \3 R% q
'name' => 'Site Wide Notices',+ H1 q) \8 e8 {9 C) B$ s
'singular_name' => 'Site Wide Notice',7 x [* E N8 `' ]& Q
'add_new' => 'Add New',% E# m K. o H+ w- K$ R4 A+ l
'add_new_item' => 'Add New Site Wide Notice',
- j3 ]; u- b4 z, i/ a5 C+ x# M, M* [ 'edit_item' => 'Edit Site Wide Notice',! y7 X: h3 A7 V5 h
'new_item' => 'New Site Wide Notice',
8 w2 l& r" w5 k: \ 'view_item' => 'View Site Wide Notice',; J4 N+ C z, d1 |% A# F
'search_items' => 'Search Site Wide Notices',
- @: c, O; q: b i/ v 'not_found' => 'No site-wide notices found',: `& b7 R' Y5 Y0 K" t
'not_found_in_trash' => 'No site-wide notices found in trash'
* m6 z# J, \& y6 ` );( K1 G1 n' s H) @' T' S' z" D
5 R4 Z9 `3 X5 k n- s% t( ^+ A
$args = array(
D6 b y6 o# R6 q7 B 'labels' => $labels, r7 s. L* D4 f Q, s6 T
'public' => true,1 S: A; u7 P# k- l7 b3 c+ `
'has_archive' => true,
0 l J9 Q3 \6 M 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),* a) ?* z6 z S" u4 ?% b' H! l
'taxonomies' => array('category', 'post_tag'),
' X6 i; m3 I4 s! z 'menu_icon' => 'dashicons-megaphone',/ ~1 U9 w8 ~2 t" y4 {$ P
'menu_position' => 5,# p a3 F* N4 _/ R% L
'rewrite' => array('slug' => 'site-wide-notices'), q6 ^. H3 L+ J, @
);
) o1 h8 X) N( `) }) N2 i" q2 y5 r# O) K! W* h, a$ h( D) b9 I! J" v
register_post_type('site-wide-notices', $args);: V7 ~3 i0 \" v' o0 d
}/ \/ m! T9 T; [0 u& c/ Q( D" Q
```
) ?! \3 o# T) J M; I$ x
) h( N/ `5 j* B4 p9 J 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。; ^* L. L0 k7 ?0 a8 q
3 d$ a3 F9 W, w* t* T! K7 B3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
% P8 T9 K. |$ P& h/ }3 y5 v1 p! A4 }& b' s9 N. t E, `0 O
```( x$ c- f/ A7 P9 u1 O3 y
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');+ g8 w2 }6 }& N- H4 g: L5 d
function add_site_wide_notices_boxes() {
5 Z' m5 U& q8 Y7 H: B6 C0 N# B+ l6 V add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');+ U& \4 X3 g- I* `/ `4 k f5 h5 E
}
1 ] a& G5 S6 N: K- P: B$ c! a& v% b1 R
function notice_details_meta_box($post) {& o% ^% z/ G" ]) C; y
wp_nonce_field(basename(__FILE__), 'notices_nonce');5 |" i1 P4 `, Y6 ]2 p+ K! E
$notice_title = get_post_meta($post->ID, 'notice_title', true);
0 f. B! p; g B1 O $notice_content = get_post_meta($post->ID, 'notice_content', true);
/ X% N* p: F" o9 w$ T; H: g ?>
! \& E. @/ b7 C* s: k; H <p>
" o" E& A- \, F" H W( p1 r4 | <label for="notice-title">Notice Title</label><br>$ [9 J# U; z- K& g l8 x& a' p: l
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">1 \- k6 j3 [1 l& I
</p>
/ }, E) }; Q0 S* h E% r) [ <p>2 i# [2 M$ E$ L0 d" F3 z
<label for="notice-content">Notice Content</label><br>4 w2 [) L" D, |# g
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
7 g; M& m: U+ G </p>
# b7 s" x- b3 q4 S <?php
) C4 n0 |$ o y* @5 p1 r }
" Z f7 z0 [0 ?' `
: ^! M# f! h; k9 S( S add_action('save_post', 'save_site_wide_notice_meta_box');
0 X* P3 Q' V& _* M) ]: z function save_site_wide_notice_meta_box($post_id) {5 n6 c+ ~0 F# F k; w0 F
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
4 i. b0 U! N& w l8 e( |, v; z& }& k return;6 e6 {. E" `! H: F: \% j1 i, t* X, u
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
) R- j1 a4 `, @( _% A return;8 J: F9 \$ Z0 j0 J k7 X, O
% S& l9 G9 l' b if (isset($_POST['notice_title'])) {% s5 S$ d) _2 d) I2 ?( g
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title'])); P9 B8 T0 x8 O+ W5 I
}5 q9 U# W! f6 W& x$ Y
if (isset($_POST['notice_content'])) {8 h. {4 m# d2 J& `; y
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
8 X+ D) z0 \ Q" ~ }" N1 r1 O! A3 _6 L
}
" Z! x- L6 ~" V: b ```
% Q: @; l, m1 \5 T- M* r1 v3 l; A4 Q5 d8 U0 Y' v, i
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
' o7 m6 I% Y9 O- r, |% x1 H- I0 w/ E
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
8 O9 O% \) Y5 |+ E0 r) [. q4 N
r$ ]$ x; G1 c5 |# U ```
( w. ]) ?3 J5 z4 n8 j' V" t9 Y $args = array(
F! K5 ^; _! h& Z. P2 x 'post_type' => 'site-wide-notices',
1 J/ U" j# k8 V6 O1 ? 'posts_per_page' => 3,
9 D% P+ O9 \* ^ 'order' => 'DESC',
, N( b. L* F( E B 'orderby' => 'date'' ^! X* ~2 h4 y% n& a
);
S# V- D2 ?* |6 G$ d $query = new WP_Query($args);
, |; Q6 l! O/ \& ^: P, b$ G' J: s if ($query->have_posts()) :3 r( v1 A" k) F5 ~9 C2 E q
while ($query->have_posts()) : $query->the_post(); ?>* i# n8 e$ s" s% g9 W7 G% B* k
<div class="notice">! W$ R! W2 U* r
<h3><?php the_title(); ?></h3>: u" G, ^. c% _ m
<div class="notice-content"><?php the_content(); ?></div>
! R9 m, S q$ Z, ~ </div>5 _( s% [! D9 F
<?php endwhile;* y7 j* V3 |6 ^& S
wp_reset_postdata();7 N) W: x1 v, B" D
endif;
% ^# v* [9 c5 D3 Q: ~6 e2 { ```
8 d$ n# f" t+ O' O5 `. p+ n
4 E: @" j$ Y1 A R& Q' O 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|