|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?9 ]3 y0 f# o4 E, e; o- g5 G
: ]" n! D% S5 J- y$ W" y {如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。5 S% q, |1 U% T5 {) ~- H
" s; Q! a- V$ [! i4 @以下是创建自定义插件的步骤:3 z2 I% ^3 l2 x! |; {" P$ p; f* B- s6 p
' V2 t5 z( S' d( ?1 G
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
' W/ f' a2 U0 D- C- U. v/ T* z8 @) c1 r
```; \: ]4 @1 b2 A- {$ n& ^$ O
<?php8 z6 r( b& Z5 I( w* B l* P, n
/*
; \1 ]! J9 f! Y0 O! Z2 | Plugin Name: Site Wide Notices Plugin
' h5 `; D. a' m* K& K Description: Adds a new custom post type for site-wide notices.
/ a# Q7 u) H' r8 b7 H0 Y% n Version: 1.06 N3 a, S" B! J; c0 A0 T4 _4 T% k
Author: Your Name5 h% \; V4 Y9 w0 O% U) m4 M
Author URI: http://example.com
2 [! T5 k% o7 P8 H */
@ v! \6 r0 y5 _7 X+ M
+ q8 j& `7 W# h, O2 m9 X |$ i& n // Add plugin code here... _ x/ l/ h4 o" Q, M- f$ v
```
% e: p. g- J: V; a) n+ z$ t/ T) K! G3 K3 |! {4 s! n
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
% p( y9 n) r ^7 F: p+ W- y
2 f4 U# _0 u4 \" x S2 e( A0 I2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
) S0 ?/ v8 f, X" B6 ^3 r1 X
% C( W& Y' ]% ~) M/ m ```. O4 }+ J9 t; V3 Y% ]
add_action('init', 'create_custom_post_type');
8 s$ ~, m+ S5 `4 j+ W$ g function create_custom_post_type() {
9 p5 x) q) M' K' b1 K' n# i* i $labels = array(* \) \2 j" E! t+ _
'name' => 'Site Wide Notices',5 p( ]1 a$ q+ v) u$ e y) z
'singular_name' => 'Site Wide Notice',
4 ?( x2 j9 {2 h. L5 E% y 'add_new' => 'Add New',
4 T$ L A0 @( Y2 ]8 d0 G 'add_new_item' => 'Add New Site Wide Notice',. Y' c# |- Y0 T% |2 M# w
'edit_item' => 'Edit Site Wide Notice',
& K8 W. u4 b2 A' a8 m4 s" I( w 'new_item' => 'New Site Wide Notice',/ p x' m, _- O3 \+ W
'view_item' => 'View Site Wide Notice',
0 O5 ~2 D8 O: h 'search_items' => 'Search Site Wide Notices',
3 N" h" q1 Z& @0 | r$ u) {' i 'not_found' => 'No site-wide notices found',7 M h, T. t2 l/ c8 R `: ~
'not_found_in_trash' => 'No site-wide notices found in trash'! a, s# Y K: j0 w$ E
);
# L5 M$ @ }( D5 R+ R4 q2 w$ o4 H" k% _/ L5 o- _
$args = array(; ? G, |- S5 z$ ]8 G6 k0 M' x
'labels' => $labels,9 X! _# H* O f! J4 p; e7 x% B
'public' => true,& X4 V# U! ~- u
'has_archive' => true,
5 I& A- y' Q$ i: M# n. a 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),/ x: ~' w0 @# M/ U# l# m! z5 Q
'taxonomies' => array('category', 'post_tag'),
0 C: R/ J+ `6 V9 q7 g" }9 g7 d 'menu_icon' => 'dashicons-megaphone',
9 E7 o4 p; i% l! b. `4 v 'menu_position' => 5,( r. k+ Q: i; k( m" S
'rewrite' => array('slug' => 'site-wide-notices')* I% g) _- P/ H3 ?
); A4 H; T9 |6 U7 X* r. h
0 e) r- c m7 P. O* {/ q# T register_post_type('site-wide-notices', $args);
J& R* w' b# X) L' O }$ p& y5 n, C' T$ ^. J
```
, t; v3 x& G" ?/ W
- `9 O# V7 U$ a% ~. g7 P" F. G 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。, @3 _ f/ P% x. C# b1 ]$ J, M8 N
- T+ E- ?2 Q3 ]* c3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
/ i P9 g! ? I k9 ] B$ F# Q, p2 k
```. y6 m2 D2 G8 t" Y3 s2 R, [2 q
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
1 c7 _* ^# H% e+ r8 s+ D function add_site_wide_notices_boxes() {
3 x+ h# M; s# C% H3 d3 } add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');. |; G/ k; ~- Z4 e* x) t( `4 D
}
7 O: o. e- J& ?5 U2 a7 r, L/ ?; M& z
function notice_details_meta_box($post) {
; B* L0 F$ ?9 P wp_nonce_field(basename(__FILE__), 'notices_nonce');2 U' R* ` h8 X8 Z0 H" e) x5 S5 B8 i
$notice_title = get_post_meta($post->ID, 'notice_title', true);. U* X3 ] y- q* O
$notice_content = get_post_meta($post->ID, 'notice_content', true);
3 E3 A0 d% j/ [ ?>
& |" D/ w0 Z# U% y3 o <p>
8 \! \+ D. j/ B# V) D7 K <label for="notice-title">Notice Title</label><br>
( `6 a- O1 a* M u <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">' E/ N% ] G. D4 J4 b* B
</p>
5 }% ^$ m! v E8 q <p>
' n; H8 |! S. y- \; i8 u9 n# q& M <label for="notice-content">Notice Content</label><br>
4 e; |3 N& J3 N# U6 r y <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
) n/ k3 b) \$ n- f </p>
% @6 r# a, q/ L% S$ y+ c8 M <?php; d) A* I* E1 U ~7 f( {
}
+ B# m, v s* W" k7 `# `- H, K2 j) @5 A
add_action('save_post', 'save_site_wide_notice_meta_box');+ R7 e Q3 ^; e' O* H4 Q4 v
function save_site_wide_notice_meta_box($post_id) {
6 Q- @$ |' ^) _# ]6 b" G- o if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))3 T. S2 b* g: T, | H8 Z
return;
0 d1 S, z" ^, I, U5 p if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
Z" s D' N+ R& ~- q' t return;
0 @7 h# h+ l( I3 [2 K; B7 x- Y" U
4 P9 O. I$ V- V* k) ]# Y if (isset($_POST['notice_title'])) {
5 L/ r% T6 Q% D- O d1 q update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));. |% ]; u6 v5 H A# w8 D
}+ t- @3 E$ s3 b. J# `- U
if (isset($_POST['notice_content'])) {. m) f3 C& S3 l
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
5 W6 Z; Y9 C7 q6 k }6 z" r' I1 D a3 d
}
6 r- [% ^% V/ d! r ```
* r5 @% L* f; i# b3 U9 K) C) h N U$ d1 a) T& a+ [; c( @# J
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
& v! `9 A! E( U- ~2 ]! U, E3 L0 z# x' M
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:6 H" ]) D! Z( l3 ^6 z Y8 ]
3 c9 C, Q) q# k k ```
u( E) g% z+ f+ k, E ~0 P/ T $args = array(
4 p! e2 c7 Y4 t; ]7 S& V1 n, Z/ I 'post_type' => 'site-wide-notices',
: f5 [! \* ]% h5 p8 S 'posts_per_page' => 3,8 Z! C, W' |) d" r/ O; H
'order' => 'DESC',) I/ ~8 ~" o& g
'orderby' => 'date'
* D# P4 q! f K6 ~9 P0 ^7 o+ _ );7 Q3 \7 l" Z: o* D; B, u- v0 V
$query = new WP_Query($args);
4 r5 c+ T5 c }% y if ($query->have_posts()) :
7 H. g& W$ K2 t$ B while ($query->have_posts()) : $query->the_post(); ?>
! X" _: _8 p# N5 {2 T <div class="notice">
' W5 H/ t6 d, O% ^/ V <h3><?php the_title(); ?></h3>) i( r" X+ F. b+ U
<div class="notice-content"><?php the_content(); ?></div>
7 ^3 V# ~' R7 B/ x8 K+ L </div>+ Z8 y: z3 q# X' O6 i. e' s
<?php endwhile;( p; ~/ H+ @8 b* c5 L" o5 S0 s
wp_reset_postdata();2 F% s1 v; O* j3 Z3 j) T/ k0 F& a1 l
endif;
4 J( x0 N, Y8 Q- b2 D9 ~ ```( n- w5 ?& T" m$ Z
7 d- R# G2 a1 ^4 h9 z$ P
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|