|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?: x9 H. \- b( v5 Z; Z4 `. C
, t$ }+ N$ ?6 B- f1 H5 f
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。* v8 {6 w) {. L4 T( m# X" ^ x
) w1 v& H/ |+ Y& F& g以下是创建自定义插件的步骤:& z# \, Y, U# Q0 l
g' ]" H0 E D5 r1 Y1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
- n0 y2 Y: s R3 J0 P
: s8 R. k F$ y, I- Q ```
+ E$ g1 u) l# \6 V9 a' C <?php
8 m6 W5 \0 z* \( p /*: {$ y/ r9 T7 w/ F2 X/ b, p; k
Plugin Name: Site Wide Notices Plugin5 |# z7 A/ L5 j- i, i
Description: Adds a new custom post type for site-wide notices.
! i* c+ U; B, q9 G" k8 C$ e Version: 1.0
: Y% W8 R- @' E& S Author: Your Name3 C9 ~! W d/ C7 e0 O! j
Author URI: http://example.com7 B' [: ]; C% g" g9 F% ^
*/. W) w6 v" Z6 O K
( @7 E: v* a9 z* ?5 P
// Add plugin code here...
" {) S+ }( l- u1 k) ]3 g ```
( B# V( ^8 Y3 ?/ L/ e* R; R0 r9 k( w5 f r' _6 y( Z
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
, v' ~% V1 E' Z9 \2 Y, G5 O' x/ V/ N; V7 a; V
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:( C$ J3 Q: X+ V! [
" S- x7 ~, U# ~& k9 T, g5 [# u ```/ G1 F7 q% s& Z( f2 u1 s
add_action('init', 'create_custom_post_type');& q4 R- O; V, i7 }) z: V. M) z) {
function create_custom_post_type() {
! j( D b/ e4 D $labels = array(
( E8 c* Z5 O9 K$ @ 'name' => 'Site Wide Notices',- K" ~: i, \2 a/ Y
'singular_name' => 'Site Wide Notice',6 q( p( [& |8 n5 E" G
'add_new' => 'Add New',. A) v4 k; j& W6 G+ R
'add_new_item' => 'Add New Site Wide Notice',+ [9 j0 h3 S- h& c
'edit_item' => 'Edit Site Wide Notice',( M }5 z4 \* \8 \) A" [2 n1 B" g
'new_item' => 'New Site Wide Notice',
& U! E$ k- Z& \, O9 T# k 'view_item' => 'View Site Wide Notice',- h1 i4 F p+ n$ Q7 ^
'search_items' => 'Search Site Wide Notices',! T+ v! p8 T* t% N5 `, F9 ]9 Q
'not_found' => 'No site-wide notices found',
+ l L1 t: H3 Y5 ?1 p% u$ m1 ~ 'not_found_in_trash' => 'No site-wide notices found in trash'% L, \0 `# O3 i0 j* @
);
3 ]6 u' i# ]% M1 ?$ R& d6 p. N
# d/ k! I9 T4 `, F $args = array(
" ~9 ?9 X7 [3 w* ]. F 'labels' => $labels,
c' C3 Y+ i: M2 e+ A/ g 'public' => true,
2 C5 t- o9 P' V7 D 'has_archive' => true,% ?' t! U- n z* K( S
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),# l0 e8 L" o! i* Y c
'taxonomies' => array('category', 'post_tag'),5 a; R1 w0 v, P
'menu_icon' => 'dashicons-megaphone',: A0 e1 j/ I8 s" m! H
'menu_position' => 5,2 R% v3 m. L9 D: r9 x. {$ d1 M
'rewrite' => array('slug' => 'site-wide-notices')+ J: R7 t- O& J' v; {& z( O; z4 m
);
) i) e, d0 f$ s. `1 Y
0 \1 H4 L7 a0 Q9 ^7 u! ` register_post_type('site-wide-notices', $args);
F% m! b8 n1 X9 C% ~3 O B }. a7 @+ c) T3 P
```- W) T2 d$ N0 S; ^
# G% K' l! T% {3 S9 b) A3 q9 m) k- Q 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
[9 | w% a& U+ V1 {! Y! r- W7 Z0 n" @6 E% t
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:/ V: k7 ~. Z) ^% n
G# `9 c/ C. {' `2 R2 F- H% H, V
```
; C5 E# w7 J( y2 c9 O2 |6 P7 Z& { add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
* H. k1 |6 `1 S9 V4 E4 l k function add_site_wide_notices_boxes() {
0 K4 x; h, q; }9 |2 m. w1 u, N5 | add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
$ N' e* ~" y1 n }
8 x) l/ i' p) I: ]4 A9 @
& I) [9 h0 K5 t% S7 N6 v function notice_details_meta_box($post) {+ P+ ]8 b. t6 t2 v0 l
wp_nonce_field(basename(__FILE__), 'notices_nonce');: I. A; W a4 T4 ~6 ^ W0 |) p u \( W
$notice_title = get_post_meta($post->ID, 'notice_title', true);3 G5 y% ]9 Z# G, P+ z
$notice_content = get_post_meta($post->ID, 'notice_content', true);3 u+ I m4 ]2 J) w% ^) [' R5 s
?>
8 @- w3 m6 N* m @ <p>2 [* P" q L2 o/ X7 S1 ?' W
<label for="notice-title">Notice Title</label><br>
# R" W7 K( f* l: F: | <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
3 `8 P, f: M0 {( @2 }* d </p>. K) ], h1 E. Z8 M
<p>+ n3 E* [% ?6 i! ]/ C
<label for="notice-content">Notice Content</label><br>
9 Y) ?7 ?* C8 Z* ^' W <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
: W" U5 m" D0 [, r </p>; q' z' ? m- R: G3 v
<?php( `& r) w! M( m4 @
}
4 I; V; o3 i( j4 J2 m E& w/ C* n8 T
+ l4 o( }1 Y1 |/ k" J add_action('save_post', 'save_site_wide_notice_meta_box');
# N }. G3 ]3 ~ function save_site_wide_notice_meta_box($post_id) {$ r5 t& D( D5 W& d9 j0 @
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))), s0 @, I" a9 |# }# V
return;
3 X/ k2 a5 Q% ?# V2 O$ T9 e/ f! S if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
( `: V9 ^. p: O7 \) H return;
0 Y+ b3 P) P% p/ |
6 a& z' I2 x; t if (isset($_POST['notice_title'])) {
- G3 H6 g. w }+ l& h( j4 u update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
& |: }* z" I+ s1 r# K' \0 D" k4 q }
4 v; b- f- u1 h/ d/ r/ p if (isset($_POST['notice_content'])) {
) G% A4 R# d- ?& e update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
& O, \! I! H) R/ w3 i }6 K# | i+ Y& \* R
}) |/ {( U- @/ U/ e# t
```3 d6 v4 n! B& V
3 ^1 T, Z5 Y/ E; H' s 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。( S1 S% R2 z' J0 R$ g# E" O& @) `& H
' p [5 g8 y6 ^
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
0 U$ f5 u% H4 m, M2 n9 X- A4 L3 i o
```
1 J" p& p E+ `( T $args = array(# J9 S T, @& D
'post_type' => 'site-wide-notices',
- S% O i9 a9 s' q3 [5 B: V8 t 'posts_per_page' => 3,
1 E! F, Z- S# J2 B2 q) B6 v 'order' => 'DESC', f0 I8 r0 r4 k/ q. h! F
'orderby' => 'date'6 u* U1 n! D* d
);
2 A( c/ U6 i: M $query = new WP_Query($args);; F* l* G! R. r1 j5 e
if ($query->have_posts()) :
" B* Q' Y+ s: j+ B$ o: A/ J" d; c7 ~8 r" g while ($query->have_posts()) : $query->the_post(); ?>
0 s$ b( \$ N$ F7 M* i <div class="notice">
) K7 K; F! [* G: p# x <h3><?php the_title(); ?></h3># I% D& T( v3 c, x5 K% a6 q
<div class="notice-content"><?php the_content(); ?></div>8 W P* k5 L5 c- Y. g# ~& ~$ }
</div>
) j2 u$ O! _: D <?php endwhile;
& C5 \, b. g J+ `5 r wp_reset_postdata();
* F E- U& X/ u4 \. d endif;
4 n8 `* Z4 _8 u8 p: X+ ?, Y ```0 r4 v+ J. H- O. j8 ^
3 h. h3 O# n9 n8 G' h! `: \ 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|