|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?0 b+ W; d) B2 V
1 X" R' \1 H3 c3 ^如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
! j! u3 v6 k; u& v& f# E |
; T$ O4 k1 k. h/ P9 l$ ^ L以下是创建自定义插件的步骤:
$ N$ n% h. J. S. m# `% f9 i& I6 G+ }4 ?# i8 d/ ?* g
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
4 j8 e& z' L3 ~. T" X
; u ?' U" G- M* S7 z% N& b3 h8 M3 R4 r ```
0 u1 J9 s6 g3 r, L, g: F4 W <?php
8 I! l# @8 I; n# M /*
/ L; G) r# [! h. [; q. x+ H+ ~ Plugin Name: Site Wide Notices Plugin
5 E1 m# E& k& B) h Description: Adds a new custom post type for site-wide notices. x! O( I! S7 m z
Version: 1.05 d$ Y* D+ r- |$ P
Author: Your Name
7 [) H& ^6 ?1 a! F Author URI: http://example.com+ K6 S( N+ i4 R* C/ B7 o
*/
, k+ ^/ K5 e' K ^* M3 ?# p5 K+ _" j6 s$ J6 D, Z
// Add plugin code here...
8 j4 H2 _* W' D7 D( ~ ```
0 [: ?) x+ j& {1 A. \* B1 C
3 G- L. `( Y! A% H1 z1 n& _/ b 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
0 m0 h. I y' E" b# J. W; c8 F6 l# X
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:1 m, K9 Q. W+ [5 n" r& t
2 F( z' V5 W9 L0 }; w, Z: \7 h
```$ l! c9 G3 Z3 K% I9 n/ ~* H
add_action('init', 'create_custom_post_type');
) ~5 K$ }. e0 W2 r function create_custom_post_type() {3 f6 ^8 t. p7 E' t/ V% C0 Y
$labels = array(
~: S5 g2 A; j7 y: ^* w3 t 'name' => 'Site Wide Notices',
9 \, f% r1 Z% E# o6 e. [) @0 P: }" R8 B: Y 'singular_name' => 'Site Wide Notice',
! d( j# u- \) |0 v/ j 'add_new' => 'Add New',8 g8 G/ N% Y- l
'add_new_item' => 'Add New Site Wide Notice',
$ ?1 q- y: S' `8 s& b3 E2 ] 'edit_item' => 'Edit Site Wide Notice',, ^" y$ S1 j' ]& @' c
'new_item' => 'New Site Wide Notice',7 b) g9 k" [3 e' X' d) N/ a
'view_item' => 'View Site Wide Notice',# z, O; e+ e h: o. j/ ~) f
'search_items' => 'Search Site Wide Notices',, ^; e3 V T- S" o9 }* I, ^( T l
'not_found' => 'No site-wide notices found',
- E, G3 ? Q/ U6 N$ W; K$ b- M/ D 'not_found_in_trash' => 'No site-wide notices found in trash'
. c+ T& e4 c1 c8 ^ J6 i );
2 b0 ~5 d1 S1 h1 L8 z/ L
( o7 z, h) |- I) u. X! Z4 V $args = array(
/ i# ?( y, g7 a# S9 h2 G 'labels' => $labels,, S: ?7 t- c; p7 `% n( x; H% S+ I1 c7 O
'public' => true,
) H$ e" n/ v9 z4 Z 'has_archive' => true,4 U7 S3 Z( c+ O- ?5 K4 H( ^; ~
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),: P% w4 S( ?9 w/ N# z8 c
'taxonomies' => array('category', 'post_tag'),: R, ]1 s3 u3 z/ \1 X9 o
'menu_icon' => 'dashicons-megaphone',9 z: B$ d% \3 I6 E- U) L: U
'menu_position' => 5,
. H$ @9 M; q: P6 C 'rewrite' => array('slug' => 'site-wide-notices')3 o) _( [( k- W: }3 U
);- ^2 x2 ~: }8 P( `4 r
! e z! m5 N- R+ z- M) |1 } P3 [ register_post_type('site-wide-notices', $args);
0 C7 b" l/ h( b6 P W }
7 I$ O" q$ [6 D: h! ] ```# f- q; [* `1 [
" r5 C4 W d$ P! @% u: `) t2 E7 [1 i 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
# r" C6 L( P2 O, _5 [( W4 k. h" E
. \. j- k# W$ m) m* l+ n) K3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:" Z, G: q4 I" e* G* n6 J; \
6 A9 q3 N$ `) j9 f( A5 M
```4 B d3 Q( N! z9 \0 q
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
3 ~$ `8 O/ Q* s/ x function add_site_wide_notices_boxes() {3 ^9 q" U% q) F" A: k5 b6 J% j
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');5 f% W4 I# |% n i5 F, e& ^: H: O
}
1 \( w2 U- m- G D$ a1 m
" G4 T& q- o! Z- i function notice_details_meta_box($post) {
) y! r f# G* O5 [ wp_nonce_field(basename(__FILE__), 'notices_nonce');$ @' z* H p9 I/ P- A# t! e" f; P
$notice_title = get_post_meta($post->ID, 'notice_title', true);& ?( ]. X9 k; b( ?" H1 {/ E
$notice_content = get_post_meta($post->ID, 'notice_content', true);( u. `; f0 A! U9 e6 @
?>/ o3 {' v9 h2 k! [
<p>; W* ~! p+ j7 D7 _, I0 {
<label for="notice-title">Notice Title</label><br>, P2 p# F7 d1 a# z, y
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">$ [* @( X0 S7 ^2 p4 `
</p>: r" B K0 J3 Y; {* X1 l
<p>
* h4 z8 h& b$ b3 h) ^( `9 ]* q- b <label for="notice-content">Notice Content</label><br>' Y. \. E% c2 Z. y0 a
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
2 x5 ]# ~, t3 T7 R </p>
; M! M- e/ b1 B8 g1 Z8 G' i9 j+ y. d <?php
8 w6 j" Y* a4 s/ W. ? }! `; f1 _/ ^1 G2 u! {
1 v- m G j# E/ B% j# h
add_action('save_post', 'save_site_wide_notice_meta_box');$ s9 z- y2 O6 s5 X/ u
function save_site_wide_notice_meta_box($post_id) {
( r4 v' o, n+ C& {8 ` if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))6 u( r: U) N" O
return;
t- y( Q& n8 u if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)6 v1 I$ s1 e( j( a% L, F: a
return;
& Z; C2 ]- L: I4 i; v/ N3 r0 P
2 b2 k+ e9 {8 c9 {6 R if (isset($_POST['notice_title'])) {# m8 d" Z! g6 H& J, H5 f" c6 E
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
6 h* W5 d% D! O; D* G }2 q! W2 g0 o* T2 R
if (isset($_POST['notice_content'])) {, M/ U/ @ m* y, T' n$ Y; t" V
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
& Q9 V; q1 L% i3 d( c( p# v- n+ S }
; Q& O) J% L7 M& s( Q3 }9 [0 N" k }
% Q1 C- U) H& E5 W& s3 G ```
3 I+ X9 a: X$ I4 C' l( {, s. y: H/ ?0 A: F! {0 U
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
$ d: R3 ?9 a7 E% u) l* U% w8 R3 i. \
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
. P/ v# e# |" n9 l8 }" |6 g8 C9 y5 t
. X) [! _& C s. s! J; C3 r3 W+ W ```( A: Z- x. m- Z
$args = array(9 {8 M1 E5 `/ N* R
'post_type' => 'site-wide-notices',! R+ c: L, s3 O; U7 k& [
'posts_per_page' => 3,
, D! \/ @0 I& R4 U4 Y) d, [4 n 'order' => 'DESC',. O% Y4 r3 Z. F7 y; V9 p
'orderby' => 'date'# s2 {$ D4 I7 I% R- ^4 K7 j* y- }
);
/ g5 b+ {* ?& e9 s' N0 ? $query = new WP_Query($args);
8 Y% n; z! l# p: s3 P if ($query->have_posts()) :
3 D% O6 s* t% c while ($query->have_posts()) : $query->the_post(); ?>
) @) q, _0 ]% W9 ~, Z <div class="notice">7 G& u3 F9 s4 D- N9 C- w/ Y
<h3><?php the_title(); ?></h3>
( B5 x y/ V0 Z7 W! Z% \8 e <div class="notice-content"><?php the_content(); ?></div>1 X4 C/ R) x9 a8 O. L. P
</div>
2 |9 c3 V2 X* g1 K <?php endwhile;
7 y. c0 N5 x: B' e5 J wp_reset_postdata();1 G# ~; P, I# U3 r3 t; @) c A
endif;
- x; R2 v; h5 c5 t ```
* O5 W1 d5 r$ I- q4 C* L- W
* d! G0 K t% y6 I b1 ?/ \ 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|