|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
5 [% Z; [2 n c" o; u0 O F
% ^3 {( @ R& R+ P' c, x. y+ Y8 {如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。% i0 I4 C1 W1 s7 F5 D
4 }) e; S8 X$ J4 Y) G; J+ L1 C以下是创建自定义插件的步骤:
: M8 v w6 a% @
- }) D/ S* B! S( g: U2 T1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
; P$ t0 H4 K3 T' s. c. b
0 h- x* Y0 Y" Q6 A" T( N9 P ```8 D# k8 k2 s) V$ m) \! [
<?php9 a2 B A+ D* O; m- g8 l
/*
2 h2 ^+ D9 \9 W A$ w7 }+ g Plugin Name: Site Wide Notices Plugin
]! U8 o" r7 m# k Description: Adds a new custom post type for site-wide notices.' H( |. e7 C" u, C3 x* W
Version: 1.0
; v L0 `# T( {( @; J2 [! W Author: Your Name
8 h' G& O( s: A1 h* ~% X6 _3 Q6 S Author URI: http://example.com
* H! k4 }5 o6 k8 Z# C) ?" U+ h& v */& [' M) D4 @0 P) z
6 @0 X* _- X% P% e2 Q$ T7 h( @: v
// Add plugin code here...- O' x/ y* A" l5 i
```
, R5 O B( F3 u6 [# X2 e, l, M$ l$ I& }
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
( Z+ c- X! A9 {$ L
7 B9 t# j* O. O+ {& F2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:9 `9 v' V; U# C1 `+ G: C s
$ |* }4 g H7 B+ K" F/ L ```
: x K, g) g$ z- f: [ add_action('init', 'create_custom_post_type');
6 s& i) Z& X* V: d- F0 b* d( B) w0 ? function create_custom_post_type() {
( h9 f8 b9 z* b7 i $labels = array(1 X2 D( C6 d9 l( A/ X0 t
'name' => 'Site Wide Notices',3 J. k8 u" C& |+ V
'singular_name' => 'Site Wide Notice',; b- C# A9 R8 q& y
'add_new' => 'Add New',
! z; o( g& P; N1 `1 b6 E7 y 'add_new_item' => 'Add New Site Wide Notice',& N; `- a2 l/ J
'edit_item' => 'Edit Site Wide Notice',$ [4 i8 C! x; D: Z4 r8 u' `
'new_item' => 'New Site Wide Notice',8 a6 {& u# W2 z5 }; g! F
'view_item' => 'View Site Wide Notice',
( h, h" b' B9 P% T& x 'search_items' => 'Search Site Wide Notices',8 L: s- d7 g o U; D
'not_found' => 'No site-wide notices found',
4 S2 E; \7 ~! u6 f 'not_found_in_trash' => 'No site-wide notices found in trash'
6 T6 q! ]; H. m8 u7 V1 X( x1 ] );6 Z! C" `* \7 l) \- h& M) O5 D9 V4 M
+ s( |3 x- z( J
$args = array(
/ V. Z, T- J: s8 B$ q 'labels' => $labels,
/ l: @6 j! [0 \ R" g( b3 r2 m 'public' => true,0 p6 A% G3 b. ?0 N$ f9 _ a* C
'has_archive' => true,
8 J0 U* S+ ~$ F( w( F 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),' u5 _$ H) h! P' P
'taxonomies' => array('category', 'post_tag'),% O7 b2 B+ B7 _6 L4 j! n; P. {3 O
'menu_icon' => 'dashicons-megaphone',+ P: L( X5 ?% B2 y
'menu_position' => 5,
n( j8 r% V6 Q9 y3 \4 d! Q 'rewrite' => array('slug' => 'site-wide-notices')
+ E2 b) }) K% {! h e c1 ] ); G5 I; \, b3 \- d. `% Q8 _9 b K$ g
" t5 z8 T6 H- h$ }2 D' j# S
register_post_type('site-wide-notices', $args);; m+ X' d. }- f( B6 l1 U2 J9 H
}
4 [# R4 Y1 A) E ```
/ j& T+ a9 @, X" i4 `
2 b9 W$ r' `' T+ M, m 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。5 V' j: Q3 N( x( ?
4 E; e7 j5 c/ O8 a% r, ^1 ?, I3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
; V, X; {3 m5 [" E0 I9 z
- ?6 w2 j% B9 p$ H7 U! } ```/ c) r0 V2 G! f5 V* M! A& u, w8 n
add_action('add_meta_boxes', 'add_site_wide_notices_boxes'); p# T0 ?5 ~* a# [' K ~/ Y' q
function add_site_wide_notices_boxes() {3 E9 I- T4 S, I
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
1 |3 h- _9 d* s: _; s5 N1 Z1 }- Q1 u }, i2 M- W1 o5 o; B8 b$ t
8 A) p u- n; K# j, w) `/ J function notice_details_meta_box($post) {' y4 a" k/ j# O7 N
wp_nonce_field(basename(__FILE__), 'notices_nonce');
7 d' b C$ D" X8 i0 W B7 b) I $notice_title = get_post_meta($post->ID, 'notice_title', true);/ k# I! M! |3 V. H
$notice_content = get_post_meta($post->ID, 'notice_content', true);: \& Z! X7 O; X/ R3 `( _
?>6 @* @+ W1 [( m1 z$ W3 H; C
<p>' v7 ]* Y9 o! Y) T4 z$ u! K
<label for="notice-title">Notice Title</label><br>
; F, |$ A, v" Z3 @/ w <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
2 W" o3 Y% |# m" A6 \( y& b </p>
2 ?& X, i% G$ t$ Q: y <p>
, l) j/ F0 K. F/ r' A6 {$ m <label for="notice-content">Notice Content</label><br>
; [" ?. v* \9 f' S8 H7 d <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
3 b$ j' A8 g- r- J+ ^ </p>+ C) q. m6 w# s% ]. Q& U8 @) }
<?php
9 @" I a! b2 ^( P8 @! ^ }5 s1 ~% z( W( f1 b: s2 N3 h
. U# b% \+ p) H# z3 M+ o add_action('save_post', 'save_site_wide_notice_meta_box');# a$ Q4 @0 K! A5 o% v! D/ d
function save_site_wide_notice_meta_box($post_id) {
G g- o: `/ L! x/ x if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
: Q, \8 {* t* D! I return;
: @$ S0 C$ b% |) x; s if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
) \4 ~! F4 E. i/ f- p; p1 v return;
5 X; p: f# J( D: h: b9 G
2 f3 ^' z, N/ t2 U- i O if (isset($_POST['notice_title'])) {; C& }. _$ R5 L6 A; \; o3 n2 _6 a
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));# ^7 R2 @" u5 b+ G
}% k) c% u( A9 F* e6 s! d0 n5 P% ?
if (isset($_POST['notice_content'])) {
( ~# z( Q1 u0 C/ z update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
% C- \+ ?! s6 l& G2 _) s7 l8 B }) |5 ?" r8 f/ J" S9 e" M7 O
}
' ?$ ?+ l8 Q9 R1 {0 H' R ```" \& ^% G3 y+ B
$ U; e' S' D* o9 E* w 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
( t# p U# K3 W& i2 z5 s8 V% z& H7 {2 X, x! m
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
, n- j$ n2 U: f4 ~ ^. y7 y7 ?( M6 Z5 \
```
# {- ~" i, j$ S& t; w $args = array() q) S' P4 I2 d) P
'post_type' => 'site-wide-notices',
5 Y3 i) E7 l! [ Q' H6 }+ D 'posts_per_page' => 3,
# V% Y& Y3 u% X* w2 q- E 'order' => 'DESC',9 {. n2 O0 u2 z. g9 f" |6 e5 s
'orderby' => 'date'6 I( t; ^( I" F, a9 [) v" ~
);
0 F/ H, m: z3 \" k $query = new WP_Query($args);
$ _- h# G6 E' F# q- T& b/ p8 c if ($query->have_posts()) :9 n& e5 T& e+ c. ^
while ($query->have_posts()) : $query->the_post(); ?>
9 U, c% G8 a) t" ~ e& Z <div class="notice">
" k3 q- `) m4 S8 O) ]) Y; C) Z <h3><?php the_title(); ?></h3>
1 e' ^/ I( c7 X) N <div class="notice-content"><?php the_content(); ?></div>( x5 T& m& v$ [; f! ~& H* d
</div>
- B$ K. A& r3 h- E# V# l7 z( D) K+ m <?php endwhile;
7 [! ]1 l4 ^& C- ^: `$ y wp_reset_postdata();$ s, b# X# u; }( u. }9 Q
endif;3 `8 J r* ^' n$ Y; [" W
```! J: G6 g4 i8 @7 ?- e, X' r4 {
0 Z' I/ w Z# P4 |& g
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|