|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?" d- D+ v" k: C2 ]
5 t& E- H! [- c0 ~9 w) w0 v1 m
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
" [8 m; a* i/ _& ?) ^; c5 K+ G1 Y2 P5 k! H8 O% H" g8 I
以下是创建自定义插件的步骤:( u: x' d- T$ R% v9 W0 P9 b; c
( C6 a$ y, [9 F$ H: U& p+ p2 v( X1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
5 C4 G5 g1 z k
+ d7 {9 M2 X8 T9 u ```
2 j5 w( u p- }& t <?php
( _5 @% Q; w. }% [ /*) {! O. `0 Q# m) _% U1 c
Plugin Name: Site Wide Notices Plugin
( i5 s; w7 e& L4 ` M# L3 q6 Q Description: Adds a new custom post type for site-wide notices.
, k( l" L# @' T# k Version: 1.0
' A0 m7 P# q, v# x' s0 h Author: Your Name3 c# R0 ^$ V9 f# Y8 I
Author URI: http://example.com
: A: ]; m9 i8 R* S8 L */
* H6 W. o) a4 Q3 [0 d! I& U# K' b" }+ b
// Add plugin code here...
6 y( N D! g3 C$ m ```. d/ S! e1 u% ~$ Z
7 ^% V/ l; ^1 `! h; S" d
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
0 K$ ?9 H& S/ U6 r$ [
8 V) c' Y" Y; a/ T! }2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型: e! l& p2 r9 D$ s% D6 Q4 r
* l7 w1 z1 s" j
```5 X2 O% h* x6 y6 @2 ?) |- @. K
add_action('init', 'create_custom_post_type');
7 ^; e" y) v& { function create_custom_post_type() {
) M7 n: A6 c8 m. u5 c/ s $labels = array(8 Y3 [1 v! c1 z5 B, v y# A1 P
'name' => 'Site Wide Notices',3 u/ s2 u5 R# ~2 n- f
'singular_name' => 'Site Wide Notice',
7 d2 l7 Y/ E9 z 'add_new' => 'Add New',
( w+ s, Z% P2 y( h% x) i, m& T0 O 'add_new_item' => 'Add New Site Wide Notice',
; j. |( k7 p- a1 O8 b 'edit_item' => 'Edit Site Wide Notice',1 ~7 w, K( a* E$ t0 E- x) g! W
'new_item' => 'New Site Wide Notice',0 N6 a" D/ @( i" o3 W1 K
'view_item' => 'View Site Wide Notice',
+ |+ r: R8 ?* A$ m% C 'search_items' => 'Search Site Wide Notices',; k4 }& Q7 A$ ` U j I
'not_found' => 'No site-wide notices found',, [7 z' W7 K7 ^% {! Q5 i, D
'not_found_in_trash' => 'No site-wide notices found in trash'
k2 T! ~9 g$ c. ~ );1 _# z' q1 h3 }: z& x
: e0 Q% X/ D6 s
$args = array(
1 @. T: T# K& I* b 'labels' => $labels,* T8 s4 O* x8 V! b6 t7 K: _: ^1 l
'public' => true,2 Q( A/ s7 H h' H
'has_archive' => true,
. [0 O( J9 X ^& f4 E% d 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
( ~/ y9 Q" Y6 ~- _# x: [% B$ H0 } 'taxonomies' => array('category', 'post_tag'),1 d! C% ~9 J: Y( k# M
'menu_icon' => 'dashicons-megaphone',
" T/ C2 k" L3 O9 ^- H9 y 'menu_position' => 5,
9 D1 G1 T& }/ S 'rewrite' => array('slug' => 'site-wide-notices'). B U. u; K! f, |; P% u0 R
);
: { `+ X8 R9 S" W/ D% I2 t$ x; Q$ p7 L0 h; { S+ z6 d% k4 R
register_post_type('site-wide-notices', $args); J, c9 s" p. c8 L2 w& y
}" e6 a% i ?$ h Q: m' @
```
6 s( B5 H* M# L; J5 S, z1 w
- f* l1 o8 A' v3 D7 M" G' \& O 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
, @7 N+ z7 ?, b6 `, M8 d, D4 y+ J. g( \1 r7 M& O$ t, x
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
% U3 _# s* G2 X' w/ x6 u: l& `5 R/ q! V2 E2 H8 ~% g5 n: N$ S. K
```$ ~ [/ g; A2 o: ^7 {5 w
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
4 Y* Z1 @0 k- t+ [7 C; h+ q function add_site_wide_notices_boxes() {
& @) O" k6 U: t# [! z# n add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
3 {+ K% H6 r) c' U3 H- u }5 M' R3 U$ h. A7 }' G
( w0 B; g/ H* @
function notice_details_meta_box($post) {
2 J0 P$ |/ S4 l4 m2 c$ A wp_nonce_field(basename(__FILE__), 'notices_nonce');
/ n2 _, k& b7 L $notice_title = get_post_meta($post->ID, 'notice_title', true);
. x. U, c6 t' P* p $notice_content = get_post_meta($post->ID, 'notice_content', true);! J& z2 r# z( M% ?. u
?>
5 n/ x# \+ t8 K% u3 z& i <p>! D8 D3 ?5 I5 k1 e
<label for="notice-title">Notice Title</label><br>* f8 ^* w& l; f1 f
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
6 U( {6 T) j5 I; [0 G </p>
5 v. V: O2 m% s& ~' [ <p>) T p3 P) {' `1 R4 g
<label for="notice-content">Notice Content</label><br>
& J$ X7 e" P3 i3 y <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>, f4 B7 _6 f- ]( x3 S0 l* G1 f
</p>
. x: B, L6 n1 @ <?php
$ N9 \# Q( S" T$ g }3 l/ c0 L9 k6 K) W$ k3 C
* @( _- n$ U4 ]# q$ E add_action('save_post', 'save_site_wide_notice_meta_box');
5 s; H& h: ?: Y- y: Y1 b% ^/ J function save_site_wide_notice_meta_box($post_id) {
. Q' a# v% \5 w \ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))& n4 f$ J0 M- [7 B+ Q
return;
& r- Q }) k/ f/ z- L* ^( r if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
- `0 E3 u+ _7 U5 s+ K return;8 T3 z/ ?5 s, b
, F4 w' H1 U- i/ q* L v) M
if (isset($_POST['notice_title'])) {
0 k+ |8 o0 y; O. D: b a: H update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));8 ^2 i9 _8 `5 C$ s" l. E
}
: m' {3 K. x7 F3 X# Q# t" K) B if (isset($_POST['notice_content'])) {, ~/ ] V' [1 _8 I& n. w
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
# Z/ o3 a- N8 J; i }: e6 r( M4 u- p( I# L+ e- m1 g2 H9 a
}
9 X$ g' s+ i; K$ u* ^ ```$ Q5 Z8 v! I8 X" _# z: s
9 N J {5 {( [! }. H e 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。/ \3 C& w' n; I7 V9 i; M W8 u
+ p0 o# j3 W6 s8 N1 K! z+ \7 o4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:' r; z6 e% j- T- P. p
# F: u& c* H" D! }% f! F, O* U0 R ```
% b" ?6 ^% J8 F7 |& F j$ w" h $args = array(
5 s: F. b9 J- z I) R5 v/ x1 J Y' V 'post_type' => 'site-wide-notices',
0 f& j+ o, B/ |+ G8 N& g) p4 m 'posts_per_page' => 3,
0 o4 I1 V$ F, y: L, } 'order' => 'DESC',2 Q: |( n5 z1 v
'orderby' => 'date'
. i! |: Y7 n3 ] );
/ P' M* ?$ Y+ z! c% u: o- d6 Q; L $query = new WP_Query($args);
5 Y$ Z0 |; u7 ]+ Q( J) S if ($query->have_posts()) :$ G2 A- z$ V, C9 G7 P) E- o
while ($query->have_posts()) : $query->the_post(); ?>
+ s9 ?$ Y: h# N% _7 c- d <div class="notice">* Z" J9 \. b) f- @" J
<h3><?php the_title(); ?></h3>
/ A6 ^) Y0 k0 W4 e( ]6 I! C <div class="notice-content"><?php the_content(); ?></div># f2 |4 ?3 ~4 f/ @/ g/ n6 s( e/ x
</div>4 L/ {) h+ S# k' e% e
<?php endwhile;
( c7 g- \: ~ @# W9 Q wp_reset_postdata();: M- U0 S2 w# x% D( R
endif;" R& b4 o2 j$ S+ g8 f3 W
```
1 u/ s/ t+ |" m2 X, Y0 b: z* V: F+ [! z1 T$ B0 j. b& S
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|