|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
+ H2 q' U' B+ m9 S
9 G$ u5 q& Q- Z! k如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
0 _ g7 [+ q3 S' `! J
8 N5 Q w" T' W7 f9 z以下是创建自定义插件的步骤:
% Q2 l; e7 j6 |/ X8 M% A+ }: J% {# E3 f2 ]0 u3 s3 P% z
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:, H4 d8 |2 W! G
5 ^% ?+ T- M, {5 h ```
0 b( C/ J% e' k+ L6 y <?php0 a6 G# r. [. Q* w
/*- e5 Q% u6 q# J" z# d6 `8 Y9 ?
Plugin Name: Site Wide Notices Plugin" _4 H2 g/ Y2 }- H( i& M8 v& x
Description: Adds a new custom post type for site-wide notices.
& L) p# q7 D# |' ^7 N/ Q Version: 1.0
& A% c/ ]$ _5 d Author: Your Name
9 B- K9 v8 A7 d9 S+ X Author URI: http://example.com
. e5 N7 u% \$ T6 V0 ` */; H5 s0 N1 M6 S: _- f2 n! W
3 ~6 _, x* d! H7 _8 f( D3 j; f // Add plugin code here...9 S# t4 ^2 g( F: e! N4 n
```
8 p3 n2 i( W1 Y1 f: u
3 p w& @% @) k, c6 E9 X/ q, F R 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
) Y6 E7 L! P) T2 c3 T
- b {- v4 G% ^9 Z1 V7 O) f2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
+ I! k2 k$ o9 K2 n3 i- }% P& W( x+ w2 r9 [9 {" V( A
```
8 }0 M7 x1 s$ r0 I add_action('init', 'create_custom_post_type');
4 r @5 _( T# t4 t V# W4 _# d function create_custom_post_type() {
6 i" `: Y" z. g' _& F2 E- w $labels = array(; W& e; f# `" Z/ V
'name' => 'Site Wide Notices',
+ x R3 r1 h$ o$ J( t4 _ 'singular_name' => 'Site Wide Notice',
8 Q/ A$ v2 W: m 'add_new' => 'Add New',
$ t( l }' c* ? 'add_new_item' => 'Add New Site Wide Notice',: M2 m8 v) h6 [ o8 W* H" n% M
'edit_item' => 'Edit Site Wide Notice',$ s! L" |9 z* t0 _4 Q5 ?
'new_item' => 'New Site Wide Notice',6 a7 x$ O3 T8 A1 g; n
'view_item' => 'View Site Wide Notice',+ y. U1 Q: S$ S+ }( V0 {3 H4 A
'search_items' => 'Search Site Wide Notices',! ?5 Y4 {5 K* o; o3 R
'not_found' => 'No site-wide notices found',+ | I4 p- P1 N, w3 p! Q8 g
'not_found_in_trash' => 'No site-wide notices found in trash'
2 D F; u4 K1 u5 [* s8 i0 ? );( F- e; `- k" ^6 E9 \3 A/ Z/ N
! A* W- s, l6 V: q! M$ S* q* x
$args = array(( M3 z* O, N! Z4 H! N
'labels' => $labels,) a% F" D+ N7 _: O& D
'public' => true,
4 }" I, S6 A" Y 'has_archive' => true,
5 d3 @. Z. R6 I2 ]$ |4 m3 M' p6 ` 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
4 D# p1 i2 W; `: E: [ 'taxonomies' => array('category', 'post_tag'),
# g6 \4 H1 R$ T+ l 'menu_icon' => 'dashicons-megaphone',- f& t0 k7 V5 r6 {; K4 N
'menu_position' => 5,
) p. ?: w' K9 J 'rewrite' => array('slug' => 'site-wide-notices')
! L' M- `, Q2 k! D' @: p& L );
( I$ a9 S1 o* E$ y3 W6 [; j9 I u6 J. H1 k! S3 l7 N5 } G1 w
register_post_type('site-wide-notices', $args);
% f$ w. y+ Z* G6 w+ b* H% [+ e* P( N }
|. ^% [+ A, ]8 ?9 x+ ? ```! U2 f3 B) ]! m$ _, j) p8 [
5 O0 N4 i5 D7 y6 V
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。6 d9 _0 N9 h, L% [+ W( A
" q1 v9 J: c' x8 @
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
7 g& P h, Q" u' N% O+ G
2 d4 R7 F3 P3 d$ L ```
' }+ ]! a5 R) ]. z! I" U1 e+ Z add_action('add_meta_boxes', 'add_site_wide_notices_boxes');( t' B x0 H! ]0 }- h6 q4 \: f
function add_site_wide_notices_boxes() {
! [3 \! [/ c" J6 e add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');/ T3 q1 O! ~4 D. G6 o( S5 ?
}
' F2 f% Q0 H! Y- r8 }7 H/ s9 U5 Z! k, \" @6 ^3 T) P) W, e' N
function notice_details_meta_box($post) {
4 O$ P3 W" ^: K# r9 b wp_nonce_field(basename(__FILE__), 'notices_nonce');' m4 h/ n) t6 f5 |) S( M
$notice_title = get_post_meta($post->ID, 'notice_title', true);
6 A' e1 ^+ _) P7 F v $notice_content = get_post_meta($post->ID, 'notice_content', true);/ z' ^4 X" ?' {5 b- Z6 ~$ l
?>
1 l9 V: U/ G6 p9 h8 w <p>: s) Z; O5 R7 u3 H, `7 |% i
<label for="notice-title">Notice Title</label><br>$ s& r1 s( {. x& Z
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">* Y- \$ F1 ]6 c r1 w
</p>2 k/ w$ t! M$ s. O" J# K& v
<p>
4 T! a0 E( H$ m; B/ q* t. U <label for="notice-content">Notice Content</label><br>* i Q, ^5 I& A* p( x
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>; U6 w# W1 c2 F/ x% k8 ~6 J9 \8 W
</p>. z& H4 R$ R% j" D) J
<?php8 T, O5 M2 A* I
}6 ^6 G# |1 C- }& u$ Y0 ~. @2 g
! t0 ]) [3 S" |% _( X add_action('save_post', 'save_site_wide_notice_meta_box');8 ^3 N/ \" u0 C+ Q! A! I/ m5 E- @; u
function save_site_wide_notice_meta_box($post_id) {
1 v* w$ E, Z8 ^) G& }, T0 A7 v. G if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
/ X! Z5 P$ J! A6 i- y9 U( p return;4 Y5 ^$ p' k0 {& Z
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE). P- D3 _8 K3 e; F4 ~/ R* i
return;
' N- G& v- |5 g7 f' Q) y, ^& Y3 x9 m6 b# B6 @' [" W
if (isset($_POST['notice_title'])) {8 W( N, w7 @2 W
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
+ P3 J4 U, H. e% a1 x* o% i* c Z }+ I6 f7 J$ `6 \
if (isset($_POST['notice_content'])) {
% Z' `) m& T9 C* t2 l update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));( z& d0 d( L; A$ E' P
}
4 I7 [+ b8 G2 L% r, P }
& F$ [1 S. \7 g ```$ X/ d# k2 Z: B( v4 c
% C# s5 @( g" h7 T 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。6 }1 m' R5 J& |* {1 _4 U8 r- _
; q" J* w5 {5 C- h$ ~$ v- l/ R- f( b4 l% O4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
9 D$ D' c" A4 p' D3 F
0 {5 h& S& Y( E5 H2 s ```' ]2 t+ q% m' I7 A( p$ c7 x& _
$args = array( D S1 I% i9 E* W
'post_type' => 'site-wide-notices',: R4 \$ q& g6 S5 \
'posts_per_page' => 3,
3 N. @7 a" d% \% {% Q6 [( k$ t$ d5 _$ Z 'order' => 'DESC',$ e( {( R: N1 p
'orderby' => 'date'
- p2 i8 X' C3 X4 @ M' Y' I );) \& J% h5 [$ R& s
$query = new WP_Query($args);0 ^8 E$ f6 h9 }
if ($query->have_posts()) :: |6 C" R6 {4 ~* x( h
while ($query->have_posts()) : $query->the_post(); ?>
) d' V4 q I4 |) o <div class="notice">
1 \) I, ]! g) P <h3><?php the_title(); ?></h3>0 }2 T$ s$ N) n) D
<div class="notice-content"><?php the_content(); ?></div>' ?% e7 Q# ]; m3 Z" x/ h/ V5 [0 E
</div>
5 K: V9 A: n$ [$ W) c7 I <?php endwhile;
7 b# |1 F# d: l1 e" _ J' N wp_reset_postdata();- D$ V7 x1 ?: z C
endif;/ }; a4 s2 K) A8 H, O4 l
```5 e* X, I" i! V* O6 Y+ V* p* h
% T' g6 [6 h7 Q, b' E* F! T
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|