|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
6 P7 ?; m' T3 H! j- l+ a3 J: M+ O& X6 ~- S Z& c' i
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。6 C. q0 F- u5 }$ l3 N% p2 y
6 N' s5 f) y/ D% y, |, Z
以下是创建自定义插件的步骤:1 g8 c4 P6 {# o4 \0 Y. F
- m( @! X, _! V3 J
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
+ s7 h9 I: {% y6 R1 s7 B& j5 q
( }; R4 D' X# o: f ```6 Z, @0 R/ H G* v0 L9 ~
<?php
1 N6 _4 M. x. R& r) `1 c; Z: t /*
3 m( P5 x+ @( e4 O" v Plugin Name: Site Wide Notices Plugin# W( `/ o( a' c
Description: Adds a new custom post type for site-wide notices.* [" ^* A i* @0 @
Version: 1.0! S3 [' _( a. D, t
Author: Your Name9 u! j9 q; H4 W% v7 e
Author URI: http://example.com# a0 \; u. |1 n$ ~
*/
* L* F; N$ A! x. ^& }' V) G8 x7 S: m* G) ` R3 |
// Add plugin code here...
( F' G2 P; Y, ~( B" _/ ^; [/ V! a ```8 x. [2 `4 D( R. K
! z3 u1 [2 B6 E, v' f1 m0 d 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。6 U9 J$ E4 z4 V
) s$ T+ X# k b) S0 c, Z; o+ R2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
. O: J+ W4 e) _# @$ U8 G5 J* p# c! I
```6 B9 Q6 d: P/ N; b( @
add_action('init', 'create_custom_post_type');7 r% v! Z# @" ^3 ]6 D- C5 m% N
function create_custom_post_type() {
6 b: f& a0 A5 { S& G' C. Z5 } $labels = array(* o7 B" l3 K2 H
'name' => 'Site Wide Notices',$ T+ a( m( X. b( Z @! D
'singular_name' => 'Site Wide Notice',3 P2 A9 v; {# M2 }6 N, _: J
'add_new' => 'Add New',
6 L' T1 {/ B- s: C 'add_new_item' => 'Add New Site Wide Notice',
5 M! r" E6 C6 B/ v3 \ 'edit_item' => 'Edit Site Wide Notice',
- P2 t7 W4 W% h! G: | u1 m 'new_item' => 'New Site Wide Notice',* Z- ~1 m" \& n) F. V( d1 u6 P7 e
'view_item' => 'View Site Wide Notice',2 r6 n# S% q+ F9 f2 a- i
'search_items' => 'Search Site Wide Notices',) C3 g8 N! R* F. \ u
'not_found' => 'No site-wide notices found',+ D2 a9 A7 D8 T+ o! G
'not_found_in_trash' => 'No site-wide notices found in trash'4 m, u% C5 H Q2 |( _
);
: D" C; }! H5 O7 o3 }: F" z& Y# X
+ h9 M9 m- p& j- U $args = array(% a: ?0 Q4 Q# D0 X1 @+ S4 l
'labels' => $labels,
- \, c8 F+ I4 j6 V- l. s 'public' => true,
/ q: {: E/ e% u! U* x 'has_archive' => true,
; P' |( r# m/ J 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),0 ^5 I+ P5 L$ j+ `' o
'taxonomies' => array('category', 'post_tag'),
/ \% s! U; a/ P Q 'menu_icon' => 'dashicons-megaphone',7 o2 W; K* ?! M* j& n
'menu_position' => 5,
/ V( z/ T" {5 O 'rewrite' => array('slug' => 'site-wide-notices')
& o+ e( f! q: {5 h% Y );
, }. Z# o/ a- N8 Z) h1 z" P9 A2 M5 G+ I) J6 ?% Y
register_post_type('site-wide-notices', $args);4 I1 U9 U5 A. \( K: G+ b }/ b' u
}
& l6 m, V' V6 S) ~% g; Y% {# D ```5 m8 v6 g0 Q2 b9 e, M2 ^( f/ w5 q3 O
+ c) i' h; O; K+ N' [ 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
- Z1 t7 L0 `3 Z, Q: \1 `+ ~( ^4 C9 [5 T2 q( u9 y; I
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:" P4 c% b9 c! t6 ]
) u2 n& q" j9 `9 y+ o( h ```
# C' ?. r. v$ p; b4 M+ a4 Z, N, Y add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
0 V0 A- N- U6 i/ d! }* g function add_site_wide_notices_boxes() {9 r4 j: y* f, D2 C' N
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');7 u' y$ n1 ~5 a! [8 {9 c
}5 w. ?& C7 H3 c
- k. d6 s; }6 }- H5 @/ p ~' R& z
function notice_details_meta_box($post) {( C6 z9 Q; D. {; C! }0 U
wp_nonce_field(basename(__FILE__), 'notices_nonce');: o8 x8 _, Y1 u" X9 A
$notice_title = get_post_meta($post->ID, 'notice_title', true);
' \9 c, G: [, l6 P3 O& r; e $notice_content = get_post_meta($post->ID, 'notice_content', true);) M; s; a+ D( C" f
?>- G" K& U" ?; P b! @8 z$ ^
<p>' Q' d& c4 ?2 ?
<label for="notice-title">Notice Title</label><br>2 J$ l/ z! f2 w7 z6 X6 y& |2 W
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
' e! x. r7 Q' z4 [; O7 A% g- h </p>2 w6 I6 M9 O/ h& t
<p>
$ s* @2 Z# u* P <label for="notice-content">Notice Content</label><br>, d. Y( h9 y" \( G" t
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>! e, E: q, w8 _8 }! M* X) z2 o
</p>
1 k) a4 ^. \. R- B Z8 I <?php9 F4 z% `0 U$ t' k8 z
}
: P2 A2 ~4 G' w9 Z" _6 _5 w' Q8 h. x2 ]( C
add_action('save_post', 'save_site_wide_notice_meta_box');% o6 @9 b3 l$ W) @5 \
function save_site_wide_notice_meta_box($post_id) {( ?8 B/ W& n" U. {
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))): O5 J9 p, D5 o7 y
return;1 T, Z! [* [5 a' T
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
7 e# r( j& q3 ]+ y( { L# l return;1 o8 s) ]( P7 @ ~9 P1 p3 |, z* m
8 s# `: c5 J, I) V if (isset($_POST['notice_title'])) {
8 D: j' c1 s( k update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
3 L% e7 K f- @3 [6 x# |' Z }
5 b) c% N3 U8 M3 ]) _- Q if (isset($_POST['notice_content'])) {
2 U* k; P% D5 w% c update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));! I" A/ o+ m b; J( J
}
& r' Q9 }5 `3 f7 ` }" t( m9 E/ Z! W( G4 z
```
3 Y1 {# o) T- Z( I. y4 ~7 h
) c* |9 }( M# } f* X8 N+ O 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。. s# Q. B% }6 g" T! p& f8 D
5 N% Z( n, I. V9 T! A1 o- @
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
2 [; n. y `% d' O" I! n2 r7 p
g7 R: ~) ~" ] E ``` L8 A% G* I b9 t( {
$args = array(
' Y9 `! R$ J) } 'post_type' => 'site-wide-notices',& D @* M. c b! s9 a
'posts_per_page' => 3,7 T( y; N4 i) i7 ]3 q( r8 m& @
'order' => 'DESC',
; t0 ^: b! I9 T4 c7 z' I9 l+ S4 d 'orderby' => 'date'
! V4 n/ t+ r/ e' ?; H7 D );
& |% S# s) c, y2 L v& d $query = new WP_Query($args);0 B5 i9 A5 C" G" V
if ($query->have_posts()) :
- G" \* C: J0 i D. o5 h1 Y8 W( ] while ($query->have_posts()) : $query->the_post(); ?>
' q+ a! `2 f5 f1 I% \) _0 Z6 R6 ^: F <div class="notice">8 H& F/ s" d4 b
<h3><?php the_title(); ?></h3>" R' f3 w7 ]1 V3 _- [
<div class="notice-content"><?php the_content(); ?></div>6 b1 S( _0 j$ f
</div>
4 e9 |' c! l( f' T0 y2 ]% P6 z% K <?php endwhile;
9 Y- j4 L- [3 Q" ` wp_reset_postdata();& v) J [) _$ n/ v g+ {
endif;
# T( U( G) K3 [) w- p ```
/ s' R8 g! Z- q9 V
! a& ]9 F$ U' s0 _. L# k! ? 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|