|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
. Y7 d7 e6 ^' n# }* }" J) t: K% ]1 ~( h# x. U. [/ Q
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
+ U9 u: B3 I, i- ?1 E0 n7 F0 X, \
# m3 x. A& D5 l. p8 E以下是创建自定义插件的步骤:) y# {$ I+ S9 ~5 F. l% t& U/ }
6 l4 y$ F7 j! C+ Z) z3 m1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
% c6 B+ \) m( H7 z3 o% Z8 \$ L
$ k3 ?) E' Z3 x2 d ```
6 p* t9 J9 v5 L. d* R% }% U' W <?php
- g6 q) u/ R/ h- {8 S /*
9 ]! ^8 K6 B: r7 x Plugin Name: Site Wide Notices Plugin; k" q* a! Q% D4 w
Description: Adds a new custom post type for site-wide notices.& _' ~( H6 {2 A( V
Version: 1.0! p7 S& s j* h: i# }& m
Author: Your Name5 E- l5 l) L8 ]& o
Author URI: http://example.com
B+ t( a! t7 ^: b' ]: O- B+ c */; F/ k4 d) w6 ?5 w9 w) [) p! {0 V
! y8 H! g$ n: q& R' S' z8 j# m // Add plugin code here...
* \7 m( g: `/ k: ?0 }) E ```
( w3 H, V, h! t1 o& {# \
: O, B! p% H) N+ R5 |% q0 q 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。( J3 y3 H* E5 @. X
# ~7 f+ @0 b5 T2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
7 q( j) ? d. F$ q
Z6 ]5 Z7 F# ~/ B: T, |5 e# h ```
0 o8 y& D) u- `% N add_action('init', 'create_custom_post_type'); N" \5 Y0 g1 [; ^6 E' v; a+ _- p
function create_custom_post_type() {: t& e1 U* O) k# m8 _
$labels = array(3 t8 d9 |! u) g( F5 n3 E' U( k, @
'name' => 'Site Wide Notices',
3 B# Y. d: O* [ 'singular_name' => 'Site Wide Notice',( l+ o" v. w& D3 e/ ~
'add_new' => 'Add New',
- ]3 x- e: u* w, q 'add_new_item' => 'Add New Site Wide Notice',! |5 i, v1 T, x0 k
'edit_item' => 'Edit Site Wide Notice',. {. Z T0 f6 a' o u
'new_item' => 'New Site Wide Notice',
* B2 F' ^* C7 E$ P# G& C' a 'view_item' => 'View Site Wide Notice',- ]" U( [. U R6 B: `5 Y
'search_items' => 'Search Site Wide Notices',
* c6 M1 U/ l/ y: R1 o 'not_found' => 'No site-wide notices found',
7 m- B3 q# v8 C! Y 'not_found_in_trash' => 'No site-wide notices found in trash'5 Z3 T7 T$ z i+ @, X' Z Q
);9 _) a9 s9 I( Y. Y
; O7 Z6 U) X7 i# \
$args = array(3 @% @3 M9 f# G
'labels' => $labels,+ I1 T# ]) O. X% a
'public' => true,
$ {5 u/ f- p# p a* [% R. n9 p+ u- A 'has_archive' => true,
' o" q+ u1 b1 e h9 ~4 p9 D* n 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
" o1 ^0 z- b- K 'taxonomies' => array('category', 'post_tag')," Z" {* L: _7 f9 V% a# w
'menu_icon' => 'dashicons-megaphone',
: g& r( v8 T. W& x7 h/ k* A% G 'menu_position' => 5,
' w# A3 ]" D+ ^: o. R1 _! v7 V 'rewrite' => array('slug' => 'site-wide-notices') {: c6 z. E6 s, }
);
0 v7 N& n; l. T7 O; e1 B/ H
4 [: _* `( ~# Q! H4 u8 |* Y register_post_type('site-wide-notices', $args);
1 ]9 [& ^" L/ n }
" i5 r9 h* U* C, p/ r. f ```" C) N6 M* x( X+ z. H s
$ o% F. [) T0 V" `" w% b 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。9 U) b- ?* L& Z3 a5 D# L- S
7 i; q: {# X* M5 F. y# q( C' P+ G3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:" d4 y' p4 t% I: H- x5 h
2 g% O6 a5 Q7 \: x
```$ t2 l9 z" g' u
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
, l7 M" s% ^) k* T0 G8 H function add_site_wide_notices_boxes() { [- X. B) t3 n$ S, ^9 E
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');8 W( c/ c1 `6 s4 y% k
}
0 E3 @% h% j: M6 ]. [: H' z8 ~: J5 N& v$ m
function notice_details_meta_box($post) {# |6 P: D5 W( t
wp_nonce_field(basename(__FILE__), 'notices_nonce');, |% W: A6 R9 o* h6 l" d
$notice_title = get_post_meta($post->ID, 'notice_title', true);
9 Z) s# d3 k& b6 l: C! J: d $notice_content = get_post_meta($post->ID, 'notice_content', true);. B' c; i& c0 v: J; E
?>
3 j+ V r6 a7 {7 [4 [+ K; k# e1 m <p>( V0 R$ L( L1 \: k5 f5 ~
<label for="notice-title">Notice Title</label><br>
. z j% Q' L+ s+ d9 P <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">. y- p6 M3 v% `# S6 r
</p>: L# B, z. H+ {+ s, k" M
<p>
9 a9 s3 H8 t# F: @ <label for="notice-content">Notice Content</label><br># h* i+ V- ?* u1 M
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
K; O7 n: }3 |& U7 C7 l8 [ </p>
& l9 b$ I0 U# X. c+ x, g/ [. U <?php
6 j& h3 w* S/ Y* ^6 J' Y }0 H+ v, X) u! n, h( n4 H& ^
1 C% [& E5 \9 o1 D" s7 U
add_action('save_post', 'save_site_wide_notice_meta_box');
% K! C2 H. k2 t, a4 b4 C. K function save_site_wide_notice_meta_box($post_id) {# n A0 m" G9 `7 G4 ?9 {2 j
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))), ~" V* l2 S/ ^$ i6 a0 B8 [: K& E
return;. U" A/ P' o2 ^$ x1 a* _, a4 v
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)2 [& h; K" c/ \' _9 n1 \+ l3 N8 \
return;
% U7 O& w+ }/ j# F2 n# D% I8 g0 g4 s* f2 O+ i
if (isset($_POST['notice_title'])) {
) H0 B: ^6 Y+ X, N$ H update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
! C j( J5 I: t+ O7 l. j0 j }
# ~7 P' g- K& j8 \" ~" t2 |5 l, H if (isset($_POST['notice_content'])) {, R, {4 G: y' L: G
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));# p: C& I; \* Z$ e4 w
}7 R9 a9 x l! C
}6 y! V3 c% ]* E* i3 t
```
, b$ B% K; \- u1 s4 v* i3 Q% H) X6 c# g
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
. F+ S$ ~) @$ T& ]' v/ a4 K" d0 }; V, d' a
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
1 w5 N7 J' \$ N* V2 T- r6 ~6 n. Y' f6 i1 n
```
5 v( M" z" g" s9 t# y4 K' r $args = array(+ Z; p* l5 [ D0 j* o
'post_type' => 'site-wide-notices',( M# D& s1 Y" M
'posts_per_page' => 3,( `/ i. \9 i- \: n/ L7 x) U; ?) v
'order' => 'DESC',
0 m2 v' a. `6 R0 U% Q 'orderby' => 'date'. Q+ o( \2 j0 e$ l5 M) Z8 z% e
);' j* r& V, Y+ D% f
$query = new WP_Query($args);
0 i" j& o1 a6 c; t7 E5 F if ($query->have_posts()) :% _5 s# q( c4 K# [5 V2 G
while ($query->have_posts()) : $query->the_post(); ?>6 C, T! z- o3 E' M: R" j
<div class="notice">/ ?9 |1 [0 D2 H7 w+ u+ r- x# _
<h3><?php the_title(); ?></h3>& w" j& C0 _8 Y
<div class="notice-content"><?php the_content(); ?></div>, m4 ?* G. T! M9 j! H
</div>
. M; C" s2 e# V4 v U& \: _& r <?php endwhile;8 m- D: h7 h8 B3 X7 w5 ^
wp_reset_postdata();
3 K) Q r: Y% Z9 S3 E! w endif;
) g+ @; f6 o2 `8 d+ I ```9 [6 B! K$ v6 X9 e
% w+ v, w/ Z! N4 g+ U O1 |4 q4 A
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|