|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
l8 E9 z1 g% k+ A y: j2 z5 Z+ S, E2 p L, L0 ~3 ]
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。- b. \( A* }9 ]
3 J! R" M" u# s( N9 s f) Q; t以下是创建自定义插件的步骤:0 ^2 u5 o6 d' [( B+ p3 I# Z6 w
/ {0 S+ H. |9 m8 [4 ~$ @) ?* ^0 n5 C
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:7 R, {* H$ t& Z5 b
- a3 U! J5 L; ^7 U4 C, p/ C ```" Y' z% J1 D$ B2 `& @
<?php
( e0 x$ L( S4 s7 L/ k /*
! v! M1 q- r9 ], Y* R) S$ v Plugin Name: Site Wide Notices Plugin. u }/ Q7 A& U3 q
Description: Adds a new custom post type for site-wide notices.
$ ~) B# N$ p* J! A% I; z Version: 1.09 e# u9 Y9 b* `" I1 z4 e8 S: }/ n
Author: Your Name% `# w. T# r8 l% ^
Author URI: http://example.com
! h- Q4 n4 q4 n- c */ {; T" H3 s7 M0 i( K$ l
( {5 z' M- Y/ J8 }* F/ H' _
// Add plugin code here...
7 ]0 a! Y2 q' r& s ```
' y( G, r X! e; K! @. X- e% M" q$ ~8 G* w* E
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
$ ~' N; Z/ V1 C; l; }
, f3 J3 u* M: p, z- G2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
; n4 {* W6 F: k: o( {1 I4 m$ C* U/ A) Q. O0 T
```
' O, N; Y. @; n. h/ k, U add_action('init', 'create_custom_post_type');; Z4 L8 i$ E" _ O- u! T
function create_custom_post_type() {
+ R* M7 j; O6 Z3 V/ T $labels = array(- Z7 R. B6 a* D4 f8 Y2 r! l. k8 {
'name' => 'Site Wide Notices',- W8 t7 L' d4 ^8 I1 D+ j
'singular_name' => 'Site Wide Notice',; J ? ^* t9 f0 q$ k4 u
'add_new' => 'Add New',
E4 m! H. T# c5 {$ r 'add_new_item' => 'Add New Site Wide Notice',# M/ H' |+ L* M- h5 z
'edit_item' => 'Edit Site Wide Notice',
7 h% i: k6 n. X+ y 'new_item' => 'New Site Wide Notice',% T& f6 R- L* G/ f4 B; m5 U
'view_item' => 'View Site Wide Notice',* l/ r0 G5 Y8 l, ^
'search_items' => 'Search Site Wide Notices',3 X& ^. W, l0 g8 k0 N' q6 c' q
'not_found' => 'No site-wide notices found',
2 e- U% R( {# Z9 w& g; I 'not_found_in_trash' => 'No site-wide notices found in trash'1 a* C1 m6 J. @7 x/ H
);
* O( ^9 N! g! N4 ^) j _" K
2 `. M# S, y% ~/ U# e7 b i H $args = array(" }6 i. u" a- w0 z. i) s
'labels' => $labels,$ T5 \1 w# {# I; V
'public' => true,
( s/ M4 S+ v" d 'has_archive' => true,( m& o- ^0 d% ]9 e1 | T/ M2 H4 V
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),; S8 R( c' h; t8 i" Z0 V& r) @
'taxonomies' => array('category', 'post_tag'),& @( d8 }: H) e! v2 g4 ~) P. o. q
'menu_icon' => 'dashicons-megaphone',! l; M7 T7 V2 b& |
'menu_position' => 5,
# b5 f }/ o" w9 h/ y2 q 'rewrite' => array('slug' => 'site-wide-notices')% \- f5 [9 n1 @
);
1 U; O+ [8 U4 w) \; \* z# ^5 ~3 L, o
register_post_type('site-wide-notices', $args);
- K& ^! J, A, v, n0 K! u/ d }" \# l8 {% C4 S7 Q5 J
```% R( h- }6 e2 `) e4 \& }# T# H q
, v" r) l4 T0 h- \
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。, P W$ B& e$ j+ k# Y: R" A
& f& E0 W/ ^7 p
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
" q$ ?9 _) p! F2 G, v) u+ `
4 q2 o# A$ |$ k$ _" e( K: Y ```
- }8 |* S3 b- w" ^ V5 C% y; U add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
- u2 u3 t: O2 }9 [ function add_site_wide_notices_boxes() {6 C( F& y& z, @
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
q# B4 X( L8 d. j4 E1 X4 E }
# |- }" u7 y3 d2 Y% n) A1 ~" \, ^6 l( I4 W, q3 i' I0 r8 Z
function notice_details_meta_box($post) {
5 p" s: l+ y. o wp_nonce_field(basename(__FILE__), 'notices_nonce');
* X' |( U1 x. x! U# ^; c7 b4 n: \8 t $notice_title = get_post_meta($post->ID, 'notice_title', true);& ~% G$ d' ^2 E8 f
$notice_content = get_post_meta($post->ID, 'notice_content', true);& N# a% L" |4 S2 K r. G5 [; j# C
?>
1 q. [, a! L+ R M) s: b2 @( u) T <p>
3 F6 o) W) z4 O" z8 C) | <label for="notice-title">Notice Title</label><br>
3 t ~! F+ S% ?* l; S <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
0 B) }/ q4 J" }8 ^$ v T; K </p>
; @7 W, L4 f$ y( l7 o' i4 [ <p>3 C/ [, K5 i3 B- H7 P
<label for="notice-content">Notice Content</label><br>
1 O- t# M- o# x( t6 u; z <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>% R/ X+ ~: P3 R- U
</p>
! B- D: o/ e5 [2 o* O0 e <?php, X. _5 ~" ~' S( K: A
}( C& n+ [2 n) Z7 }
, [: n @6 \* {1 f1 k
add_action('save_post', 'save_site_wide_notice_meta_box');0 i: e U, C# c! y
function save_site_wide_notice_meta_box($post_id) {9 V# g0 ^& ~4 S M
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
" A; ^9 K. W! g( k2 e return;
; s! T+ t) m# m, n if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
' O# ~' \# {9 X* H# ~3 o/ [ k5 f return;
5 f/ p; }4 ^ j, q' U& h* x& ?
6 N. r# M4 H0 ] if (isset($_POST['notice_title'])) {0 n1 h" @; U( w9 {4 O
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
3 t/ S+ J/ K% H* ~* {! O, n% v1 b) h }2 ]8 z) K% }% x2 [: I
if (isset($_POST['notice_content'])) {
. K) Q# B0 ?6 v4 U) m update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));3 }& K: [! {) g$ f3 t d: `9 \ ^
}& y3 @$ ]! T5 r
}' x3 w* k2 Z' ?5 V8 B
```
$ T3 K! x* `/ N5 z! D
- v% B2 I" g* h/ B' r3 g( Q 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。3 K7 [, G( H0 x# E! S6 _9 B
) D9 _: e& h, o7 K6 Q+ U4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
8 f/ A: C0 ^4 }3 `% o" _3 Y5 V. b1 Q- y* W" K+ @: k5 h
```" Z! Z! w' F0 f
$args = array(( h' ?, }8 {* v; x2 O) l
'post_type' => 'site-wide-notices',
- b, N9 `' l) A3 w# ~) a. Z, Z 'posts_per_page' => 3,
7 i- u% }7 c: X. u" E1 r& } 'order' => 'DESC',9 B1 R, Z9 n; k
'orderby' => 'date'& s* q# [. d2 n: l/ o, I' k
);
8 h& m) L: p9 H $query = new WP_Query($args);
4 r* |( b5 I* ?5 ~0 P! ~& ~ if ($query->have_posts()) :! V" V! M1 z3 N# ?) v
while ($query->have_posts()) : $query->the_post(); ?>( k& l ?: f5 Z' h' c& ~
<div class="notice">& |6 m6 P/ E8 i: S
<h3><?php the_title(); ?></h3>3 ^' X! R# V& |5 U+ W6 A
<div class="notice-content"><?php the_content(); ?></div>! `% l9 p, q4 u8 k
</div>
0 Y. d8 q" ?* [" s; O <?php endwhile;" o/ A4 A) E4 g F, E
wp_reset_postdata();% l9 D" N" \/ [: A6 {
endif;% d4 ]. k! G3 O) g" B# W
```
% d E3 d0 H# O9 B7 o" V; M: u7 H- ? R
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|