|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
% W9 w. |) s% Y* r" T$ M- B) x' J6 ?: M: T! m0 C( B7 Z7 r6 J$ v
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
) M/ e! c( A& J) I) ?, l/ A q7 d$ L5 G- q
以下是创建自定义插件的步骤:
6 m S( K& n* m# E% e+ e0 \( S; L
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:" Z' Q( r/ ~' g6 O: ]& c5 E4 z
. \1 c3 [8 [" b: d$ |" b$ W( }8 |6 {$ ^
```8 n3 y' N7 L% o3 i B8 F- K
<?php
2 H# x8 g0 U! m' a$ ^ /*1 R4 o& V( L0 A6 A% V6 ]
Plugin Name: Site Wide Notices Plugin
2 @( `1 A5 {4 e4 j$ [5 c Description: Adds a new custom post type for site-wide notices.1 _( K* M0 f) M4 E
Version: 1.0& u) `, J$ t& u6 O
Author: Your Name
& P7 A1 m5 o2 F/ v o1 x Author URI: http://example.com" T4 i# ~0 h$ a4 q8 @& p4 M
*/ D: e/ j% A- i. P7 g( M! E# t
2 p) t, E4 W& Y0 N R+ x; z
// Add plugin code here...
, e9 m9 |% l1 y- ~( X ```
9 B/ x% _7 k7 e" ^8 ^
8 f$ y0 H1 M& o# c0 ^2 k 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。) y+ {' L! W" D: v9 ~0 h# F1 j
$ {5 v9 U/ e+ s: d: o7 S2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
5 u+ Y, d+ B! l( s. i6 \& P# C, ^* Q6 \- Y9 j
```: X# t/ l1 a a( A6 m* H
add_action('init', 'create_custom_post_type');
, v7 I- D# c' b! b% J7 [/ R, o5 f7 [ function create_custom_post_type() {
5 N6 T' V2 y8 d3 u. h) W $labels = array(4 X0 l2 r. |; d8 n" r
'name' => 'Site Wide Notices',
6 H$ w \1 w$ o1 E+ ]" W 'singular_name' => 'Site Wide Notice',
; w2 J, O' U1 A6 Q. |+ N/ g; g6 p K 'add_new' => 'Add New',
. {" C! A ~& x) L/ E" Z* y 'add_new_item' => 'Add New Site Wide Notice',9 R1 Y5 T" S1 P7 f8 U- u4 a
'edit_item' => 'Edit Site Wide Notice',& ?+ @8 ]$ k9 [
'new_item' => 'New Site Wide Notice',
+ U$ m: b& _; Y; O 'view_item' => 'View Site Wide Notice'," @" s( W1 h' ]
'search_items' => 'Search Site Wide Notices',' K& b4 x6 V6 A% A0 ^+ j. U
'not_found' => 'No site-wide notices found',1 Q( U( H5 |0 s* L" `- s
'not_found_in_trash' => 'No site-wide notices found in trash'$ K* G% R5 N z
);( @# @/ V- F! o0 R9 S
& {! [) g; P/ d4 Z3 l: f $args = array(
# d4 Y! i! L0 n3 \. T e% _- g 'labels' => $labels,: ~. G- i. a. j0 `
'public' => true,8 b- y+ y" q! ~! ]7 ]
'has_archive' => true,5 Q+ s: d/ c0 N: M0 J' C1 X1 T
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
( }+ h( z2 u' z+ h( U: l6 u 'taxonomies' => array('category', 'post_tag'),& o+ ]# c' Y5 C% E5 z$ O Q
'menu_icon' => 'dashicons-megaphone',
. J& r. r- s2 b; N1 }6 ? 'menu_position' => 5, p: p/ M$ P" U. e
'rewrite' => array('slug' => 'site-wide-notices'); |! r. u3 {6 s" O) z: |' S
);
- j w5 E" |0 y9 C ^# F# Q0 X1 _. @, s+ j
register_post_type('site-wide-notices', $args);/ o9 w$ q6 V7 @; E
}
/ J+ g4 x* M4 E. y. } ```, _) c% {8 P# ?0 A6 q
. Y. J- [$ Q6 D- N9 b( S7 D
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。& j: @3 i7 ^: U2 m4 Y6 c. E0 J7 c
% m9 M+ k) T' s& o- }- x3 \$ F
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
) K7 {/ A+ p) z0 t+ X
! n" p. U$ {7 B/ \" d* d ```: V1 m8 {" S: } J- I
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');& ~5 t. Q) m, J; d9 L
function add_site_wide_notices_boxes() {, @1 L# ^1 X9 F. H$ b+ B; Y G1 c
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
! E/ J3 O/ v3 X, L2 b+ n" B, |( e }
9 p' N4 V" ?1 N1 X. e& {( V' E/ x D3 C1 T. p* r
function notice_details_meta_box($post) {3 D7 |" @( }6 {8 A$ P2 J6 W6 h- u
wp_nonce_field(basename(__FILE__), 'notices_nonce');5 @ S, v, }* ?4 q$ C
$notice_title = get_post_meta($post->ID, 'notice_title', true);
3 Q; @( S: r; \! z# v $notice_content = get_post_meta($post->ID, 'notice_content', true);2 R) [8 ^) G9 P( h. l( {9 u% N7 d
?>7 p0 d- I+ d- T5 L% q6 v
<p>$ X; s7 r0 n0 w: U' f5 c
<label for="notice-title">Notice Title</label><br>
7 s7 R; B6 v0 x$ s4 s <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
% y. Y+ w+ v, q </p>
9 k2 P- S+ ~/ O2 V <p>
$ ]$ H/ i6 o9 |6 m4 H: [" L5 P <label for="notice-content">Notice Content</label><br>7 S# b3 O& ^( n2 y
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>' n% K- K8 T+ u7 g
</p>6 P6 X) Y0 A$ q
<?php: |5 p5 X O! j% f6 H" D% a
}
1 r9 u. s: k4 }. v% h5 l3 J0 p# l4 U7 l8 y) ?: @! G4 I
add_action('save_post', 'save_site_wide_notice_meta_box');
) \9 L$ \2 ^! c. o9 {9 A: M8 u function save_site_wide_notice_meta_box($post_id) {; e/ C& N. u* M& \. e+ _' `
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))); _- b1 j2 j, q) d) X
return;
1 @2 J, ^" u! c. I if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
; k; P5 a# I& s# k' @/ Q return;
, O1 l" O9 B+ }- \% x
' p7 t! j4 [- w) h if (isset($_POST['notice_title'])) {0 j* H" X4 ~% F8 s' D* d
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
$ M# {6 J4 @$ S }1 K; F; a: K0 D% }3 t7 [0 j7 v5 e) u! W
if (isset($_POST['notice_content'])) {" M6 v4 \: \* i: V
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content'])); Q7 P7 I0 B: Y/ v3 A3 B- w- S
}0 J2 [, e& p. x5 f8 r
}" M& [, P+ [: z3 T( W
```" N) m- r. n- Z1 Y R, w
! X* k/ _1 S& f+ S0 A
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。& y- W" x6 i0 L4 I1 b
6 a+ I G& O/ h3 W& P8 M* n1 B' ^
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
0 P, X0 l( M+ L1 A5 }
3 F, H9 S" q& L; z6 P6 }' b' R+ H ```2 I k6 n: ?8 L* S& c& O ]& d- ]
$args = array(
7 `7 M/ q2 L% `7 ~) O9 | 'post_type' => 'site-wide-notices',( V' ] x3 H: Z5 e* z p- D( c
'posts_per_page' => 3,! o# N5 Z6 X/ M4 R$ D
'order' => 'DESC',
8 R$ V7 ^. m' \# S( O 'orderby' => 'date'8 P9 g. \3 D( O( E7 p5 q8 L: |
);
# [: o5 a7 { v3 f; ]; x3 L4 t1 C $query = new WP_Query($args); X$ E* }) A1 f6 U7 v3 P
if ($query->have_posts()) :
4 W1 C( Z; Y0 ]7 G) c while ($query->have_posts()) : $query->the_post(); ?>5 c5 g. c' v# o7 |/ t' O$ ?" t. @
<div class="notice">
* I/ x: l, L3 @) Q <h3><?php the_title(); ?></h3>% j" Q4 V+ c+ g0 m# b( M$ N
<div class="notice-content"><?php the_content(); ?></div>
6 E# e3 ^4 g, R* s' @) o </div>0 T v" J" E( N7 Q6 Y# ]
<?php endwhile;; X, i M8 ]0 y9 c2 r
wp_reset_postdata();- O9 X" E0 P3 @& J& u
endif;' G; X9 e+ q T0 P. K
```2 m# e- @( L, F8 b E$ J) W
7 o- G' Q8 Z6 B1 A8 W$ a1 L 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|