|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?: J% }8 P8 M: W0 g2 ~" R$ o
# S( d w8 `" j
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。. _: B5 o: I% T/ E$ T) \0 v
( E' L- w: K, j# y$ {! Y- d- E以下是创建自定义插件的步骤:
4 `4 |/ ]- n2 c$ q- s- B8 V) L6 z- b/ I
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:* Z5 W- }' I3 T+ C8 G8 t" d
" L9 a+ q9 ^$ a1 d% |+ T
```
- J9 c0 c/ L$ n/ V( T# m) l8 n. P <?php n5 r+ M* x+ K4 _) l! X% A$ |* t% L* V9 m
/*
* T7 n7 b! P/ y! @ Plugin Name: Site Wide Notices Plugin
# I4 S Q. o- _$ Y; _3 o% w; _ Description: Adds a new custom post type for site-wide notices.
+ ^% w$ p* q4 e0 B, W( ] Version: 1.0
6 R! t x) A i) t. E8 r$ x% K Author: Your Name" m S4 d1 u6 Y: X+ v
Author URI: http://example.com3 ]" R3 e( u5 a: |
*/
: }2 r& q, I8 G) s
9 T% m0 P7 _. ^$ x: r4 r( n // Add plugin code here...! P. w8 P' u9 E1 J- X% G% C
```
: G& \$ a8 F o$ s) Y3 T/ u3 B4 L* [) D
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
; P2 K: S. m) k5 |' V
% y& E& |" r: X( S; P) r! \9 L2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
2 ?* A6 m9 Q2 V: B r5 @( o- Q* L: G# x6 _1 O3 p6 a: [' v
```: k0 W, X3 ~' j2 R( y" }
add_action('init', 'create_custom_post_type');
( Y8 x/ A/ w: ?: z, @9 O0 l function create_custom_post_type() {' ]- f3 }+ B8 F" H0 [
$labels = array(7 h/ Q$ ^" z: W
'name' => 'Site Wide Notices',
- A$ L" Y" s, G6 M9 T: v 'singular_name' => 'Site Wide Notice',
" J" G/ q. s+ o1 O9 h6 J' G- B% o 'add_new' => 'Add New',
e" W* c. i: v4 R" Y' [- q9 N- m 'add_new_item' => 'Add New Site Wide Notice',6 d/ q6 ]* l! A' H4 ` q5 A; B4 I
'edit_item' => 'Edit Site Wide Notice',* b% C5 u: k P- {8 S
'new_item' => 'New Site Wide Notice',
2 Q5 b4 ]* W B9 ~ 'view_item' => 'View Site Wide Notice',
; F5 \3 k/ c. Q1 X 'search_items' => 'Search Site Wide Notices',6 e( j4 a- q1 S* }2 o3 G
'not_found' => 'No site-wide notices found',
. C( \+ ^6 ?, n 'not_found_in_trash' => 'No site-wide notices found in trash'/ R8 n5 D" F ?1 {6 G: N! w. G
);
7 s& a D: b! o, x2 ^! h" Y1 p- [. ]( V' x6 G; x. H
$args = array(
* t: N4 d- r, F6 ?' H, L 'labels' => $labels,6 G6 m# [, J: k3 [9 P# x+ _
'public' => true,2 W$ K" k2 g, ?7 P
'has_archive' => true,* s, k j: E6 K. K
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),7 w+ ^% c& P j( b' e Z: _
'taxonomies' => array('category', 'post_tag'),
7 ]& A3 _4 h8 \" n: r 'menu_icon' => 'dashicons-megaphone',5 T2 w+ Q6 g. F9 {, D: B+ A: f1 h" H7 u
'menu_position' => 5, B3 ?; k* e7 Z! Q
'rewrite' => array('slug' => 'site-wide-notices')
9 m) q! T9 T9 i( f/ T4 R );- |/ T1 S% a* C9 c2 u
, y$ `4 l! J5 k8 b: E
register_post_type('site-wide-notices', $args);
% h9 r, q0 D/ y2 P# w }
; Z9 s; ?2 |0 E1 C9 T& r5 O- J ```
! ]4 F9 m2 T A' M
& {6 v' V. B+ Y# `9 _7 }* G 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。$ U, M) R1 d1 p+ l1 N4 T4 A4 g8 V4 n" T' H
: R9 p0 X8 [! t: Y$ G3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:) `- I/ B) f: u2 T( c& j
( v9 ?/ D; M `' L1 j6 k8 i! Y
```
2 z; \) `( Q+ P7 X' L+ Y/ F& _ add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
& a- l" Z: g3 N" R function add_site_wide_notices_boxes() {
+ P/ \2 g7 `7 z add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
. d& m3 d# W) n( T' d2 x" ^2 H }
) |: g1 k( ^/ I7 \8 N/ ]/ ]- B( Q! O2 W
function notice_details_meta_box($post) {
1 o4 g# G2 T9 g! r7 p. s wp_nonce_field(basename(__FILE__), 'notices_nonce');
+ `3 r4 h. m. `/ y $notice_title = get_post_meta($post->ID, 'notice_title', true);
3 Z/ K8 @/ t5 Z& W) _- M# q $notice_content = get_post_meta($post->ID, 'notice_content', true);
, U. o6 z7 [5 ?0 g# Y7 C ?>2 |; H- g" ?. T; Z) z5 E1 E
<p>
5 w7 l4 \0 t+ }+ ] <label for="notice-title">Notice Title</label><br>
$ _; V0 @& ^' P% f <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
! m. R; j3 k' d1 e$ `! S </p>
1 H" [0 W+ [6 W, m' I* X$ A# i <p>7 }0 i0 @( b2 y0 K
<label for="notice-content">Notice Content</label><br>( U8 W8 y' ]6 o4 Z7 j" S
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>* p8 k/ n6 u/ Y5 b
</p>. p! l' `1 G) o( q; \: o
<?php) X3 @3 ~! o0 Q/ a
}) S- Q% S" A/ o/ A: B" P
4 L. p. o) L) v# ]# ?
add_action('save_post', 'save_site_wide_notice_meta_box');
3 e. s) V9 a* F! Z' s$ j function save_site_wide_notice_meta_box($post_id) {9 G( p' D& i3 a4 W% @
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))7 o( E3 C5 {4 I, x# O
return;
- `4 l6 U4 I: T1 @7 E) ]+ | if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)2 v4 {7 a& q7 e/ o9 D- i. S
return;0 a% ^' u' c. s+ y0 P, p: `
, j+ j* m; B6 Q" }8 ~% t9 b
if (isset($_POST['notice_title'])) {0 O7 |+ B; F/ g) @! m
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));, y6 `& T4 l& d3 \: \% z, e! }
}+ i& T' I1 ~- u- v% [& D
if (isset($_POST['notice_content'])) {
, l$ s B+ |* s* c update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
2 \2 [2 e" H% C& T- r/ F$ m }
1 J' p6 P; ]1 x+ T }% J! t6 o$ T C6 J8 l
```9 F% K4 @1 m# e" ~! P
7 o4 m, O5 V! d4 N) W$ \" F 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
. q& Y4 V" d: \1 N# P) k/ T N2 n. l0 f& e/ c; @" f& G, Y
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
* E7 f, Y* M, n/ Q0 G" I. C
, x/ O1 b) R4 @2 J s( D ```
5 t. t2 F3 W: x $args = array(
6 A* s% w% n k$ C6 e6 B 'post_type' => 'site-wide-notices',
+ Q' I9 A2 d4 l 'posts_per_page' => 3,2 X; I: \2 w# F0 Q) i
'order' => 'DESC',
q( W( [+ g/ m/ j4 U# T 'orderby' => 'date'
2 g2 y4 Z$ B* {' j* D );
3 J- l7 Y0 d% w/ d+ }2 f G $query = new WP_Query($args);
6 i2 k/ I$ h2 x+ O. N if ($query->have_posts()) :; [8 O* {! G- j/ S
while ($query->have_posts()) : $query->the_post(); ?>1 I+ p& ?5 u5 k3 {" Q
<div class="notice">
* `4 z* \) J) v3 I. | <h3><?php the_title(); ?></h3>
) t7 ]6 n2 N9 I. C# X; A! M/ C <div class="notice-content"><?php the_content(); ?></div>
! g5 a2 |6 Q& ^' m3 h8 i! b" m9 k </div>
6 }1 N! O- \8 U <?php endwhile;" |- T, A# w# B" K0 j5 g) o- C, ^# T
wp_reset_postdata();2 R' d& p: R+ t$ ], _
endif;
" C$ m x0 `, M1 E# D4 S; Z ```
8 P/ @3 t5 X# _# v2 y3 f; k7 Q4 @: A) J/ {/ E2 J
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|