|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
; H- d* F, a4 L9 T" |5 I7 P
) i- Z% h5 B, |. g如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。; k! z6 B2 h8 g/ [6 r" T
: e, r9 d2 I1 V) c+ `% }: M$ ~
以下是创建自定义插件的步骤:1 D2 y, i: s8 o
! Q* g" j- S' N- R, e0 P) d1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
0 r" F$ r2 R8 w" X! K
5 S6 O$ x; x) w7 A) {; s# } ```; |% }8 g- c% m
<?php; J! i" F/ }" d# m. L$ W* } b/ A
/*. N" S! N! O: b* o4 ]; Q
Plugin Name: Site Wide Notices Plugin
# {( v2 ?3 K( a Description: Adds a new custom post type for site-wide notices.
" N1 \) V& n' Q+ [# t% @- g1 T. }( @- ^ Version: 1.0
4 o8 I. ?, p9 E0 v( D( k Author: Your Name
2 ^% g# ~* O6 Y$ L4 Q Author URI: http://example.com
& f1 G2 q) P2 L' d+ z */3 C6 H/ i& B# H
; ~8 J. C& ?$ t2 }7 b // Add plugin code here...
1 ^% w: k2 d6 {6 g; v ```
) _7 ]# ~6 T8 V$ j
: r3 {8 D4 _# G# b5 T0 z 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
6 f. i$ c! ^) N6 B( X
+ O. M+ v8 T- P: Q3 D5 _4 q2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
, F2 E8 x. W( C+ n8 @
! b1 R( Y& b8 F: Z ```
6 T! p* u' p; \# X9 k3 Y add_action('init', 'create_custom_post_type');% L9 u8 I, i! W+ t9 @7 o2 \
function create_custom_post_type() {
$ z7 W+ t4 W: W $labels = array(7 l2 n- ?+ @0 C2 O4 V# v
'name' => 'Site Wide Notices',
$ d3 t5 r2 ~( x) O( }. ? 'singular_name' => 'Site Wide Notice',. c/ [8 U; i+ D5 L
'add_new' => 'Add New',
' N7 b- _" {. m; Q; L9 V 'add_new_item' => 'Add New Site Wide Notice',, S0 `. p9 i! n' U( ~! o0 L2 r
'edit_item' => 'Edit Site Wide Notice',
6 j& c+ ^- ^! p 'new_item' => 'New Site Wide Notice',
1 x$ u; B5 B1 U) i* v! o7 k 'view_item' => 'View Site Wide Notice',* _! k" z: r8 a* |, \" K9 b8 k
'search_items' => 'Search Site Wide Notices',& i% r# r8 \0 v+ k
'not_found' => 'No site-wide notices found',8 c. \1 C: u! O
'not_found_in_trash' => 'No site-wide notices found in trash', p7 h) }3 `- F+ i, t8 R% j% |) i- [5 p1 G
);
3 a3 I* t, V) t" k/ f& S# D, E& Z' h y3 ^# t
$args = array(8 h6 D/ s5 }" @8 I
'labels' => $labels,' s1 u4 M) X0 K' K0 j
'public' => true,
' k) D2 `% P+ J. D P6 E$ Z 'has_archive' => true,, P- I4 P) E B- r5 U
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
" G {8 e. E4 e% c. ^ 'taxonomies' => array('category', 'post_tag'),
5 x2 I- v* @; p( [ 'menu_icon' => 'dashicons-megaphone',
( v+ [6 h/ N5 y) B 'menu_position' => 5,+ v' i* f) m$ ~
'rewrite' => array('slug' => 'site-wide-notices')5 K3 X& P5 V8 M* P. F
);
: C. \1 x h) L, N: V1 W4 @( D
register_post_type('site-wide-notices', $args);
p5 q. z. M0 I: M ]5 ` }
, q+ |$ B8 a$ C ```8 R' j/ R6 g/ o8 N4 F# h/ N
% {! A+ v2 k g _$ ?6 h 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
! Z# w! c! z7 o. v! j+ m+ L2 l
( z7 ]& Q2 o/ c; i k D! R3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
% d1 v! B) Q$ g5 T4 D9 z/ Q, {8 D5 T4 i; z1 \$ P1 T. _0 _( g
```
5 F' b$ A) _9 \. g# L5 ^' c add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
! F I) v5 b: b( j- M function add_site_wide_notices_boxes() {; w; \/ J% @" H3 i: S: ^# D
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');5 O# u$ G7 {* p
}1 ~+ \" q' n7 C3 ~7 |+ `( D E
9 Q$ e* M* b L2 ^5 I6 e
function notice_details_meta_box($post) {# `: {( H& O2 \/ R3 P+ G
wp_nonce_field(basename(__FILE__), 'notices_nonce');
( v& g4 Y* J' \7 t. ~, v9 L $notice_title = get_post_meta($post->ID, 'notice_title', true);
7 Y; \: d# e5 K; z- h# \: h! F $notice_content = get_post_meta($post->ID, 'notice_content', true);+ B/ R- G! i4 }& c* ?
?>9 ?; d$ k ]; c5 [7 ~
<p>
9 [( X- r) W* s& L <label for="notice-title">Notice Title</label><br>
. P- ]" x' f3 K+ q' o <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">, k3 B3 o% S4 X0 F: W
</p>
) J0 b* q2 G# x: `, c) q <p>3 O$ u, ^3 { q. S9 k T
<label for="notice-content">Notice Content</label><br>3 w0 ?( h& C6 S8 N- o
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>1 ?0 ~3 ^4 `, W' I L8 E# x5 N
</p>, K) q. W: |. T6 C% V9 M: M
<?php
" S/ `( W7 c9 J$ S' O }
. u' A8 X6 p O$ M
8 r( d/ i% C& f add_action('save_post', 'save_site_wide_notice_meta_box');) }0 I. o9 M: j- s0 K j. Z" d
function save_site_wide_notice_meta_box($post_id) {
% X8 J( E6 M _ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
& a6 |$ w% ^6 ~" }5 E* y return;3 G, m: [- S) U* B) h* l
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
, O/ |5 Y7 y* M. L return;7 O4 [* q) K& {4 n# _* A
3 p7 k3 w% ]2 R( Q1 C" a
if (isset($_POST['notice_title'])) {
' J0 F$ Z- k: f: C' J! v, H update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
' c' e' l, |/ M7 w2 c }
" V# `# J, _, ~: c+ D, | l1 Y if (isset($_POST['notice_content'])) {
' U/ q5 o* e* s( O5 G# Z7 ` update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));1 ?" @! m! {6 M/ U! I6 ]$ G
}
; i. v6 O; a" o$ V8 x& q* l6 i1 _ }
& i' k+ a+ m& X5 u/ g9 k ```
2 M" l9 q: [% a8 G8 ^1 E2 a4 Y/ S" |- b
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。' Z+ K" X9 b% S4 o
7 X# S, e$ e9 F" F1 }( h& N& P0 X
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
4 @' L0 x- p7 t6 O/ f' U7 i. Q2 _) F" R, p9 X0 z7 J
```
: y6 I) v: Z7 o& j# d $args = array(
4 c Q k H5 @/ a5 n: _ a+ U 'post_type' => 'site-wide-notices'," l' G3 p9 f$ V* L8 l# `
'posts_per_page' => 3,( P" a" [$ V! O, U2 g
'order' => 'DESC',
# ^0 \4 \# u$ z" B0 D 'orderby' => 'date'0 I9 E, o' f, |: X% [; Z
);1 @, r- ]+ D2 Z9 J3 U
$query = new WP_Query($args);
t+ C& H) [' K: ^0 x: M+ l) ?- ] if ($query->have_posts()) : O" k: m, z5 J2 p
while ($query->have_posts()) : $query->the_post(); ?>
( f; B0 K9 b; l. ~7 S7 {) i, }. d' u <div class="notice">
, b% t/ X5 c0 S7 k5 a, T <h3><?php the_title(); ?></h3>/ O4 p5 J, w3 w3 G2 D2 N% e8 g
<div class="notice-content"><?php the_content(); ?></div>9 H, ]$ ]+ q. a4 r
</div>
: a" I) q4 v$ A0 M <?php endwhile;
. H G' N4 ^* b0 H" J7 v wp_reset_postdata();
1 n; Y+ I6 j* a- `7 w& O4 S7 t# W endif; u7 ^7 b" \( b% B; ?: k# s
```5 u2 a7 e- a) k9 [! |% Q: a1 Q
; Z: |' V( Z; c0 y8 f+ d& u% P* ` 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|