|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?0 M" p( W$ P6 C- H& y5 F
1 h" ~* {* F I, x! h4 ?如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
# l8 {$ H# p' y! h6 W. c, c
' _8 i4 u9 O) A: B$ s! P以下是创建自定义插件的步骤:
8 b* i6 A' ]+ ]+ O( m3 F3 K8 L( i6 a' u0 R" t
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:3 m( e) m/ m- \. I& u' q
5 E8 l1 j- Q. T, r* ~2 k
```
% l+ o. x6 m H) I2 W <?php
$ y, b+ B% m& W& d1 _: _ v /*
0 [% C/ _+ l% A1 {, n8 W$ m Plugin Name: Site Wide Notices Plugin
5 ~/ C( n5 D/ Z5 H# ^& ~ Description: Adds a new custom post type for site-wide notices.
/ |3 ~4 E# T" b8 N* k3 r Version: 1.03 H P" f3 o0 H) t7 X
Author: Your Name
6 u9 [ a) `3 y5 o; G7 J Author URI: http://example.com K, N& Q+ T: g, {: X
*/# S: \/ H; i; T0 S. `
N. G z Y) c# m3 F // Add plugin code here...
* ^ I. o, h5 E/ I- d- V ```% o. O" Q+ y# [4 H7 s& D
% p, Q! E/ a D! k
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
4 y$ q" U% ^1 u* O6 D7 }$ x0 x9 {- _) R0 M
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型: Y2 V- t$ t0 T' |
7 C1 d# z; X/ q7 x
```
9 Q# E/ G, Y0 z. ~& \. y. h; j add_action('init', 'create_custom_post_type');; b4 C) ]7 y5 W: @/ p* _
function create_custom_post_type() {( T8 p) a8 S2 C. h/ [6 f Z: j5 U* Z0 ?5 a
$labels = array(
0 N: g8 ?; c6 Q$ ^, @5 Z4 M e: k 'name' => 'Site Wide Notices',
- i* r5 ?: F+ j: u0 ]2 v 'singular_name' => 'Site Wide Notice',& _+ S: j7 ]3 C1 \, b" X, G; S
'add_new' => 'Add New',) E$ e" ~" f6 T" x7 N
'add_new_item' => 'Add New Site Wide Notice',
4 C% P9 o+ s! {5 A0 W: H 'edit_item' => 'Edit Site Wide Notice',
! l C( o+ A- S" J' o* D0 n9 x 'new_item' => 'New Site Wide Notice',
# {8 \8 ]. D2 T2 g! ^! j 'view_item' => 'View Site Wide Notice',
6 r7 c' r. P( m- P 'search_items' => 'Search Site Wide Notices',3 s4 W: b* u$ c8 h
'not_found' => 'No site-wide notices found',6 }& i* A% N7 b$ Q) F- ^1 [
'not_found_in_trash' => 'No site-wide notices found in trash'9 @6 R" v( ]/ d* D7 Y* p0 S+ W
);8 j8 D9 T' V3 [$ }1 k7 E
# n# w$ [8 c. q; q t
$args = array(! \$ I% j# a/ l" i
'labels' => $labels,
" Y# r) v6 c& ]( U5 j$ g* X 'public' => true,4 r2 A6 U; {5 b3 k! [0 q7 l
'has_archive' => true, D g7 r4 u; q, z- B
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
1 t% {- ^" m3 R6 y- ` 'taxonomies' => array('category', 'post_tag'),
5 ?$ D4 ?! C7 }& n 'menu_icon' => 'dashicons-megaphone', x0 y7 G0 }4 w" s6 J. f& _7 f5 K
'menu_position' => 5,4 @2 W* U0 A1 o4 E5 @( n
'rewrite' => array('slug' => 'site-wide-notices')
. I# n& f, C u& O- g+ v* n, J );4 R% l: V) y* b/ u2 e
& C3 r1 T. Z0 Q- p- k register_post_type('site-wide-notices', $args);
: Z& c3 W# B& q9 M0 J5 [+ d" W2 q }" b/ H& _0 u& O3 }
```0 H( F2 Q8 g/ j5 J
4 D# q! i' l2 r( L C8 n4 N0 }
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。; c" @% A0 M+ J
$ [4 `0 e) K0 D3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:0 H; k. I: ^' }, g8 X
: X0 p: g# _3 h, Y ```, W+ h3 V7 [4 m% k& l6 m
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');* ~5 t% k# ~( }3 B3 |
function add_site_wide_notices_boxes() {
* G9 b! p9 ]/ {1 B. t add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');: n- c+ X c, {
}. d) ?1 {& i9 }# V9 B, y/ [% k
* }% O: s/ Y3 k0 K8 S1 |+ \ function notice_details_meta_box($post) {/ J# v+ `" H; Z( T' `' f3 u8 _
wp_nonce_field(basename(__FILE__), 'notices_nonce');- i$ `, C: k5 e/ a9 u4 `+ d0 q7 q
$notice_title = get_post_meta($post->ID, 'notice_title', true);
6 D$ k9 l" [2 ` $notice_content = get_post_meta($post->ID, 'notice_content', true);0 c7 j* q1 ?; |/ r) A6 F" L
?>
' t0 F) X1 k( O <p>
& N: c) s* F- H9 q& s M3 r1 S <label for="notice-title">Notice Title</label><br>6 t: U9 j0 ^! J/ L
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">& V k& f( E0 A# K
</p>0 g8 @" |6 Q, k' v/ c0 l2 b
<p>
5 Z+ u5 K- ^- F c5 k' B <label for="notice-content">Notice Content</label><br>
& _2 M9 a% z* ~ <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?> B' c. U* M& x2 Z" a7 m
</p>
- U' _% e4 @" l7 a- L& t <?php8 ?9 M' c/ F$ r ~$ y4 A
}2 B" _8 C# h1 B( ?: ^
( C4 s# `: m3 o8 c/ h# c add_action('save_post', 'save_site_wide_notice_meta_box');
+ a( i) Q7 h1 F) H M5 G. N0 d6 } function save_site_wide_notice_meta_box($post_id) {
' t6 s4 C4 G/ n" z5 l if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))8 I% z+ S) u' K. D
return;
; I6 {6 L3 J4 [ R7 f% ?( @ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)1 {- H+ T5 n1 q2 k# t
return;/ z! u- l* Q6 g1 a$ S/ F
1 N- U& U$ Z: o' W& Y9 ^, C0 _
if (isset($_POST['notice_title'])) {
# l: ?/ z( x9 ^7 p" w/ M- T; f+ Y" r update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
1 T) k/ X( `/ g5 O8 ^% P5 ] }
) [: M7 o9 Z w4 Y Q if (isset($_POST['notice_content'])) {: N, ~* H) {* {& V8 e: |+ {' j
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
# g' g; ]9 E) j' } }# [1 W, j, z }1 g8 E0 s
}
" ?" {/ Q5 K: [: E7 G' z) a ```
( ?2 K4 ~7 U, |% S$ G! ^7 `* }6 ?* U
6 p* r: W: v/ @# M; X 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。- v( }9 E, a4 x1 H
: x9 a4 i5 P: t$ `) T" t
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
( R+ S, }+ ?8 a) w' W. r5 ?
0 E: j$ D7 L) I2 U) H& J ```7 O1 o2 D) B0 ^/ i) A$ P
$args = array(
8 w- H8 ?! o# r2 b 'post_type' => 'site-wide-notices',# P: d( U) T- G2 ^. B* a
'posts_per_page' => 3, `" F5 u, `" e- [
'order' => 'DESC',
G3 H& ? g6 K4 S+ i6 h 'orderby' => 'date'
# ^& S* Y$ D a( I! }1 X );" r8 @, L7 d9 ]- K/ Q6 o2 Q
$query = new WP_Query($args);4 w- I) z5 A2 j3 G! W7 x: \
if ($query->have_posts()) :
8 q- d/ t7 x6 x% J$ \" d while ($query->have_posts()) : $query->the_post(); ?>, U/ I. N( m& l
<div class="notice">& E0 ^/ W$ q0 i; \, Y* C
<h3><?php the_title(); ?></h3>+ y9 `" u; s" F8 Q. @
<div class="notice-content"><?php the_content(); ?></div>
: I# G Z5 K, |) u* j' A/ e+ ` </div>1 b) a1 T0 G0 a% g' z8 X
<?php endwhile;9 V8 U+ s h/ ?! B& p$ R
wp_reset_postdata();" k" @; L9 i7 ^3 J1 F; X. I) n
endif;
9 G1 N9 Y' Z( @8 ], ?# r" q ```
0 S+ d ^5 e! T) _) u4 ?7 a9 \. q+ e: J( R8 j/ x8 R+ n
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|