|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
: }/ T8 k& C5 g* v! ^, b) M7 t) L/ m! b2 o" r3 h
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。+ _( v, A5 z7 s( B- P& o/ C/ j
- C* a9 j( N) h! t4 [以下是创建自定义插件的步骤:
. _3 I5 U) `9 m
' {( ^5 H* B1 I1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:+ J5 b# T3 Q1 |, n
1 _' I) v, U [& w8 r- u2 r8 b
```
# t* p8 ^" K& F( P" _5 Y2 \3 Q& T <?php
3 ?& b. q$ g5 O. x$ s p6 K3 I1 y /*8 _1 f. I% C) Y8 H: K# p7 B0 ]
Plugin Name: Site Wide Notices Plugin
* _* s5 S* K/ O Description: Adds a new custom post type for site-wide notices.: V, |. M1 C/ @, p1 B
Version: 1.0! Y# [/ W0 u# G7 W2 i' F; E6 K
Author: Your Name# I9 i* x& n! c7 Q) N
Author URI: http://example.com2 @% T. ^# \( [* S F5 r/ m
*/4 [; N4 y" X: D. h
+ n5 i* z4 g7 f // Add plugin code here...
8 p4 Q) R, f+ H- A ```& ` P! b, I' z
% I7 r# D6 H3 m) q2 X4 l
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
5 X5 {" K# I* ]5 q# V$ {, X( N9 N4 o# ?- ^/ m
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
O% g) _% i1 K2 L7 H; l) }4 B+ G/ ~ R
``` W+ j# e8 n" D2 P
add_action('init', 'create_custom_post_type');; k+ {/ A: b0 `; R
function create_custom_post_type() {
' P5 ` J# O) R $labels = array(
. H9 ^7 E5 ^: _- N5 v 'name' => 'Site Wide Notices',1 ?; i, j, [+ W# g* K) h
'singular_name' => 'Site Wide Notice',* P! u) ^4 n; Z) p/ Y
'add_new' => 'Add New',
" p( z! x, S S3 k8 ~ 'add_new_item' => 'Add New Site Wide Notice',
' i' f7 n' M1 i; l* M 'edit_item' => 'Edit Site Wide Notice',
# L8 d, C: s* T- d* F p+ ^$ e( O 'new_item' => 'New Site Wide Notice',
7 Z5 @/ b6 o% M+ m" K1 I% f2 v* a' [ 'view_item' => 'View Site Wide Notice',
7 v& w o" I. i5 g" h8 o- z, H* ? 'search_items' => 'Search Site Wide Notices',: n* H1 Q/ H. f; j
'not_found' => 'No site-wide notices found',9 S+ i* f! ~8 o1 N
'not_found_in_trash' => 'No site-wide notices found in trash'9 [/ s9 v- c5 Y) C' e: p2 {
);
; W( y" V. S" W3 K, @8 s0 ^, [, J8 m) T8 B2 u4 |6 G. t$ ?
$args = array(9 V9 `1 U& _7 e/ l( _9 G( ]6 R; H0 t: W
'labels' => $labels,3 e) c" m6 y- A% @7 t2 B% h9 b
'public' => true,
" x1 h6 J& k: n 'has_archive' => true,1 |1 f# }) m# Y% i1 U, j
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
5 u% O9 |+ I V; e 'taxonomies' => array('category', 'post_tag'),6 Q9 y/ D5 n' Y3 h: J1 Y Q. {3 R
'menu_icon' => 'dashicons-megaphone',5 z" \" v* j! M) h! E: c m- [
'menu_position' => 5,
! l" f; G! f! V) x( n 'rewrite' => array('slug' => 'site-wide-notices')
$ r1 [ h7 c$ l9 E );1 A! d \0 H6 }
7 D' x) ?+ x8 M: w, M8 H
register_post_type('site-wide-notices', $args);
$ x& O6 Z9 W6 Y7 ? }
2 S7 g% a' _# ?, \. x1 R ```5 ^ ^1 w* |( f7 ]& K
$ @% I! [1 u! D. M+ U& S
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。8 |1 h9 A/ Q. ^3 H: A# J- p) ^9 j
8 b0 Z6 Q0 A$ e4 t, L2 W2 r
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:& E! S) v. Q+ [
0 @. l& E9 t7 r- Z2 L& S8 _
```' W: F# ]; s3 U8 @: H+ t
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
* w) R! {$ c2 u/ j9 ^! I9 _ function add_site_wide_notices_boxes() {8 v' z& f- w% J2 `& R" Y
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');1 i1 _" L/ C3 w' r! ~
}
. K) n8 [7 q- o4 S* n" x* g
) ^" F0 y1 \9 t( A% Z) P" | function notice_details_meta_box($post) {
( i4 Z9 [ _# S" q& z! B- p wp_nonce_field(basename(__FILE__), 'notices_nonce');
* g% l' U1 @; ?, J* r $notice_title = get_post_meta($post->ID, 'notice_title', true);
8 s! {' l3 j1 G: A* @% D $notice_content = get_post_meta($post->ID, 'notice_content', true);; n, @& Z7 X' E/ y
?>+ v) z$ D! B0 l0 V& @7 B' J) q5 e1 v! d
<p>
, l- U8 x r' o; P <label for="notice-title">Notice Title</label><br>
: q0 ]. U. U" e! b7 I, n <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
9 x2 P6 G& T0 o, J </p>
: m8 N1 |" v) _. G <p>
: i. r; G8 i* n5 E <label for="notice-content">Notice Content</label><br>2 S5 e& ~5 R2 k6 T
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>/ B0 e* e$ s' p1 k5 J ? F
</p>2 j7 @) {8 `& ?; w
<?php9 M" n; h* R( @' L9 D- {
}( L% \3 R4 @& q3 k! Q2 F
1 t- ^2 t$ _0 p: w add_action('save_post', 'save_site_wide_notice_meta_box');1 u9 h% K2 ~- I/ f: Y: O
function save_site_wide_notice_meta_box($post_id) {
0 D, @& A# R% c V1 z0 L if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))- X" q q# q0 R3 W
return;
9 G3 @& \6 d% G5 a if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE). [7 q/ B5 R/ Z; z R
return;
2 L5 F' h! d% Y3 k+ @) h" z3 w7 L6 q
if (isset($_POST['notice_title'])) {
8 y! @2 v2 X; e9 D/ K update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
1 Q n1 k7 m7 [( Z) y# K0 v+ ^ }
9 f% Q2 f, ~# g( ^4 M( N' u if (isset($_POST['notice_content'])) {& k5 w9 i' c0 C! w, _" k3 S4 c
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));: E7 m0 m$ T- g( C2 I' M
}
" L* {4 `- U1 n/ L- z }( k* j8 k0 O8 Z- d; s$ i8 ]$ D
```/ `! {5 J' _5 E- U% V6 Y5 [$ H
2 i3 j& [0 a: N5 [& F
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。) u* g0 J/ j: j; ^5 v2 X+ q6 F! D
: z' @, N4 g: K7 b0 y8 S
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
1 [/ {1 f, `) q# p2 j9 f( ]4 k
1 O! G+ S& G! c" |+ h9 n& c+ h: @ ```
0 f, p% Y$ v2 @% u4 T% Z, z $args = array(
3 J h' k# J/ c$ ~7 r" B 'post_type' => 'site-wide-notices'," x% C5 i" b: G" b; K
'posts_per_page' => 3,( ^. R, ]( ]/ t3 Q( [7 D
'order' => 'DESC',
5 f. S' ^% x# b/ }$ w6 H6 q 'orderby' => 'date'( C2 N! t {/ \
);
: `/ k& g; `8 f! q4 U $query = new WP_Query($args);1 c0 p( {5 ]! r& R
if ($query->have_posts()) :
" k$ n; g F; k* u4 u$ U while ($query->have_posts()) : $query->the_post(); ?>
+ l5 E A! t9 [* R <div class="notice">
$ i8 m& U& a- l5 i <h3><?php the_title(); ?></h3>+ p ?+ x% Q ^. k+ T. W( c
<div class="notice-content"><?php the_content(); ?></div>
6 Q9 P1 {2 ]" L! O7 @) ~ </div># i5 L' w' r" V, p: U0 P# t
<?php endwhile;5 v/ E# ^6 q: L, ?; a
wp_reset_postdata();
- R* m7 q! b5 z+ I* ^ endif;6 B8 ]; G% D9 ^# P; ^4 K$ h
```) _* [! u0 x. B5 j
' j/ F9 D+ R5 r6 z# w* U 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|