|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?# ^% }! X+ C0 J4 V. R2 _
0 o# v: q2 z) ~如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。: p4 L( h) ~5 i d+ S7 G( {6 ?
$ ^' }3 ]# B. f4 N
以下是创建自定义插件的步骤:1 ?4 J1 _/ p8 d! ~! S3 z
$ q2 |$ y! a, _9 p6 N7 \3 q1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
) S( S4 K3 _5 ^: C/ d, L0 P" ^! u# k- B+ U7 _6 |* \8 W6 O
```
6 U5 ^2 P% u5 [( u# } <?php8 l# X# R: k! a$ z3 b4 S+ @
/*3 j: }: T/ B/ L0 s# _* t
Plugin Name: Site Wide Notices Plugin
; Q" m4 d$ d% Q: v& ` Description: Adds a new custom post type for site-wide notices.1 }, ^' h* X! i) b
Version: 1.0
4 Y# x% u+ v; @. d$ v4 P Author: Your Name
9 w Y$ B( s% P; h Author URI: http://example.com+ S0 I1 z# o1 m/ q0 J$ G6 c
*/9 @7 _0 Z5 x% F' V& t i0 q7 d! O
* {* { Z4 f; _7 J // Add plugin code here...
# T, w* o0 ]& j+ W5 y ```
0 Q8 e9 y4 S! Q( h4 N
: a: D* }0 A* L8 Y, T 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。$ w1 g( z" K8 N/ A
q& X% p/ U* |7 E$ V) a( s* n% s
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
5 V2 L( n2 A0 q4 c) m9 ?/ V5 N1 }9 G5 O
```* J; i$ {' E8 A \" ?" \3 `1 u# J
add_action('init', 'create_custom_post_type');/ D$ q$ z F$ n5 P. P' y
function create_custom_post_type() {
1 j$ @; M+ a+ v' j( e T* k $labels = array(# }% g U; e$ i( ~8 j/ w
'name' => 'Site Wide Notices',, x' D T- H* Q. ~! [# C- o
'singular_name' => 'Site Wide Notice',/ G; h `* X; W- L7 c
'add_new' => 'Add New',
( g; X, s' d& F- v0 B 'add_new_item' => 'Add New Site Wide Notice',4 ]: l7 F a. P( R
'edit_item' => 'Edit Site Wide Notice',9 d8 q `. j' F
'new_item' => 'New Site Wide Notice',
! d7 [! t4 _7 h 'view_item' => 'View Site Wide Notice',; {% v* z L: F9 g3 D: ]& h1 K
'search_items' => 'Search Site Wide Notices',
/ m" G+ M: w! F% X5 H" B 'not_found' => 'No site-wide notices found',
% U8 z2 J: M/ k2 s 'not_found_in_trash' => 'No site-wide notices found in trash'+ K, c: N+ x8 b: Q7 p7 b/ ?" i& c, |
);" u5 y* h! p0 S
6 S5 f4 a+ B9 c' c+ D0 g' S( H& { $args = array(
' P5 G3 k" U" u( Q 'labels' => $labels,
0 O# m G# D8 g 'public' => true,/ u. n5 C0 p0 f
'has_archive' => true,
2 l, D6 ^8 A4 b2 ?7 s 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
9 [6 d+ g. K, J% u. m+ v 'taxonomies' => array('category', 'post_tag'),- ~ w- j2 L( H4 z3 |# b
'menu_icon' => 'dashicons-megaphone',
+ r! F3 `7 Y6 K) s+ Y 'menu_position' => 5,7 x) D/ u+ G3 J, Z
'rewrite' => array('slug' => 'site-wide-notices')
! q a+ S! X0 W! P8 y );
- K% b2 V! {7 g% I" P
, [% M' Q% w/ K4 Q O! I register_post_type('site-wide-notices', $args);( V% F% z# j/ w9 _
}
. \1 R7 F5 n3 U& S% I) h ```/ a9 G4 u9 A& P+ ~3 G! `1 ~
1 z" q4 e7 x! V, S3 q
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。6 f( |5 |) f" |
" `$ V5 H0 U, V0 h* E
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:+ a! F4 H% s1 ?6 _* r
# l1 B# _4 o6 \6 q* m
```, W# A# J3 ^1 [
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');) t6 b* g) j& O" u t
function add_site_wide_notices_boxes() {
/ z9 C4 f. p# \ add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
+ j( b2 ^2 x. m1 m9 C# G3 D }& S) C! y) ]8 F- |. i% u
+ }* b# j# q- ]3 l. W6 g) L function notice_details_meta_box($post) {! Q5 r- l' M' p+ a7 K1 y8 h" q9 w
wp_nonce_field(basename(__FILE__), 'notices_nonce');
; v6 s8 f E: J; W% y+ s- J6 E2 a& j! o $notice_title = get_post_meta($post->ID, 'notice_title', true);) q! L+ W; S* I* [7 R4 a! M! V# w, U
$notice_content = get_post_meta($post->ID, 'notice_content', true);" f2 B6 Q8 H% T( |3 @7 D
?>5 I; z5 Q- K7 Y) t
<p>1 a9 o! r$ h$ {2 E3 N
<label for="notice-title">Notice Title</label><br>, G. z) ]4 x: o1 |) D) C
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">! x" V- `- e" |! i
</p>
0 t3 V q- r1 J% t8 |" \) n <p>5 `# R* c, ?* Z, D/ S- j
<label for="notice-content">Notice Content</label><br>
6 Q$ `' ^* A; o <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
. a/ x$ K7 @6 y/ t0 k* a2 L9 t </p>9 G% ?. Q V5 z8 Q+ t2 q
<?php1 w8 ?: j9 m6 D2 P% @, F% Z
}
& U& y) u9 S& K9 O4 e9 L) j3 \; }: {& `0 C
add_action('save_post', 'save_site_wide_notice_meta_box');7 m; V1 x. w# i" \; I$ W
function save_site_wide_notice_meta_box($post_id) {
$ U, C) ^0 b6 |+ Z+ G, ]" I if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
% m3 M0 O( W" N return;5 I! F8 [1 E- [% a' `( a5 k
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)" R$ w' e y( N5 {: d b
return;' P$ N8 i6 k% c T" {" i/ R
4 z, h$ ?0 V# A4 A8 w if (isset($_POST['notice_title'])) {
, U2 J7 S! B. z& y. `. ]& g( g# S update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
$ {" T& r# ]0 F: O }7 h, u/ m0 Q) v% C$ i; w6 `
if (isset($_POST['notice_content'])) {
9 {5 J; h) |& O update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));2 v5 |0 [$ ~& ^9 D7 T- @ h
}1 V. D: } Q d
}
( j T# x: [8 m0 g* { ```
" E6 x' Z9 D* o) Z* i2 y/ A! ^: v7 J! w! N' B) E3 o
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。5 I6 [, z# p( p( x! B+ \
* x# j/ I# h/ b* R; i: \
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:; _7 `6 O" I( d ]9 }7 v+ ~# n
- [) Q3 a1 G; m3 O- A
```# M2 {: Y! T" j& r/ U, g4 ~
$args = array(1 K. c4 d2 S/ x r( {
'post_type' => 'site-wide-notices'," u! s! ~8 m' B
'posts_per_page' => 3,# D; n* ^# p; A8 N: b- o8 Z
'order' => 'DESC',
" { m6 \- W" P4 R4 E 'orderby' => 'date'
1 f+ s) u. p5 }9 b. l J( R: F );
' ]5 P, Q' k4 o8 K) a $query = new WP_Query($args);0 m+ w+ P$ x! K: b d% Y
if ($query->have_posts()) :. ^& s* N/ U/ j3 G
while ($query->have_posts()) : $query->the_post(); ?>2 A6 O' m' t$ v# T
<div class="notice">
' D# L" L* l$ J) S/ V <h3><?php the_title(); ?></h3>
5 Z$ I. O% T' `1 u' s <div class="notice-content"><?php the_content(); ?></div>: S* O z, U0 F7 Q9 j7 z' ?
</div>% Y, h0 b+ Y$ {1 N) [8 k& \* B
<?php endwhile;
7 [7 _+ Q# a2 R9 ^% x5 f wp_reset_postdata();' Y# w: {6 ?$ ?! m" E/ n( i
endif;
8 b9 C8 @' }9 I ```3 o3 D5 l, W4 c5 r' u; i7 V( \2 [
1 ~" J. A. H3 x3 F2 |* d
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|