|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?1 W( b7 Y' p8 d% D
2 U2 i, V/ F3 \2 R
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。) }1 L* z! u. _/ V$ E
( ?2 f- C* F, {& w- c* ^* u以下是创建自定义插件的步骤:( O; J$ G5 B4 z! J' N7 r9 F0 N
) T; Y& {- E7 _, U5 ~* D, N1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:% t8 K9 a# b1 z, g$ \
7 ?2 n: ]* c2 I
```
; Y3 y% r5 _+ i1 F3 h" t( U; D <?php& q2 N+ E/ L q/ s* z+ W
/*2 D( I: T3 g, @& U$ q
Plugin Name: Site Wide Notices Plugin
R4 s) ~1 T0 ~* h- k2 H Description: Adds a new custom post type for site-wide notices.
: i3 g, T3 B4 P6 x: Z' o* M Version: 1.0. d9 [- o; p- G% v4 g8 s" z
Author: Your Name
. z: [! e8 O: P9 C, q Author URI: http://example.com
$ E; o) e) S& a- N. m) O, R */% x: M. _, d+ C( v, Q& r
$ k% }# m- |8 ~+ B // Add plugin code here...
% Y' O/ C g! E/ b: c6 A: {* b [ ```9 r7 I3 [) Q. e* |! p3 A
7 \, ]- @" v/ Z* }0 I, ]
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
+ E" U! H- a! g1 u' b6 Q( R) A3 ?" h Y- V/ f3 n9 n. h& T
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:& T" ` I4 v9 ]; X3 m. f
' {. l$ X: p6 V8 x0 b/ J- P0 Z
```) v5 Z; \2 F6 n, P
add_action('init', 'create_custom_post_type');4 m- C4 B6 e2 l7 Y; P
function create_custom_post_type() {
' U3 v& b. _0 W: y# v& H $labels = array(
; C) V3 y5 @1 Y! {8 I" b' z( ^ 'name' => 'Site Wide Notices',) U. e3 b6 b8 T9 V- K6 r' m9 C4 }
'singular_name' => 'Site Wide Notice', b2 j4 f. e4 j! U P
'add_new' => 'Add New',. J' h* _- \$ C G1 w
'add_new_item' => 'Add New Site Wide Notice',
- Y; K6 T+ o# j. Q0 P9 w 'edit_item' => 'Edit Site Wide Notice',8 Z. Z, }1 C+ a
'new_item' => 'New Site Wide Notice', [) B8 W( G& d7 H3 Q; {
'view_item' => 'View Site Wide Notice',! @2 m5 p9 O3 H1 N. }, D; R& x- ], [
'search_items' => 'Search Site Wide Notices',
0 w. F Q- R$ u 'not_found' => 'No site-wide notices found',
) v5 j$ O9 X2 h 'not_found_in_trash' => 'No site-wide notices found in trash'
! J% {6 Y# W0 U8 W );
& e# W8 ?, U7 k4 W5 o$ e; s5 s# s2 g+ m% y" f) X. X4 E; ]
$args = array(3 @ w1 K0 P. F4 D7 s/ `
'labels' => $labels,2 w- r- n9 `) f
'public' => true,7 N1 w! d, ~' l( x9 Y2 H' S* i
'has_archive' => true,
- Y! p5 D& A- x3 p0 ]0 n; s2 Z 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),- F5 q( x- s; d& b; t
'taxonomies' => array('category', 'post_tag'),
+ ^0 f6 f* x) a2 ` 'menu_icon' => 'dashicons-megaphone',
+ Q# z1 K6 @4 B& h5 q3 N/ N 'menu_position' => 5,
+ x8 |- g m5 G: n, z 'rewrite' => array('slug' => 'site-wide-notices')8 m; s; _7 P1 v5 }
);% @: ?& B# o, H/ X$ A
; x0 v. v0 \) C0 }# M0 I' l register_post_type('site-wide-notices', $args);" g( B) s5 U1 s: q" Z {" u9 M
}
: z7 A9 r2 @, [1 Z2 S ```
: ^7 n, ?) H2 x/ \3 L3 @. a' f% ~2 j' i1 Z: ^3 t f5 `! D
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
" c+ e; z1 u) c9 Z: _5 B+ r& w: p) t, i/ c0 o# C) q4 _: }
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:& b+ I1 Q- z4 O6 m1 h: q
) B N+ e' ^& e% F6 u1 E ```
5 T+ `$ C: L" ~ x, j1 M7 d' X4 L add_action('add_meta_boxes', 'add_site_wide_notices_boxes'); L$ ?- o# Q! } ^; U
function add_site_wide_notices_boxes() {) f& V1 ]+ }+ X; \
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
6 d( ^8 X) `/ Z& E" E1 d( B: H }
; M4 a0 Q$ d3 B& P% D* Q) v ?
1 m1 F& z! g! i) b9 g function notice_details_meta_box($post) {# G# W* Y* r0 C7 W3 b
wp_nonce_field(basename(__FILE__), 'notices_nonce');
, R; v9 p2 C! K; [ $notice_title = get_post_meta($post->ID, 'notice_title', true);
7 e% J5 S5 C! R4 k. ? $notice_content = get_post_meta($post->ID, 'notice_content', true);
1 S: V# r7 d" Q9 | ?>7 C& L# a& E; F& C6 j
<p>% f" h9 u- Q) [2 `
<label for="notice-title">Notice Title</label><br>% o. S, C: }$ V( w' n' z
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">; I/ v& X+ v$ L5 j) k5 w
</p>* L1 L( j! y( T) a
<p>" C# v" ~2 S# X) \0 w
<label for="notice-content">Notice Content</label><br>
& J& D! R) H% ] <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
1 o: f. V0 ]2 ?! k: R4 S y </p>
# S; b3 @* {8 d" P) G <?php. d, g' J6 g# \; }1 | l! q7 X! F
}
( |/ J8 a8 {9 ?" U
/ g4 L% y/ Z! z& H* ^ j add_action('save_post', 'save_site_wide_notice_meta_box');, y- o% B0 g8 Q# n) p: A) b
function save_site_wide_notice_meta_box($post_id) {4 f8 [3 f, A5 {! u
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))/ \: D2 {- G7 z6 t, x# N8 i. Y5 f
return;, ^$ C. g! ]7 Y# P
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
1 K$ [) i, B1 E' |8 G return;
3 U( C2 l4 k0 u1 a8 Z- e+ O
1 Z, O. C$ Y- {8 ` if (isset($_POST['notice_title'])) {& e& N1 y3 a9 n3 m y0 I3 @
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));0 K) a7 Z5 }- s( Z* @
}& X E, [; ?" {! c# O. q1 x
if (isset($_POST['notice_content'])) {7 Q' l& y! j' E
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));9 Y7 a; g. o, [5 a7 C
}
/ I( x$ o5 |; t9 p: n }
2 x Y" K6 i( ^3 Z; G ```- G, }* E$ l0 p* p" P/ H6 p
7 j/ K' Q* ^' T% ^9 z4 z' [. R3 }
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
# b4 U" ~% c- e0 N6 s3 i x" p5 _6 R7 P- a4 O) P. P6 n
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:; q( n' n& k7 e+ O1 M' c
3 @$ |; E+ O0 Q9 I- ^* q' e ```9 S: v) x0 e+ z2 Z/ P
$args = array($ G6 ?4 b( ~) T
'post_type' => 'site-wide-notices',
( S5 C; C4 j& o) A" ?+ H1 j 'posts_per_page' => 3,- l# }* }" t- y8 ]6 i. M
'order' => 'DESC',
$ m) P7 v- T. Q& s6 e8 d 'orderby' => 'date'( Y: G. _, t- M6 o, E/ g
);
+ T, X i: d/ `' |4 A $query = new WP_Query($args);
% X$ l- ~1 H. Q P$ ^ if ($query->have_posts()) :/ p3 t+ T; e9 {; X
while ($query->have_posts()) : $query->the_post(); ?>
( w+ v; Y, P1 F: W/ F" F0 J, o <div class="notice">& S) D1 `! u! N+ {2 T; p3 M
<h3><?php the_title(); ?></h3>
- F' L8 C9 \3 M7 F4 l+ A6 `: b <div class="notice-content"><?php the_content(); ?></div>. u n( [% S9 C; j- w: w/ K8 p
</div>
% R- _% {9 Q+ u$ \, h <?php endwhile;
) D3 z% G! x; [0 }6 b9 a3 ^ wp_reset_postdata();: U+ G, G9 [9 m' a) O4 U
endif;8 j- E9 {( I8 t1 S
```
c- ~8 {, r& y/ _ f9 B1 ^2 t" ], B h: L2 s. T5 D2 G
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|