|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
0 S; W% X. e+ r! h9 i0 `/ K. ~
* V$ M" L9 Z8 @9 _+ t/ v如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
- r3 O% {! N: n: v; ~( X. b+ P2 _0 E3 r+ c1 d. D6 l
以下是创建自定义插件的步骤:0 w: ^0 C2 ]0 m. r: \
" |- }1 N* {1 Y% l* _; X2 c" A$ w: C
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
6 V; u. Z* [) l# o: A
6 B- h5 E1 l1 a" E1 k9 _ ```
! o J9 p$ H* }7 d1 Y; h- y <?php8 ?' N5 S' f. _$ k
/*
3 e }/ V. U1 v2 g7 S# L) u Plugin Name: Site Wide Notices Plugin
8 v# F7 p4 G: s/ p Description: Adds a new custom post type for site-wide notices.
% z$ M. y/ C, w Version: 1.0' [! p! v5 C2 X+ b
Author: Your Name& ~5 y; S8 j! v& R6 u3 ^: G) P, i
Author URI: http://example.com0 X' V" ^" ?: M: E1 e
*/
3 h$ r! B0 n* m) N5 J# a$ c6 S+ v: g- g8 I
// Add plugin code here...
& [: p/ Q5 y8 ?0 r ```
$ {5 O8 N- \+ n( B
+ D1 S3 [9 S4 D' E 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。8 d5 B: J* l; t, z( X* a/ l
H; b( T4 d$ v E7 P; S' M
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:8 V6 X- W2 S1 M( m2 q( V
# D! w& R: r( h- }6 q, C
```6 e# u9 i5 w; h9 h$ F' B3 y1 [3 j$ O
add_action('init', 'create_custom_post_type');* @- y3 x4 c: q6 C
function create_custom_post_type() {! b( V+ T `* W" N
$labels = array(
" O( M1 m$ i i 'name' => 'Site Wide Notices'," |1 B3 i9 I' {3 {- ?" @# Q
'singular_name' => 'Site Wide Notice',
9 o% \6 N W: W7 e# @1 x 'add_new' => 'Add New',$ s0 O- j& y! L5 k& J6 j% \. ~
'add_new_item' => 'Add New Site Wide Notice',3 L# K2 _# y4 p% [3 |1 l8 }
'edit_item' => 'Edit Site Wide Notice',1 _. f; s4 n" h% `" h- O5 ^ ~* `5 x
'new_item' => 'New Site Wide Notice',+ ]% E% ?9 S: _! p1 }
'view_item' => 'View Site Wide Notice',
4 j5 d" F! `0 t) Z$ F7 Q! o Q 'search_items' => 'Search Site Wide Notices',
& x; i9 x& b8 ~! e0 F 'not_found' => 'No site-wide notices found',# F+ @2 u- I, `" m1 F* |
'not_found_in_trash' => 'No site-wide notices found in trash'
5 \! K* m; S- E0 p9 |( c, L# ] );8 N& z: n0 e$ ~! j
; N# G8 C% ]" O+ ^ r
$args = array(
( I' B9 ?0 M5 E: ?. u2 g 'labels' => $labels," A' ?3 _0 ]2 s% O) T
'public' => true,
, j! o, L- l: D5 Z# L3 V# g- O 'has_archive' => true,
* Z- Y a1 ~3 C. K+ D# c/ [ 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
, W: H2 c! t' Z: B 'taxonomies' => array('category', 'post_tag'),
/ z+ Q6 e: V$ I+ _/ S( c 'menu_icon' => 'dashicons-megaphone',& R, p+ J% O/ N; p
'menu_position' => 5,# _/ @" e8 E. H8 X/ y* i- l! e t; N( W
'rewrite' => array('slug' => 'site-wide-notices')! q/ s8 {7 |; {$ C6 g
);8 z) a" ?0 r G3 t1 C
6 `" c% H2 k- Q; v register_post_type('site-wide-notices', $args);
& }* G# O p0 W o6 H }
4 V/ R1 b9 \ I& |8 V ```
5 `" `6 U( V: i$ m" t; H M( x* p" g& ]" d# O
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。0 [9 o- b( f3 ^1 j' u, D! o* s
0 @* z/ i: Z4 h6 c! h( h; m4 n3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
+ a; e$ r4 y5 r7 ?1 P$ V3 B% Y7 d+ X" Y* I+ F Y' u' a. ~# U
```
4 M2 k* m- k6 T- s/ | add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
t3 a' J# G! M x) W( i" [8 y/ Z function add_site_wide_notices_boxes() {
3 A; F* [, t8 ^, U% _1 H add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');- k* W# Q" ? u+ G5 ~% r7 m
}
/ ]3 R. G6 r+ U o8 p+ @( z+ M4 ^; `2 [7 \
function notice_details_meta_box($post) {
0 X. J! Y* p' Y) Y$ X4 s wp_nonce_field(basename(__FILE__), 'notices_nonce');
1 n6 U1 O O- s2 ~% U* X$ Q $notice_title = get_post_meta($post->ID, 'notice_title', true);) ]4 H$ }1 g( b$ @, g
$notice_content = get_post_meta($post->ID, 'notice_content', true);) u* g u f' s4 W# ^1 U
?>
$ H4 m* ?* l. z2 M <p>
. j6 D& A) J- L7 k ^8 Q; p8 G- [ <label for="notice-title">Notice Title</label><br>( v8 {6 }4 Z5 p T6 [; W4 O( z" o
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
! l1 o$ v" R! d2 X6 { </p>
/ [) j; J3 p0 Q: _5 N, }) Y5 t# } <p>& ] C8 a! r* f
<label for="notice-content">Notice Content</label><br>
: g/ B7 k; J* d: a3 [, Q" d1 l m <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
" h8 S: }0 z) S$ I) ^2 V </p>
) }4 S9 [( N# j* w <?php
0 M+ V7 U* t4 y; N' p }: t& `) {* W1 T+ q- r g
]6 s% |7 {+ h
add_action('save_post', 'save_site_wide_notice_meta_box');
6 B B+ y3 B$ p- r3 _ function save_site_wide_notice_meta_box($post_id) {" W) C# ]+ n8 z0 u
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
" t/ s9 k% k# d return;
$ w, v: \, x9 k& b8 [* z( k if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)6 g) u: P+ r# h- Z
return;
3 z- F6 O. |& p1 n- o7 G8 {
! X a+ U! f5 R5 O, {6 t. x: J; c if (isset($_POST['notice_title'])) {9 E6 ]3 w- u( L1 c
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
/ ?/ }6 [8 c. s }1 I* |8 T6 M% g1 }4 ` ]
if (isset($_POST['notice_content'])) {
5 I' l# U B$ Y update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));' B* U3 Q& ]; ]7 n( F, f( z
}5 i7 V5 b8 _$ x7 _6 O" [, K3 F8 S8 ^
}5 |2 C- z; |& R& f3 ~) }
```" y3 X1 q: D) c; ^3 z. }
( i) ^6 [# a) \& t* c
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
9 |6 t' k" S1 v# ^$ t! W8 l, V. u/ c2 V
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
* @# b# l; q8 h" n
; {, L9 {2 @- o/ }4 G( B ``` K. I9 x8 L5 n. I( p2 J9 O# A
$args = array(
' |2 i$ X- K; M5 d( u( F( S 'post_type' => 'site-wide-notices',# {0 v5 u2 f; j! G& N
'posts_per_page' => 3,
+ X# \9 V# D( l 'order' => 'DESC',
- b. p6 U* Q6 d1 J: t! c 'orderby' => 'date'
( W( E* G: l/ I4 |, V6 w );
6 c3 n2 h. r: y9 u2 H $query = new WP_Query($args);/ G8 y8 y3 c- |7 g- q" e0 a
if ($query->have_posts()) :
9 t2 K# c% H2 {* B# l6 [ while ($query->have_posts()) : $query->the_post(); ?>* @8 p u/ v0 X. T
<div class="notice">
4 ?' G. l, P' q <h3><?php the_title(); ?></h3># @" f% L# I/ G& }8 r a: T: y7 A
<div class="notice-content"><?php the_content(); ?></div>& F) ]7 J6 I2 X# x9 r# Q# d9 N0 B; x' i
</div>
5 Y6 w2 L& J) n5 i4 H <?php endwhile;
" o0 ]: ?6 S; g) i9 \0 ~- q$ m wp_reset_postdata();( A8 Z+ L) f/ o" z2 ]4 G8 j& p8 A
endif;5 L' W: m/ W/ {1 B# d# U
```
: O2 r# K- \* Q6 d7 Z; _
7 `- _/ K7 L% M5 A8 Z8 M 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|