|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?/ Q' x( c1 w% ?- n
r7 m) V# u' R1 P, x: q
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。4 M! C! {1 z+ O1 V" k
, I) z8 U! [. H/ r' ~以下是创建自定义插件的步骤:
# O7 F" I( O5 b/ P y5 C6 [4 L9 Q. Y1 ?0 c d3 ?9 T$ ~& m
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:4 r, l4 G% R6 }) B
/ T8 t* U, V& R2 c# i ```; j& g) E& \+ F" i4 W
<?php
3 c* s) U5 n3 g# _ C/ I- t7 s ` /*
' P' ~2 y. I) N Plugin Name: Site Wide Notices Plugin
- n1 o5 Z* b) D' b" d T Description: Adds a new custom post type for site-wide notices.6 _% s4 N8 [- r' \/ ~8 i
Version: 1.0
$ E* b# Z- T8 V( n6 a) d1 K Author: Your Name/ K, |$ J5 V, w( e
Author URI: http://example.com) O$ L% Z* i- O. M! |8 _
*/- b9 {+ D* p4 x, s7 `6 l
) K2 B! u; Y- B1 b3 g* r // Add plugin code here...& n; x4 ?) u, H9 P$ Q8 e8 M( X# B$ m& B
```) F8 ^* |' i8 X6 i5 M" C8 o9 b8 V
! _" {! ?9 O( P1 H& a; C
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
7 b2 ~6 C! M' U% g+ z( R& {) u' L4 |% a/ v* G
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
1 V8 w" {- I7 s; w% D& b5 |8 F( V! Z0 T7 P m3 [
```! H0 x& m$ b7 {/ V* e
add_action('init', 'create_custom_post_type');
' h- k0 I `0 e function create_custom_post_type() {
" f) A' p }0 g: U. p$ m, s $labels = array(" }& R8 s0 e9 K) Q
'name' => 'Site Wide Notices',$ J$ b" e5 f5 \) K f
'singular_name' => 'Site Wide Notice',- W1 m0 [( D+ E r" E
'add_new' => 'Add New',9 t% q' i5 A% a% P7 i. ?' k
'add_new_item' => 'Add New Site Wide Notice',
& L% l9 p- B# q! Y3 a* c 'edit_item' => 'Edit Site Wide Notice',/ Z0 [9 U/ D8 ], v+ i- O* u: F) e
'new_item' => 'New Site Wide Notice',) O: j0 V& K4 | I- ~( ^5 W; }1 q7 K
'view_item' => 'View Site Wide Notice',# r) `; e1 m! ]! |
'search_items' => 'Search Site Wide Notices',' [! {* P2 t( r
'not_found' => 'No site-wide notices found',
/ J7 H. q: ]4 ~: f/ p: @" I6 p 'not_found_in_trash' => 'No site-wide notices found in trash'
+ x n1 |4 x1 N+ }1 a );2 ^. _/ o4 e L: u( Y
1 \2 }4 ^. D" }9 z* @
$args = array(
; g; h1 ~# A+ c! F 'labels' => $labels,
, ~% o# i: E5 V, y$ v 'public' => true,( J# ~- E; r8 m! E G8 |
'has_archive' => true,
7 e4 K3 L: s( i+ E, O# D 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),& S" ]/ p0 J- ?) h9 U9 _2 h; x
'taxonomies' => array('category', 'post_tag'),1 H8 f( V. C) e9 a) c
'menu_icon' => 'dashicons-megaphone',
/ v! j( r1 m/ F3 g 'menu_position' => 5,) z, E4 E# t0 T% `
'rewrite' => array('slug' => 'site-wide-notices')) P# N; q2 k0 e" x* K7 b+ ^
); E1 }; {. q+ t8 V
1 i8 w, `. G9 z. b) e2 h
register_post_type('site-wide-notices', $args);. u0 j! C Q3 W5 S' f: T. q
}3 m( j! A1 T% a2 b
```
4 q" |. T5 {0 ]9 D0 I5 [% I; C
/ l1 X) E4 H$ }' q 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。- u% s8 p, P0 M/ V4 ?
( X: n) B* f; d
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
+ _' Y; ]- m! Y2 M% S' {3 r
- R* x' O. s0 V- z# J4 { ```$ S8 K& U( g2 a
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');& T4 R1 ~9 A: a: ^5 x3 }
function add_site_wide_notices_boxes() {5 C$ |# X7 r0 I Y3 P+ C, k$ _2 F2 B* u
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
& O/ w5 y' {( ^! r3 [' [ f }% o) e& K% i; c$ z+ A
1 i4 Y6 c( E* @$ k
function notice_details_meta_box($post) {
: ]5 `1 w( B+ G* i wp_nonce_field(basename(__FILE__), 'notices_nonce');
2 p( [/ F1 r! T; d4 l2 e6 B$ E) _* c $notice_title = get_post_meta($post->ID, 'notice_title', true); k% v! |1 F' Y+ R5 A
$notice_content = get_post_meta($post->ID, 'notice_content', true);9 E- ]8 m* t, E" w" z7 ^2 y' d
?>2 x& b$ B# ?! w. k* O' u
<p>) D; n. k' G* t& u
<label for="notice-title">Notice Title</label><br>: c' d$ Y. e7 F
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
1 d' G: X% M$ W4 ~5 l/ \. k1 L </p>
: v! s; h8 ?9 N2 v8 B2 r <p>
& N! ]5 _8 ^: S9 z <label for="notice-content">Notice Content</label><br>
7 K6 z+ B' \* x0 l <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
' \1 k1 n1 p$ G, Z7 X* E; Z9 A </p>
2 h, r* X7 _+ B2 g- s <?php7 j7 Q H0 ^+ p
}3 ]. y4 r+ O) T/ L3 I
4 l3 ^ A; B" H" o" C5 @ add_action('save_post', 'save_site_wide_notice_meta_box');
) O0 g& k- J% s8 j2 { d function save_site_wide_notice_meta_box($post_id) {
/ q. F! a# D) ^/ ? if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))+ c6 ^$ ^1 y) w$ O) S9 o% R% n
return;
+ q( i$ _. M; A if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)+ l! p( s3 [' D2 [8 W
return;4 g8 @; Z1 G: t5 C$ M2 R6 E% G
/ e% W0 G, y" ]9 N) [: Y6 [ if (isset($_POST['notice_title'])) {/ @" |" [6 h4 D. d# ~
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));. o, i; B5 U% \8 L Y2 ?* s. f
}
. J$ d4 t' b( Y if (isset($_POST['notice_content'])) {
: Y, T6 d$ r& T! J update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
6 f1 y R& K7 d- j: W* R5 T/ L7 ` Q }
/ Y+ x$ v' X5 ?( m' q }
- t8 X r# J, R ```
, _$ K! {8 V3 |. ~& P" Q+ P. J4 Y. e) L8 R e! P' Y G# ]
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
4 ?' A% D' q$ m- X
# T5 m+ M& X: h, ^- u# {: @4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:3 P3 {! x5 s- s8 K1 W: `4 R0 m
1 V& e6 j8 R- O; H
```
8 d* X3 z; |- P* y" y& [3 O $args = array(1 n ?! z# o$ {
'post_type' => 'site-wide-notices',
8 |) N; h+ Z# a# w$ I. t9 p/ v' P 'posts_per_page' => 3,/ X# |+ [5 G5 K! K
'order' => 'DESC',8 q% w6 A/ S$ `" t5 ]2 ^
'orderby' => 'date'% h! y, w1 s' z# i9 o7 N
);
% n4 N1 h+ t a $query = new WP_Query($args);1 J/ D$ h4 g8 V6 _% m3 W. K; l* q
if ($query->have_posts()) :# Z) U9 |- {5 O' m/ b. A* \
while ($query->have_posts()) : $query->the_post(); ?>
; j' }$ Q7 _; P: ~. ]; } <div class="notice">
! c; P- k/ ` x$ ^ <h3><?php the_title(); ?></h3>
- t' C7 n5 x$ }, B) Z' N; k <div class="notice-content"><?php the_content(); ?></div>* e% {2 V! e& B
</div>
" t# f$ x R; M- q5 k <?php endwhile; C* k3 D" ~( N; F
wp_reset_postdata();9 i' A7 w) j" u: ?" p
endif;, Y: w- ]7 z6 G1 M
```0 Z" y! `9 \: Y
3 H% k) e, K+ a5 g 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|