|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
* w; g- `. P0 c$ z: V5 _6 q; ~; F8 z/ C+ Z! D; W! i9 r
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
5 m+ Z' r$ s# r: M" @( l0 A& P* Y: l0 M- g" \0 g& m( x
以下是创建自定义插件的步骤:
$ Z/ ]2 i" _/ \
3 J% s) D) t& r; [, \1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:# V' l% c0 D7 ^7 |; o" B' |
- q1 O o( T3 p( R ```; a1 m1 J$ C a( C K+ l
<?php
0 O. U! Y1 q/ L% A" e) X2 R6 d /*
& t, ?' i) D& v Plugin Name: Site Wide Notices Plugin5 b$ `) a) [5 n: F3 U+ u
Description: Adds a new custom post type for site-wide notices.
6 j# J1 F! i$ J( B2 D; v$ s l Version: 1.0
4 x& g9 z8 r% g3 [1 b" ` Y) t* z) J Author: Your Name
) ]4 U) x- v5 D) V g9 m Author URI: http://example.com
2 B" a* U9 g/ J6 z1 A4 d2 S/ u */# [( i: s9 \0 F* c" n; x
& z9 w! |1 v! u% Y! s // Add plugin code here...
; k/ k5 N7 @$ D: h% @6 Q ```' W, v5 K1 r ]! c- ~
- c% R' R4 s4 m* _ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
9 O% k) r! R( e F0 E% V
- z# ?3 Y! ~8 k2 {, a2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:" a# X+ o- s3 G$ w/ K
" b( J4 W. C3 e" L ```
, b' X, U" [; Z3 P5 d* O add_action('init', 'create_custom_post_type');- I f1 F- C6 D
function create_custom_post_type() {. ]" o, V" l2 ^3 Q* {2 Z
$labels = array(
, W) s( L6 w0 Q) s7 l 'name' => 'Site Wide Notices',
/ K# G7 b0 P: P3 q! l% i6 c 'singular_name' => 'Site Wide Notice',
# ~" s/ {0 k& R4 \1 i1 U" g 'add_new' => 'Add New',0 ]% q m- g, H+ W: j4 c& i: t* c h
'add_new_item' => 'Add New Site Wide Notice',& f' H) ?+ Y' j* j/ S
'edit_item' => 'Edit Site Wide Notice',! X1 b$ G9 k4 m* ^) j* D
'new_item' => 'New Site Wide Notice',
# |; P! }8 ^" f5 g2 I$ K( \ 'view_item' => 'View Site Wide Notice',
3 w% \2 g" N8 q) q( t 'search_items' => 'Search Site Wide Notices',
. h8 [) Q4 t) u5 k2 J 'not_found' => 'No site-wide notices found'," C' O( U, i! n
'not_found_in_trash' => 'No site-wide notices found in trash'# {# L* h9 g. p7 h( D3 y9 r6 s# R( ~
);# N# R/ Y! u- p3 q) L5 z
' {: [3 r7 ]7 y7 @ j) `
$args = array(
( e0 _- |; Q, y; ]- L 'labels' => $labels,2 p3 d' @: w- L, P5 _$ A5 G
'public' => true,
- ^/ Y* r* x6 Z3 N4 F9 ~ 'has_archive' => true,
% o: w; A! q8 m/ q& j3 c 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),0 {. a6 ]0 y4 Y, {" P
'taxonomies' => array('category', 'post_tag'),
9 z$ Q/ m# l* \ 'menu_icon' => 'dashicons-megaphone',9 j9 A( ^3 B K ~1 ^
'menu_position' => 5,
, Z5 O7 w v. D2 v* R6 X5 x 'rewrite' => array('slug' => 'site-wide-notices')
5 Z: X- i& K! O* w7 R, d );3 S: ?8 R( f ^& p: o, G
' D4 Q4 v1 `; G+ l- A% F4 j register_post_type('site-wide-notices', $args); o8 T: Z1 e+ r; L
}
) Q: |7 J' { W: L. n( F ```, i8 U$ ~" I; j( {$ M- B
7 w2 v) { d/ U7 Y& a- c1 o* P
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
7 G0 s$ e- T. |1 v) i8 G
$ C1 @' `% F( Z3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
; G7 P9 B! E6 d: E; b) F$ O" _2 S5 ^4 }7 K" X$ N7 v( x9 f2 u4 O
``` i7 ~: F6 [6 y8 E' v; u6 @
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
: t# q- p1 E8 W1 A2 z8 ? function add_site_wide_notices_boxes() {
4 z; b; D+ b) \- {) e5 k add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
- [+ J9 o/ @( q4 o+ k* c) { }
- s9 y& d, h( y$ \; i8 z
& }6 p: u+ W; t8 o/ {. c8 I- q5 U function notice_details_meta_box($post) {. t L* k6 L) Z5 f2 A4 U$ r
wp_nonce_field(basename(__FILE__), 'notices_nonce');3 L/ I7 a- a! R0 M5 Q1 H& z( n
$notice_title = get_post_meta($post->ID, 'notice_title', true);
1 p# l/ R" n- S4 X5 e M2 A+ V $notice_content = get_post_meta($post->ID, 'notice_content', true);/ C4 p1 c( v0 q6 m
?>
( S, w9 _- E+ d2 J" \ <p>: d; D5 [- w, e2 u5 o
<label for="notice-title">Notice Title</label><br>7 @$ T9 l" O# W
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">' i' V2 Q* K; A4 l5 t
</p>
% M' J# [" r# v. ?" }) v <p>
# n6 Z& s! I$ ]: w <label for="notice-content">Notice Content</label><br>
* x i" X& Q% w) L& ]! o1 |8 P) u, g <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>! Y# t* z9 C" l+ k5 P- E8 r
</p>$ O6 L5 R" Q. p k' _- h
<?php4 d$ r% z, x$ W) V# J) u
}4 F' N5 Q5 F) \% s" q7 w- w# E: a
h! _* F2 v- Y: u! U( |
add_action('save_post', 'save_site_wide_notice_meta_box');7 I( S6 F( c# y) H
function save_site_wide_notice_meta_box($post_id) {
$ s5 B& p5 `/ }* B. Z7 _ x, V if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
0 z: h" C- J$ T1 n return;& B6 ]+ h. n" F. l/ {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) n3 I. S; H1 r; B j
return;0 J% ]. j, i, l5 C {
' u- \* i8 U8 T2 d$ M) w if (isset($_POST['notice_title'])) {
1 i5 D, {8 B" M: l" ? update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));3 M4 ]$ ]; d* F* d/ Z9 z
}, E# b& V: H, ^" L; O& |
if (isset($_POST['notice_content'])) {8 \/ r6 a8 O+ O; V
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
" m, u9 n9 `9 o1 J }! b5 B% }: s& b) C V3 @; r* A
}" U' `9 m( T6 U1 ?1 n4 B( l
```6 X1 P2 z% T. Y! t2 _ `2 l |, ^
7 `9 |5 i7 i; n% y7 K8 s, A/ U 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
1 Q2 p, W) g/ ]' a8 h% f4 L4 p" J. D4 h" k) k6 b
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:1 X `6 P$ e T2 P ?9 q
' E1 e4 c2 F$ b( e$ D ```
2 J. D7 d' F. N1 j& n0 t7 N $args = array(# }& S$ y0 l/ u4 t9 g
'post_type' => 'site-wide-notices',
# O- p7 V& m0 l! o" F' r( P1 ~8 L) W0 @3 P 'posts_per_page' => 3,
: L/ o- I3 `' v5 A# B, ] 'order' => 'DESC',
' M* u+ T- d$ l4 T 'orderby' => 'date'# }2 |8 |; d" m" M2 h; I
);- k" A) z6 O9 A" `9 t
$query = new WP_Query($args);" i% \* L' ?7 l1 M6 Y
if ($query->have_posts()) :
& Q3 i* d' G3 @# G# H, ?. ^5 ^ while ($query->have_posts()) : $query->the_post(); ?>
* @% q) }1 X3 D% r* Z) O7 O7 c <div class="notice">( \+ U/ d+ \* R2 X9 _5 q9 G' N
<h3><?php the_title(); ?></h3>2 ^$ G, H# A) w% r
<div class="notice-content"><?php the_content(); ?></div>" B( ~3 x, @: j3 t0 ^
</div>
4 ^1 W$ H4 }4 b! A <?php endwhile;
3 }7 }' s5 J6 E- U& L wp_reset_postdata();9 n7 X) F, c) C
endif;! w! L) N( D3 n, O) q& m
```9 A- R/ m X3 }& E; S
T; i! Z2 T7 H. c6 M
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|