|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?& f5 k4 R5 `. N' g
# A- d; i, V1 R/ K# O如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
5 H1 j$ s, ^8 {+ I% b6 q# I7 l7 ?4 ~$ ?8 \
以下是创建自定义插件的步骤:
3 j$ Y1 g1 W" P
3 F, D% e _" Z1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:2 s8 r. V7 @7 L1 }& Q- Z# p
n" \" t( t" V$ x5 w5 s. r+ B( e ```: a/ L0 J# r$ S
<?php3 R# x- C& {3 m! C
/*4 c1 g Y: m$ E/ T+ d
Plugin Name: Site Wide Notices Plugin
; _& X+ }' l8 C6 j j: Z7 G; R Description: Adds a new custom post type for site-wide notices.9 r$ v! Y! X) [# v3 Z; [+ s; T9 j
Version: 1.0
+ l* J: C1 y* l- c2 E Author: Your Name
' Z) D* H! b5 ~/ C: ` Author URI: http://example.com6 j, S# S( P, u0 d- Z
*/3 e% ?7 ?% z" H7 v& b2 G( Z; H
1 r7 [- j' W# m* f0 u; y3 u
// Add plugin code here...; E+ e. G+ s/ k6 K! @& Z: J- |3 r. a
```
7 A6 o. E7 [; d. x" [/ l6 T& q1 ]0 _ N* G
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。/ I/ q& D2 U+ I0 P0 T# N
- M4 C/ ^; J; v7 p8 h
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
' O8 w. F5 s9 _) F y' w: p- X8 [1 x
```+ x5 x1 _* r$ W9 C
add_action('init', 'create_custom_post_type');
( f! x- l& e; [ function create_custom_post_type() {6 S5 m& g% C2 s& y8 s$ h
$labels = array(+ `+ S+ U5 M* Z. |
'name' => 'Site Wide Notices',
7 e1 d0 Z4 Q3 t1 B/ x 'singular_name' => 'Site Wide Notice',8 S6 ^0 @4 ^6 _1 z
'add_new' => 'Add New',% B( J. u; D: J) m! k* |
'add_new_item' => 'Add New Site Wide Notice',
2 P% {- }- m- b* Y) l# u# v 'edit_item' => 'Edit Site Wide Notice',+ S( l! E; S" \% N
'new_item' => 'New Site Wide Notice', H$ L/ \( @' C3 t' y# i
'view_item' => 'View Site Wide Notice',
' e, T2 x9 M- E7 m8 } 'search_items' => 'Search Site Wide Notices',* J, t4 F% O1 M2 ?4 v: I
'not_found' => 'No site-wide notices found',& X6 N2 Z' p$ f
'not_found_in_trash' => 'No site-wide notices found in trash'" ]3 F t+ @& g; f" b- K* s
);
: X! |. d) {0 f% D& m5 M8 k9 m- l2 T5 k. A# F
$args = array(
2 a" l& x! h- X9 J4 U; Z 'labels' => $labels,
/ V0 R1 I8 N9 P$ k! h" P% n( [ 'public' => true,
2 v0 m! n; ~0 j \0 A; Q 'has_archive' => true,
) o3 ]$ Z6 i/ Q: W9 D0 b8 U 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
5 C& l y; ~1 s) W. U* M8 q( W 'taxonomies' => array('category', 'post_tag'),6 M' x Z7 }0 x5 g. i
'menu_icon' => 'dashicons-megaphone',8 n, }0 P0 x6 d
'menu_position' => 5,5 h4 [) h- P% v- A6 w- o
'rewrite' => array('slug' => 'site-wide-notices')
/ P9 j4 n, ]! U0 G! N. B+ _6 T3 u );( H9 z4 {& L) d( L8 r) Y$ d
( m/ k7 G1 c( w; G- o
register_post_type('site-wide-notices', $args);
+ _' ?- m- U8 ?* Q }
; D$ H% _8 |! J" {2 x ```$ z8 o, B. H# p) w4 R
$ H- q+ ~0 _' R5 b 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。* k" b6 G- U! T/ |5 T) H# L3 f
/ {- B5 d+ e6 y) k1 Q3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
$ i& J# i0 ^8 z5 r3 J6 x' B
8 J; Z* {4 `: r. }8 F! V: ` ```4 V; f, F" P# A& P
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');; ^. Q- |. V3 w5 g( x
function add_site_wide_notices_boxes() {6 n2 t1 O) F0 q' D v: M
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
- h, p, w r$ M- Z3 ~) h }
( H, P0 N G. ^) V; c& R9 e
6 k1 m* p C! g* G/ h function notice_details_meta_box($post) {
% z p( ?) g8 I0 `! G& I wp_nonce_field(basename(__FILE__), 'notices_nonce');0 Z; D2 o4 l8 r; V, v
$notice_title = get_post_meta($post->ID, 'notice_title', true);5 j4 i6 b, U- D" ^
$notice_content = get_post_meta($post->ID, 'notice_content', true);
: e! H" d" C7 S" Q ?>7 T M% o/ }' o2 j( [! Y8 w. @
<p>
8 t4 k4 S; I* R6 K' d <label for="notice-title">Notice Title</label><br>
3 |# m N4 I3 x! J. O$ z3 z <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
# |9 L: J8 K" h' _ </p>; H8 K% f7 C4 P5 S) B7 o# s
<p>
+ ]5 k: i+ a; }' P: e/ r# o <label for="notice-content">Notice Content</label><br>! M" h. H/ B; q6 j4 U! W2 S+ s
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>% E. y! _' ]1 w
</p>
4 j% N/ F7 x3 z I7 B, B <?php# B' i9 R2 ^& K2 P4 k* r7 G
}6 p/ ^! w$ h N. F5 P
: |' i3 {. U; K: E& w! w
add_action('save_post', 'save_site_wide_notice_meta_box');
) L* v' @$ y7 u( H* n j! k function save_site_wide_notice_meta_box($post_id) {
& O# C b9 ?$ U& _! ^6 ? if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))). N/ h6 x( G8 `6 ^+ S1 n2 {
return;0 f$ v# [4 q" q# x( Z& t
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
4 O7 N- P" e5 M' q! C) j& T return;
- Q) m5 s$ p0 }3 a5 ~" U, N' ~9 X( m
4 x6 {7 b! }3 I! A8 G( f. G if (isset($_POST['notice_title'])) {
7 z! W' i0 F! j, {0 E2 @2 \- p update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));" a" f/ p- ?9 c$ m
}0 B3 G) `6 ?" b
if (isset($_POST['notice_content'])) {
/ C3 G0 o! z" g: D" k update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));# |) l& P+ B9 T
}. |; P4 L# j' v
}
- g3 j# A \/ _3 [6 y ```$ j0 n9 J3 w# {5 {4 ?
& A4 E0 |5 y& ?* y3 E* |
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。 `; \& S& X& M% I7 b
z/ ?: \6 s4 U* y4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:' |2 p% W* Q( r5 `0 ?
8 X1 E8 ^1 t7 W2 t
```3 P1 q3 m' K, }$ R
$args = array(, `, S$ F0 t' H* `6 e4 p0 O4 [
'post_type' => 'site-wide-notices',8 L P2 N3 {( z
'posts_per_page' => 3,
: t; ?: h( g8 J7 T+ F+ Q& \' t+ W 'order' => 'DESC',! t$ [* e+ x! j9 @' c+ X
'orderby' => 'date'+ [) I4 Y6 w' T* {
);
+ x" A, H9 t/ K $query = new WP_Query($args);+ c: D, ?8 E( F) t
if ($query->have_posts()) :% {' w3 {) n, A( }3 P
while ($query->have_posts()) : $query->the_post(); ?>4 ~+ v4 y- F, |2 s" x
<div class="notice">
, ?, S1 v$ q, g" a0 h5 z1 \ <h3><?php the_title(); ?></h3>
, b4 j- i {3 e <div class="notice-content"><?php the_content(); ?></div>) E4 ]! ]* S7 t+ O" S
</div>1 m6 V$ ]- u% W0 r
<?php endwhile;' a( y: h( j( x
wp_reset_postdata();
4 B1 g3 u b* ~9 e. \0 h h endif;
. S6 ? A& z- W2 M6 ^ O0 U ```- Z( h) Y9 s# K+ D) |% P
5 T: S- w! E4 w 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|