|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?4 |0 E$ m, |" a8 h6 T3 r; t' c
( j. X+ X; y- s! p如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
' V+ \5 t$ \7 ~
7 N$ A2 @; R, ]. |3 }0 `以下是创建自定义插件的步骤:: h5 m5 O4 R" X# ]2 R G7 D
. a9 S( ^4 x! \* k* D A1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
" Q$ L5 E9 ^4 \( @
3 |( L6 ?+ c3 w! }6 U5 x' C ```
- \ c5 f% k9 _5 C8 y <?php8 r7 ~5 x9 X3 J/ R8 V, I
/*! M6 N, n- S7 R+ y
Plugin Name: Site Wide Notices Plugin! `/ x0 ^$ J! i ]7 X
Description: Adds a new custom post type for site-wide notices.
1 q* N% Y& ]: H+ ]% W, Y% R Version: 1.0
9 H n6 F# |; @- S; X8 ~ c Author: Your Name& z! Z) e; x$ U" f% ]; K7 T) x9 b6 D4 S
Author URI: http://example.com) ?. b& C+ P I. s
*/* X3 A' J# K2 Y- H
m2 _$ k6 Z& y" ^$ p1 x // Add plugin code here...
# Z( }( s) o* s, V3 g ```
c6 g: C2 O4 }9 X% u" a: f% Z$ n. h" D# W; {
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。$ Q6 q \* y8 N$ M
) U& `( {% S3 L) }& Q2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:& z3 S2 W7 v% X+ c
, ?; }% W! S* u6 g7 O
```
0 X" n, A# H4 }5 v) E$ R add_action('init', 'create_custom_post_type');# v+ k2 U6 h( o" G
function create_custom_post_type() {
j8 ?6 ]- P$ T $labels = array(
2 ^& E" s/ q, I7 Q, J$ t& e1 M 'name' => 'Site Wide Notices',+ f$ i1 }2 b4 h4 x) a
'singular_name' => 'Site Wide Notice', G9 X6 k$ i7 o1 n G/ e
'add_new' => 'Add New'," ]+ T3 z6 _+ `2 c4 X
'add_new_item' => 'Add New Site Wide Notice',
; M# t1 F. @ v9 G; z, a 'edit_item' => 'Edit Site Wide Notice',
6 Z- z/ ^% l$ Y( G$ ?& i 'new_item' => 'New Site Wide Notice',+ D% i' `4 F0 c! K* b
'view_item' => 'View Site Wide Notice',
" F) ], ^: M" |9 q: y. t, R 'search_items' => 'Search Site Wide Notices',& B# }- E" @! H0 @: J
'not_found' => 'No site-wide notices found',
9 k0 L1 {: k0 h: c- k 'not_found_in_trash' => 'No site-wide notices found in trash'% K4 q' V! y+ R6 p
);
- K6 y: K0 H9 I/ O; k6 ]9 h
; h0 @! `5 j, W5 Q $args = array(" P$ B1 b: U' r) `7 e
'labels' => $labels,
" \9 y/ q1 v0 ^& c# k 'public' => true,+ V9 h0 O0 B7 R& c- U
'has_archive' => true,
* n8 }/ p' b6 `8 R, B3 S* ` 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
( n1 _- V' [# |* T- l" A# I6 w, }( D 'taxonomies' => array('category', 'post_tag'),9 [* h8 i8 |, o7 E. z# m# V. w6 M5 Z; j
'menu_icon' => 'dashicons-megaphone',
M# ] d6 {! R6 m' { 'menu_position' => 5,& f4 c- Z7 ^$ ]3 T# z6 E
'rewrite' => array('slug' => 'site-wide-notices')
# G( h* y4 O! i: y9 c) F% p3 [" t );2 d; Z2 `) b h
3 s0 q4 k- |) k7 q1 G register_post_type('site-wide-notices', $args);' l; U3 i, ?- C7 }8 b- @/ g
}% {8 N5 ]& s: S7 O7 |9 d5 w
```3 y' X9 Y( W- v. Z: r& m9 V
% J0 w' X- P+ i. j! A
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。- ?, ^# u6 B: |
" b8 @& e" H4 z8 s4 U3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
& |& C% f3 I( r% }$ e: K4 `# f: |0 v1 y* J2 g& b( y# F! ~. c
```* g/ V- k7 h$ `7 _
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
' g C. u. H* {- V function add_site_wide_notices_boxes() {
6 c9 d8 n/ Z7 ` add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
% S2 M8 t' r0 i }
; B! N( _& ~$ h$ v: ~
$ `0 @5 g. _1 e# m: { function notice_details_meta_box($post) {9 f% V, r4 K% [& E1 ?
wp_nonce_field(basename(__FILE__), 'notices_nonce');. s' _7 A4 d, G
$notice_title = get_post_meta($post->ID, 'notice_title', true);9 ]: G8 h+ C! r
$notice_content = get_post_meta($post->ID, 'notice_content', true); u9 @4 f- z6 x' P( n& l' t- J: U
?>
6 B7 Z3 I8 j! v0 ? <p>! B6 M* F4 X+ c
<label for="notice-title">Notice Title</label><br>
, o0 R* X8 O$ ^ <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>"># P, B. a* c& H
</p>
- H$ _& Y8 N/ H- n3 I$ o& F <p>8 V" J0 ?2 Z$ Y/ r! Y2 @& o
<label for="notice-content">Notice Content</label><br>
m" @+ b; O' z1 P8 {5 Z/ G! g0 s <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
! Z+ e S1 W$ A: X y2 G </p>1 g1 l0 T- ~! x( B: }, Y& O
<?php
5 }- V8 p# ^; P7 \# L" A* h }
( l9 K0 i8 ]3 K9 ~ X Q8 F
* h1 H8 _9 a J7 D3 K add_action('save_post', 'save_site_wide_notice_meta_box');
* t& p0 L1 l! J1 W function save_site_wide_notice_meta_box($post_id) {
& c2 M3 W. S7 c* l; X5 q, p1 d4 M if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))), i, }5 r' ~+ F3 w) s2 ~, @, M; C; G% R
return;
+ ]9 e1 a+ y( E5 G8 F/ T# ? if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)6 O G# J7 S) B E* O) t( S6 H
return;0 v# W" g" L [/ Z# k6 ]" ?
+ ^1 g- R, v# T3 \8 { if (isset($_POST['notice_title'])) {( G& I- i9 W6 C3 s4 [2 x
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));( Z* [/ t/ T w+ [
}
& B7 ~0 Y. d3 A; Z+ q3 c. k if (isset($_POST['notice_content'])) {
p& B0 m" O6 { update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
+ Z9 I2 ~6 X# K9 F: Y1 E }
" I: c* s/ S. h: v8 c. E }
7 L6 y$ p; V0 ?( ? ```
: ]1 o' {7 [5 t6 p! s9 {9 k- ?
& l5 U. H4 n) Z2 q 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
& g' X! G- p# z q4 S+ }3 }0 D/ |0 c5 p9 w! F" H3 F! U3 w
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
# P7 s4 [3 a. N; A. e; g4 n. e7 A' Z( \7 ?
``` m0 J; t$ c8 {" X/ _* F
$args = array(
) o# a" I$ z( G: ^+ y+ y 'post_type' => 'site-wide-notices',3 }# @, h4 G+ C3 B' F
'posts_per_page' => 3,
5 m3 l; Y* F1 c) p- s) w" p 'order' => 'DESC',$ ^# s( L# R9 E
'orderby' => 'date'4 I; N1 `3 d/ n& W
);
$ r5 {2 [( Q8 l: K& N $query = new WP_Query($args);9 B, G) K; ]& q l# R$ ]8 h, B
if ($query->have_posts()) :1 }3 w. F2 a; C" J* K
while ($query->have_posts()) : $query->the_post(); ?>
2 t3 E, U0 g# x% y' s <div class="notice">
- |0 z- F* ?; t) \" Z <h3><?php the_title(); ?></h3>' r0 H* B' i6 X) D a5 P X) }
<div class="notice-content"><?php the_content(); ?></div>
. G! H, i" Q5 a </div>7 g! j, }7 h& i$ E0 M& r( B' w, ]
<?php endwhile;+ x7 c; R: x( M( N# Y/ ]- f- n
wp_reset_postdata();* T, z D m$ @3 ^) X% O7 ]& _
endif;2 e- f' y: N; L# n1 p
```5 _8 r# V2 W8 x9 J# f6 ]( G$ |& ^% {$ N2 b
8 F# M" \+ x( u/ m' M( a
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|