|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
9 D; n2 s% X9 B% n& i& A7 r
& j9 ^( _! k' o8 f3 e3 R& n如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
1 m1 C1 E( S) ~* p2 H/ J8 @* L6 W; k, L( z( d v
以下是创建自定义插件的步骤:# T; B- { W! F. A! J( y
0 M/ U7 J8 `# {8 j3 d8 U
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:' H. q3 Z% ]! j1 Q% I
9 O! r2 B* k2 X. _( ~! j6 F ```0 H7 P+ z# b7 V+ J. Z* O
<?php( l$ _) w# ~3 p' n3 a) ^2 u
/*2 o2 Z+ T/ N* `% K
Plugin Name: Site Wide Notices Plugin
$ }1 F3 ?/ H! s7 S$ @+ ] Description: Adds a new custom post type for site-wide notices.
2 }. M: v( R( O; g' q8 D9 i Version: 1.0
F' D8 v! {& W Author: Your Name
5 _! b! M$ x' D5 d+ N; e Author URI: http://example.com J; s' Y- l3 K; y1 n$ ]- D ~, }" M
*/2 t! i/ y0 z9 _
% l; B5 }! ], c [ // Add plugin code here...
3 ~! Y f$ t F0 X" n8 S. N ```
7 J9 `" m) W& g5 x- n3 n1 G& Y
& ~7 x/ Z: e0 t! w( G2 q 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。; f1 Y0 R" a# \' z# A, X
# `7 `4 q- @4 p0 W
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
. d- J& U2 s4 `* I: o) f. G/ E9 J5 p2 T, Q1 `7 V% [$ m7 a
``` K" }: [( O$ Q, m0 A
add_action('init', 'create_custom_post_type'); T) q0 N/ ~! Q8 U1 F: d" C
function create_custom_post_type() {
! p: o6 s$ I$ E# b+ ^& S6 P* J $labels = array(
" A& B8 c6 Y4 i' G+ X1 a* ?& [5 e 'name' => 'Site Wide Notices',2 ]3 J S- ?7 p/ c$ j/ z f
'singular_name' => 'Site Wide Notice',
& o+ ^. Q% \7 u+ F' c }% t. ~+ j 'add_new' => 'Add New',
$ v; N X1 U; a8 U* G 'add_new_item' => 'Add New Site Wide Notice',
1 a. Z* U, B0 Y- x) B; n/ f: E 'edit_item' => 'Edit Site Wide Notice',. D7 h7 Q6 g) J3 A' N( V
'new_item' => 'New Site Wide Notice',& ~; Z$ o3 v/ n
'view_item' => 'View Site Wide Notice',
6 G& m9 K2 l* ?; }: B$ W 'search_items' => 'Search Site Wide Notices',1 M+ z6 L, d& `: ^
'not_found' => 'No site-wide notices found',
a0 ?* r8 k4 A 'not_found_in_trash' => 'No site-wide notices found in trash', O1 U3 }; E8 y" o; V! s
);
x$ O/ Z( }8 m/ G- [5 {% ~7 [& l6 l
$args = array(/ K1 D, {) r1 o- s: l1 E' K4 H. |
'labels' => $labels,8 R1 u! v [$ |( T: D1 D
'public' => true,. j8 m4 r, c/ D- [2 I0 H; I
'has_archive' => true,1 I+ q E6 _' Q$ J7 C% A
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),- k1 N0 |7 }' F. G7 J- @* ~
'taxonomies' => array('category', 'post_tag'),, U* y2 B* B6 H7 s; \" F8 {6 j3 [
'menu_icon' => 'dashicons-megaphone',2 A" W4 O& I( z/ C0 ^4 ?1 |
'menu_position' => 5," C' F0 s( ]" x9 b
'rewrite' => array('slug' => 'site-wide-notices')
6 m4 P) C1 Z# w) c1 ~7 c: Y z );9 i; D+ n; Z2 {: c9 k
# S- [6 X. B O" F- y9 T5 `2 m
register_post_type('site-wide-notices', $args);. h1 T4 x) |3 Z, K; L( q2 ~
}4 Q' u# q* G( P- n& M4 {
```
& V/ `' H \) X, ?# ]" f
- I6 y) |, \4 E+ V 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
/ E; B/ A/ [9 u+ u2 c4 V# f& M5 C4 O% C/ L
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
" Q8 }3 A/ P# J9 c4 e$ _" {- c, f% z( o+ ^7 V, V: v1 ^
```5 R; T& e" C# _# h0 B3 R% @ S8 N
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
{- v% ^: f3 j5 L4 w function add_site_wide_notices_boxes() {
8 h' A+ n7 |2 o( I1 u4 E add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');1 N( L% @$ P) {! x* [0 D6 Y) Z" `
}
' L2 y' S' Z: ?3 C$ v4 ?) ^, _ K% z6 Y
function notice_details_meta_box($post) {3 Z1 M3 J9 N2 B$ s4 p, U
wp_nonce_field(basename(__FILE__), 'notices_nonce');$ Z- `9 |2 D' j; g x9 k. M5 B
$notice_title = get_post_meta($post->ID, 'notice_title', true);4 ]* j4 Y1 _5 h z1 l
$notice_content = get_post_meta($post->ID, 'notice_content', true);
5 X, j/ {) T$ b$ c3 i7 n ?>
8 x( g1 c# K/ [6 }& x <p>2 d* s+ H/ ]5 A! W# }5 v5 t4 Y
<label for="notice-title">Notice Title</label><br>
' ^5 f1 e! b! v4 p6 C; B <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
! x' A' Z1 b% S </p>4 }. ]2 @. T4 |" b9 H
<p>
: w$ F( `; P$ K- R. v# T. t <label for="notice-content">Notice Content</label><br>/ Q, B* y& `" ~. x- r8 s/ ^
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
2 e8 d8 \) ]- i# X4 T/ s( \' y </p>
- M G% S1 {- `& l0 `+ t1 y4 z <?php
$ T; w( E' c) f2 @ }
+ q/ A, C1 I6 ?; t+ H1 w
5 U" _/ D4 N9 [$ q& r! ? add_action('save_post', 'save_site_wide_notice_meta_box');
% S" r/ n7 a& P3 }7 X function save_site_wide_notice_meta_box($post_id) {
2 \4 q& `) J% W) P, o+ g1 F* p if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
6 C" s# U! J9 B' _ return;
% T+ O$ [$ J/ N/ t. k. ^" g: M if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)' v( x& n6 X( J! s) l+ v8 C
return;
s {, W& q! w: ]# [6 y
- r& \: V2 |# n9 \. l4 T# m if (isset($_POST['notice_title'])) {" `8 k& X# D. L! n' S
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));' U' O( J4 `1 P# o* t' ]
}
" F, L) L9 P; a$ Y4 \ if (isset($_POST['notice_content'])) {2 {* W0 t, I8 ~9 f
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));6 W8 w& }) m8 t( t/ O! V" a
}, w- n4 Z1 w- u. ` g% w- l
}
2 ]' G6 {* e: ~6 M% y3 @+ f ```
7 L: _4 y9 q0 n, b! V+ x( k# ]# c5 o) d/ x
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
. x/ D' X2 R+ F0 ?0 g
! Y2 b% V% M# @* n$ z0 n6 {4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:" V& x# g/ c; U: s: u3 M4 y9 _
- C- B( A) c( e+ t( _+ S ```
0 e0 z& ]3 q% ~: v $args = array(
. Y( g( q0 j T1 o" Y" D 'post_type' => 'site-wide-notices',
9 V( A7 H* Z$ T3 r2 P; F; ~8 y 'posts_per_page' => 3,
" R/ ~* M. P# g! { 'order' => 'DESC',2 R. Q: C2 y0 t" D
'orderby' => 'date'* _% p1 P" p2 A% s4 i
);
/ z7 O+ y, @, ^; ] $query = new WP_Query($args);
: a1 k4 x* ?; `/ Y9 N3 E if ($query->have_posts()) :
6 e$ P# T: g& R while ($query->have_posts()) : $query->the_post(); ?>6 C* ~- _; ?& C2 C
<div class="notice">
* j6 ?$ I/ c6 M: \/ W+ d <h3><?php the_title(); ?></h3>
9 u$ N3 K; i% G! K' V! p# l <div class="notice-content"><?php the_content(); ?></div>
& ^8 }4 N) Q4 M/ D) x# [- y8 ^, U; h </div>
/ y- [( A, S y1 w, e1 T <?php endwhile;5 s0 n5 M2 d0 x7 Z5 N3 Z5 D6 h& u6 f
wp_reset_postdata();; U n. V1 o. \% r4 M" i
endif;% u( D- \% E ]
```
6 ?* Q1 r% g. R! S8 S9 b
/ N7 b' u7 |. e7 F& k3 @ 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|