|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?. u3 C2 x% e- p) C( k
5 i' A2 N0 _7 ? v如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。0 [3 _% x' X$ E: |) v3 _. H
9 B9 T, D# f; p: P; a, c; M1 B
以下是创建自定义插件的步骤:$ I5 w$ L% a4 G+ d
- s5 m2 s, B! t2 u
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
2 y5 h+ p9 G+ L, u& }# m
3 t( U* J/ q4 y+ N: @ i/ w ```
& L6 A/ v. V( |2 ^9 M+ G <?php2 }- X0 }. Z. f; I) Z
/*
# i5 _5 p8 E$ D; e2 y Plugin Name: Site Wide Notices Plugin2 Z5 Y, g+ h+ L4 g# [2 w4 n
Description: Adds a new custom post type for site-wide notices.
) X, Q' J: w$ a% V( t* f Version: 1.0( L0 a$ C1 T& }( t4 L R1 K4 j
Author: Your Name
6 m7 K! E8 c3 l* I9 G5 ]9 v Author URI: http://example.com
: t6 b, Q5 {- q6 H: l0 v( |9 T* v- t% E */4 M$ [! D1 q, O* |4 d' b! F4 b$ c
0 R# d4 ]3 M2 b // Add plugin code here...
7 K. `0 F' u* f/ h5 ~6 h: _ H. ` ```
) K( h4 R* B9 `, R4 _& Z! o8 b$ F3 d1 i% P$ C
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
, J' c% R/ E+ [$ `1 J; r
) S" b& S1 p* s% j3 {2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型: j0 Y1 ~% _; z7 @+ b" `' C1 T# b
9 v1 h# p" C) S
```! `/ o5 P3 r- @
add_action('init', 'create_custom_post_type');
! l6 W5 I) c2 w% m6 r function create_custom_post_type() {3 x4 s$ {5 a! m
$labels = array(" u0 l- K5 H$ U* V
'name' => 'Site Wide Notices',
4 c W* v, {4 |5 h* C( M 'singular_name' => 'Site Wide Notice',
& Z2 U7 @ r# L9 E. U0 A1 E 'add_new' => 'Add New', H1 p: M' `- s, X- v
'add_new_item' => 'Add New Site Wide Notice',( Z6 B. u5 u9 p% Q9 z
'edit_item' => 'Edit Site Wide Notice',
# V# Y$ ]) T% X. s# E; P$ e 'new_item' => 'New Site Wide Notice',
3 E5 f' A+ N7 G$ m8 i 'view_item' => 'View Site Wide Notice',1 x: Z& T! A% B
'search_items' => 'Search Site Wide Notices',
5 O4 X; ?) v7 a) q B" V* d 'not_found' => 'No site-wide notices found',
! r( e0 e b' x- A4 B4 I" f$ c$ T 'not_found_in_trash' => 'No site-wide notices found in trash'2 p. G$ C( _* K6 g: p* n
);
1 b* g+ Y" r6 _& p, e3 ~( u0 Y5 F# L6 B' F8 s/ \- A- }3 i+ j
$args = array(
' t5 Q2 Z/ M3 l; d7 H7 c6 K 'labels' => $labels,: G m$ h% w0 G5 m6 `: x* M$ F( X
'public' => true,
. ]2 r/ J" [+ i2 S" Y 'has_archive' => true,, W* q2 ]" d% O1 K4 |9 y* s6 Q
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),- {: l6 e" x0 l* ?/ _/ E
'taxonomies' => array('category', 'post_tag'),5 u! X2 t6 E3 _5 l5 I( @. Z
'menu_icon' => 'dashicons-megaphone',
2 O* r# Y# M* Y, X8 w 'menu_position' => 5,* [+ G0 | T- t) b c* K6 ~
'rewrite' => array('slug' => 'site-wide-notices')( H3 m7 |! |9 u/ Y# U. P
);
! R* W2 A) ?/ R2 k
3 v, ^) Z# J5 ~# \' ~ register_post_type('site-wide-notices', $args);
6 c( P1 c1 _! M9 p C }
( T" f& I W/ y" F ```0 y/ x$ r3 F8 q7 k
4 m7 R4 B& v+ V y' i# H/ [2 E 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。/ C; b( t' o7 t& Q; @! ?! s
4 u5 G& c$ X _" t, O
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:8 ~1 f3 r, N7 k4 z
( ^. R1 j$ r3 ?2 N8 J
```
9 L1 v$ ^, ]" e add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
, i. `. y3 n0 O" J7 J' P1 Z function add_site_wide_notices_boxes() {+ T7 v; Q& W5 `2 c D# U
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
9 e# F# S/ R, q( F- j p }
, s, ~ K" J% Y% j- K' U7 X' O8 @, U1 b+ R% W0 S
function notice_details_meta_box($post) {: C) u: B, a: [
wp_nonce_field(basename(__FILE__), 'notices_nonce');
) A( J. b4 p9 N" m9 f5 G! [* \ $notice_title = get_post_meta($post->ID, 'notice_title', true);" T& e; k) E1 u ]
$notice_content = get_post_meta($post->ID, 'notice_content', true);
, \1 e- ]. o( O, G) V7 y: Q: ^ ?>
1 t3 G5 C0 B5 b/ o$ R, t <p>
7 J6 k+ Q" p( k) x5 U* F; U' Z <label for="notice-title">Notice Title</label><br>+ m- r! y: n* r* ^
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
$ \7 o9 [6 g9 u1 | </p>- [( L6 _( ?7 u% F i9 K
<p>+ [" z& ^% G; f% l" }. b4 j
<label for="notice-content">Notice Content</label><br>- j; d2 M& G: C
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
( S- s7 z! z F </p>8 I* M3 V& [2 g+ N
<?php
3 w l+ L' t e3 l! O9 f }. ~% x0 {2 l6 \% M! h, h& ~' q
3 o: I. a8 A' `2 V$ \
add_action('save_post', 'save_site_wide_notice_meta_box');5 x' j0 \) [% u5 D
function save_site_wide_notice_meta_box($post_id) {
0 m) n: V+ {9 r& f4 Z, i. w" s if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
, ]/ C! s3 N6 ] return;
- d( \7 w/ N/ R1 _! x if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)% y y! v* O' Y* M; t6 j8 V$ Q
return;- j! r) [% n* m$ f* L
, _% |* w3 l1 ^ S. X, R1 u
if (isset($_POST['notice_title'])) {9 r) l+ ], n1 |. Q3 a' H2 i0 C. [# q
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));, e) O! [. N+ j3 b; |
}
3 Y( O$ {' h) J/ D6 f if (isset($_POST['notice_content'])) {
" b( s9 T I0 O) t' u! |; e: d1 w update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));% n X2 D& i0 |- B Y
}9 H4 g: T5 s4 d' L, p
}5 |9 S4 r; W e3 {* X3 m; b
```
4 N! q* H& ^$ q; T/ \# K0 y; ^7 u
2 e' M! M" g/ a! t0 H 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。; Z% [! w- d% U# m
: @5 P9 S0 p/ Q( Q' }9 r
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:+ Y: i. f: T7 N% ^- \+ G
2 X. W0 d( @8 W% w8 s- B/ S
```5 D+ D7 Q) `7 o- W; F; ?; [# |
$args = array(! |/ X0 q- [; l9 J- Z4 \! o4 m
'post_type' => 'site-wide-notices',
8 }' f% S! t- E- X: K2 A 'posts_per_page' => 3,) r/ R% X% V8 N4 h2 z
'order' => 'DESC',5 _( z, v" l! _' x' L$ }5 w
'orderby' => 'date'
2 r9 x7 K, r. h) @6 o );
F+ f! _4 ?1 v# J' ?/ Q $query = new WP_Query($args);: i0 K1 [/ w' A) _ x% H
if ($query->have_posts()) :
9 y5 O& a, O; D8 l* R8 x% R) b while ($query->have_posts()) : $query->the_post(); ?>
; E p7 F; a( k' w3 d" B <div class="notice">
( s- {. x; l% H! m <h3><?php the_title(); ?></h3>8 s) l' p* S0 r, I9 I
<div class="notice-content"><?php the_content(); ?></div>
5 a+ l( { O7 j' [8 v </div>
8 b+ w: ]" s* U+ w3 M3 {5 Y! { <?php endwhile;
- q s- Y0 m' } wp_reset_postdata();; J" J0 ^+ D& |# s/ V
endif;) |2 \9 A( [' A+ w; _ q
```$ L& X0 d) g5 `( o) J; e( y! v/ t
8 c& p8 N" a) D, Z3 O
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|