|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?7 M8 E% i$ y7 |. B1 v& P' {
% ?- l1 d% q% _
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
) a' r$ G' ]- _
* f" M0 N9 p% Q- p5 f; s以下是创建自定义插件的步骤:; C! K( Q% D- g$ \* d
. p2 ~7 |# Q; l R& [
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
1 `! a7 }* [" k+ s' o: L
" h$ F9 |8 h6 K- v* {3 b. l8 o/ f ```
( J" W$ i9 L( ^. e' ?, G3 D7 H* J <?php" t, p7 j7 b3 q
/*# T9 y8 Y" t! C Q) z$ j. F7 m
Plugin Name: Site Wide Notices Plugin# c/ k! t( G. G/ J% i& O' }3 w( L
Description: Adds a new custom post type for site-wide notices.
0 @9 F; {) |2 K Version: 1.09 E1 W8 u) p. |- p
Author: Your Name
' b Z5 M* J* ~2 m' H, T7 H( z( p Author URI: http://example.com
/ l" j6 t. j v */
5 {8 M ~' S& f7 Y. d/ C2 x) S
9 x) J2 D o/ Z$ Z/ [ // Add plugin code here...
5 g8 a! Y- M$ `( B% R ```
! q3 z4 P. R8 J: M F, H G8 `' e4 i! F/ l
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。) R8 F+ ?0 i5 H2 Q6 X% V& n6 t
7 j3 J( [7 K+ R) ?) m6 I& a" Y2 O. i2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:- L3 n( T. e* k7 `0 \1 \
# o' ]/ `6 X$ N) _ ```
7 `% @. s* H# w- x0 S% H add_action('init', 'create_custom_post_type');
: k) c% E' t5 l E6 _8 S/ H function create_custom_post_type() {; i. ^/ W6 y2 k9 }, \1 c
$labels = array(. o9 w( p+ M0 M. t7 {' V4 r/ I
'name' => 'Site Wide Notices',
* \8 p. P/ V- O `$ Q( f! c 'singular_name' => 'Site Wide Notice',8 l8 z* p8 M5 R# a" z
'add_new' => 'Add New',
5 N5 D3 K/ w. Q8 O8 K 'add_new_item' => 'Add New Site Wide Notice',5 Q! S( K* F8 Y l. x
'edit_item' => 'Edit Site Wide Notice',
& d3 p. E! p' f( g8 f% B% w 'new_item' => 'New Site Wide Notice',
, d# J1 |0 O n" h2 R 'view_item' => 'View Site Wide Notice',1 f1 x( [, V' u
'search_items' => 'Search Site Wide Notices',
) j @6 D" R' z8 I 'not_found' => 'No site-wide notices found',
8 M! X/ G9 c; B/ ^$ W0 y; q 'not_found_in_trash' => 'No site-wide notices found in trash'* x; g9 \2 F3 R& U$ j5 o- e) L
);, J+ Q g% n! O3 A4 C) U% F
& q* F/ Y3 x: P3 y; H7 f5 B: h
$args = array(! G. s; ?1 L ]9 e: x: e/ _" P% h
'labels' => $labels,+ f4 X5 k( h0 u; I
'public' => true,
6 i4 i$ c" M1 O3 X4 G c; a+ M 'has_archive' => true,; z8 t; h, I/ z; m- f& Y/ ?; N
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),8 E9 H5 c9 v0 u5 D" A+ m0 a9 M- L4 q
'taxonomies' => array('category', 'post_tag'),
8 {+ x' Y9 \4 J2 I 'menu_icon' => 'dashicons-megaphone',4 _* ~1 k7 ?% F- X: I+ W
'menu_position' => 5,. ^, R4 N: W, [5 @& I( M6 o. k
'rewrite' => array('slug' => 'site-wide-notices')
# s1 e" l: |- p# R% n8 ] );
& }( B& q6 r: h# f0 C: \
9 V8 l" j7 y' K& R) a% n8 w register_post_type('site-wide-notices', $args);( f% N( X& p V& _# j" a
}
: ?5 X! x9 h8 N ?$ Z* J: L7 W ```
0 M2 O$ [7 y X
: X; V, Y' V$ k( V 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。# t. N7 `+ J3 {( Y" I0 H
% j/ j5 D4 L0 ]: G0 G) p
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:- r/ ^" w& q3 |; i- G
% C/ y" t+ G$ t; N
```/ P3 z9 r' y# m" G+ v$ E4 R7 [
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');0 P& d |0 ]) W) @0 X2 F
function add_site_wide_notices_boxes() {
3 K( g1 K0 S1 b/ y! q+ Z, r2 i add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
5 x* F9 m. g6 Y! ]1 X/ U }
' C9 t4 h2 {4 l4 W' T
% q: z6 A* m8 G" p1 C function notice_details_meta_box($post) {
; u7 D5 V# N5 N) k K% p$ v wp_nonce_field(basename(__FILE__), 'notices_nonce');( z( b" @) g7 Q3 G3 E
$notice_title = get_post_meta($post->ID, 'notice_title', true);
8 n( X8 K- O2 ~. o5 z) Z $notice_content = get_post_meta($post->ID, 'notice_content', true);
5 K6 b! j V- X# x; J" H5 g ?>
, \5 n) k8 Z) m" u! h* s <p>5 _3 |- s0 L! |. p8 w; q9 \7 O6 O
<label for="notice-title">Notice Title</label><br>) H% W, _ R8 z0 g( U& n# J* ?
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">$ n& J" Z, D& S
</p>& X$ h8 K1 _- }) r5 S- ^
<p>
6 q$ k3 h- G/ D6 o+ Y, C9 h <label for="notice-content">Notice Content</label><br>4 C7 z4 _5 n5 A/ D7 r1 e( u
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?># A. N/ v# l' F/ S) ]4 D8 E% ?4 H
</p>
2 @ |* S) x U: x' N v <?php
Q6 p. u% [! k- O6 M/ Q0 s# w+ A }
- d$ k$ Z$ {& G7 E6 o8 ?7 S
4 z7 r. V3 K6 r x4 q) C; o% t+ ~ add_action('save_post', 'save_site_wide_notice_meta_box');
, f9 u' Q+ Y, [! J function save_site_wide_notice_meta_box($post_id) {; n% X' ]1 b9 V6 @9 \
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
P4 q1 |3 X, |1 X& h: R6 f( h0 w return;
( d- c+ i# M/ h if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)% Y' C; `) ]# Y0 ]5 o# |1 F
return;( j; { o7 t. z# ?- }: f
1 @# m, w$ I( f! a1 _ if (isset($_POST['notice_title'])) {
0 a+ P3 s: Z( {; z6 { update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));3 k4 ?9 n1 `( e( z& i# t. k/ b
}
3 |8 b6 g7 m- H; f# _ if (isset($_POST['notice_content'])) {+ |4 n. u/ U" R/ `/ y
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
, ^5 \3 b& Q- G0 k9 |( X }. R. I* w& [6 {) k' S
}
1 I% S ^8 ^2 b! A6 u5 Q ```
# c" @3 K! H) z5 n, z: ^( f' @ X( K4 f+ I: S+ _& {- b
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
: p, L$ d; {) ?# w' a/ P/ Q/ j# v
# V# y8 ^/ s" J8 h& K% n4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
1 i3 B# ?. F- H, [8 V& t9 S2 b9 c/ `( a5 Q* K
```
3 R) ~4 W3 |( W $args = array(
) Y$ R7 k7 f% i 'post_type' => 'site-wide-notices',
# r) b7 n( y6 @( H* t9 N/ T 'posts_per_page' => 3,
9 L+ e5 L6 J1 L0 }) R; M 'order' => 'DESC',
' Y" e; I3 B7 x" k6 x. _7 x& a5 D 'orderby' => 'date'
* `* T7 {9 I% B: O, h6 C );8 ?9 h5 x8 r+ ?$ p
$query = new WP_Query($args);
F7 @( n: s! J if ($query->have_posts()) :
# M/ G D# g. x" V# p2 Q while ($query->have_posts()) : $query->the_post(); ?>
; B& D# m# \" s+ r% ^ <div class="notice">
# S! y4 ?2 `* o5 h <h3><?php the_title(); ?></h3># G" l# m1 a' K6 N5 V) a+ }: b* G
<div class="notice-content"><?php the_content(); ?></div># G4 k4 Z, x& R0 Z- e0 J8 e
</div>1 m2 I8 }' M" k" i& m/ B( [# ^% e
<?php endwhile;
% g2 N# } J3 q) m7 u wp_reset_postdata();9 Y, {5 B& n5 `% Q9 z: t
endif;
" v7 c7 i8 W& @' r2 k ```
5 [/ x+ `% W/ e7 r' N' E5 N1 g6 R
1 J4 |" U, p6 T& |% b! |: C4 w 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|