|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
0 l* u8 M+ {: P% K5 _& \8 s* H. a/ w+ s* n/ P
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
( p/ V$ P- J' e! {3 z4 b; L2 o! |( o8 t6 n2 \' U8 x
以下是创建自定义插件的步骤:& R: Z) l' S, g$ d& ]. c: t
8 ?0 ^- \' u- }7 [1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
) o7 B- w/ L- b5 f! Q0 O7 m5 U" S" c, J. T
```
6 i" U% P; D# y1 k <?php5 x) x9 l6 A8 m- ~! z0 ~, }
/** I: [2 M' }8 N6 v( s5 q% ~
Plugin Name: Site Wide Notices Plugin; ^1 @. L3 ?# l* J. W, j3 u# _
Description: Adds a new custom post type for site-wide notices.
& W; D) H: ]0 K) Z2 y' E% X Version: 1.0
- Z9 M; X. U6 }& ` i Author: Your Name4 _, M7 o6 `. R z. L; i$ F% \
Author URI: http://example.com
" [& h! u, R3 e4 G' Q: m0 Z */& K( j% t1 A, z% W6 \
1 `) J, P3 y; n' W7 ?7 g: R+ L5 K* u // Add plugin code here...: r5 m1 V& o* B# U, {# R
```
- ^% m, O& ?$ r% P8 a r# a( j2 i r& S" a$ f
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。: X$ A+ e( Y3 Y
( u" _+ h h4 {4 E! @! a0 x) Y2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
! v7 u1 c0 a: a; j( A8 R1 h
8 Y' L! X' j0 z% t6 W& }1 Q ```
& K& y3 s( }: H7 R add_action('init', 'create_custom_post_type');
4 z. i; m# X: U9 ~3 K+ y function create_custom_post_type() {
' j# s. o) j" ^8 c% b5 L $labels = array(( P- [; S/ I) U: H4 |; R
'name' => 'Site Wide Notices',3 T: ~8 l' D5 X1 g8 S
'singular_name' => 'Site Wide Notice',
& r. T. \0 a. c% t9 L. u, P6 Z# X# j V 'add_new' => 'Add New',, j' Y( H- [7 y6 Z& I6 H
'add_new_item' => 'Add New Site Wide Notice',
4 G# T0 ~& [( V9 P 'edit_item' => 'Edit Site Wide Notice',
' {4 Q* A& M7 G! ^5 u/ m2 W 'new_item' => 'New Site Wide Notice',
6 _) b9 \ I" O- O 'view_item' => 'View Site Wide Notice',/ U# @/ {; Q9 M. G0 F: p6 p
'search_items' => 'Search Site Wide Notices',
* `# U1 n7 n$ _" H7 O7 u C# E2 z6 c 'not_found' => 'No site-wide notices found',
5 @1 Y" q& W8 U9 X 'not_found_in_trash' => 'No site-wide notices found in trash'
: }7 Y: d" J& K& y/ p# y6 q/ @, E );# A. Z# E% k) w3 w
- X- R' F- V* k$ [8 s$ z7 [ $args = array(
; S4 [# r' H: i1 C2 D 'labels' => $labels,) B* e" j) H) [7 ^2 a0 g7 e: M* B
'public' => true,
! s0 d3 |9 O! a* V3 e9 h! n: g8 M 'has_archive' => true,% r0 W/ {9 }, E
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
$ G5 V7 O i9 I/ \2 t4 q2 E( \ 'taxonomies' => array('category', 'post_tag'),5 W4 ?3 ^! \1 w9 \
'menu_icon' => 'dashicons-megaphone',. |5 Z7 L- \" |! M$ K( I
'menu_position' => 5,2 c4 h# Y5 V: M2 l" g* d9 U
'rewrite' => array('slug' => 'site-wide-notices'): D% z/ D9 d2 v F, ]6 c3 B4 s
);) x" U* p: L9 ?( f
" d+ T$ M" _% `+ J! Q1 Y
register_post_type('site-wide-notices', $args);8 \0 X1 D$ f! k. X
}+ x9 ^5 |9 V9 i Y/ ~
```
+ D) l5 ]+ C7 Z" O% a! Y! v0 a& m2 h3 y' g* f" V! }3 P# _
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
/ y% y- v. R7 R: u; {" O# }' {5 w
0 G4 W2 N) |2 p0 z3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
, `. @1 [$ @* R2 `( m$ e j# t
7 S5 L- _9 r7 _+ n ```
: a/ V# @# _, o. D) W add_action('add_meta_boxes', 'add_site_wide_notices_boxes');( K" o. |/ L% Y( V/ n
function add_site_wide_notices_boxes() {. S; i* M: [0 ^5 S4 |7 }+ ~
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');4 ^9 @$ N) n- [5 o3 j) @
}( e* Q/ h6 K7 Q( x: \; e2 g0 Z
2 y0 N5 f/ J& j7 w' b! w9 T5 L' b/ w( |
function notice_details_meta_box($post) {( ]0 I6 N8 {2 o3 Z6 ^- h
wp_nonce_field(basename(__FILE__), 'notices_nonce');
' l7 Z. F& A! [1 a' M! s7 @% B $notice_title = get_post_meta($post->ID, 'notice_title', true);8 ^/ W/ `: {; q B0 c/ e
$notice_content = get_post_meta($post->ID, 'notice_content', true);! t( C, g3 H) m6 K4 c4 \' z# i
?>8 o/ W' \, N9 c4 @
<p>, }1 i+ Q1 u1 m) g
<label for="notice-title">Notice Title</label><br>8 r/ i5 V) S7 G Z+ `8 u) S
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
3 ?$ Y: A1 `) V* Z% c- Z </p>
: q) K+ Y' j3 }9 l* E1 b ^ <p>. V% t: N1 k+ `6 o7 |/ C( ?
<label for="notice-content">Notice Content</label><br>2 R( c4 h f t# ~4 N, H4 ~
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
b2 n8 U' o8 s+ P </p>
/ N( @% ^0 v6 y3 w. D <?php
' S1 N G1 n/ R# f: i' U } e v& e1 y" O0 `' g
/ s0 o+ P5 e7 A/ e/ f6 [
add_action('save_post', 'save_site_wide_notice_meta_box');9 \. z" [/ i+ c8 P4 y' M5 G! I: x/ @
function save_site_wide_notice_meta_box($post_id) {
4 v- `) s; H, ]2 |$ D/ ^ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))3 T5 K$ @1 H( f; {0 E
return;, z0 \+ d' X7 F: s$ G' n: L! ^
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)2 X9 G% c, b4 V; m
return;
4 \) ~+ X6 g6 _* _$ l$ E6 L% U& V( m, ^% H* q0 e& [4 q8 E
if (isset($_POST['notice_title'])) {% o6 j: p, o2 }8 g0 t0 j* }
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));$ r* y' z# T: y* y; P C) {4 B
}
! y# R3 R1 w* m1 h! Y if (isset($_POST['notice_content'])) {' U7 x6 y/ i3 M
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
3 L. k4 ~* g2 [ }% w! w0 \: w6 [/ V4 I0 T. ~% J
}
4 _( K# C+ T9 k0 Q ```
$ @* ^! d# S" L* n3 Z6 B* T4 x3 M* B# C0 h4 p. c8 e$ a. w9 p
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
6 Y6 n" w8 C" e" I# m( ^8 ], `$ q# b. u; ~$ i$ O
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:8 J5 h3 R) L: I( q3 Z9 \
: Q8 j9 v f. T9 X& ~2 m' i7 F% r ```4 e' U/ K% V( E( p! c7 ?
$args = array(
; P% A: y. s L: E, x) E$ G. Q; ] 'post_type' => 'site-wide-notices',
+ ^7 n2 O2 m$ O( S4 U2 s1 u. E 'posts_per_page' => 3,* P% d8 K! e. V; _
'order' => 'DESC',; b4 I) J% U( O( \
'orderby' => 'date', A, u; T3 Y3 _2 m7 _# B4 q
);
; i9 P7 X% `* J" d0 K $query = new WP_Query($args);
; T* `0 E) f* ^, ?7 ]- S if ($query->have_posts()) :. p# P: j; x1 J2 M. Q2 x- z* h4 z
while ($query->have_posts()) : $query->the_post(); ?>5 R' V- d, Y# P4 j+ V+ b% A
<div class="notice">
; H0 ^% r; f1 z1 _ <h3><?php the_title(); ?></h3>$ n- Y$ Z* J' }/ M. h/ f
<div class="notice-content"><?php the_content(); ?></div>7 r/ A: _6 \" z, D& @. q% H9 {
</div>
7 Y/ Q$ M- o$ C9 p- p( Y <?php endwhile;/ H5 y( {- P& u9 L7 U% p6 G
wp_reset_postdata();9 q: `) `/ J6 [2 C9 t
endif;6 ]# B d: P, H5 ^3 Q# e o
```
@) S S, c7 Y7 e* U5 j! Z" Y- _# f: e* c7 P" d; ~: H$ Q
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|