|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?# K0 H, Y0 y: n( |
/ |( U w9 M7 u
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。# a. \+ o% @; N
4 `! e/ y1 w5 h( m' ~以下是创建自定义插件的步骤:, w& Q+ N) j( k% n1 W% S0 @: n, u
! l" B" \+ r1 z7 B1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
; i4 K- u5 d7 g- b2 [$ [0 @+ }0 a% R! D9 J2 n4 u& `, g: ]- @# ]
```
4 r T' U. m- ^5 o) X4 r" D <?php
M$ T3 h. U) E. g$ }" x7 ? /*
0 C5 f2 v$ N3 U; p Plugin Name: Site Wide Notices Plugin* n" ~+ R! ]/ q$ p: g( N, k% e7 O
Description: Adds a new custom post type for site-wide notices.- [$ F8 ^$ I9 {9 f5 F" J0 g
Version: 1.0
/ P9 b$ d% z) o1 { Author: Your Name
6 L9 ]$ e. n: S Author URI: http://example.com( V" D* M$ Q7 x0 P: S& q7 m
*/
: }. p1 l W, u6 P) m
1 ~0 u, D+ d9 s9 G# i* {6 d // Add plugin code here...
- ~" i. `$ U: n* X ```
8 h9 e" D" ] a7 r+ n6 n0 Q, T% p, D3 l; x
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
6 R$ V9 ?- l6 ]9 u1 q B; b# s
! ^: R4 Z) I a2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
2 g7 G+ G7 b5 g' {! v
& r; R8 n4 H; ]+ o/ ? ```
; P1 B I5 v, _+ y/ z! Q add_action('init', 'create_custom_post_type');( V d2 t' Z" d- Q' M+ P
function create_custom_post_type() {
2 A4 v- r8 ?! ] } $labels = array(
; e1 n c. s8 F& {3 D, | 'name' => 'Site Wide Notices',- z+ e3 X$ V# J( ]1 d$ z* N' l
'singular_name' => 'Site Wide Notice',
4 h! z* V4 U, n, _ 'add_new' => 'Add New',+ c" t% G# }4 q3 c, r2 L7 D2 k# E
'add_new_item' => 'Add New Site Wide Notice',
9 X2 k0 Q9 q& x1 g 'edit_item' => 'Edit Site Wide Notice',
: k/ a9 V4 T1 ^ 'new_item' => 'New Site Wide Notice',6 B$ j8 D, _7 p' ]& ?' {
'view_item' => 'View Site Wide Notice',
" h! W' t) b4 a 'search_items' => 'Search Site Wide Notices',- {* _0 u' \6 w8 D8 }" Y ~
'not_found' => 'No site-wide notices found',
8 P' {- K% v9 a, w* l 'not_found_in_trash' => 'No site-wide notices found in trash') z7 u/ H) F* @4 C9 }; S
);, z+ r4 c" I% `+ ~( D# o' M. F
, { C: f$ W& Z7 ? M, ~* g: ? $args = array(
% e) D: C3 f, }; C6 n 'labels' => $labels,- M9 S, c; p( h+ d: m$ f
'public' => true,
4 p: v0 x% o. o8 k% ] 'has_archive' => true,/ L7 v* w+ E4 m# o- T8 w
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),7 F8 T; |2 r2 \- ]6 i6 m4 ~
'taxonomies' => array('category', 'post_tag'),. p# n6 {5 x7 Z3 n1 I7 t7 @
'menu_icon' => 'dashicons-megaphone',
5 ?# a( u, a8 m8 a. T8 I$ k+ X 'menu_position' => 5,5 ]% j# m! J; ^* Y2 X% U
'rewrite' => array('slug' => 'site-wide-notices')% i7 K' Y- F% E; F" v T: d
);( Y- h. _$ a1 V
+ C; U6 n) G- \ I* h; v/ H
register_post_type('site-wide-notices', $args);) l) i+ o! h0 Q+ C1 V
}
9 z7 _/ K* K8 h. X! m ```
0 F* E4 @) [; h6 E0 h! m0 G7 m8 {/ \6 ?- @
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。% k. s8 \' t3 @
) ^+ B+ W% ?1 |3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:7 Z3 ^2 c+ l/ r, Z7 L8 W; b
O( {) T: `5 K
```7 w% r2 T# A/ _( A3 l3 C$ g9 M5 ^
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
/ W; w& X: S {' ] function add_site_wide_notices_boxes() {/ g0 Z$ E- w2 x* t- G
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');, R3 N+ j- F. f$ y) W! z
}
6 W) v! Q2 o( E4 n: s6 h
0 @$ }; F( N, R$ T' k function notice_details_meta_box($post) {, s1 I* J J' L/ g% ^ u" e
wp_nonce_field(basename(__FILE__), 'notices_nonce');
$ y& g7 g0 Z* l+ x# {0 d $notice_title = get_post_meta($post->ID, 'notice_title', true);
. Z3 P4 g$ V( P4 k $notice_content = get_post_meta($post->ID, 'notice_content', true);
* ~- |& G. p% U9 ?' F ?>
" ?+ C9 P% m$ c2 c% ?& O1 V! L <p>4 J: B1 C9 j) X$ H- Y. r- i* t
<label for="notice-title">Notice Title</label><br>& T0 u) r% T5 i$ e3 u8 q7 Q' O3 L
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">3 j: j, G+ m+ X" G$ T0 @2 N. V
</p>
, J; N# T1 Y+ z, @9 m" Y <p>0 U/ C) K4 S$ _9 b
<label for="notice-content">Notice Content</label><br>3 v2 P; U( Q1 @% P
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?> A2 ` j6 _# S2 i, F2 _# e; P' z
</p>4 Q& @5 h8 `2 F3 L" I* D3 {, w
<?php
$ C4 f: \; ~/ s" n& U L# w W1 V }3 n1 k0 s( b# E: J" b! P$ V
5 b; H0 `# s% ~& P3 R$ ?1 B
add_action('save_post', 'save_site_wide_notice_meta_box');) U( u- I2 v$ \! y
function save_site_wide_notice_meta_box($post_id) {
1 Z" s. {% ^) F( u+ y8 w. M; m if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
4 i, y* H' L" p2 B! b5 \ return;1 [* G) {7 q: p. N
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
3 V+ t7 Y1 {- r) f m return;5 w+ l! @- l2 p, H; N
. L4 @1 I! R$ [3 p0 ~6 z% a if (isset($_POST['notice_title'])) {
& b- N( L- b7 Z( ^* @ update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
7 @. c3 }" M4 A7 M- z* I7 ~ }
' M( N+ b6 K; g8 r/ n9 @1 \- { if (isset($_POST['notice_content'])) {
" n7 x# O) P5 Q3 i$ f4 a update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));6 H" Q9 M9 u; B6 Z, w8 Q3 ?/ M
}
$ ^ a5 ]& F8 x$ D0 g- g }
: ]5 W# `) o9 @* a' j ```
( {0 t, @: L* P: \
1 D6 v+ Y: d& L# |2 g 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
# y: i: c! \. k; e' a' C
4 |3 E) R. L- V* P4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告: h* l K; P9 C( T, L
8 B% n! r; M) {: k
```* k2 t$ ? z$ m& q9 b. Q
$args = array(7 m: y; `# e: P2 E/ {/ G( Q# F
'post_type' => 'site-wide-notices',8 K: ?2 F/ ~/ j3 i2 d/ [) l5 K
'posts_per_page' => 3, i8 `3 K1 ~7 E% T# k7 S$ u
'order' => 'DESC',
$ N) |: Z! o! R: C/ ?% W" e( @ 'orderby' => 'date'. @! p2 {+ U1 n+ ~( o0 w) i
);6 U/ L \5 w9 G1 ^8 r8 h7 B
$query = new WP_Query($args);
& L- N! u0 N7 g if ($query->have_posts()) :
5 a. p- ?6 h! C& T: f5 h/ d; W- y6 p while ($query->have_posts()) : $query->the_post(); ?>% \. p% i. l8 E" c; b, B
<div class="notice"># F% r+ N! K* Q! E8 y. Q9 s! x
<h3><?php the_title(); ?></h3>
4 ?1 I) ?* s2 p8 X <div class="notice-content"><?php the_content(); ?></div>* j0 i% t. ?+ A1 `" N7 v* |
</div>$ Z' G% Q6 n+ a, {6 i
<?php endwhile;
% k" Q l, u! u wp_reset_postdata();
! W( `: B+ u1 v; ~; @3 Y& z0 o! p endif;
. E" _; m2 I& ^7 e/ I' v ```
( l. w& p$ g5 ?- z( C: i( X* ]
( \* L( b5 Y( m! h5 U4 Q 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|