|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
! T; g5 P9 x% J6 N( n, Z0 a8 ^+ x; ?# W
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。' y, H6 Z" L* B& C9 I6 O: p
, }7 d7 ?- e: j以下是创建自定义插件的步骤:8 h. ]( L- K$ J; D( U A
$ a+ W) \. }/ }" G1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
, X9 F+ |7 W8 ~5 a. k5 {& e3 ~* @
```* _5 z+ X$ z) V5 {3 Y0 a
<?php, Q( C7 Y3 j$ o. ?( y
/*
: @2 G& N' Z& p0 p T Plugin Name: Site Wide Notices Plugin4 L8 x- L0 l6 S( N% T* a6 |2 B; K
Description: Adds a new custom post type for site-wide notices.
# D6 r3 F M* J9 d Version: 1.0
; K) _; v& y% X1 _# v Author: Your Name
# F! w! R5 y, n" @) M- h- V. F( U1 N Y Author URI: http://example.com
8 _( ?5 L) `, M */
: j) M+ U. @9 O6 J _6 J2 S0 ^! f: p. t* K% z: d7 m
// Add plugin code here...
; G4 k( _6 S2 K ```8 T! K8 [% H5 R0 J+ [9 M0 r/ B
2 O" y; C: n' R$ D1 X: F z 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
- Q( f- T1 c4 @7 w4 _1 z# b* {- v& d2 d3 \* Q+ S4 f
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
! x# \8 _* r: h1 X
4 \/ J& V+ H$ s# F/ \( o" f7 W, a. r ```$ y9 T8 D- N2 M8 x, G) _
add_action('init', 'create_custom_post_type');; M$ `# w5 K; l# o: G5 e; x+ j
function create_custom_post_type() {
4 L* z: m3 x4 {; c" u) _ $labels = array(
0 p- {2 r4 q, t0 c1 Z 'name' => 'Site Wide Notices',
2 b. u0 K0 {" Q5 s 'singular_name' => 'Site Wide Notice',
@, m. k$ G2 f7 i1 u8 } 'add_new' => 'Add New',8 W4 V. X( r; _" D
'add_new_item' => 'Add New Site Wide Notice',
# L( K/ Q0 }. e6 z m) m3 S 'edit_item' => 'Edit Site Wide Notice', {9 s5 O0 X: j v2 x9 W
'new_item' => 'New Site Wide Notice',- N2 `4 E' q0 M7 |. ?% q
'view_item' => 'View Site Wide Notice'," B# g7 W. T" `' @) d5 l
'search_items' => 'Search Site Wide Notices',; \' B, i o! D
'not_found' => 'No site-wide notices found'," R5 N( l* Y7 K# @0 J+ i
'not_found_in_trash' => 'No site-wide notices found in trash'
* A' t4 B, W6 f0 ~ );1 L6 L: v$ s6 g# P, j+ s9 ^' k
- P+ C7 d+ y0 g8 p- m
$args = array(" k) I% A! U$ G8 Q- t, |% a
'labels' => $labels,7 O( ^9 L: J1 E/ Q: k( b
'public' => true,: @7 t9 F4 a$ r. j4 x1 q% H
'has_archive' => true,& P" i9 y, k- { {# P
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),) U5 X* [6 ]0 |) R7 ~+ x& b
'taxonomies' => array('category', 'post_tag'),% ?! v {# n7 x% x6 p
'menu_icon' => 'dashicons-megaphone',0 r# B' V$ B, q' S5 ?9 z1 O
'menu_position' => 5,( S. n/ M8 V0 e
'rewrite' => array('slug' => 'site-wide-notices')
2 H. f0 T1 y f5 {0 T" E );
/ c8 |, Z; m7 E2 p, Y0 U, w! d+ [# a# h% K9 ] U/ U
register_post_type('site-wide-notices', $args);
& O# e5 l8 t T! _+ y% F: x( e }
) v: }- D& [& J9 j- P" Z ```
- ~" b* [* V& q* J& v& l) y" ?' Y* u* s6 {# D& @) r3 h, c
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
+ P+ O1 T _- n8 U" ], q5 }- x3 Q) o0 l6 J* J6 S t
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
5 B5 E( C: P/ Q" Q- Z; M6 a' n0 X/ u* a4 d+ U% D- I
```3 Y# \" t2 `8 \7 u1 B ?/ }
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');# q' E( E( j. W& B) D1 V$ H) ]* `! E
function add_site_wide_notices_boxes() {
* V- y& W! g, S8 e add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
2 Z! O, d1 [% R. x5 Y5 Q }$ k8 n' t5 \3 k& e& L
: p/ a, U5 y# M+ K! t* J" A: u. q function notice_details_meta_box($post) {- ^$ X/ n+ F& g+ Q" p' `
wp_nonce_field(basename(__FILE__), 'notices_nonce');
% j; S/ |8 g) y1 P! d $notice_title = get_post_meta($post->ID, 'notice_title', true);) K8 ?+ w9 \4 ?; a( T
$notice_content = get_post_meta($post->ID, 'notice_content', true);$ |! ~4 i" f) M& j% ~. U4 _6 J2 i
?>
, y) ]+ j `8 |1 \ <p>
& ?4 Y9 i5 A: [# [ q3 y6 W <label for="notice-title">Notice Title</label><br>8 o6 ^2 d. e% \: y
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">/ x7 z. \% L4 z( U/ r# k! V: D
</p>
5 o% t2 U1 s- a! A1 J <p>
0 [ b) s$ I# p <label for="notice-content">Notice Content</label><br>
$ h3 d& n8 w4 V <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>5 j9 N/ E: f8 M1 B3 A$ s3 [
</p>
) h; M$ @9 ~& [( B, q: i, W" l <?php4 L$ `+ T$ ~; W/ N$ ], \5 ?
}
, C, z3 c8 W$ P- p8 V+ U
/ ]" _4 w3 T. n- l" q add_action('save_post', 'save_site_wide_notice_meta_box');
9 h: l/ E. \! D- @# S$ [" c function save_site_wide_notice_meta_box($post_id) {: S, c5 X1 R, R0 T( B, Y
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))3 j. }$ V' h# M0 e9 ?
return;5 E: S% z0 r! T5 ~
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
4 F' S% X( z7 N- x+ z4 o return;
, [7 Z* k5 ], s! H0 t2 W7 O; ~7 Z% M# {" W3 A! f1 Q2 X1 F( [
if (isset($_POST['notice_title'])) {
# I5 }3 C) F) K# o/ X update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));$ h, p7 D/ |: \
}/ `* N& F- l4 V; E" D p
if (isset($_POST['notice_content'])) {
: T. ?, I0 V# G, |; Z update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));' c* P# A+ X7 u) [
}
. m) E( \2 J1 }0 r( @ }: ~6 d/ y( p/ X8 M2 w* h
```
5 a) z7 `4 M5 n: U: @8 h; m. R) A* [2 W! k1 t6 k7 ]
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。' v: q# c! o9 X, w8 {. x2 C
; c( l- B) V* s/ W6 [
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:- d/ ~$ q- E! i" k6 o
1 p& [' d# H* I5 C l; s/ z$ Y ```2 \" i. L: X u" I' {8 N% @
$args = array(( P5 U2 c( @1 W4 t* d) z( d6 U7 v
'post_type' => 'site-wide-notices',9 c* k& K2 z0 W$ G M2 _& E" b }7 S
'posts_per_page' => 3, F. ~5 f2 d: t
'order' => 'DESC'," @* N) p8 e' C5 @! l
'orderby' => 'date'$ Q9 ~: k: W; [) k8 Y5 G
);$ P$ c$ t, t# N! ?4 x( |; [
$query = new WP_Query($args);
/ X# V) P0 g* ]1 @6 \$ h if ($query->have_posts()) :
5 c" u4 ^. \2 c) R while ($query->have_posts()) : $query->the_post(); ?>
/ @1 Z" ^% X" G& a/ ? e$ h, m: I* i <div class="notice">; O7 z6 _- l* x" d: M( L
<h3><?php the_title(); ?></h3>
- S, y0 U Z8 M- ? <div class="notice-content"><?php the_content(); ?></div>
& W. ^/ P. G2 C g1 T9 x* Z6 I. G </div>! _4 { j& \: I, F3 l* d- T
<?php endwhile;
/ [& E3 ^& s; ~# K: Q* ?9 }: K wp_reset_postdata();' R0 D/ \8 Z5 e$ T I1 O6 a5 G
endif;* j2 N3 l% t9 Q m7 T1 Y
```
+ r3 r! @, x! r1 u4 A5 d9 g; }. [& f! m: k7 T; _: A8 \8 P# m) m4 C
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|