|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?' w* S: V; I8 V( X: p+ J; e) Y. b
' s3 D! {/ ^5 Z2 n% l4 e如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
6 U8 {8 c! q1 R5 Y, {& B; @ V: h, _! B0 e- x8 x& A
以下是创建自定义插件的步骤:
- Q6 {, ?: t& r7 f
H* p9 k5 C& ^* O; d1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
& b8 q& N/ e, d A$ I( R t; h( D+ O$ d6 a2 n
```; V! K h- Z& K
<?php; Q9 A7 m S3 K
/*0 `/ I+ N% w% ~/ |0 h
Plugin Name: Site Wide Notices Plugin
- x2 k/ g, I9 K/ b3 m1 n Description: Adds a new custom post type for site-wide notices.
& ]$ |. F2 W# ]9 H Version: 1.0
3 l; J% B2 Z8 r: E6 { b4 l Author: Your Name5 G0 Z# N4 L5 p" u v
Author URI: http://example.com4 ]7 g) z5 `& S2 ~- K
*/8 y8 W3 I$ ^! X0 f- T# Y V
1 z% `% s9 f0 z* p: z, W
// Add plugin code here...
, T& x2 M# L7 V- W4 u6 F% E ```
: X! f) D/ \1 Z
0 C d/ l) N, a# Y1 l/ U" q 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。0 [; k8 B2 m' r* b( O4 F) K
8 M6 a. {2 E- [& W2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
1 a* O o. j6 J6 n' o( m
5 R. Z: }) y8 D. E, ] ```6 d/ J# R. f6 p
add_action('init', 'create_custom_post_type');
+ N7 V7 |" p3 f% n$ p5 H+ h function create_custom_post_type() {
. H$ L7 F8 {( V# q( I& \ $labels = array(' j$ { n$ u4 v6 z) A. \7 F
'name' => 'Site Wide Notices',
! i% U( t' U* j 'singular_name' => 'Site Wide Notice',6 K' A5 D$ Z6 Z e2 _& t
'add_new' => 'Add New',
! P6 P! U% ~( n 'add_new_item' => 'Add New Site Wide Notice',: H2 |/ L# M5 t' g& {% K
'edit_item' => 'Edit Site Wide Notice',: m& M; f% I1 n8 q
'new_item' => 'New Site Wide Notice',
# s5 w) g6 Z+ `; m 'view_item' => 'View Site Wide Notice',
3 H( B4 e$ m5 B7 \ 'search_items' => 'Search Site Wide Notices',! W9 u1 y7 f- i, q
'not_found' => 'No site-wide notices found',
! g$ B6 x* v. R# C5 D7 o) ^ 'not_found_in_trash' => 'No site-wide notices found in trash'& J2 y9 |/ V( ^7 k7 e( a: ?
);5 V7 C# a; W1 v1 t, w
8 w& {1 d. I9 K% T1 w$ y% S: [6 ^ P $args = array(- o& f, g2 v& x0 f X
'labels' => $labels,# R" S N9 j. Q' b( I
'public' => true,
a: u( ]* [+ W) D8 W3 @* s2 N* Z 'has_archive' => true,! k/ k8 g! N7 J
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),6 X6 Z4 w& ]3 p8 e" g+ q# I/ F# j
'taxonomies' => array('category', 'post_tag'),& Z+ h) ~' p7 b+ N6 p
'menu_icon' => 'dashicons-megaphone',2 I( ]* i9 t; C) |8 b' \+ f* d
'menu_position' => 5,, ]- `% J* c# F
'rewrite' => array('slug' => 'site-wide-notices'). Q9 z5 P$ ~2 C
);
. m9 S! r/ O2 j7 ^
( Y. P/ R- N# }8 i5 g9 R& O w register_post_type('site-wide-notices', $args);
4 B" N1 ^5 }8 T0 ] }
" _- j5 U r# H) S# u9 |1 D6 c ```
8 Y$ g$ Z! J7 g; J3 G9 b& v
) i6 _4 u' `) o3 _5 p2 O 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。0 k, j2 A8 P: K" L
. b8 f* e. c$ O# C J. R3 h
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:7 d8 B6 i: j7 C% z: V, C
# t7 j& u4 q" B/ t* k) _ ```8 \/ H, ~4 D7 Z; d) `9 x
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');: s1 C; Y7 C( @! W; ~, ?1 j
function add_site_wide_notices_boxes() {
9 |+ p8 b5 _6 S9 L# e5 H3 ]2 C add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
. [) E4 M4 F- ]. p6 v }, {( D: ^" g/ w8 I' ^. s- }
0 w, q; m3 ~& _+ {) f
function notice_details_meta_box($post) {9 ~7 m F- j: E: g- P' V
wp_nonce_field(basename(__FILE__), 'notices_nonce');
" w8 M& _3 ~6 a* K0 w6 r7 F* d $notice_title = get_post_meta($post->ID, 'notice_title', true);
' d# y; D' O! R/ L6 {1 L $notice_content = get_post_meta($post->ID, 'notice_content', true);
0 z; b! v7 l7 `5 t5 j, ^# O+ L, ~6 B: ^ ?>0 N3 P; U9 j; g/ C' ]
<p>3 A3 |: P t4 c& w# Z4 r
<label for="notice-title">Notice Title</label><br>% O7 W/ |+ R0 e' T
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
0 e a- M7 G9 A! T </p>
5 |2 o% |- o* l6 v6 z7 o) ] <p>
5 N* {4 v8 S4 o2 A* g <label for="notice-content">Notice Content</label><br>
O0 w1 I4 Z8 X+ e$ ^; j- t <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
1 j+ V s( P$ q# B </p>
% S G! h; `5 @3 ]* Q4 S* G5 c1 F <?php7 ]$ H2 _ t% s' g _6 T
}- x( v, r/ z$ v0 O4 L. w3 p
$ ~3 j1 y; N3 s add_action('save_post', 'save_site_wide_notice_meta_box');6 [ m2 K( z) ^. M! D( D$ s
function save_site_wide_notice_meta_box($post_id) {
; g$ v6 |7 l. ~" S0 K6 Q& P if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
0 \6 Q+ ?$ a/ c, A return;7 w3 }- _+ F$ [ T7 @0 f' X
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)8 s4 m" D$ U; N) `
return; x, q/ c- P' H) l, v0 e
- u4 {% `5 t7 ^! c/ R2 S7 {! f
if (isset($_POST['notice_title'])) {
" \; ]& w: B* G; G L, ^3 i' I2 ~, ~ update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));" J, g0 O, ~4 P
}
! |# x7 g: ?& ?" V: K if (isset($_POST['notice_content'])) {% o6 w5 }# M: o0 v. s
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
5 `& v& }- G% K+ R1 O+ ~! U }
- N1 {; C: Q$ B" J* T }
* b5 n% ], A& ?( u# \ ```2 `7 V. g2 [5 ~& J
3 W5 u( X, Q6 c9 G1 I
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
/ {: j( _1 x5 o0 }' f' e# E+ Y8 q* L/ S- F& X; }8 E; m" n
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:4 m8 t- w g+ L' i8 d! `" ]9 F
+ f# F8 c! H% g" y5 R1 A1 U ```* A1 h+ M) l. G0 n% y7 Q R4 Q
$args = array(
) n8 B/ U8 v8 K 'post_type' => 'site-wide-notices',: d* a+ L5 q% l2 N3 d J# t( r
'posts_per_page' => 3,
* N+ F8 b( v& D3 Y @0 M0 L9 [ 'order' => 'DESC',2 O3 ?; I4 M8 T% i4 Y3 z
'orderby' => 'date'. `& j- j G9 P% V# ]# q
);
+ y; @) b, h% q $query = new WP_Query($args);2 M! A( [! F4 M
if ($query->have_posts()) :
9 c2 p4 |/ t& B, g% N, p9 C, \( ~ while ($query->have_posts()) : $query->the_post(); ?>+ K: h2 j' }( Y; |
<div class="notice">
/ L5 r: S1 |) C A/ t8 Z% G+ q1 j* C <h3><?php the_title(); ?></h3>+ e+ S+ [! h; a7 t2 @6 {; X* V
<div class="notice-content"><?php the_content(); ?></div>
3 B, K( ^3 z& g6 _. C" j; e </div>
# x& I$ g" U y V) K* l <?php endwhile;
, g% P: K, t9 K4 p' g! S wp_reset_postdata();8 O4 \9 U$ z6 l+ ?& f2 V1 _
endif;8 c7 E- S% Q) ~5 W
```# G9 N+ S+ E( L/ E+ k/ I
) f. _. H8 v: S' Q0 l 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|