|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
" w) |, g) [2 C. s& v, K
6 T s; d) ]! d如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
3 w4 X6 P" a7 y3 x
6 i8 E, {+ P/ V4 L9 w" d! I W以下是创建自定义插件的步骤:% K1 j& ]# k1 @. O% L* }5 b0 \& `
/ c+ j( E# C# p+ F1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
; m: S: M( n* N; p- }0 \) `& H$ G3 U8 b+ g" [- \/ O9 q( Z: Y
```
8 P- v5 [" F* M: k. Q <?php( o2 N3 `) t! b$ v7 y; Q. r5 b' A
/*# b6 v2 H* \/ ?7 p: S$ A3 P
Plugin Name: Site Wide Notices Plugin. b) ^* @. k' q2 C7 t
Description: Adds a new custom post type for site-wide notices.
9 s7 e& C4 `# g" C G. \) L Version: 1.0
! Y8 _3 r0 F1 W _( X$ D Author: Your Name
3 w8 f1 k4 \) g Author URI: http://example.com
) O2 C9 g3 E2 ^ */1 o$ K- d) K0 B% Y4 x
$ S& U0 }( x7 F6 p // Add plugin code here...: o% y/ N# g, I1 p( \
```0 M3 c) q- C- O+ B& L" g$ N# D& s
" r* v! ]5 d- i& \: u
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
( b* U8 k5 L0 F; r7 i$ `/ p0 o- H) b" i: R" j, ?8 j+ C
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
" a7 b/ E5 e" T' _) F) a
: F/ x, y+ T, H0 H8 y4 {5 O+ s ```
0 w7 E7 \; K% F1 _; W add_action('init', 'create_custom_post_type');
! v3 A; a3 Z) x function create_custom_post_type() {% M3 |: i2 r# L. t# A- J( y, V! b
$labels = array(, x6 `2 n* M0 N
'name' => 'Site Wide Notices',
6 o( O; ^& K. [$ G2 v( z 'singular_name' => 'Site Wide Notice',
! V) a9 m4 H$ O2 |+ P 'add_new' => 'Add New',
& ]2 K: G( u9 _# T. I 'add_new_item' => 'Add New Site Wide Notice',) }$ w' d8 r8 o; _' z
'edit_item' => 'Edit Site Wide Notice',
i# e2 p$ w5 M/ w: ^, f 'new_item' => 'New Site Wide Notice',* u# T2 Z/ Y& t
'view_item' => 'View Site Wide Notice',3 J- t9 }; e0 r2 q
'search_items' => 'Search Site Wide Notices',
# D- E' H* R7 N; _ 'not_found' => 'No site-wide notices found',. f' y3 c7 m8 V0 t$ V! M: G
'not_found_in_trash' => 'No site-wide notices found in trash'' p2 `" w& b" Q9 g
);
2 T/ D3 [# F3 ]; G j# H. u# P) Q% T: `: }) T1 z4 F6 v* [3 O) k
$args = array(( J8 h" z% c$ V
'labels' => $labels, R& l6 C, M9 x. m' j/ b
'public' => true,+ v8 K% N/ V; F5 P/ D5 E
'has_archive' => true,
0 H+ J( Y; w) t$ y8 ^3 N" g9 ?7 ` 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),. p) @+ d6 v6 u. H1 D Z8 A1 L( f
'taxonomies' => array('category', 'post_tag'),
, I$ o7 v7 u7 M5 { 'menu_icon' => 'dashicons-megaphone',- g, O9 d+ |; r% H) r+ S7 [
'menu_position' => 5,* ]' v) |) [5 E
'rewrite' => array('slug' => 'site-wide-notices')
- w; N% F! |) u" z! `- } );
0 G! J$ S! i5 C* S2 q$ I
; }' J7 r4 c. H4 S* E register_post_type('site-wide-notices', $args);
+ n& B- I7 ^1 M0 x }
8 U# b% \' ~0 C' T$ r; w$ E ``` f4 ?5 R4 z4 d' Y+ e9 l
/ \9 G5 l+ N, s- \: V$ @ v6 H
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。! E0 k$ `* v& Q' B) D
A, v* U0 w2 i1 G9 [3 A- p3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:9 q" l6 g4 h% h2 l% v
$ {4 a* A/ }- M, {# s l
```; Q4 C+ P4 ^& t0 L- L1 _+ N4 ?
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
6 g1 T7 l8 Q2 t5 z( L ^ function add_site_wide_notices_boxes() {, I" H9 F' t! v
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');7 O( o3 y+ p% E# {
}
6 |/ _9 |( ], h: |+ F% C6 Z
+ G% R3 u4 ]/ j% m function notice_details_meta_box($post) {0 ~5 {" i7 w8 r/ N: |
wp_nonce_field(basename(__FILE__), 'notices_nonce');& Q% s F0 k2 n( L
$notice_title = get_post_meta($post->ID, 'notice_title', true);
; F0 D" V5 F- N! V $notice_content = get_post_meta($post->ID, 'notice_content', true);. t7 X( t5 W# @0 d8 M7 V1 {% x
?> Q9 z- t8 ^( c. L
<p>5 S; [2 f6 K/ l: b v
<label for="notice-title">Notice Title</label><br>
$ w4 v M. K. E! v% D3 V; u2 f' ]8 G <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>"> m4 N0 S/ m' P# G* F/ V; I- q
</p>
6 z7 o. m1 V( [) Q* ` <p>7 H* k6 I5 _" {) @. M }
<label for="notice-content">Notice Content</label><br>
) v. l8 Z& Y. U# }6 O$ _- Z7 u- `4 D <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>. @$ f9 j% v9 Z! E# B/ m
</p>
' L6 P- z$ B4 `# @/ Y. B <?php
! D3 _' A' d6 n2 d }
- P! r. ]( D# F' t) m- h: k
- K7 p# e* y! E add_action('save_post', 'save_site_wide_notice_meta_box');9 J% K! }; W7 x5 K: d x
function save_site_wide_notice_meta_box($post_id) {
* G6 u F3 y5 j' b- X* {% K if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
9 P: S9 ~ a. O( @' J1 n0 n9 X4 B% I return;
1 X7 ]( Y& M8 A" E4 z3 q if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
7 N0 D& o1 y% |0 r return;
. B) W6 R& d4 m8 u6 w- g3 f7 i
d' j5 \ Z4 d! R3 w" b t3 f" W- q) ? if (isset($_POST['notice_title'])) {# I+ b- J7 k. D! ?
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));: j$ I1 O; W/ e+ g) i7 c
}% i% w) c! f# j
if (isset($_POST['notice_content'])) {
* P n- |; \' ~4 ~" n6 y% [ update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
, q! R; R) K- e }
" O/ [! p$ e: C/ ?: G7 w5 r }
0 p- d2 k4 @4 z6 J9 r5 y+ Y ```! _8 k" @( _/ l- a( T' q
! o8 f" q* K: u A 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
0 |1 K. |2 m7 M5 m6 k1 B5 I; l- t: r6 E) T: b; j8 o* G) W/ L% t
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:. [' I, y: T7 G
7 S2 T# S$ V$ _; g4 P2 V# l/ d
```
4 x; f; i1 s J$ ` $args = array(
! K6 _; k1 d+ v; Z$ l) R5 x2 Y0 }& f 'post_type' => 'site-wide-notices',
- ^6 e+ {1 b& \/ p7 O 'posts_per_page' => 3,
4 Y% v2 R3 u6 } 'order' => 'DESC',& `# i! ^& a' F5 ~- E% H; F u
'orderby' => 'date'% x& h& H( h" W+ V4 @0 N
);
: d* ^( z- k3 {+ G0 E5 b $query = new WP_Query($args);
8 G' Y, i) K1 _ if ($query->have_posts()) :2 ^: T' I( ^+ U9 [
while ($query->have_posts()) : $query->the_post(); ?>! I. U k# r8 Z+ I
<div class="notice">9 U* l' o# W( q( _" g4 s
<h3><?php the_title(); ?></h3>1 i) F) ~8 c* [4 e0 Y2 F' I
<div class="notice-content"><?php the_content(); ?></div>
, t9 `3 c7 t+ ]5 z- R/ |, k </div>
4 p* P2 X. B% M% X) M! ? <?php endwhile;
# r: p, x. N( [' j6 t wp_reset_postdata();$ y5 W' |2 Q1 \& g8 a3 v$ R; }
endif;
( Y8 S" S: j8 c7 k ```# X3 X% p, C4 S/ @2 U5 |9 K
+ }1 {! ] \3 S7 R 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|