|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?/ M' v1 [+ o. U& `
3 K3 t5 W; Q/ V8 n& o' E2 j
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
. u0 w8 e' E% e' \ e% A
5 M! c( Q( n/ v3 A( n$ y以下是创建自定义插件的步骤:
" b, K& b( `& V# q+ {: ?( A' T' H& V* g2 r$ Q
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
6 D' b: X4 s& b7 Y$ k9 X ~4 c/ l
```
g2 I8 P. C7 S3 x% z- ` <?php
2 _- @, g2 `! H) ]' a& [0 I /*. r5 ~; K/ Q/ w+ z; s b
Plugin Name: Site Wide Notices Plugin
^" X9 Y3 W7 @( S) z' i a$ p Description: Adds a new custom post type for site-wide notices.
g- Y6 X: @) A, E/ l9 u# B/ z: O Version: 1.0+ k( E6 e. b3 l+ L0 @! \* Q
Author: Your Name( [- y6 m3 o" {% F! i/ ^
Author URI: http://example.com& w/ f; O5 N4 Q7 T7 `, @: T
*/( i8 y( U/ F* z8 i; e
* S4 p( i2 y2 b/ Y
// Add plugin code here...
7 X. a% y& M; o% o ```
7 B& x! w* C8 N# n
: e' q- g7 ]+ Z, V2 L5 t4 U1 g 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。' s; Z4 P! [* }( s4 T' I5 c5 m+ B
2 H. x" x2 n a, q! t/ }* T
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
- T0 E) i k. D3 [1 P7 E' U; [4 A M. C
```
3 O( g- T# u$ L0 U3 B add_action('init', 'create_custom_post_type');
2 f" J% ^7 B \* W2 o. D4 [5 I function create_custom_post_type() {
" @$ e) P' M/ V9 j& w7 L $labels = array(- M2 w" L% q" W! |* ^; e
'name' => 'Site Wide Notices',
# W" S; d8 G( y$ p# D/ U& K4 h, ^3 o 'singular_name' => 'Site Wide Notice',' {. J# m& z: N) O9 Y
'add_new' => 'Add New',0 Q' {3 U2 {6 t ?9 U0 p; u Y
'add_new_item' => 'Add New Site Wide Notice',
# q# c9 q( J8 M1 [ 'edit_item' => 'Edit Site Wide Notice',
1 R, O3 M# V* h 'new_item' => 'New Site Wide Notice'," g( a3 g) G. g6 I
'view_item' => 'View Site Wide Notice',. A8 }/ L8 I" J g
'search_items' => 'Search Site Wide Notices',
2 O7 c# z i9 a; s1 h 'not_found' => 'No site-wide notices found',* o! w4 M: w/ U- f1 g# f# V* L! x
'not_found_in_trash' => 'No site-wide notices found in trash'
0 k$ ]+ e- w0 x' {+ K- F- d );; N: s* t# S2 D8 R5 z" l+ h" U' R
; t* i; l' o5 f
$args = array(; w/ O& u h5 @2 K. Q- v$ K
'labels' => $labels,, }# M2 v; V7 \( |2 a
'public' => true,
- H$ S2 M- `! |% j 'has_archive' => true,0 h9 I! O5 W+ b% T7 w
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),8 }' E: P- I8 d U7 }7 s& O, @$ S( S
'taxonomies' => array('category', 'post_tag'),
/ M) _4 G5 m+ A i& s+ o; U5 ? 'menu_icon' => 'dashicons-megaphone',
! A3 x6 }) ~0 {/ l( @1 o 'menu_position' => 5,
. P2 r) J, w5 ? 'rewrite' => array('slug' => 'site-wide-notices')
" }" _8 ]1 Z8 V8 }. U );9 ~* ^% ^ r, V$ W0 p& ?& t
1 a# N/ l% z0 ]% i" V N
register_post_type('site-wide-notices', $args);
( a+ v0 R: }5 {: X" t }8 J4 ?4 a# Z$ b7 S% R; g
```
* ?/ M o: F6 l- T
- O9 \2 o% o. g z; H8 I. T 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
" V* S6 ~; Q, D& @. g% P
! H- v" w! r. l5 e( R3 r4 A! i$ I3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:* j9 o* {. f: ^1 g
: `) \" N: [6 _" v% N
```% d$ W4 G" a9 s+ [, k N
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');0 I4 F; N& Y }$ }; s# L
function add_site_wide_notices_boxes() {1 }+ m- F& E: r& k z+ E# y0 G1 H
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
2 k% v4 m% |" l% i }% U& L8 t' ]9 i7 x
) F/ R. J" p1 c0 i function notice_details_meta_box($post) {" r/ v) Y! X+ }5 N* ~. J1 t
wp_nonce_field(basename(__FILE__), 'notices_nonce');
( A# a: S) s9 a9 q* M $notice_title = get_post_meta($post->ID, 'notice_title', true);$ }) q& |+ v) d" Y) S* C
$notice_content = get_post_meta($post->ID, 'notice_content', true);8 }8 Y1 ^1 l4 n/ ]
?>& ^- }2 B3 t8 w1 t0 L% S" F9 y
<p>
& U. b7 W$ K- T9 v <label for="notice-title">Notice Title</label><br># ^# U) j, B2 }' U# h% ~
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">8 `8 x6 C; g3 O) I5 @
</p>
8 B- h; P% m( ^% q& R' d; |& t! Y <p>
! d! S* W# j6 ~# D( a: w <label for="notice-content">Notice Content</label><br>
8 n/ J7 Q8 n1 M <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>: u8 `2 M9 E0 z
</p>
) J5 q6 _, r6 D+ M$ i% [ ]( E, U <?php* T. `9 E( ~, A
}' @' i! l* n2 B v4 e5 w& R2 o$ G1 N
9 H" T; W( ?( M0 Z9 }) O! k2 W
add_action('save_post', 'save_site_wide_notice_meta_box');* [- Y6 w0 q" o5 L8 `
function save_site_wide_notice_meta_box($post_id) {
3 a4 q, |6 F, R2 \ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))$ h. a- M9 G9 \7 ~9 ]) n
return;
/ f+ } S! A* }2 l& I4 ] if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
+ r6 C$ ^2 p( c% W: E! X9 ~4 P return;
" p0 Z/ @/ E. P' l/ |3 I" a* c0 |0 F' s
if (isset($_POST['notice_title'])) {8 d) L% J$ ]6 K7 V1 a3 b" I. g
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
) N( X8 S$ ?7 ^( z# b4 \ }% ^7 C2 V7 `4 m8 E0 s
if (isset($_POST['notice_content'])) {
* m( s- Q# c6 J( \# d update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
; L) R$ l9 k# Q% y }: c0 g6 b- U& y% G
}8 b8 c( v/ P2 J0 t T; b
```
1 g( J% I0 P* }2 h9 T% z, D) C P4 `7 R S1 Y0 T/ V+ |
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。, y4 e$ t0 L9 m: |0 ~' S/ w6 k
. G H- U `8 |6 _) g
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:5 o6 R6 n: a, O
' q; _7 F6 d/ V, H* J ```7 p. D# M* P7 l* f$ t
$args = array(
: [3 {) b- o: ~* z! ] 'post_type' => 'site-wide-notices', e' Q3 ]+ S, C5 T6 L
'posts_per_page' => 3,1 f) ^& G/ e* f6 g8 R$ o
'order' => 'DESC',
. r. Q8 g( V U+ `, N2 y% N 'orderby' => 'date'
5 [% f: A% [/ A! { );* T) a. Z1 e$ z( F1 s* }
$query = new WP_Query($args);' X9 r6 W. V8 h4 N' {, Z
if ($query->have_posts()) :; {' k/ I% I ]
while ($query->have_posts()) : $query->the_post(); ?>9 s7 s) [3 ]3 N7 y4 o
<div class="notice">2 p& k+ a+ ]7 S" g
<h3><?php the_title(); ?></h3>
1 S2 C L6 ~+ F6 D3 D <div class="notice-content"><?php the_content(); ?></div>* X g) O- }" u& B2 U( o
</div>
: k$ S% t5 c* G! m( Z" z <?php endwhile;7 _2 }4 h: q* @/ E
wp_reset_postdata();4 g; a( ^5 E3 P3 S
endif;
7 h/ Q; W* K, G" q4 }5 y ```
% h* c1 L$ f. e% o9 U" L+ F) O9 U/ @! j: q. i
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|