|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?! Q( R# P* ]7 ~; I
/ r( z, S! \! x) ]( p- t4 G% W
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。 | z/ f5 g2 z
$ c+ y- k! |2 W9 b. v7 W
以下是创建自定义插件的步骤:! X& `* E0 N, r) R! W
* U6 N+ k8 X1 `, l1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
4 C4 Y) V% T8 ~, ^* _4 L/ ?& U) T- i5 D1 H: |
```
! K& P$ t6 g- U' G <?php1 S2 Z( `8 L( I" s6 }" g( `: |
/*
5 i. p/ z$ R# w: Z5 `5 | { Plugin Name: Site Wide Notices Plugin
/ K& E/ ?. M! E; ?. \1 V0 V. p Description: Adds a new custom post type for site-wide notices.
* c. @" r2 H9 N4 ] Version: 1.0
1 G* b: ]2 j1 ~& a# p" s Author: Your Name
; b: T! u* }: V4 r1 w, j( `) k Author URI: http://example.com s$ ^ X9 f, b* K1 h& G
*/
: w! n5 f/ ?3 X" M. w4 a+ w2 ?8 ?+ u5 k
// Add plugin code here...5 k) H3 @& |, Z( M: _8 C
```3 k1 A- k& a, Z
0 n& M0 R3 j8 I1 o# `; G
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
' o) a0 R+ n& F9 h0 \& ~, s+ o0 j
: s4 j8 A2 }; f" \5 G2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:) x. b5 m. V! B8 j5 r( |' v
) q- y& T4 m5 l0 f3 Y1 F
```3 a! W- l( V0 G9 `1 f3 C
add_action('init', 'create_custom_post_type');
! _' q# W1 W& J7 s" a' M; G function create_custom_post_type() {: E# G9 M2 v9 ?) J6 E
$labels = array(
# r$ B* i" E+ B 'name' => 'Site Wide Notices',1 N' ^: T1 n' |- x% ]# O8 B# f
'singular_name' => 'Site Wide Notice',- ]/ K0 D4 z# W
'add_new' => 'Add New',
p4 f7 Z! |4 \( ` 'add_new_item' => 'Add New Site Wide Notice',
, e3 j2 l/ U# q1 s* i 'edit_item' => 'Edit Site Wide Notice',
0 M& R% U) [6 T4 e5 @ 'new_item' => 'New Site Wide Notice',
1 G0 s7 C& e& R" } u 'view_item' => 'View Site Wide Notice',0 A% I* L" I* U/ G, c, c' i
'search_items' => 'Search Site Wide Notices',
& q8 ^ E) m7 H- Z1 A. F1 N 'not_found' => 'No site-wide notices found',
# b" E+ k" B t: M% b 'not_found_in_trash' => 'No site-wide notices found in trash'
# u) ]% F# t# z {" P: M* f );$ v# M/ b' g; x% \
: k8 o2 n! O# {7 _7 z; m $args = array(2 k, F: T/ k/ {2 y" N
'labels' => $labels,# b) c# z- } O/ h" |3 W" k, f
'public' => true,3 u P* h+ C: r" a' X. r- {
'has_archive' => true,, s j8 @, H) o+ `! ^6 c1 u- N
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),- t7 t5 v, G5 \3 @( j5 U
'taxonomies' => array('category', 'post_tag'),- W# n( r/ h6 K' e6 }" b2 e: x9 A
'menu_icon' => 'dashicons-megaphone',
9 r$ g5 w9 n2 K$ R. G" U 'menu_position' => 5,
5 _2 J1 {7 a7 v8 k4 O 'rewrite' => array('slug' => 'site-wide-notices')# @" ~) y0 j: V0 L/ }1 |2 K
);
' Q, K+ I3 P8 l; P% O9 V8 a
/ T( B# G ~2 y$ x2 w* h: b) F register_post_type('site-wide-notices', $args);
+ l4 y: G( ]! v# B7 R }/ T J q6 ^6 R& E
```
/ {/ c* F$ I' x) I$ ? b
+ ?! A, b" f0 B 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。+ r7 h: M; N4 |
' G6 A" o- r' {0 I3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
: ]0 H2 H7 Q" W' }$ Q% a* {2 q4 e, e4 I2 ~
```3 Q$ r1 M/ |0 W: P& o+ V/ v
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');6 I7 O* y ~3 D. m0 y: \
function add_site_wide_notices_boxes() {: }; [) e w% K5 c# n
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
& n) j- m2 t" R }
7 W8 R# ^. x) w/ f5 \0 w' w; }5 h
* c- ]; E# n# {* I* \' k) q function notice_details_meta_box($post) {+ B" X0 I3 N7 L$ m- m" @. z' @
wp_nonce_field(basename(__FILE__), 'notices_nonce');
5 O9 _) K3 {; [' o $notice_title = get_post_meta($post->ID, 'notice_title', true);
/ z ^' v8 T# f L' d3 b $notice_content = get_post_meta($post->ID, 'notice_content', true);/ `% l A( R, Y1 K& D! e; E, M8 f& y3 N
?>8 q1 ?+ l9 D0 h
<p>
/ c$ T7 g# t* G% b& E) l! k1 F <label for="notice-title">Notice Title</label><br>4 y/ P% O9 h" h$ ]
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
4 X" | s: Z& d3 S0 s f </p>
0 o5 l8 L, D" V x5 y5 M4 h <p>
& J$ U; |4 k7 v2 N6 }0 u$ e' o <label for="notice-content">Notice Content</label><br>7 T& p7 J4 y* F0 m$ O+ E
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>% P; ]! J( ~) T" X( J# ]6 I
</p>
( [6 s; R8 l% t+ O; @4 U8 m& t <?php1 i B. P5 q! z) u+ L' C7 g& E5 o
}
2 C& {/ _) `. J' q y; m5 k2 ~' o- M6 m f% d' a/ |
add_action('save_post', 'save_site_wide_notice_meta_box');
( n* n9 S5 z/ Y! p& B function save_site_wide_notice_meta_box($post_id) {% B1 x( C8 w& T- Z
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
1 R. s' O" S% s" D% P% X return;# P$ L6 [& p0 E9 F$ T) w; h
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)1 k9 j8 R, N6 ~- P. w4 P9 ^3 n
return;
- c3 s3 c/ g" {
/ S: h1 W% _1 g8 V+ O if (isset($_POST['notice_title'])) {( g9 h1 T: i+ j: X# w
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
. F; i8 o( h, S" A }
* C" t8 E. Q8 o9 }2 [: L/ ^) m if (isset($_POST['notice_content'])) {7 J; E" B, w4 u L# p( T
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
; H j1 d; L2 U- h' J }$ A: V1 E) R; J" V) t+ t; Y
}
( f/ j* i% z1 f: S$ ? ```& E% l, M6 k& |$ N1 q) a4 ~ E8 i
. V M; v( Q- [
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
( s% ^% [7 c: M6 w5 V3 H3 }$ i2 c+ C/ C7 S
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:) @% Y i( Y/ X$ k) A! B p6 o/ \
+ j1 u5 @$ D& ?9 V! W% s0 K ```8 M- V4 o- {* V9 _2 [7 A! y n" x8 ]
$args = array(
1 Q, w- ]& r( A- m. L 'post_type' => 'site-wide-notices',
3 D, N9 ~( R% P9 H 'posts_per_page' => 3,# c, k+ `& P: x; D
'order' => 'DESC',
! s A& _4 e0 ^; f+ s& _8 Y) u 'orderby' => 'date', i4 y% Y* ?) t, J. G( I: }( Q0 k
);
- T8 R5 F. H" Z! D0 l7 d $query = new WP_Query($args);
% T4 A& G* H/ Z; I9 g3 y& d( X if ($query->have_posts()) :
$ h& R3 B# G3 J while ($query->have_posts()) : $query->the_post(); ?>
. Q5 ?& |5 @& R <div class="notice">
w( v) X/ |, L5 U( ] <h3><?php the_title(); ?></h3>
1 H" o7 ~" Y( [2 \' ~! w <div class="notice-content"><?php the_content(); ?></div>
: [7 V$ A! J9 i Y* o7 L </div>* g" T% z9 B* i$ p# f8 u7 b
<?php endwhile;5 S3 s% k8 a5 {1 l/ w5 V
wp_reset_postdata();
, ]# O% L! h B8 r4 u endif;& b: R- @4 g9 m8 \$ g, a4 J
```
' z" e( b( f! I: u* p) i+ Q. T( ?
" ]4 J. v$ ?1 F) ~/ j5 J 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|