|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
' D- C& X- L5 z* g* c
# f: b" F8 ~( k9 a2 s如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
* z) i: Z3 j( ~& h6 i/ e2 V% g" `) @. W
以下是创建自定义插件的步骤:
+ X5 Z1 u: W/ ?) |) ]/ x( P6 f* Z; z" ^
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
" @! T% V9 B8 ^/ S: W" B# ~0 y$ E+ O2 ]. V, v( d
```4 V/ {6 L( l5 a; p
<?php
2 s) I! L( U$ G9 H' h' Z /*- M6 [/ H2 q1 p
Plugin Name: Site Wide Notices Plugin: F h [( Y. E7 V1 e
Description: Adds a new custom post type for site-wide notices.9 X: w( k6 ~2 P7 c* g1 ?
Version: 1.0
% u, ~4 h1 m$ B0 t# a" p7 V$ G Author: Your Name
+ ~2 G$ U2 {; l( K; T1 b" j4 V6 d Author URI: http://example.com
6 ^ P3 \) F/ g2 A */
' B% E5 M: O8 O# ~) F6 ~2 V
/ x1 r( u- e9 Q \& S( @ // Add plugin code here...
# W9 \2 M' u% U* K# G2 w4 [; g ```
c. N& d# y2 M0 D$ l9 k) [7 b
, C$ w8 |( v1 m, B. F$ { 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
/ Z- o5 O2 M& ?
: b& c# b7 U; N: `! b2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:! a& R9 x; @1 G9 h3 C# X( J4 I
3 B: K; k; m5 |# e$ \/ P) T ```
( S/ m4 a o/ B. f" G/ L add_action('init', 'create_custom_post_type');9 x4 `$ |. q( B
function create_custom_post_type() {
* p+ O" V# b" ?1 v% h! j1 R1 }+ D $labels = array(
* B' t9 q; @# N8 Q 'name' => 'Site Wide Notices',+ D) k8 C3 |+ {0 P* h4 L; N- X7 n
'singular_name' => 'Site Wide Notice',
/ T- l: R0 H: b A1 j r 'add_new' => 'Add New', b, G6 I' F1 \
'add_new_item' => 'Add New Site Wide Notice',
' G, f( _* [# M8 b9 l% I) F% r8 T. O( R 'edit_item' => 'Edit Site Wide Notice',* K. u, H6 Z& m1 \) \
'new_item' => 'New Site Wide Notice'," o, P$ u$ \' b1 m7 i! Y- Z- O
'view_item' => 'View Site Wide Notice',% }- v) a" a0 V
'search_items' => 'Search Site Wide Notices',
$ G+ ^2 [" f( H+ F6 F; H E 'not_found' => 'No site-wide notices found',
/ K, }2 w5 t, a' l( R" L7 e 'not_found_in_trash' => 'No site-wide notices found in trash'
8 m, C9 \+ N% `$ c5 z2 u m );! h5 B0 ]1 E1 I7 k
0 b) x$ E4 I8 u $args = array(3 ^% Q& ]* j" O Y. p% `% U
'labels' => $labels,/ _# s$ [9 t: X! X9 g" A0 G
'public' => true,
& t! l" S% N) P; h# u' F 'has_archive' => true,6 K( x. p4 f& H0 e$ z' m
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),; n/ v. }* `- s/ j P
'taxonomies' => array('category', 'post_tag'),+ A8 v9 `: P; h+ k- \
'menu_icon' => 'dashicons-megaphone',
" r: L" ]9 q6 V4 W 'menu_position' => 5,. G* ~+ F. j$ D C0 U* Z
'rewrite' => array('slug' => 'site-wide-notices')
- [* |7 e p( A" w" Q );
' x* H( o: P/ V4 j+ K
3 |0 @# c% `( [- | m register_post_type('site-wide-notices', $args);. p9 @8 E1 p9 V3 u# L
}
3 d* F9 S, t! w9 x2 {2 ?3 ~& \ ```8 F, ?' x% O- U2 x; D- u" R
3 |! R2 {3 M' W6 J" S 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
: e r# i1 S2 O' l5 L* O9 h& _% D- N) d6 m9 O
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:$ F+ ]% s& Q5 }* ?' x L- O) C
) F; S" p! D7 ^6 p) j0 [6 n ```
/ H% B# _3 n( A- F n6 p; t add_action('add_meta_boxes', 'add_site_wide_notices_boxes');( Y* M f) h2 k8 i% h
function add_site_wide_notices_boxes() {
2 a. v$ ?$ d9 o0 `3 J5 K- S add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');3 y0 W; B2 X$ ?' o4 c, D
}+ ~6 U [* m* b/ t
; x- }* [9 M; x& f! g( f
function notice_details_meta_box($post) {
) A" @2 M: _7 t! C wp_nonce_field(basename(__FILE__), 'notices_nonce');
# q, @ W& b, _9 E$ p/ k% |. o1 F $notice_title = get_post_meta($post->ID, 'notice_title', true);* u* E* ^+ b$ }6 z
$notice_content = get_post_meta($post->ID, 'notice_content', true);
* ]% L. O; z# ] w' u/ J ?>
' R) y$ l, j* g8 V( k6 g <p>* `3 ?! w! M9 L1 e$ \
<label for="notice-title">Notice Title</label><br>
1 D% M0 e( H# K% T <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">6 {# J& J3 X5 N. P. h: h% K- m$ a
</p>
" l/ D' I j; f% v1 a- Q <p># Q1 r) C; r# s* F- M1 @
<label for="notice-content">Notice Content</label><br>
+ N' {: a+ r2 [ <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>0 e/ J+ _ F* [
</p>' R6 i) F3 V6 U4 _
<?php; U/ X! b4 K( E9 f& G6 g& ]
}- b1 \$ l+ k9 M3 K; k
# a6 N* |# F- G/ }) u$ x
add_action('save_post', 'save_site_wide_notice_meta_box');
. O) p# n, v3 z' r, } function save_site_wide_notice_meta_box($post_id) {
2 S% c* Y* `" i& g+ h$ ] if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))2 @, `/ v& X9 C6 v# [8 q9 x; p
return;
9 j- S4 Q- n* X+ o- Q if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
" O5 ^5 ?1 h# i, F( C return;
% u( U4 x9 G6 g: I o ~- y$ }. O' {6 m" K0 A0 T! y+ q
if (isset($_POST['notice_title'])) {
! j4 |' ?# C) v% Q. b- Z update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));' v( c) c8 h3 ~* \
}
( F ] X' b2 k. u" {; P7 f if (isset($_POST['notice_content'])) {
- {# q& I8 N, G4 U6 M update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
1 O0 {7 V$ C$ h. R5 ~ }4 C- {) N0 K& l3 M- S3 D
}4 U: E3 a( {6 K3 }2 f
```
; b# T% [! x- h3 ^$ K, D$ _0 M, _5 e; Z& ~/ c2 W
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。5 U. W, V0 q2 z1 [! ^+ C
% B/ {# Q' O* O! Z6 D
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
5 H) s3 C6 _8 o- i( _& K0 X# i5 F c; G% ?4 Y+ V7 R2 D
```* v8 L+ Y& d! i( x' i* s% |
$args = array( x% c7 r9 L8 D+ D
'post_type' => 'site-wide-notices',7 M. e2 D# f; a* G3 Y1 K1 e
'posts_per_page' => 3,
- J/ x& u# f/ L0 G 'order' => 'DESC',. m4 @7 c9 M& `3 M9 b
'orderby' => 'date'
% o' a% {! K7 A2 x0 w/ t );! F. X) d0 F3 A. h4 a6 e' F1 N
$query = new WP_Query($args);0 i+ X9 D' X7 d: {) b, s
if ($query->have_posts()) :
5 r/ c( Z" Y8 h! W" Z# _- v1 l while ($query->have_posts()) : $query->the_post(); ?>8 `; M: w5 Z2 G) ~
<div class="notice">
5 g! ]5 K" G4 m* L7 J' @" N8 T8 r <h3><?php the_title(); ?></h3>
% G- M0 A/ w* F, m$ }3 `# M. s <div class="notice-content"><?php the_content(); ?></div>
1 v) Z* N* d- l5 U" n </div>& _$ Y* i0 K$ B- T
<?php endwhile;
2 T* _" b% {8 h7 D wp_reset_postdata();: J& H: v K, v2 e4 q
endif;) m$ S: U9 t; Q( E' B H' ?
```0 z, N7 n) n6 v
3 g4 [) s6 i3 F+ z; ?/ \; o
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|