|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?' {% Z& S/ V4 Y' G: y ^
% N, k. O9 ]- X3 o
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。# o' q. G3 a0 S' g# d- Q
. Y D! G/ o2 `" S5 J" P- L以下是创建自定义插件的步骤:6 }0 j" \4 ^9 [- e
0 z" F1 l. r3 u6 Z5 H, j, v9 O
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
3 @: h9 N( i; k& e/ P+ D$ t3 O& U$ _; f+ i
```
2 n0 d! z+ s( _8 L$ ^( M4 p <?php
# `( e; g7 e, w6 a6 x /*
+ V+ j8 Z! ~* r Plugin Name: Site Wide Notices Plugin) s* e; R) m" {- G
Description: Adds a new custom post type for site-wide notices.
. N! o4 R8 B# p* ~ Version: 1.0
1 z* t; y8 o, r# w9 X* c Author: Your Name6 n# J3 N2 i# h; b# K. ^
Author URI: http://example.com
/ l& ?$ {& ]: y3 _ */, `) _! T& m3 }' ^ {
$ L+ ~* W+ K! O8 W
// Add plugin code here...( k D5 V- h5 [" u4 Z6 f! F
```
2 Q0 s% g9 n7 d3 R" F7 k4 f7 b) p- X% h* s2 @6 n; v7 q/ Q5 R
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
; e7 u }/ I$ J5 F$ a0 H
( h Y, ~0 q# u+ v5 b( j3 ~$ f% S2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
& a9 A" G* K* Q$ [ K
' I' g& k. [2 F+ y3 Q6 H; r ```: x, e" O6 |' C' y% d$ N! n2 b
add_action('init', 'create_custom_post_type');
# F0 I# t3 w, x function create_custom_post_type() {
5 E, ]* Y/ e' \6 E- [9 \( X7 H* m $labels = array(
5 {: \9 X' H/ E% t4 ~) p- V 'name' => 'Site Wide Notices',& [" E" w$ u4 \- z5 ]
'singular_name' => 'Site Wide Notice', ]6 X" b, u/ @2 C" V
'add_new' => 'Add New',6 h2 U% E5 Q9 `+ k1 F7 z8 W. e
'add_new_item' => 'Add New Site Wide Notice',3 q f# W7 x5 H3 l
'edit_item' => 'Edit Site Wide Notice',3 V$ p1 @# `( c' C4 `
'new_item' => 'New Site Wide Notice',0 Z; s2 r5 D% r5 S, x; R0 b
'view_item' => 'View Site Wide Notice',% ` i# G2 U N# g. K9 N
'search_items' => 'Search Site Wide Notices',* X" A# s$ w* U. I% ~( j: _
'not_found' => 'No site-wide notices found',; u; A8 z( K7 \4 n4 c
'not_found_in_trash' => 'No site-wide notices found in trash'1 w4 n F- A2 U0 `/ X3 V+ M- a" u: j
);! Q' Z2 D3 g, \, J' v ?9 H6 c' c
. w: A6 Z% p% t! S' C& ~3 s $args = array(* K9 [" V. K1 D. @+ N
'labels' => $labels,1 {) Y5 l- v2 k# I$ l8 c
'public' => true,
\: y9 g& Y1 v0 S* Q 'has_archive' => true,
; F, \- U1 R! P5 F 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),) q2 J% d; }7 j( d/ n
'taxonomies' => array('category', 'post_tag'),
# J, C3 Z* R2 Z 'menu_icon' => 'dashicons-megaphone',
4 o) ]# w9 e H3 a+ f# x0 w 'menu_position' => 5,
! }' B9 G0 U9 K8 K" p 'rewrite' => array('slug' => 'site-wide-notices')
! I' ^, e9 x3 e$ v+ r. { );% K# \ \# o' m# g
' X0 d1 r) `( G* Z register_post_type('site-wide-notices', $args);4 A# _8 y9 w) O& k& j! P
}0 C- u* v* Y+ P! ^( {; |
```
' q* b* l( A R: ?8 V+ z: |: i# n5 D: x
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。1 i# s3 K/ z7 Z9 T, H8 x( S; q
! R* S5 y% }5 w* a3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
7 T. o+ C: q: Z2 \# \- I
5 N2 j: Z* u5 U. Z* W5 I& F2 u ```& Q4 J* R3 T) y d, T1 s5 P& e6 @
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');0 n- f- p4 y0 K+ p) n. Z0 W+ ~/ g: q
function add_site_wide_notices_boxes() {9 m8 H) C# ^$ v i
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
9 J; ^9 J+ F+ I1 O4 f% c }5 _$ Q8 p! T0 P7 T9 _: ?* a
6 x! l4 o/ l& z9 W0 V; K3 `. N
function notice_details_meta_box($post) {
2 C9 ]" K- c* e( b, T W: W2 I" f7 X, P wp_nonce_field(basename(__FILE__), 'notices_nonce');
" C' s. ^4 M8 F% O. E $notice_title = get_post_meta($post->ID, 'notice_title', true);, L2 b8 P2 K. d% `, S$ d9 [
$notice_content = get_post_meta($post->ID, 'notice_content', true);" S; S* `: L3 T. A& A4 _) g- q
?>
& a, C5 f8 c, P% g0 S1 o; k! o <p>
' G( b' ^$ G2 a1 K <label for="notice-title">Notice Title</label><br>
& J2 v2 n( J' w <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>"> W# M3 i9 l! }
</p>
% v4 _* G. J+ i <p>- W% h3 f! ^7 S# C
<label for="notice-content">Notice Content</label><br>
% U) F, v4 P) W <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
5 e l0 W! z/ K6 i </p>: t' l* O. _) p
<?php `0 }$ @8 B8 W% n
}
1 X4 H3 ]" h7 ^: q# K
+ u4 _2 @3 v2 ~0 k. V, o8 N D add_action('save_post', 'save_site_wide_notice_meta_box');, M. Y2 `$ ` y
function save_site_wide_notice_meta_box($post_id) {
3 x8 t1 C+ m% G. @. f7 a) `7 W if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
: J% s& @! O6 J/ d9 i return;' s. R0 y! ^3 g1 J& Y. i% H
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
; a& r: s/ X+ l; H. K: B return;
4 }( A: U7 m6 p: n S! w( u
& v' G0 S4 s1 ~- S/ X if (isset($_POST['notice_title'])) {
A" s3 o6 R( O9 m6 }$ R- A5 n update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));7 H5 q7 X6 q& G B# k
}
* G; H/ c9 r* d% w$ x, ] u7 C1 U0 J if (isset($_POST['notice_content'])) {
4 q# K0 n: w' t( o update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));% H$ O7 X8 i- M9 R0 R) z
}7 a/ V2 i9 `6 S
}% U( @; _$ `) {- t
```! P+ c$ ^6 m5 K7 p3 T! P6 k
1 ^1 n% _& l" o 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
3 u+ W5 n. d" @2 C$ P) \& V' H) w: g& l6 @6 u
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:: N2 x M4 f) r
9 J" ?) A& h2 }& f. n# T& w
```
- e' s& [" `! _ $args = array(
$ s: c& q2 X X: ~8 e 'post_type' => 'site-wide-notices',
/ E6 X4 D& y5 G8 h" X2 X6 q 'posts_per_page' => 3,# J, D! Z% M+ ]. k% d
'order' => 'DESC',! a. H w6 |- y0 p- ^7 L
'orderby' => 'date'
3 F; g8 n7 h1 N' m );
% r# o. q6 B0 O7 n I $query = new WP_Query($args);
& F* H- T3 w1 }& d; R if ($query->have_posts()) :
9 Q% W/ E* Z# A+ M% w while ($query->have_posts()) : $query->the_post(); ?>: i2 l: S5 E( ]7 F2 ^' F, J2 ^
<div class="notice">% X) K( L" b8 `0 N- {+ ?! [9 B4 X7 ^
<h3><?php the_title(); ?></h3> a) b# u' G6 g2 l4 `: A, S+ t
<div class="notice-content"><?php the_content(); ?></div>) f- d2 f% O2 g- g
</div>
) ~' }( E1 |, I' o <?php endwhile;
! P) F; l% a1 c" o wp_reset_postdata();5 W& b. F2 L- m" E$ B! k6 U
endif;
# x1 Q' \. E8 Y8 [, h7 y& Z ```
" s9 F# N. E4 R, s7 m& \0 M9 R
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|