|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
7 O% d% s- h8 {% g1 q6 C+ F! D/ N% O% [1 }) G/ x
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。, r5 ]$ L2 M6 L& `% \
' |! ^$ r1 B- Q( P6 H4 F7 @. T2 P
以下是创建自定义插件的步骤:
6 `) E4 j' c x3 L4 W, g1 n5 a# s9 g; ~/ c- l" d' [
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:1 x1 H! }" {1 S7 b
! Y# t6 f% X F' J r) |
```
2 G7 X$ L0 @' N4 w, o! l <?php
) p: T3 k# d0 Y; ` /*
/ T7 O* z+ w2 i; x/ D! u: y Plugin Name: Site Wide Notices Plugin
# M# s- Z6 N1 V; O; p; @! \8 t Description: Adds a new custom post type for site-wide notices.: `' t8 W4 B9 q" b) Z# k
Version: 1.0
( P" C, P! _+ n4 L Author: Your Name
6 B2 \6 |, y! W. d7 s Author URI: http://example.com& s: m- g+ f" [ X" r. Z; Y$ V
*/
9 Y' N# @7 R. z9 {. S1 O. z: W ]7 R/ W9 e. C5 }4 P3 |
// Add plugin code here...) n' n9 ^" E* f9 F6 A: S8 p5 d: O
```( t d% K" ?2 T
9 ]/ j6 z' N/ U7 m A- T( j! ] 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。1 M1 q$ E6 c' ^3 |
( F' l5 v z8 x, [& `7 } E2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:& z7 o5 y& ^0 B: |
/ }% {- u% c' d$ K( l
```
5 C9 G' v7 U+ I6 W$ b) i% z add_action('init', 'create_custom_post_type');) g, m% }& H& g7 s* I. ]
function create_custom_post_type() {7 E0 b7 V( C1 X( p! E4 ]" W* f
$labels = array(
1 e6 {1 V) W# L+ D7 U! X' K9 } 'name' => 'Site Wide Notices',5 ~( m9 f! H* q6 g$ V: z4 u
'singular_name' => 'Site Wide Notice',
4 E& B, o6 \7 O4 O 'add_new' => 'Add New',
6 O1 g) i7 I0 \8 |" y' r 'add_new_item' => 'Add New Site Wide Notice',1 Y, ]6 O! ]4 V9 N9 Z& }$ l4 G
'edit_item' => 'Edit Site Wide Notice',
( L" @2 {4 I: `& Q; ^ 'new_item' => 'New Site Wide Notice',
3 U! p3 G9 x5 ?, s$ A 'view_item' => 'View Site Wide Notice',6 \ D$ R% S. ~2 N8 r p' c
'search_items' => 'Search Site Wide Notices',, x6 T/ l8 d' n! j- L0 l! `5 U( _8 g% E
'not_found' => 'No site-wide notices found',
' i* t- z/ X! X' ] 'not_found_in_trash' => 'No site-wide notices found in trash'1 j$ R0 A7 ^4 v$ |* B6 {
);
9 [5 w6 `6 W" x0 Z4 U1 ?/ I& j2 t, K0 y! s1 t5 j3 S
$args = array(
" d* @) F, \+ v2 e ?. F 'labels' => $labels,
5 \8 M0 H: }3 T/ z. ? } 'public' => true,; B+ h! W6 s# Y, J( \
'has_archive' => true,& F$ S, g3 S5 P0 J
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),6 f L7 T$ ~5 Z/ b2 z8 v! b5 [
'taxonomies' => array('category', 'post_tag'),
/ F% R; \- s) {3 h* g 'menu_icon' => 'dashicons-megaphone'," j6 B3 l$ t/ F' _
'menu_position' => 5,7 F8 T* Q# e0 J/ d! v
'rewrite' => array('slug' => 'site-wide-notices')
7 H; y+ _) Q* H# M" }3 ` );
3 ~! R" [4 \7 |- @, g* I0 U K- m$ q' Q5 X! n0 c7 M
register_post_type('site-wide-notices', $args);1 R' ~6 [. w6 a
}9 H$ t/ K/ N! l; c4 g% e( v3 k
```
5 ~ A! V& ^: T' ^9 \0 t4 Z: y" W" ?0 p% V: U! S3 _
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。" |8 y* }0 E" d7 U3 ]9 y8 p
2 D5 u9 B9 w I8 W; R3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:0 i7 }8 J/ v5 ]5 e2 @
! n/ ^8 d, g4 s: P0 x/ o, ^
```
$ ]% J! ?2 B& I. p1 ^8 m9 c add_action('add_meta_boxes', 'add_site_wide_notices_boxes');9 O; o" b$ y6 h5 x. I! n% X
function add_site_wide_notices_boxes() {% a) C4 j( A, b& ~
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');/ P+ N$ ?% ]" _% t/ ^0 n5 u
}
5 i* W3 G2 z7 P5 n; H
1 I# l0 e4 O% ] function notice_details_meta_box($post) {
0 u% T; d( O# b. Y' ~ x* x wp_nonce_field(basename(__FILE__), 'notices_nonce');
$ o# T5 i" Z$ Y. B $notice_title = get_post_meta($post->ID, 'notice_title', true);5 M. B/ j) i: Z d+ S
$notice_content = get_post_meta($post->ID, 'notice_content', true);! L0 j- _8 _. R. W' N
?>
|; Z: K- d2 p6 T. p* o- n <p>& G5 j8 T$ h: x; C
<label for="notice-title">Notice Title</label><br>. i$ B9 p8 O/ t
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">: n2 @( h0 t/ E5 s6 n
</p>
7 n" v: p% P8 H# |5 I <p>; Q3 s$ z, K, l7 c; z1 F/ f) D
<label for="notice-content">Notice Content</label><br>) H9 z0 \' x5 ~5 M7 p
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
% h: M" I$ G: G" N4 T: C </p>
8 O" d' h5 v2 {" O( v <?php4 b( F* ^2 f" v2 @1 T
}
% u& E, [. o4 z+ \3 h( W/ _+ ]
3 Z& Q+ D" c4 \& e( `7 X add_action('save_post', 'save_site_wide_notice_meta_box');
! s6 X- n. T6 p6 D2 r% Y function save_site_wide_notice_meta_box($post_id) {2 r% W B) c/ t6 s
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))). J7 H+ m* Z' t5 J" E
return;+ ~& U$ ~1 `5 | J( u4 H
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
, m& t" O; r& E. G7 B9 Z* n( O return;
3 t# w }3 y5 z: [- U# Z
: F/ V5 [/ y1 b3 N+ o9 z if (isset($_POST['notice_title'])) {# T& F& S( E- O! E$ O& w
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
# ]. v( y% Q* R7 S2 A. f N, S }! T* F0 `4 Q9 T' {8 \0 O, M& K
if (isset($_POST['notice_content'])) {$ M& k& S/ }+ D6 t* u
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));8 X3 B( S& O3 w" S |8 ~2 W- W! M
}: p: q' K4 B' i2 ?
}" n: @- |) o, `& l* t
```
8 l3 u3 {2 O4 e# E5 q5 S# q) k5 h3 @; w% r7 W+ ~4 i
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。5 C* c) }0 n# T# x
5 N% A2 A0 M: r R4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:" M6 X& f) r' s- g
- x' S+ n9 u' F* o: w& B4 A ```4 z! v2 f, }# c; p* L' G' K
$args = array(6 ?0 ]7 o' j! [" H7 e9 a4 d* Q
'post_type' => 'site-wide-notices',
8 `& g8 ]& f8 Q 'posts_per_page' => 3,
5 |' j5 A# R& @# [ H7 C0 T* |$ o 'order' => 'DESC',
7 r) Q+ ^3 e+ y, x. _; h! \ 'orderby' => 'date'
& B# d6 S/ u/ l2 o ); T! j2 l( S" x% g
$query = new WP_Query($args);) e" w4 l* |9 M6 O5 G7 G
if ($query->have_posts()) :
. j& r3 n# O6 B3 c$ {6 h% p1 _4 Y/ a while ($query->have_posts()) : $query->the_post(); ?>
8 `2 o' {$ x! q+ ~4 Z' [+ e' r <div class="notice">
. r$ ?" n5 C, n <h3><?php the_title(); ?></h3>
+ c ], P8 k3 u: J, J. s, y <div class="notice-content"><?php the_content(); ?></div>
$ ?+ p; v$ C3 d3 p7 S </div>
" ^6 y: h7 y# e" f* h4 L" u <?php endwhile;
`! K( f/ B( R& ?7 R1 X wp_reset_postdata();
; L6 J0 M5 a7 u/ V! B5 r endif;
3 o7 z8 E( N+ d( L! w/ @* |5 Z# g ```. W* i: C7 I/ y- o* I0 `0 x; o
- _, o, G/ M8 ^0 @1 e0 @6 X. Y 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|