|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
5 o. G! D `+ h
2 h1 ?: t6 e4 R; G1 t9 e( ]如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。2 {0 Y* D- r) q8 f6 P
5 ?2 v8 [5 f2 }' j- ]5 W6 g以下是创建自定义插件的步骤:
}" d9 o H' m/ [' H! P% O4 k- c/ @3 i- n7 I3 J. W$ c4 D. d& k
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
$ u7 v" e2 i0 w0 Q! L" D! I* y1 E- P, |, ]7 R
```0 \ m0 ~+ l- f$ p# v7 v5 p
<?php, |; C& \5 K' F0 W% X
/*. A+ {, m2 g: i
Plugin Name: Site Wide Notices Plugin* \- I! Y3 w! Q
Description: Adds a new custom post type for site-wide notices.8 I8 e1 u3 Y4 n
Version: 1.0, \# B/ ~1 c3 \2 T: \& g+ m2 W
Author: Your Name
: _7 E( e1 c; F Author URI: http://example.com
6 L0 U) f* T0 u3 {2 G- t7 Y */' A1 W3 i9 K- l( N
+ B8 M0 N1 k/ J$ P0 R; ]
// Add plugin code here...
+ A/ t" F8 Y5 f- I [ ]9 l- D ```
8 S; T) ?3 Q& @+ ~% Y* r
h. B8 W$ r$ ?$ t0 C! \$ C 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。8 R& N$ Q% H! q# e
, u( U( m* [% K$ {. b! u Y- U/ B2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
. P, ^6 {: J K7 Z/ k. p% P, s. p
: H; {( R' @; @% a ```; u; U8 J9 i" h3 q! [' E; S$ k
add_action('init', 'create_custom_post_type');
" }' v$ T/ v0 j$ I function create_custom_post_type() {8 }+ y: j% k4 h3 ?' w& B& P
$labels = array(' i9 r M5 ~6 }: e% {1 K$ I
'name' => 'Site Wide Notices',
, \( s8 B' q+ G1 C# P- M& t 'singular_name' => 'Site Wide Notice',
\5 W! A- |* `; U' d8 h2 p8 L, h 'add_new' => 'Add New',
. h. K' u W2 Z0 w9 }% Q0 E 'add_new_item' => 'Add New Site Wide Notice',
% i: k8 Q7 g' T2 Y, B' } F 'edit_item' => 'Edit Site Wide Notice',
) [' Z# [+ Z9 ~/ K* w 'new_item' => 'New Site Wide Notice',
" B: C* l' U0 a/ ~ 'view_item' => 'View Site Wide Notice', X/ r7 h0 Z+ u" j4 V
'search_items' => 'Search Site Wide Notices',1 Z: V6 _( w4 f! a* `
'not_found' => 'No site-wide notices found',
a, \( f% d3 F; _2 v* W Q% i1 h 'not_found_in_trash' => 'No site-wide notices found in trash'
% @7 H* K* a' u4 Z3 ~5 t# o8 Z );% ]% _! E4 V5 l
8 d& v7 T4 V8 n& `& v
$args = array(
7 ^( `+ Y2 f% F9 w 'labels' => $labels,
# |7 @' `$ @* F4 W4 C* z {9 Q1 V 'public' => true,# d0 r7 C. P, y2 h P8 I
'has_archive' => true,& @2 Y' J6 @4 k% m. C! W4 G& J/ v! }3 a
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),) j& P4 m+ D3 Q% j; I$ ^
'taxonomies' => array('category', 'post_tag'),
8 ~* h. ^6 z' i5 C6 g2 Z 'menu_icon' => 'dashicons-megaphone',- E: X. X8 z: C/ V0 D
'menu_position' => 5,! y: p6 {- q! a6 f
'rewrite' => array('slug' => 'site-wide-notices')0 N5 u. D c' w/ M) }0 O
);
& F( P( {" n- s) ]( W+ T& k
) C/ ^1 F5 n0 h& s register_post_type('site-wide-notices', $args);
& I" M2 @" y9 ]1 j, k }
7 q8 `$ y: O% G8 c3 N) S, M4 U ```
! E( q4 D M9 h/ S4 p* r& L# Y- S. k6 R: ]4 q5 N2 X% {8 _- k, F3 d
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。( C$ ~0 O+ ~$ f
7 G! x+ j8 s$ a( {& U7 i
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:* T& z& |7 u2 M& p
$ p: z1 N ?+ N& l# C: \, s
```( A% d& V3 u. j' C. r: F% M
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');+ u/ O$ n. K' v8 _' T1 W. j; r6 c
function add_site_wide_notices_boxes() {
: Q1 o' O5 a& X' G) B add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
# M/ ?& P9 {0 [ }
- b/ z9 d4 |* K [' d/ V j3 z4 }9 r( Y0 c7 ]. k" l5 ]! i- `
function notice_details_meta_box($post) {
! F. b& L% J6 ?# w& i wp_nonce_field(basename(__FILE__), 'notices_nonce');
% {( @( @* n- q7 d3 |- x, I $notice_title = get_post_meta($post->ID, 'notice_title', true);
0 t) K7 X5 X4 s6 L ? P* d) p $notice_content = get_post_meta($post->ID, 'notice_content', true);
# I$ \- b$ k F8 {, k ?>
4 P; o& S2 _! [+ n3 c4 `$ ]# r, o <p>
6 y/ x9 u+ y3 m5 C3 c5 l0 | <label for="notice-title">Notice Title</label><br>
! Z% c* }7 K: C2 l( E& E <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">3 [: x) E8 ?4 r0 _( |
</p>
8 _" ^( ^$ J/ d <p>
) V, n2 ^$ ]% G, M( \ <label for="notice-content">Notice Content</label><br>
' u) f) h% d5 L) { <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
2 b" Y; G2 ~4 [9 T </p>" u3 \+ t6 a+ t2 B9 }4 c
<?php7 N; g6 {2 d( Z5 \# t. t% w
}
+ a/ {8 O/ e9 p, K& }. r: S& |. ~" D
add_action('save_post', 'save_site_wide_notice_meta_box');
: R# p+ X& C' ]- g' E4 w' q9 Y function save_site_wide_notice_meta_box($post_id) {+ D+ m# W9 G' Q
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))$ K }: H o, h# |3 X- X4 `* R8 e* X
return;9 O+ k) {7 W& x; A2 e5 C2 z
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) @" b, C/ G9 S0 ]
return;
* T" T: ]$ e9 ?. y; x
7 E& }1 _! t; d: A. p( W, T! Y0 p if (isset($_POST['notice_title'])) {) _) s% r5 T% A4 P$ O
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title'])); s8 e) P1 j, q/ W/ c9 q
}
# O& N$ z* j1 E3 H( G3 p# L if (isset($_POST['notice_content'])) {. M% `' X6 X& i* r6 p
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));0 g& `. P+ q$ [) [" u& @
}
5 T4 o! i1 c3 @4 P+ o$ z }% V8 G: u M, W- i! l" Q
```
' U8 b7 p' X9 _% K9 u0 A3 [7 a& ]# o/ \7 c6 v# a. b/ G1 F
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
i' k+ S4 f! H# e2 A0 s
. A. C5 r, q& R3 V+ ~# w5 X4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:& f- W+ c) u' H% P+ `. O U6 D T
& p& P, a; c0 |+ Q1 O ```
- r1 j. t2 k# e4 Y1 n $args = array(+ Z& c2 m. g1 K" N$ X* [
'post_type' => 'site-wide-notices',/ n3 |. D |6 O& G4 ?
'posts_per_page' => 3,
* m* c7 I* U0 A& u- y/ P 'order' => 'DESC',
! W v" k, g* Z! u% |) g# d 'orderby' => 'date'
" i3 e) B$ D( R$ ?" k$ M );
7 u1 S! M' r9 |4 C* }! x $query = new WP_Query($args);9 \# y; Y7 }4 P$ q$ v: P
if ($query->have_posts()) :/ D6 J( ^# ~ x; d' S
while ($query->have_posts()) : $query->the_post(); ?>; K- ]: _! m& {& E% W9 w
<div class="notice">/ c' B! h. K. H' e
<h3><?php the_title(); ?></h3># U. L" x: Z8 ^9 B6 a9 p
<div class="notice-content"><?php the_content(); ?></div>
K. X/ f$ W4 h8 P# e& `: I </div>- x0 g/ G0 e# n9 E
<?php endwhile;) J, J3 [% H C* w3 o$ I
wp_reset_postdata();$ c$ T3 \& p" ~% V' y# |1 ^2 U
endif;
5 S# G6 B+ @, z2 L/ \6 K9 N ```
9 p, N* `- o/ z9 W( A# [. K1 O ~, Q9 {. }
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|