|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
4 T$ b2 u# J" H: }; X# R: B S& s6 r! y" W7 E2 n* `
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
4 V, K% b# S; J: {% r, d3 R- R# L
7 n$ L0 a4 ?! H5 U) c以下是创建自定义插件的步骤:$ T5 ?4 a8 u0 p" D. p
u q1 \" n* E$ x1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
1 I& Y- b5 y2 J4 V/ E1 J" c3 c: i) S! f( |& q
```5 y t' B% [4 a: ]* V/ k% f. H
<?php
& y0 w, S1 D! E2 g- d7 t /*0 ~1 c' \4 Q: [' A. B8 V) e! t
Plugin Name: Site Wide Notices Plugin
5 C' t% ^& l" k4 l Description: Adds a new custom post type for site-wide notices., x* `( `/ a% Z
Version: 1.0
C- J* ^) e4 T- u+ z3 B& q, D q Author: Your Name
. O) }/ Q# b; p3 h P0 ] Author URI: http://example.com' U3 D, \5 J- V
*/1 \" t5 z2 y7 ]8 Y [8 _
, y5 Z, v0 n6 e# G; x% O7 n // Add plugin code here...
! _/ [ S3 L- j+ M; j+ B ```
% e4 L3 ^/ w' \; l# P( W- K/ ?$ v8 _& g; L
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
9 |+ h' e- f5 q+ Z8 g! ?% d0 R7 ]( ^1 o7 P, k8 P/ y
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:/ F) D* n1 m; O. q/ T* a% d
7 E H7 S# c- f1 _: Z ```1 D: N6 \. w2 T
add_action('init', 'create_custom_post_type');' C, [& ~" O: m& }# X4 H& P, }8 R
function create_custom_post_type() {" g9 R1 d" e7 V" `; F! G$ m
$labels = array(4 p4 I/ j# p) O- A; \% F
'name' => 'Site Wide Notices',0 Q1 ^' G: t7 p
'singular_name' => 'Site Wide Notice',; a5 k, Q( W! M5 s5 m
'add_new' => 'Add New',
1 \( G) t; }4 T& W- U s, Z$ u+ Q* F6 S 'add_new_item' => 'Add New Site Wide Notice',
9 S* U2 c% x+ u4 z0 @3 Y 'edit_item' => 'Edit Site Wide Notice',
; V, B/ M0 Q# k( U# G 'new_item' => 'New Site Wide Notice',
- j* J4 F9 B( G E6 h' Q 'view_item' => 'View Site Wide Notice',
4 e$ C/ y8 O$ d- v) ` 'search_items' => 'Search Site Wide Notices',5 a Y ]! c% W3 Q6 O$ x/ b0 ]* J
'not_found' => 'No site-wide notices found',
[, j; r- B& J; O+ K, t6 H, m8 C 'not_found_in_trash' => 'No site-wide notices found in trash'
* R: t' }& | n );
- u. J& c! D) i. M8 K' @/ k* m( h5 D7 M( Z( X% ?. H
$args = array(
/ |$ }% N" V/ b5 K1 n4 v" @: b6 J 'labels' => $labels,- w+ \. ^: F- N e0 u: u8 k
'public' => true,$ p6 @9 z8 C/ q" r5 B" U T4 ?% j
'has_archive' => true,
, e. n3 D+ B9 B& x! e# h 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
: V- j& P. G) J# C0 A 'taxonomies' => array('category', 'post_tag'),; {6 P/ S. Z4 O: F
'menu_icon' => 'dashicons-megaphone',
( }8 A9 `8 ^1 D 'menu_position' => 5,. |% a9 D( L% W1 I, V# u) X
'rewrite' => array('slug' => 'site-wide-notices')
8 [6 ~6 d3 d: A/ _) e) W- K: u );
/ |9 i% K4 f" n" P) r- g; A ?7 y7 u2 Y
register_post_type('site-wide-notices', $args);6 P+ v3 `! v+ }$ m) h
}( L* N, d1 x& j0 ]7 O/ m; o2 X1 _0 c
```
. e" p% ^; b$ J. q1 J3 F
5 c1 I5 x8 M$ Y8 D. K" I0 P/ D8 R0 } 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。$ g1 X( [9 U* x" u
# A1 t: t( n: |" C! K$ v, s. M
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:% C! s( o% }5 \& M1 V2 l8 O
! A% |- i6 N; X' h6 ~) M6 k
```$ q8 ]& L" k+ Z" J1 L
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
* y1 m2 ^, @* _4 B- u function add_site_wide_notices_boxes() {# ~1 F% k% R7 Z# L% \
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');& g. m5 Y0 g/ ?/ z# g1 x
}
: b! J% X5 p+ @; u" K8 I% r q' T: t" Z* M4 M1 n
function notice_details_meta_box($post) {
2 \+ U" Y$ Q* }1 ? a0 F wp_nonce_field(basename(__FILE__), 'notices_nonce');
" ~, `* o+ h$ Y( { $notice_title = get_post_meta($post->ID, 'notice_title', true);
6 o6 w* [& S# y: S9 o0 v $notice_content = get_post_meta($post->ID, 'notice_content', true);
1 }9 K$ n, a: n3 l ?>
: {% ]' ^0 Z+ F <p>
6 \7 R: M2 M" Y" X+ j0 O, i, K <label for="notice-title">Notice Title</label><br>6 V$ _% x2 m7 C2 A
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">& T4 H+ N7 a- H2 s+ m4 N# L! o. [
</p>! O) V+ u# X3 g
<p>
& [5 D/ G) N+ Y2 B2 V" } <label for="notice-content">Notice Content</label><br>$ E& j$ u3 I) P' L O r
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>& H7 `9 i D: \! l* g2 [: [
</p>
. C6 c$ L: s6 {8 S <?php- d7 x$ f. J7 r& \4 w7 [% E+ b
}( Y0 n) {2 G, C) M: f- H _* ~
4 J9 l9 a. b. ?
add_action('save_post', 'save_site_wide_notice_meta_box');
0 r5 _) X5 K; T! r5 a6 _9 ` function save_site_wide_notice_meta_box($post_id) {
0 {) ]2 ^! X2 b% H+ Q3 ?- X- P if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
* w" [+ d& m: h+ {8 t1 c return;; X+ L" l& E" j; P
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
! ~5 S6 Z/ l. z& e return;
9 o; Y2 b+ R3 X( l l. q
9 I/ z0 p2 Y4 U+ l* m if (isset($_POST['notice_title'])) {
$ d* M) }7 f& E0 A1 Z* p update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));$ ]0 W( [3 l7 y( V
}
; J# x5 s$ b: I- G* Q0 e. g+ z" J if (isset($_POST['notice_content'])) {
- p5 G: h# k$ g9 E2 Y update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
! f0 J0 ^; \4 J" J }- T7 E) _6 r/ K7 h; k
}8 P# I' o6 f$ @& m e7 Y( ^1 E
```; X" L. e4 l( `7 `( g
2 e# Y0 x5 i" Q4 W. p. ? v 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。4 A9 v& x+ _1 x8 I
. T' e! n+ H2 [) y" N+ [. J
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
0 K5 r- }4 `, P% x! `- H. L( T7 l2 i! Y
```
+ h9 t4 h6 p" R $args = array(: P: J Z9 W4 ]' @0 m- t% \
'post_type' => 'site-wide-notices',
$ w" q' j6 |3 Y) K+ `3 O6 H 'posts_per_page' => 3,
) h& C c* g9 T/ t( C( E4 Z( C* | 'order' => 'DESC',
P3 S! k3 r! k' d6 S/ I 'orderby' => 'date': y3 n( W7 g: x- l0 \& s
);
! V; o7 m2 J. i8 V9 M $query = new WP_Query($args);" N; p' e# w) K
if ($query->have_posts()) :/ y; V& w) B! _8 v8 @4 _4 T
while ($query->have_posts()) : $query->the_post(); ?>
! u( \7 T4 `4 |! x. q <div class="notice">
' P0 b `/ m; m0 l& o; @6 w* `$ B9 n <h3><?php the_title(); ?></h3>
1 ^) C* G8 K+ `) t& H' ^* L, c <div class="notice-content"><?php the_content(); ?></div>9 {6 c( ]3 u5 s$ r5 C
</div>
( A4 ?8 q( B. }& w/ ]; x: T <?php endwhile;( s" ~) u! v* g8 Q
wp_reset_postdata();6 M: N4 _: p6 c- e/ k5 T
endif;
: X& @9 ~! a' P ```
$ R$ E. C |( [ e5 L5 y3 ]5 g Y: c1 j1 m* J3 L
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|