|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
0 o3 o g/ T, f6 b1 a
7 Z" `4 I4 X; [/ k3 e如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
5 C% z6 g; T3 E w5 J" f7 M5 M6 p# F$ n- P+ P S4 h+ Z" v! u5 ~( g- g6 I. p
以下是创建自定义插件的步骤:) \$ ]- I5 H2 t, S9 T c: b
2 t- K% R) [6 C8 I* @1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
% s0 v6 m! [9 C' b3 ~% S; Y
5 ~; u( [7 W: p/ ]' U. O ```( A, f$ o1 a1 s5 c3 d% x. C
<?php: q% B* Q' h' l: A. ~+ m
/*8 B7 P8 X0 l& {
Plugin Name: Site Wide Notices Plugin) @5 O, W! P1 Y, y. o6 w
Description: Adds a new custom post type for site-wide notices.0 P* W) l9 ~4 V+ m, ~3 j2 T% ]7 O4 E3 ]
Version: 1.0: K& N- c1 A/ D8 N3 z& q
Author: Your Name. ^* s+ q! j; F
Author URI: http://example.com2 J- K4 d2 c4 h4 C' z( I
*/" k6 ~$ T& n+ [: k0 ]
0 M" F& r4 u* j2 V, O: Q! q3 t
// Add plugin code here..., Z) U B! l v( E
```9 c9 I6 m! o/ ?2 H9 t: X
, X9 v, y0 P( q
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。/ L6 r' s+ g4 M: m
; R( f$ L+ h# T M: `
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:; s% q8 T3 U6 m4 g/ T- }
0 w5 x( }% y4 m
```# }' `+ F$ `# B" l( ~7 s# e
add_action('init', 'create_custom_post_type');
" a8 f y$ q/ r7 t- i. q P( o function create_custom_post_type() {
# c% g5 ]6 b+ U' q9 U $labels = array(
4 h9 w9 M& \/ E1 U: d# W7 | 'name' => 'Site Wide Notices',
2 u4 ~8 k, n% @+ l- b$ | 'singular_name' => 'Site Wide Notice',
& v6 h) [" O' H( |2 f 'add_new' => 'Add New',3 E& s. P/ s* k
'add_new_item' => 'Add New Site Wide Notice',4 k, I# v- u& k H% z
'edit_item' => 'Edit Site Wide Notice',& |0 n4 A5 l% G5 b/ a
'new_item' => 'New Site Wide Notice',
) ~1 j! C: D) p6 b 'view_item' => 'View Site Wide Notice',) @+ B% D9 l/ B3 A8 B0 u( a. g2 Y
'search_items' => 'Search Site Wide Notices',
$ a6 j* L z5 ?7 r/ p! `2 Q1 e 'not_found' => 'No site-wide notices found',
! }2 g8 _ W. R k& G& x8 W 'not_found_in_trash' => 'No site-wide notices found in trash'9 J. q+ v& F: \% }
);
3 q$ W$ L& q! ~# p8 A7 X; J8 Y) B2 s* `0 o! p
$args = array(1 m( o) s' t' q; X& T* p
'labels' => $labels,/ ~/ p& F- Y) D# C. @8 r: F8 b
'public' => true, x- \2 a/ M$ g7 Y# S8 w
'has_archive' => true,
% P! O+ [0 ]4 E9 P+ B0 F' \ 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
4 s- R4 j8 u& h. t 'taxonomies' => array('category', 'post_tag'),
9 l; C; e) B6 P, f2 I; n) C! L9 X0 E 'menu_icon' => 'dashicons-megaphone',- w8 s* R$ I \! J y+ _4 V; X' m
'menu_position' => 5,
: G; Q$ ~! y4 D" E 'rewrite' => array('slug' => 'site-wide-notices')" O1 t, C0 v# {/ g: v9 ^
);
4 o$ h/ {% ~% B. j2 r9 |
+ ^( w5 M; W ~/ y# t' H9 ?; n; ^ register_post_type('site-wide-notices', $args);! t2 y9 ?; d& z8 U0 P% a$ T+ v% z
}8 u) Q$ x( h) ?3 a
```' d! E2 ~/ v% t# y5 H
$ C5 |, t# j5 o
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。% p2 q5 U( I- i2 ?8 O
' ^: @. n: y" K, R1 @
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:! Y; n& O- {7 J8 l. b- j# P% N
! @) ~: p% n1 y5 M
```
4 `: p% C( N9 P0 X add_action('add_meta_boxes', 'add_site_wide_notices_boxes');* y: |5 V: \# o
function add_site_wide_notices_boxes() {2 \; R, a5 S! E1 \
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');" v& D" {( f3 \1 o8 l
}: `$ U, p$ u- H% y0 ?
" Q; O/ ]# w* o4 }( D: a2 g& Y function notice_details_meta_box($post) {
. \" N; O3 F8 M wp_nonce_field(basename(__FILE__), 'notices_nonce');1 A; v8 {% Z8 m- N6 S
$notice_title = get_post_meta($post->ID, 'notice_title', true);
5 w: Y# @. Y p3 v' G* R $notice_content = get_post_meta($post->ID, 'notice_content', true);
8 \9 o# a% t+ W9 m0 X' A ?>
; d- [( G9 a0 p7 {4 d <p>2 o/ l! |! y# J4 k' J
<label for="notice-title">Notice Title</label><br>
8 O. b ?/ U. Y <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
& M' B7 z5 L% `; c8 u; z </p>
) H) }% N- _6 K- ^, x <p>% x+ @+ o# d7 r% Q5 M/ o
<label for="notice-content">Notice Content</label><br>
% I3 H# J8 S* K: Q" I: m <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>; @0 d# Q* ?, Z3 P7 t
</p>
o+ _6 i" t* r& F& s7 d <?php2 e* ?9 h4 g# Z) y1 k
}
- w5 f. W A. K0 Z5 E/ U. R# g, i6 g% E# y1 V& F2 U. @
add_action('save_post', 'save_site_wide_notice_meta_box');
$ n4 w6 _: _' ^4 p6 f function save_site_wide_notice_meta_box($post_id) {& [6 B5 _; B9 f, W8 s& p: Q
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
) U5 m% ^ {9 N1 q- {9 c' M! f return;
$ g ]4 g' J; U- V if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
0 ?& s7 P' V$ V; Z6 {$ A: P5 r9 x return;
# L4 T) G' j5 p/ G9 z
W) M/ `2 l# N; E& E/ o2 t if (isset($_POST['notice_title'])) {
* }0 l) k& K0 n& k: a8 L; { update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));# s! E3 U& u' J) S. y
}2 l h0 o1 L/ E, u. e" D
if (isset($_POST['notice_content'])) {6 ]$ B4 t1 X- y: b( r5 {
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
! |9 @8 G* Q; @4 t1 m }9 y) ?. ^! r8 a# y- z: H
}: Z6 |' ?; A. \7 E# X& W- B' V
```
% e, s5 J7 H- ^# S v8 F( f
* s! o- y2 d6 Y+ d 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。- ]" {* Z$ o( C% M r
3 t/ D3 \ \" Z4 G: N4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:4 p. E2 v1 k9 A" n0 c
% }. T; y; a! I+ q& ?1 y
```
: T0 F; P7 j% ]* J q $args = array(
) z1 D, L3 C3 _) ^' @$ n3 F! _2 G. ] 'post_type' => 'site-wide-notices',
- M0 z5 I, R; f; S 'posts_per_page' => 3,
. g" J/ @# H5 K 'order' => 'DESC',
$ s; a8 }/ g2 y1 L4 x+ y 'orderby' => 'date'
: [8 j# _" a: Z/ ` );4 ~/ b9 K4 x5 B3 x. t& x; {" V
$query = new WP_Query($args);8 W" I' ?, h3 R# b+ N9 z
if ($query->have_posts()) :
, ^- F8 V& J( P) F: M while ($query->have_posts()) : $query->the_post(); ?>* ]" ~; u& _2 m1 t( I" r% C4 g
<div class="notice">6 d2 X6 @6 x% i+ _( `
<h3><?php the_title(); ?></h3>5 Q# X& ?7 p4 C5 b
<div class="notice-content"><?php the_content(); ?></div>" V1 L7 B! o! v" d* X. j" H; d
</div>
' d5 u J0 p+ j) O' Y, R/ D <?php endwhile;
# G6 H. E) r2 J7 L* R& o wp_reset_postdata();& w$ e) c, r/ B1 s( v! d: ]
endif;
( W2 ]/ U+ i; T3 \7 a/ @+ H ```$ M5 t) f+ j6 R! ^2 M
- I2 p. d1 ~" D; Z) L 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|