|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?$ t- M, G0 y8 F
! ~* X8 C) V m0 L% I" [如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
' ^$ Q# x e( w. b
! A7 ]3 I; Z' }: C以下是创建自定义插件的步骤:* C: S" m! S6 Z& {
9 ]$ ]$ E# M$ i$ V" {2 }. i5 R
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
& t) s% u# t# K/ s9 J) `# X5 M8 O6 K* @9 N, O( F* {2 ~
```
/ o9 g( j5 t5 O2 J+ I <?php
4 i5 ]9 Y1 n+ o6 x/ G& K( `6 j /*" w3 J( i9 C( w; x
Plugin Name: Site Wide Notices Plugin n/ w1 x' P' W9 [" A/ C
Description: Adds a new custom post type for site-wide notices.
% w2 y A& T4 c- n: F6 E Version: 1.0
+ x# C1 k3 n: D9 _% ?% N7 Z5 {% a Author: Your Name
: I6 @6 l& H$ T+ B. A2 U, J Author URI: http://example.com
9 w1 Q2 d% U/ a) J* _5 O */
|. `% n& |$ x' o: P; y: h+ A9 U# `) x5 D
// Add plugin code here...
- ~: H9 L( N2 X8 G4 l1 e ```
5 K# p2 y$ o- P0 I1 u3 G5 @4 }) b) c0 H& I& z. B$ J
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
2 B1 L, K# b0 O& K
2 _& C! q' }0 y2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
( _* |6 B7 A5 ]/ h2 x* s( ]4 |7 _7 e$ r6 n# [$ c& ~' ^
```
: \ y8 N/ r8 X6 n add_action('init', 'create_custom_post_type');/ D+ W) L9 O; h7 ?
function create_custom_post_type() {
) d' u& \: h! x% f! a" Q, @- o* V% A $labels = array(- P) S% N: _5 L" J: }' n! \
'name' => 'Site Wide Notices',6 h. F8 x( G R4 c" i
'singular_name' => 'Site Wide Notice',
" D" P5 T/ t% c [8 ^: x& d 'add_new' => 'Add New'," G+ L" J! o1 o4 ^) ~& [. [
'add_new_item' => 'Add New Site Wide Notice',
" ~; m: Y1 m+ ~' n! W; } 'edit_item' => 'Edit Site Wide Notice',
' {" L' {9 ^0 [) \" l5 P 'new_item' => 'New Site Wide Notice',3 d/ v* ~4 i" p$ L$ K6 c7 y+ b
'view_item' => 'View Site Wide Notice',* d& O# p) b, B( Y) m+ k6 x
'search_items' => 'Search Site Wide Notices',
" [: [& R: P1 W% i- r6 P A. \ 'not_found' => 'No site-wide notices found',
5 c) Y1 Q8 O5 i* B$ e8 U 'not_found_in_trash' => 'No site-wide notices found in trash'
" h2 D) \) J. B$ E );
2 x; r& K5 B+ H& l0 q
9 [! }- M( x; N% t3 _1 y+ e $args = array(
6 H6 d7 m# i7 i8 k8 J% ^ 'labels' => $labels,
" J' _7 E- ^: {2 E* A. b5 V2 { 'public' => true,
/ O+ r8 \, h0 w3 { 'has_archive' => true, F1 X; {) O; r& t0 a: F9 b0 C
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),% \' G+ z0 H1 W f
'taxonomies' => array('category', 'post_tag'),
" s) W! R6 H' N- I" ^9 ^ 'menu_icon' => 'dashicons-megaphone',9 s' L+ Y% S9 A$ ^' y
'menu_position' => 5,
3 D: a" g' d6 o 'rewrite' => array('slug' => 'site-wide-notices'): `0 s; r! b. k
);$ S }9 |7 n( P0 Y4 u9 Y" P
' F# v2 Y! T, Z+ k+ r* x9 s: r. q6 n register_post_type('site-wide-notices', $args);2 H9 c. h0 t2 R7 Z. M
}$ l; F: |- ^$ v4 t6 Z8 S
```
8 q- z' ], ~0 J# m
% c+ l; r+ z2 D1 u6 X 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。4 n8 \& |6 y R
5 Z" l% V8 ^! n9 o! F0 c; h+ c3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:6 \2 f3 v5 t1 c0 G
& c3 J% m# z O7 v5 \5 b% n; h ```) ]6 K: ?4 n9 @* I$ v6 `& F
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');4 {! [" s4 e) }2 h
function add_site_wide_notices_boxes() {
0 M$ N" \+ ~- W9 v6 a: l add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');6 Q2 e7 O- o# ?" D0 ]+ b5 }+ E- y
}
% I8 m( z$ X' W8 W
1 E0 y: `% ^* |' p% Q& W1 @ function notice_details_meta_box($post) {: R, ~* d9 E" i+ n* {+ T$ z
wp_nonce_field(basename(__FILE__), 'notices_nonce');
8 L3 |( i: L; A& s% b) E $notice_title = get_post_meta($post->ID, 'notice_title', true);
+ K1 K2 f3 U& ~$ B, V/ C $notice_content = get_post_meta($post->ID, 'notice_content', true);1 {: f# g2 M2 p6 |: h7 z
?>0 ~5 @+ s z+ n9 u$ Z* v0 i
<p>
' |- @( m5 s& z+ b" g# J/ I* D <label for="notice-title">Notice Title</label><br>
% r* j8 V+ m3 a/ u7 k5 ^* Y <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">/ L' ~* Q8 L: J* t
</p>
o, q$ }% ^% F% N <p>
, r% e% m: A+ K- ?6 R8 K <label for="notice-content">Notice Content</label><br>0 d& d0 y4 T: X& D# U
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
1 @) d, Y5 H7 O* P- [ </p>( V4 r8 K$ P$ ]# q! ]' L
<?php3 n/ A7 r5 Q5 q/ c2 u: Q
}
/ K# V" } @- |1 y
m8 L1 c$ T+ p, O/ ? P1 s add_action('save_post', 'save_site_wide_notice_meta_box');
, X6 J9 |3 H6 a function save_site_wide_notice_meta_box($post_id) {+ @3 @ ~9 ~* z8 ~) P
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
8 C' g4 a1 V4 W5 ?' X* N, k0 p return;
$ ~9 F4 M4 m/ J9 m0 ?2 t/ H. p if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)* T+ D5 n; \$ E3 y# G
return;
4 w" \! I; D. o" R+ X! _; W+ w
5 r9 ?5 }; h- K& u: d( B7 {! E0 C4 r if (isset($_POST['notice_title'])) {
- j* b5 k. h3 f4 s update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));1 v% w0 W; w( E9 H- B
}8 M; N; O6 t& e4 D
if (isset($_POST['notice_content'])) {6 D$ Q r" d* C& F9 q1 a0 H
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));: R h; X4 e) I2 @" ~
}4 c( s( x" f# K! [
}
( U% n4 _9 s% y ```
( X1 P* [8 I, w) Q& ]- [$ f4 L: e+ A9 ^6 P W4 ?
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。+ ?. o* o. u$ K
) y! I4 D. r2 L* w$ @ G; C1 v: B1 y
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:8 ?1 t$ X6 K! X) h* r( c: X$ h
# S6 w0 e5 G' S% _
```( V% w8 c7 N. V! K% R1 ]- Y" w; p5 A/ D# Q
$args = array(0 `; V _9 ]: U
'post_type' => 'site-wide-notices',4 @6 t% V8 o) D4 {
'posts_per_page' => 3,3 u/ T1 D8 B8 V2 h
'order' => 'DESC',$ V$ K6 I4 o. q2 a: h
'orderby' => 'date': I/ {6 \+ N9 i! y4 |
);
! l6 w) Z) a; P% }" H4 p0 a: \ $query = new WP_Query($args);
& N% O$ J0 y+ ]- M# c if ($query->have_posts()) :
- a& B3 N# t8 e! j while ($query->have_posts()) : $query->the_post(); ?>* E( Y5 ]6 d5 a9 a5 V: l( p9 U: G; a' A
<div class="notice">; R7 R& {' y; `& U, P, }$ Z+ f
<h3><?php the_title(); ?></h3>( @0 V1 M. o/ F- m8 ?/ U
<div class="notice-content"><?php the_content(); ?></div>
) E7 y9 Y6 |, j2 a: f8 y </div>
2 M' h; q2 J9 u <?php endwhile;
% b* @6 Q1 P4 h Y wp_reset_postdata();9 Q" k0 V& M ` A7 v) d! Q
endif;) \4 _1 A5 Q2 I3 M& f$ Y7 M
```
/ h9 w3 L+ L, ]7 P; A3 O+ L9 X1 C: m" z4 |, |3 p" n
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|