|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
2 q: t! z( f/ T- o
5 Q' G6 e$ f, Q& P& L如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
+ ~5 ]! @ J& j; U: x/ [9 N+ p5 I8 U( E. h. j; _. i& [
以下是创建自定义插件的步骤:
7 X1 P! M7 L @; K y L) I" c( {/ _3 k! H, G! i. F
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
; V. I- z2 a8 T$ K: c) [" n& `
7 F# S4 p; Z T* z1 K3 f: o ```( }7 W! }, }$ H- L" B
<?php* }- I- b, b9 G8 s5 B# @
/*
+ L4 u' N; J- C4 P& A9 R+ S Plugin Name: Site Wide Notices Plugin% L) c$ _' I! _) n8 Q
Description: Adds a new custom post type for site-wide notices.
+ B P/ Z. l( U9 w: b Version: 1.01 F( Z3 o. f4 O' ]1 M
Author: Your Name
. \8 d; {3 M/ R% I, L# x* I Author URI: http://example.com
8 a# x; {2 a+ Q1 Y, e" n. g */) ]; L0 \/ ~& q" b) f |' }
) d9 S# i% ~7 F) L/ k- ~ // Add plugin code here...
0 D& T3 W$ y2 U( F1 }8 Z ```
' e& Y1 J) y* S! D+ R
9 A7 \+ d$ K4 b+ a( Q7 i, b 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
+ a$ R- Z G% h* c g" w
. U7 g& ^; t2 T. \8 {1 Z7 A2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:) h- \# w7 e4 e" H8 {% }, x
3 G. J! m6 h, z- F. d8 Y, l ```
1 I+ Q8 H, X8 k( [- S add_action('init', 'create_custom_post_type');
& C2 I; D) e2 j0 @8 R function create_custom_post_type() {
' a4 ~- x, T4 }1 V9 ?( k5 B $labels = array(
2 {+ v% f" Z) Q: j% w 'name' => 'Site Wide Notices',
; g/ k) c/ A6 X( y 'singular_name' => 'Site Wide Notice',0 \( z) C F2 x7 L l
'add_new' => 'Add New',* @, P6 G3 f: {8 g% d/ _: o, X. {6 N
'add_new_item' => 'Add New Site Wide Notice',
; ~% Y- a3 P5 i) O0 ]" [8 r 'edit_item' => 'Edit Site Wide Notice',9 g, E# v- m4 z+ Q* I5 N0 j
'new_item' => 'New Site Wide Notice',
0 a9 ?4 P$ l) e9 P) S/ t 'view_item' => 'View Site Wide Notice',
2 d/ Z {& O; e 'search_items' => 'Search Site Wide Notices',9 Z! w0 Q8 ~ I/ q8 U
'not_found' => 'No site-wide notices found',
5 r8 H8 `4 o9 ~/ M9 ^$ E' x. ] 'not_found_in_trash' => 'No site-wide notices found in trash'
1 o5 m) h2 _6 q" R0 X0 @& M; y );
9 I% v7 U: ~' m" c6 c3 t* r& O' x& N9 d. a; x0 a# F/ o; d
$args = array(. u" Y: _! e% Q3 r
'labels' => $labels,
% {1 s7 @$ C! ^1 U- k; Y, W8 M 'public' => true,
! i8 {+ O+ k2 j8 s# Z! r: P 'has_archive' => true,
+ D) |- z' h) X; u- d. A 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
- S/ ^5 ]4 ~% p& B( _; }% O 'taxonomies' => array('category', 'post_tag'),
2 }2 Q+ T" W, A) X; l% A! R. m 'menu_icon' => 'dashicons-megaphone',
2 |- c0 G' i& R2 S 'menu_position' => 5,8 w9 a+ H1 v4 S! I
'rewrite' => array('slug' => 'site-wide-notices')
0 C" i: P; d# O- G );
4 u0 K6 K1 r( r7 _% @& w
' _9 o& F E4 a register_post_type('site-wide-notices', $args);
a" ]+ k- u6 r/ k }/ W5 u3 [- l$ k5 s
```
, L/ h5 T9 }- a' ]* k/ ]6 H
" a& \/ k# Q# v% C% L1 R$ G3 O 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。" `; X: s% R1 c4 N! h" v
3 W. G5 V+ G/ l- r8 {# }" I
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:0 V2 F1 Z$ M t- J6 T" a6 D5 f
$ j" ]: \! D6 Q9 o- c6 y
```
! _* y5 O: t# f# p add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
" H$ M9 s1 A4 L S3 b function add_site_wide_notices_boxes() {. ]' C9 T) |# G) K
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');8 ?: a: X9 Y/ l" }. W
}
; a& N. H2 I$ @! Z6 |/ g0 c4 M/ [
function notice_details_meta_box($post) {
3 h1 G! B* E, i3 a; o wp_nonce_field(basename(__FILE__), 'notices_nonce');, T5 y1 @+ w- ~5 x6 G1 P+ v3 g
$notice_title = get_post_meta($post->ID, 'notice_title', true);$ ?) V9 I5 z1 q: S$ x- Y
$notice_content = get_post_meta($post->ID, 'notice_content', true);" C; n( S; F, W/ e
?>
8 d0 f- O# d2 R1 V$ G8 D& ^ <p>
" z1 v% x/ x/ ?! j! u ^1 s <label for="notice-title">Notice Title</label><br>3 }, d! M- O3 l2 m. y( c
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
2 B8 d# h3 K, \6 s) F: b$ } </p>7 e8 c% d& s/ h" S9 ?) t5 @
<p>
- z2 O% v2 ]- f* G- ]: z# w% j- h <label for="notice-content">Notice Content</label><br>
% n# q6 L% Q b: C$ l3 j <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?># L# B' G% p2 M9 i) c$ R
</p>( g6 {% x0 L# ^$ C& u7 f/ Q
<?php
: @$ C3 @6 s% \' j1 N ] }
: B, j4 P3 i5 Q- d& z& w) q, `# `1 N, C' @9 W. N1 p
add_action('save_post', 'save_site_wide_notice_meta_box');5 T- j) L t" a. Q7 z1 ]
function save_site_wide_notice_meta_box($post_id) {$ c1 _& X7 A4 f( @, G% z" H0 F: a1 L
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))+ O3 [8 r4 O, M; D
return;1 M* A( L& H% R0 M
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
6 `# |; ~ [" p3 \+ l return;
7 c( p$ R, }+ F8 D. X' `0 F Q2 I! ]* Z/ U
if (isset($_POST['notice_title'])) {4 M1 a+ b6 S% v
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
" G: }( |- V2 t, s, W# U: h }: p5 \! a; }$ F, ]1 m
if (isset($_POST['notice_content'])) {
: _0 p: Q) k0 ]' N/ { h update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
8 N9 o9 |9 ]7 y* p! S }
& y& @: Q8 }+ \ M: z: T2 n% O, G }/ w; \3 h) O5 [+ [, C
```- J' [. }4 t* @7 _% Z
7 T7 c# C g% D9 v- M
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。9 p- T4 R% n* w4 p% `7 h3 g) b
, ^) t( A& p, S9 d" j C e3 ]) p
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
5 n0 X" M1 l0 r" e- V4 [" ]) `7 \3 G0 I
```
' @1 O7 W& g) o! S! a3 j $args = array(
y, u0 ]$ |+ [8 ~ 'post_type' => 'site-wide-notices',% g* z2 C$ m+ V/ v: g4 x" y. I! L
'posts_per_page' => 3,+ Y& j! G5 U |9 D0 d
'order' => 'DESC',
+ y& I. W* I& c- C+ U, \ 'orderby' => 'date'( s) A" z% F' S/ Y, Q0 @
);
. w5 B+ I4 b. @- u+ h/ q $query = new WP_Query($args);
4 g+ M) i i1 e! C& o) f, b! S if ($query->have_posts()) :8 }3 F! z: R i! T/ L4 B% B$ ]
while ($query->have_posts()) : $query->the_post(); ?>' ^ ~2 F' `# q( l! Z& j) B6 c
<div class="notice">; S0 F# o3 |& q, R4 R! r
<h3><?php the_title(); ?></h3>
9 [6 u' }& j% G4 O <div class="notice-content"><?php the_content(); ?></div>
+ k4 [! m+ b1 l7 H1 A! w% r7 Y1 b </div>
3 U# F% _# X$ H$ x s( O <?php endwhile;; _) y( d4 Q0 j( e
wp_reset_postdata();. u, T5 n8 i4 [8 E5 x% B2 q/ w
endif;# q/ l6 g, y- G+ F! V
```. e1 X! m) u4 c' a, p$ w+ Y( b1 V
3 ~, D3 x5 W( u- r `
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|