|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
% ~. p3 e$ F, @% f3 d+ D. {) m( Z! f
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
$ h. \, V8 V* y) L' i+ E( m2 p
7 _7 s/ U# q. \: Q |以下是创建自定义插件的步骤:
. A! f* y- a8 O+ h1 m0 p" X
1 |3 C( O$ @" n1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
3 z; q0 @& h3 ?- Q2 W
7 h1 H: Z$ Z: H# u0 T% \" ] ```
4 s4 ^0 @% Z& I; @; Q6 E5 J3 O <?php& J% b, A7 Z% J( I8 x. J% u
/*
( i2 `5 C6 d3 o- X1 t, E Plugin Name: Site Wide Notices Plugin
* e# n- \- m& j/ M Description: Adds a new custom post type for site-wide notices.# ?& C8 a3 q7 ~! t
Version: 1.08 e# ]* P4 Y1 L- M8 q+ V
Author: Your Name- k+ w3 ?1 p( v0 p" i
Author URI: http://example.com
3 g7 R5 V& {$ s( U9 `- x */
& P0 ?7 W/ J! L- ~
( @& D8 d+ V0 I+ q' | // Add plugin code here...
& H( U0 e6 F/ k9 N7 Y ```
7 Q; q q7 [& O& q/ H! }. E& ?5 V, O( n. L) X6 v0 ^
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。- F$ H, G1 [2 u2 I: S! ]: R
9 h& D) G+ B' T+ a+ ]! ?7 P2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:! A2 r `" d) t9 B8 Q" w! [
% y1 I9 i" Q8 }' p; G6 o8 y
```
' M/ i5 r% W! J; t6 b add_action('init', 'create_custom_post_type');
/ r. o6 U& o6 T! g, Z- e, r- b3 a; K function create_custom_post_type() {3 {) N) T5 \9 j
$labels = array(
% L9 c9 Q( q: k1 [( R' f 'name' => 'Site Wide Notices',
% @" b$ O2 m& }; X- Y' g+ I 'singular_name' => 'Site Wide Notice',
* ^0 M, S8 r+ C 'add_new' => 'Add New',6 Y, D# `8 K4 }- m
'add_new_item' => 'Add New Site Wide Notice',
5 T% E+ a: m0 @+ b: s, O8 A 'edit_item' => 'Edit Site Wide Notice',
K: G& \* x0 z7 }8 h+ t 'new_item' => 'New Site Wide Notice',; l% W7 s: F3 i' j5 d& Z
'view_item' => 'View Site Wide Notice',9 E+ ?5 G5 V" N. K+ \" e
'search_items' => 'Search Site Wide Notices',
& X. p9 R/ v, @$ I6 h6 I4 s0 R 'not_found' => 'No site-wide notices found',( ?4 O7 m5 I1 J2 L
'not_found_in_trash' => 'No site-wide notices found in trash'
4 O) y* N# [' X) {" }* F );
o5 v1 o4 z$ O! ~( a
; F5 C0 p; J$ h1 z$ T $args = array(
* D; U8 x: r6 l, B9 ^! U 'labels' => $labels,# L& r, }5 b( j! l
'public' => true,4 t- [9 p/ \! u8 f2 T
'has_archive' => true,
1 S3 e k X1 n% }' L y% w 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
4 h" t5 ?& v2 O0 ~3 j 'taxonomies' => array('category', 'post_tag'),
z* G4 M5 N- I( }1 q 'menu_icon' => 'dashicons-megaphone',* [1 n% V9 d( K5 G' {/ P; f6 L L
'menu_position' => 5,
2 P# J+ b- J! p- z 'rewrite' => array('slug' => 'site-wide-notices')
/ b$ U5 a6 ^" h& b );: n, v# J8 \. T$ x
. d5 c# I( D2 L& c" X& ?$ r register_post_type('site-wide-notices', $args);
, g( T& {! P. I- ~ }0 W' b# R5 B) X% ]% x) {2 s+ z
```
8 N, ]) O; B: {, @# ]* i6 b9 l: v5 P# L. e
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。) S% X$ o, |9 P2 z6 T
8 Q/ x4 J) [+ L* P0 F3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
7 _8 {7 b' s$ P9 c, R$ v2 C
& Z. y5 O& U4 f2 G0 d ```/ M |7 I) A3 x$ E) L
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');- b0 r5 O! q" x
function add_site_wide_notices_boxes() {: K# y; W" J! H- Z" |6 R% P9 o
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
^/ ~' ^2 w- I, r0 L% r }* o- G0 T7 M) \
; z3 y% l% Z3 M c
function notice_details_meta_box($post) {* ]2 w( _ N, t* X5 U
wp_nonce_field(basename(__FILE__), 'notices_nonce');7 g; |0 ` b: X& T6 Y
$notice_title = get_post_meta($post->ID, 'notice_title', true);! I+ C6 W3 U7 d0 d- K! n
$notice_content = get_post_meta($post->ID, 'notice_content', true); x Z* ]* H1 N5 ]8 j
?>
- `: O( y0 x8 w# J I4 h1 N <p>/ M2 m, i7 n* S+ @+ v
<label for="notice-title">Notice Title</label><br>! T% O1 u4 c) Z* B" `$ Z& K
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
* i6 g! g1 Q4 p+ M. P* J </p>4 X4 C- j, u5 K. ]8 @; w
<p>: ]# M: X& G, Q
<label for="notice-content">Notice Content</label><br>8 q) t! D* p( v. P3 y" N
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
8 {$ x) r/ u2 k8 S </p>9 y% @8 O2 i' n& a( }
<?php
' j& G. J+ C- {: x0 q, D }
% k# ~9 O" b8 V$ K" X# ^
5 _, y6 `* q3 z; N3 M _/ A- x1 d add_action('save_post', 'save_site_wide_notice_meta_box');
& k/ C' W- W/ Z6 R9 _ v4 r function save_site_wide_notice_meta_box($post_id) {% W8 d; w+ c& _" G. u( ^- X/ o
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))7 q8 i+ A9 O- X7 [; k) L3 v- g
return;/ p( E. V0 B' v3 z3 e: D
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)1 b1 a3 w" G2 w, {/ K" ]; s
return;- P2 @1 s* _. {
4 e: b' L$ V- j) ] if (isset($_POST['notice_title'])) {0 u. ]7 \' @, J/ ^# @
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
% |. Y" q2 a1 o- K- t! Q: L }
( x; E% a7 t2 k4 e9 W, g0 H# I$ s$ @ if (isset($_POST['notice_content'])) {: ` `( s6 a+ \) g* ]: F$ K
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));5 g" _3 m! ?5 _2 z1 g
}
3 v5 [ p5 o1 `, K4 y" B7 x; p }
( y4 G. S, s0 c3 g ```
# d* p8 @& t6 n! o; l% [/ z. _. _2 _4 }7 I/ b. x+ b# t
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
& l B2 z% Q6 e* ]$ U2 R! b+ ]
9 o. [! c- j) [& c. h/ i4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
; N! ^! U C! \; C2 t/ ~% t2 O8 `4 e% X, [% X" U& ?
```
1 O3 |$ e- o( A6 i& ]5 c& \/ V8 q$ N $args = array(" t+ m3 O; ^$ R4 N' a0 {" u& i
'post_type' => 'site-wide-notices',
3 ?1 a& {6 o3 o+ K 'posts_per_page' => 3,' j, {) }& U! {: y4 F: d( F
'order' => 'DESC',3 g* L0 z% C: p6 i6 R8 G! U
'orderby' => 'date'3 C: f1 a7 x; F: d0 j1 [
);& w n, b6 D" n- j5 S
$query = new WP_Query($args);
2 _0 U( l& S8 b: p* z9 q) u! D if ($query->have_posts()) :$ F' ^1 @) S% k, j, n
while ($query->have_posts()) : $query->the_post(); ?>
2 p& O1 S$ A. K9 h <div class="notice">$ M3 z7 N4 S2 X0 g8 r7 ^7 Z
<h3><?php the_title(); ?></h3>
0 I2 q- [. ^, Q [1 D, ^' B <div class="notice-content"><?php the_content(); ?></div>
6 U7 _/ x+ Q! w+ P </div>
7 U. |$ {, J( E$ M: ?2 `5 |3 X6 W: d& B <?php endwhile;
% H8 S9 S( M: e+ Z2 X+ a4 q wp_reset_postdata();
' b0 Y& K$ o. S+ q+ } endif;
_9 c' y8 f/ p+ |% A1 \% \8 ~ ```. t4 [! K7 S. A
# o2 [# {* T& `
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|