|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
/ N$ V! p8 y. \% O" ^# V7 c, X1 t& l' R, ]" c% ]: c' Q6 b3 o
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。; t6 B5 A4 t* O. {8 Y& F4 \, Q# p
& }: |: p. e1 ~" R5 I, m( y
以下是创建自定义插件的步骤:, ?) ~( c+ J4 U
: P1 J9 U, @$ F) Q1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
$ |; V3 D" {1 M: D, h: _ z p$ w$ L3 F& `
```1 T5 G/ X3 D. i4 t% M0 R
<?php
6 b" n Y2 p6 R1 }6 _+ ~" Y /*
$ U+ B; I! U; |5 G8 }4 y6 A" } Plugin Name: Site Wide Notices Plugin
8 d" C; y9 T0 E( E8 ~ Description: Adds a new custom post type for site-wide notices.
3 ` |4 k9 |; K# [ Version: 1.0
: d3 U' Q/ X2 i9 ]' A Author: Your Name( Y0 R/ z9 h5 T% j% j: E# r; h
Author URI: http://example.com4 y( E/ w4 Z9 T a' `& T
*/9 P9 [2 n+ m8 M
9 t: s0 x- O; F9 d6 r1 J: b+ V // Add plugin code here...* Y+ O6 e0 ~5 _5 S; \' A) S: \ Y
```
6 y2 J% A7 s, X5 V& x4 M1 o" e0 R$ R$ T
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。5 R9 l/ Z* b: e7 ]7 S
, }6 ]+ b3 C1 Q. R- J2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:9 K1 L G0 ?, Z4 O5 w
* l( H' R8 R" U0 c S8 P- o
```
+ L F# T5 x) J add_action('init', 'create_custom_post_type');
4 [/ n5 n. H1 C, U& P function create_custom_post_type() {( E* P }3 E" @2 P5 @
$labels = array(
" R! z. `4 x" Q' l) A* K% j 'name' => 'Site Wide Notices',8 a- Z: e6 m/ m; |: v
'singular_name' => 'Site Wide Notice',
; p, {# i3 {1 `! m. U% M 'add_new' => 'Add New',9 ~+ j9 U6 K ~6 S! v; M. A
'add_new_item' => 'Add New Site Wide Notice',$ T; ~; Z7 \/ \/ _
'edit_item' => 'Edit Site Wide Notice',6 ~/ Y+ L+ C2 V1 c7 u* u
'new_item' => 'New Site Wide Notice',
, ^% g, P- L4 H0 J5 q# E 'view_item' => 'View Site Wide Notice',- I# n: N# r! H$ V; }9 f7 M
'search_items' => 'Search Site Wide Notices',
) E$ @" V; D# k 'not_found' => 'No site-wide notices found',( G: A9 U3 }( P; h* w( O: T& Y
'not_found_in_trash' => 'No site-wide notices found in trash'
/ o4 n& b) G7 ?: K* @: @ );8 `0 N7 A/ x2 R& i
% e) N1 O, z: e$ F; m
$args = array(
- V; r/ a3 F! u0 [8 Y& v% p 'labels' => $labels,
. S1 n9 X, X& ] V. I 'public' => true,/ ]) _" J0 j0 G( h5 g
'has_archive' => true,
3 a+ |7 M# l1 u) }% [ 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),4 ~, _# p5 [& s4 f6 L
'taxonomies' => array('category', 'post_tag'),
. T) g) e- P% M& R+ j 'menu_icon' => 'dashicons-megaphone',; r/ @) m; S! m% c$ r
'menu_position' => 5,
3 v7 p9 T h" m6 T5 J 'rewrite' => array('slug' => 'site-wide-notices')
' \( D- X( k) m );
8 u7 y7 V u% Q3 `, i; J9 c
6 k, m+ ~8 f6 W( [5 M7 N register_post_type('site-wide-notices', $args);9 m) y( V6 A! S. V0 H
}
, S, u% f: w9 l/ C- G: w ```' l- ~2 }% I. T+ K7 l* P7 z
2 O1 [7 K* p3 {1 R- L# v
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。, Q3 a4 T: V. L- m8 ?; P
/ K( S A Y8 \; x0 j% a: n3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
+ M' O# ^& v. V$ a4 |) W: x$ `3 H* ?0 r3 V, N9 \& r6 {, R" [
```
. F8 m+ j+ V5 u" X; w. z5 g add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
3 I( g4 r5 T B t7 a) ] function add_site_wide_notices_boxes() {) }. s8 h7 H# p B
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');3 R+ _. ]) C' |7 t
}1 ]6 K" g: | s3 y
& f2 U! y+ j* N/ e6 a2 S* M function notice_details_meta_box($post) {
- Z% W l, a m9 ~, N( r s wp_nonce_field(basename(__FILE__), 'notices_nonce');
- {; [3 w; l9 ^) X/ N9 Q $notice_title = get_post_meta($post->ID, 'notice_title', true);/ _& J- J. i' e
$notice_content = get_post_meta($post->ID, 'notice_content', true);% [+ E" u1 a- a
?>* a! y& u8 Y1 `6 L [
<p>
* z/ @: y6 h6 Z* H7 N <label for="notice-title">Notice Title</label><br>
7 O: }8 d7 e- P& Y8 Q <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>"> C, s( a. K% t5 F& R3 H
</p>, y& [" P1 `: d3 I
<p>
$ H5 Q% U, ?+ i <label for="notice-content">Notice Content</label><br>& F _' U" v) U6 {" Y- h
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
5 Q ~( L! z. X K" v3 N </p>, M' N6 o) l" c5 N4 L- h
<?php
# P" ~! ]0 h6 `# z* ^6 a }
2 l* ]7 ~6 u: @8 T- U8 h- C, u7 | @% A* b1 u7 l5 R& t
add_action('save_post', 'save_site_wide_notice_meta_box');
" g% u3 Q- a/ R5 a9 T function save_site_wide_notice_meta_box($post_id) {
) F; O8 h' ]# z- u( C9 C if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))" w& b, d* w8 F
return;
X# ], e; v: q- c* t+ g6 g% d if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)3 M1 n# Q) n8 y7 Y
return;
2 }8 S8 Z: w& G# ^9 X. P% [# h& D. E4 w/ \% k( y P- g
if (isset($_POST['notice_title'])) {
$ F- Y$ P& S { update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
# ^' G0 k$ B$ E% l0 i: x4 r }
; M! K- H. n% W5 q if (isset($_POST['notice_content'])) {
! [; j4 J. m5 J8 S update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
& h0 `( Z# U; b/ I3 b2 I }2 I; v; v( Y0 O! H
}
0 w9 Q1 ^5 j% u2 ~ ```& N- z5 ~3 j: W$ m; I
8 q* D* Y# g% h% w6 [6 @ t
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
/ D' ]1 L1 [9 f7 U/ {/ f/ I# u0 a/ e y
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
" U2 w* r1 }+ U6 @( N2 ?0 J; L- C2 s& o# y
```
4 g! a% z. a! t1 `. d4 b $args = array(; ]5 B7 `# o2 k
'post_type' => 'site-wide-notices',8 C: Q- J& s: s4 y6 k: P6 p) T
'posts_per_page' => 3," \& ~ @+ \' S7 _0 y# ~& }5 _, M
'order' => 'DESC',: y/ z7 k9 r2 h: M& m
'orderby' => 'date': [2 L" }& F& S" L% v; `9 A S
);% w- |! q$ y6 t7 G: k! p& v) @
$query = new WP_Query($args);
2 Z- c' |( V! w0 U9 x if ($query->have_posts()) :
, F9 e) N! D6 n while ($query->have_posts()) : $query->the_post(); ?>
2 @$ ]2 j, ?3 g1 o <div class="notice">3 D! w- B I1 L E
<h3><?php the_title(); ?></h3>
3 }0 e1 Q% B8 m# M2 U/ I% e+ f <div class="notice-content"><?php the_content(); ?></div>) F6 N& M. O2 U+ o+ ^% A" ~
</div>* H7 B6 K) M* w) B# b% P6 u4 J8 _
<?php endwhile;5 V% r% j" p8 _! T" R' ]
wp_reset_postdata();+ u G" H+ r( _, c9 l
endif;
4 X5 h+ }& u S* m) W; w. {0 j& D ```8 V5 n; V5 e" }$ _( }+ e) q
A- ` D& v9 {! e _ 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|