|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?7 j2 L. [0 m# i6 \* K" L
0 Z, L$ t5 |& l \2 p1 m如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
. ] K/ Y& w! o! k. R
. b4 v0 b; F7 D# s以下是创建自定义插件的步骤:
: Z; c, h( \( f9 Z, ~1 Q9 |' [4 }1 ~+ R; Z* e/ _# K# L
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:6 l' f* F! f7 R5 P2 d* Y' L
1 t' \* l: a. i9 H
```2 F/ o( L: C, u
<?php
1 \% h, e+ [6 o1 v( l: P /*
+ \% D7 l8 ^$ R4 |& q0 h Plugin Name: Site Wide Notices Plugin
% ?7 L: O i' W! `, B Description: Adds a new custom post type for site-wide notices.
, O0 ]% [* f0 T Version: 1.07 O$ u$ h5 H# W& E5 i" S8 k) F" U
Author: Your Name
0 h$ M7 C. K& q! Z. N Author URI: http://example.com1 U* n b0 K# p: Y8 i3 m& w
*/* A4 i- E/ c L+ s. U+ T9 {
% ^1 ~* m* v; z- H. Z9 W* P8 m // Add plugin code here...
: `4 g4 ]. y% x* d ```/ y$ u$ }1 C, y, W. E0 k5 F6 l" p
9 A; n1 D$ U. W# d1 |, c' ~$ S 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。0 l8 ?8 r6 q9 w) Z0 P( X1 g
: A6 H8 J# v5 j8 b( Z2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:% Z) H& W2 O+ y3 n
6 T* `! L& h0 @% B ```# l3 @! E+ e0 {0 l
add_action('init', 'create_custom_post_type');( r! f. F% A, U6 A& v; D1 Z
function create_custom_post_type() {
# v, n$ M' F- Q; k5 z5 f; m4 I7 s $labels = array(
. q7 S9 Q2 H% O5 n, u. _ 'name' => 'Site Wide Notices',
$ V, o9 R6 ?9 T 'singular_name' => 'Site Wide Notice',7 |$ k9 P& ~: I$ O$ [7 V6 b) V
'add_new' => 'Add New',
+ w: t& d; j z* k8 F/ s: {. z! N 'add_new_item' => 'Add New Site Wide Notice',- u' {% M5 X) g. G" p
'edit_item' => 'Edit Site Wide Notice',
( E) B2 b! S5 L. U& @- z# ] 'new_item' => 'New Site Wide Notice',* W1 E5 c* s: M; d: c
'view_item' => 'View Site Wide Notice',/ L9 T$ I. A) f5 `) k* W
'search_items' => 'Search Site Wide Notices',8 o1 B/ f& O( ^( Z7 G
'not_found' => 'No site-wide notices found',' b! X* {6 K0 v, b0 C( e5 }5 X k6 H
'not_found_in_trash' => 'No site-wide notices found in trash'+ d& B1 P+ d$ p( Q$ R1 l
);
1 C! e! h2 I6 |6 R ]# h# t! _; a; B' {7 T
$args = array(
0 X; j# Q6 O( ? 'labels' => $labels,
1 K+ v& v) E8 P# w9 s0 f/ e 'public' => true,
6 @% i* E: D6 k4 L9 t: g 'has_archive' => true,5 i) }5 s) Q' h0 D
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
( `- @: t ~" n7 R 'taxonomies' => array('category', 'post_tag'),! Z* B: k- M/ S0 |7 f- F
'menu_icon' => 'dashicons-megaphone',7 s1 K6 N) x7 @/ [: @ J4 g
'menu_position' => 5,- b8 q! O* s( L4 A8 L
'rewrite' => array('slug' => 'site-wide-notices')
$ E. W: L7 ? t2 D# F: y );
, J/ w1 n. C" `; Q
! I5 k' E2 b# Q: w1 m register_post_type('site-wide-notices', $args);7 v# y- }0 |: l/ o9 M { ~
}
5 c+ Y% x; \5 A4 H7 { ```
3 ~* A! N m0 N. q. Y, }( h& {2 y' r7 h; N6 o% r- U& d
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
8 g& i/ |# J0 F% e
/ Y9 M$ U2 V1 ]9 W3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:8 Z0 ]; R; t' ]4 v* {$ M+ g( }
) y7 {( \/ Z, S/ ^' B
```& I0 m5 i" f/ R3 P6 I) |$ j
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
; c4 X7 v5 U# N. q+ e function add_site_wide_notices_boxes() {: U- x4 n! D' Y2 \# q
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');4 h; u L0 t+ m9 D5 F
}
& g8 W: w8 Y3 I' `, h* L4 F6 y0 T) m
function notice_details_meta_box($post) {
$ D: M6 U* K1 Q: V% a9 ~ K wp_nonce_field(basename(__FILE__), 'notices_nonce');& Y8 y5 [6 M9 _" I5 L& X2 ^
$notice_title = get_post_meta($post->ID, 'notice_title', true);3 K1 i8 w) c6 q: i) }/ z! ^
$notice_content = get_post_meta($post->ID, 'notice_content', true);/ Z F! ^, o, D, i% t
?>
! d f6 a; q0 d& A <p>
" S( N9 x8 j$ U9 Y <label for="notice-title">Notice Title</label><br>
3 C! M6 s3 ?6 v; D2 ~" `. G <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">" X5 k$ {, T& K) u8 Z
</p>5 q2 U5 q' |* d" D) \- L% W- J' X
<p>
1 D3 I0 I6 Y9 m( a <label for="notice-content">Notice Content</label><br>
8 _% O8 \2 E7 {' L <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
$ f M/ f6 ~& X- E7 h </p>) n- J- K/ w7 [) H
<?php
4 b$ y9 S9 X- W }
( k. `! c. G% C2 m1 m) m3 M, V/ H9 X$ y" Z
add_action('save_post', 'save_site_wide_notice_meta_box');$ }2 I t0 K& D* o) a
function save_site_wide_notice_meta_box($post_id) {: P! x; Z# `6 U/ D! ?
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
/ {! s, m7 Q! @4 N n( x1 L5 P @ return;+ H1 P0 r4 K: m8 G, n9 I
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)8 V& K1 K2 p( [6 b
return;
$ B9 [4 ^ u- Q6 n2 F! F9 g- l% r+ N* ]4 i9 w: x' d
if (isset($_POST['notice_title'])) {
: K4 _' g9 J. \. C+ W9 x- Q7 p) W update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
# A6 ~4 k3 }( r$ w9 M3 a }
% s& h2 [. J* T$ O# K* w if (isset($_POST['notice_content'])) {
2 z' X: Q% y3 k0 O+ T0 C update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));2 a- Q& l d( I _7 U) E( }
}1 x8 i( t0 r" F
}
~! N2 j4 ^3 n, f# f7 L q1 E5 { ```) H7 c: x! P( l) f8 h
( S6 Q0 ?* q6 c6 H) ^, O9 ~
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
" t" L5 J# M/ B3 C6 ~# E( Z- T
. s2 F- ^8 q' a: S. g8 W4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
9 J- w2 e. ^9 a* X4 x9 I9 w; l/ e; Q& S7 ^) M2 r2 Z; {
```3 {( C4 Q7 Y, A" v8 L$ j& s
$args = array(
( @" ^! i. C, e" u0 X 'post_type' => 'site-wide-notices',
2 @2 s( j& C; i 'posts_per_page' => 3,+ T+ l) I( \* v- P% [
'order' => 'DESC',) ~; e4 m" m% [" t
'orderby' => 'date'
. S: V+ t" B7 g* o1 [: b" V% g );# m$ h8 E+ ~ e. f# J
$query = new WP_Query($args);
5 Y' B) L3 o0 m; _ if ($query->have_posts()) :, m+ `* ^" j: D* ~* V) {3 G1 r
while ($query->have_posts()) : $query->the_post(); ?>
. a2 Q7 B1 E/ I, P7 R6 n <div class="notice">
; M; `$ f+ p! a7 D <h3><?php the_title(); ?></h3>
) K6 o' d. N% S w <div class="notice-content"><?php the_content(); ?></div>8 J# H. ^9 A' K1 O
</div>
9 w3 E/ U% r1 \" Q* G' ]& @ <?php endwhile;
& P7 k6 l2 T7 [" ~, T: C wp_reset_postdata();7 G$ w" n2 e7 t/ R8 Z3 ]/ ^6 X* _- ]6 L
endif;& C! J; A$ _0 b
```
# T \" b( E5 C6 n1 A3 y" J4 q T3 G
" a+ z9 U' r3 |+ F. C1 S c: }. u' O 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|