|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
! u" L. l* c0 C7 o2 @
0 A+ i6 w7 X1 K! L6 E& F' ~8 l; A0 l如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
7 ^: ^/ b% Q$ }' L5 I; T3 l0 N' {6 n1 [, S1 q
以下是创建自定义插件的步骤:
. o: \5 f$ w! ^2 ]1 i
( G( K5 P. q# e2 Q M1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:! l' f; G/ h$ h- q1 c
, Q! J% V& Y! v& e6 a0 U ```# ]: r1 d4 @; i
<?php
& S; P. l, y' w: f /*
0 i0 n& l( ~4 P( S Plugin Name: Site Wide Notices Plugin
3 L' ]" d( S7 M' N0 C Description: Adds a new custom post type for site-wide notices.7 H8 Z7 H4 l) t) p9 r5 G7 i
Version: 1.0
( |, J2 F+ S' O7 E7 x Author: Your Name
" L- O( A! ] q6 I. K+ t Author URI: http://example.com
$ ]5 W" O$ ?8 s */
% ~' j1 K6 J# e6 L8 r% a1 \0 w* J7 ]/ X" B3 p+ R
// Add plugin code here...
) b2 r4 C) q# j; h: F" K ```
/ ^9 }" O1 I2 X9 I
- d7 E. |5 H4 M! t& A 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
1 l3 i6 a- S& ~. `
, `2 D) M+ m3 k2 Z- F& ^, T, R0 L+ Z2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
: J/ m8 }5 R9 F
' m; j- s& A/ u: A6 ]. F ```
" l- ^/ m' a) g/ } i add_action('init', 'create_custom_post_type');- `* x. t: k, k$ F# B
function create_custom_post_type() {! ?& d# U1 E$ g, |
$labels = array(4 m" d3 o/ q- K4 E# u D
'name' => 'Site Wide Notices',
& ~4 v+ Y: k0 j% K6 y0 S0 @ 'singular_name' => 'Site Wide Notice',0 }( g$ ]1 m( c$ ?/ C
'add_new' => 'Add New',! p: L( Y& z* ?6 [
'add_new_item' => 'Add New Site Wide Notice',/ g! C+ N c% a. W5 [
'edit_item' => 'Edit Site Wide Notice',; U0 Z# j1 [# Z7 g2 ~# C( x
'new_item' => 'New Site Wide Notice',
% j. S9 P' q6 k: Q 'view_item' => 'View Site Wide Notice',
6 b( S* t9 X/ S' f, t 'search_items' => 'Search Site Wide Notices',
2 [) e/ s' u _( l( G$ c 'not_found' => 'No site-wide notices found',
# H+ X! H& _6 h3 r, Q p+ T 'not_found_in_trash' => 'No site-wide notices found in trash'
, k) C7 i+ t; U) n/ @ );
2 R2 c; L1 e! b# |' N: M2 J _8 Q) p6 q U
$args = array(
+ c. e" K' i( O3 A% R 'labels' => $labels,4 n7 e; U) c8 r. W% ~
'public' => true,
: d6 Z0 y N- ^ 'has_archive' => true,
$ Q% N& M7 `* `1 W c 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),4 f. K+ m6 \& K, \
'taxonomies' => array('category', 'post_tag'),
& l+ G0 y& V/ [% E! m* l6 ~( O 'menu_icon' => 'dashicons-megaphone',* v- @4 `/ c$ q' B, p( ~
'menu_position' => 5,
# F9 s! `" q; C) q, f) r" m! @" y1 G 'rewrite' => array('slug' => 'site-wide-notices')' {9 u5 C& y" l8 @( X$ n8 o
);; N, v+ t- x, L/ u) q# X: l; \
" o5 k v L' r2 i9 C+ f+ T) F
register_post_type('site-wide-notices', $args);/ ~4 [3 E. j+ H0 }, j! f( k1 e
}; L* ^. c. Y0 ~( D; ^& }
```/ U3 J+ v& S6 N' c0 f7 P; {- U0 t" [
& C" q6 O% d- T9 c& Z. [
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
1 n; M# h& v, d% y+ c) E, g; L$ E t+ M3 L4 x
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
+ s3 `7 k+ Y5 d/ N- x7 z8 q
* V2 n6 G' h. W ```& r% i; N- s% t% Y" z
add_action('add_meta_boxes', 'add_site_wide_notices_boxes'); G9 L0 c( m& y B4 W' V- Q
function add_site_wide_notices_boxes() {
9 S S; c" S7 l& k add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
f3 c; q' q( z1 o- }0 P' a; D* ~ }" I$ y2 q8 I; M! B3 ]5 w
- F, z3 @# G+ @) | function notice_details_meta_box($post) {
9 P$ S# Z: u, {6 p) w; H9 L wp_nonce_field(basename(__FILE__), 'notices_nonce');1 \0 _3 A4 @" m' F; W3 ]9 x( c$ V% B
$notice_title = get_post_meta($post->ID, 'notice_title', true);
: \- d$ o8 }$ A# i7 W $notice_content = get_post_meta($post->ID, 'notice_content', true);
& u# U; u& |! l1 g ?>
% [& J9 X3 T" `: G% N) C <p>
& a! E. i2 {9 W S0 z <label for="notice-title">Notice Title</label><br>6 G' i# H1 v" r" b, V3 Z
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
1 \! v9 ?- X5 Z5 Q' H </p>
9 l+ ?8 q" Z8 } <p>
0 [4 {3 V4 o- ?, Q6 g <label for="notice-content">Notice Content</label><br>- I3 ] c8 \& I+ x
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>, l1 K; j9 V. ?( o; t
</p>
) F4 ~+ u; ~8 _1 z <?php: o P) g/ l/ ?* t9 S" R* \
}7 C" V" q, B+ K' M" P$ i0 H
4 ^4 ^ u0 e# ^, Q add_action('save_post', 'save_site_wide_notice_meta_box');) l2 g7 L9 w% b
function save_site_wide_notice_meta_box($post_id) {
: K$ o" Y/ C- g% d1 K if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))). d6 c, @ z& W& i" d8 n
return;
1 N2 r: u0 e2 `7 F3 Q if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE); \+ G2 k x0 R" C* M7 s
return;
$ C E6 S5 l8 @* |; Y5 |3 M8 X: X1 Y( x
& V* A7 f8 ?1 I; O if (isset($_POST['notice_title'])) {
( _, I% L5 ^4 S) h# M* i: L1 y; { update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
2 j f2 L/ c1 V& }6 z+ F0 X }3 h5 O" h1 ]: o6 l
if (isset($_POST['notice_content'])) {
4 ?, Z' F R% P0 U2 O4 D% \( V, C update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
- {7 A! D/ o, W: K) s' E }, d2 t6 M1 E8 ~( c' }5 V: E b
}& z! a# T& n0 B; `" X0 w4 G3 D W
```
% [3 q% d b( @7 `! w$ K
/ U0 E/ d2 Q1 A2 d 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
0 y3 ]' T' w' a
9 K0 z5 k; k0 c; I4 ]4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
) T8 i. U6 h; i" t9 \+ I% x0 d
/ @7 H: H6 H/ k3 \5 e ``` E0 K; W) Q7 \1 s9 B
$args = array(
D$ b5 B+ o& K) g2 J$ N 'post_type' => 'site-wide-notices',
9 R/ @9 G- |) p- G& P1 q) `/ u% O1 n 'posts_per_page' => 3,% l: N! l7 V$ _ w" G* y
'order' => 'DESC', n. J: @) n# j
'orderby' => 'date'
3 D8 N- B" d4 `; W1 { );" T1 `6 O! N9 k* J$ I- j& i
$query = new WP_Query($args);
* ~% g0 s9 D5 O! m# P& \ if ($query->have_posts()) :
) _' }. j/ a; u3 y while ($query->have_posts()) : $query->the_post(); ?>
0 k* Z0 H b0 T& x- U* ~" J <div class="notice">! ]% r+ R0 }* D5 s3 B! T
<h3><?php the_title(); ?></h3>
; ?9 [5 s* a# z2 A4 k <div class="notice-content"><?php the_content(); ?></div>
6 U0 c5 f! ]9 ^: O0 I( U/ R </div>8 q) \: i; X' s# ~; z8 k5 P, g
<?php endwhile;
' M x2 m! X+ S wp_reset_postdata();
# O. [4 a* j# z8 l! a! S endif;$ H* l0 T5 E% U( X4 c
```
$ l4 s9 x3 V) s- l$ W6 |# Q2 U5 t& A
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|