|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
4 M3 [0 I& ~' N, m
# I7 r2 I$ K8 h如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。& h4 Z" O/ F3 o' C! l/ w
# {; H$ I) ~6 ^3 V以下是创建自定义插件的步骤:+ @" s: A0 Y1 t2 C4 z3 v; i; P
9 {5 s4 l1 B% A; Z7 S( Z6 R7 Y1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:/ {) y. _2 J& a2 a& |' L
/ g$ {& Z+ |0 c3 v1 p9 y" r: G8 _
```
3 g8 M5 a2 s# ~' t <?php
% H# ~& l. d$ g0 l0 q" P /*9 Y' N" G$ x" p* H0 I& Y% x
Plugin Name: Site Wide Notices Plugin
% }/ F4 N/ {) s7 Y Description: Adds a new custom post type for site-wide notices.
, [# a8 W% n1 K4 |6 F1 b Version: 1.09 F6 i2 E+ t- I- E$ w) r; l
Author: Your Name
7 t- l; W" r7 m Author URI: http://example.com, t ^5 g/ m" N: [
*/
* W* G" L- F" S1 S0 K1 j% C
5 W7 B4 h5 F% C7 ~/ e // Add plugin code here...
2 p, A# S3 C8 c1 X ```
$ w2 w u5 N' g+ i+ Z3 J, I' R
5 M0 C: t- U$ l& z; \- j 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
) I- k$ A; H ]% ]& M2 D2 s5 B M. {+ Q" ?! K
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
; J1 ^5 ?" k" x( R0 Y
' `: D- R) O. U+ ?+ f ```2 D) d* w% m# J) V; n
add_action('init', 'create_custom_post_type');6 k3 |+ m4 L* |5 d8 y
function create_custom_post_type() {& j2 Z( p; G7 a2 H0 f9 I3 P4 E; S& e
$labels = array(
5 f% J W8 {: C. A% e* P2 d 'name' => 'Site Wide Notices',
! ~1 k4 {0 h5 P! j; Z 'singular_name' => 'Site Wide Notice',
5 j! `& i6 u- o( Z% i* Q; x 'add_new' => 'Add New',/ E+ c; n0 y t
'add_new_item' => 'Add New Site Wide Notice',: H( g* K0 T9 K4 K& S
'edit_item' => 'Edit Site Wide Notice',$ b% _* A0 D9 |6 x7 ]
'new_item' => 'New Site Wide Notice',
& S, R) o" Y, R# z- N: O 'view_item' => 'View Site Wide Notice',. S! _3 d( y4 a' d/ g
'search_items' => 'Search Site Wide Notices',% @3 a: L* g+ N% O x% ?
'not_found' => 'No site-wide notices found',
3 v9 }3 ]4 g; ?5 Y8 H9 E: `4 u' K 'not_found_in_trash' => 'No site-wide notices found in trash'
7 Z3 I! t6 Q& D/ c& c, M );4 S, t7 Y0 l8 y' Z' ]1 r, L0 z
& Z4 E$ x4 ^' p& x- e/ K, N
$args = array(
& s& G& G2 ^8 I+ {; v! R 'labels' => $labels,
5 `9 u% d! c& F% E 'public' => true,
7 ? G8 |( l/ L2 U! G$ _7 m 'has_archive' => true,# o- a0 m! |/ t; w
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),! D$ ]" @7 j4 {! v. Y* } u
'taxonomies' => array('category', 'post_tag'),1 e, Y' W0 E4 p- {% S# E y
'menu_icon' => 'dashicons-megaphone',8 ?0 h$ r" d0 q7 o
'menu_position' => 5,+ E0 m# d# R$ i
'rewrite' => array('slug' => 'site-wide-notices')
' Z+ J: y# u1 p3 `" O* w );
8 l! E4 q* B9 f# q$ g* s# F
- ?) s6 k- _, n2 c& O0 |7 j register_post_type('site-wide-notices', $args);# T# B' O8 X8 ~* V5 }% T
}
4 [& ?- p0 U8 A. Z* I ```
+ N$ K( K5 z$ Q' j9 W0 L% E. G- Z1 r* I5 r! o b! F
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。7 w* k2 J2 ]8 I" b* B4 U
: p4 F- F" \7 Q/ z# b! b$ L( R3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:+ H, ^( _' u) ^9 e0 M3 N
% u* g$ q# f) r% F# H1 j2 M. r
```
/ L! p" t; ~ \" b8 }5 p# A add_action('add_meta_boxes', 'add_site_wide_notices_boxes');- y0 @ v* ^8 z, |
function add_site_wide_notices_boxes() {
# f: C( d9 u6 `6 q- Z add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');4 `2 O. a2 E5 Q: L. V: J% K
}
' L- v4 c( j* F( i! V# Y( c. |: q/ X. z( \( M; ~) m) m5 n1 h
function notice_details_meta_box($post) {
2 W! W! L3 X( @1 `) W1 l wp_nonce_field(basename(__FILE__), 'notices_nonce');* ]) p% S1 Z: W. I2 o
$notice_title = get_post_meta($post->ID, 'notice_title', true);
2 x. v+ }5 ?5 ]2 K( d" x9 p4 u $notice_content = get_post_meta($post->ID, 'notice_content', true);
4 P& W2 [9 s, l- p ?>& M8 N' f0 F! t$ i
<p>; U$ T+ s2 `0 t- s+ Z% y6 }/ Z, S
<label for="notice-title">Notice Title</label><br>
I5 Q# L1 i7 z( _- n u <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">5 [# Z0 N: H# n* D: G( x8 P
</p>: {; G/ U8 k7 U# ?7 x
<p>
( L; X. f g" O <label for="notice-content">Notice Content</label><br># T4 Z' M8 S9 |2 a8 F( @0 U0 L
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
# g$ p6 v* S% o2 c" G </p>
* U" b/ M4 p4 L/ D/ Z$ b% C" ^ <?php( o! N7 ~: C/ a/ ?0 D
}: D' a. `$ |3 m& N' J7 J, Q
2 ~9 ?* C' u# [1 R7 b& Q* S3 [7 Y
add_action('save_post', 'save_site_wide_notice_meta_box');
5 p# K( g1 |8 r! e) ` function save_site_wide_notice_meta_box($post_id) {- y$ |/ F- M: j# C& H3 W" p% m
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))* k" P3 y, A( l% L* U c$ {( e! B
return;
* N& R' {' X# m. y if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)# }# S% F& U, i0 E! S
return; u2 S0 q- b* F% ]" V( |
6 {- s* f0 I; a) u B9 k' w if (isset($_POST['notice_title'])) {
9 P' V* g' N* T- K4 \ L update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));, m# Z4 S, N2 k2 q( G) N
}$ ^# ?7 t5 O3 _) n7 h
if (isset($_POST['notice_content'])) {# e7 R) Y5 r8 P! ? h0 g
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
! z h8 s( i% z, A }' g5 Q @) l9 E% R _
}! z' ?8 D% ^% j; C$ Z# R2 p7 d
```
. o9 y8 d; D1 S& n8 R0 }1 M8 l( A7 O
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
( }; }8 s0 g# V% Y" F! }8 o* \# E4 z% f* R& s
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
8 f' T+ ]( P6 i/ J6 U" R$ p" F) b( E" r z0 K
```
$ X( X5 P5 x' s& h" v) O $args = array(
! X" O- h/ i$ y/ F1 f 'post_type' => 'site-wide-notices',2 h# O9 G) s% |5 T1 v
'posts_per_page' => 3,3 z" s' v! s% R' L9 A, D+ k, j9 s
'order' => 'DESC',
6 r! N. X0 N: x3 X* `2 B8 w8 \/ f8 ~ 'orderby' => 'date'
. z8 i3 m) U* X4 V );
% Y+ a* f+ B+ U$ C* e $query = new WP_Query($args);
- l) c, E* h: _ c' h: U if ($query->have_posts()) :
3 @9 R' B R: |1 R while ($query->have_posts()) : $query->the_post(); ?>* X. ^! w& B: y
<div class="notice">
0 D- @" g& C3 t; Q* T# r <h3><?php the_title(); ?></h3>) P+ W4 u+ \! `. ]* C& c N
<div class="notice-content"><?php the_content(); ?></div>0 M$ I1 @$ k3 F" z: Q; e% X
</div>
' a: z7 _! A2 g7 V. E1 j8 k; H2 J <?php endwhile;1 `% ^! l; Y, A k
wp_reset_postdata();
d! G& b. d K7 E( h endif;5 @$ Z' Y6 l# N' V
```; l1 e5 w2 t4 l$ h4 ^ R8 x; b+ k( I
% V8 X) E- j$ d! o: x: r6 z3 ^% h. _ 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|