|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?4 M3 I* @+ ?! K) l. G$ L: {
8 {7 l1 j/ _3 \, _. i
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
5 d3 B$ ]7 p) u$ X U1 K
) b( w+ P' \ s; N" r8 ~9 b* |. [- _以下是创建自定义插件的步骤: i( `9 \9 C) h# y/ f( R9 L5 _3 L$ s# T
) c+ b- b9 \1 E6 F2 R# @
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
" P$ p7 C. P. u( G. }/ ]5 r. u7 y. H6 L: w
```
" \2 @* J5 w( Y' H+ @& y6 V <?php$ U& V8 F5 e" Z" R* w% C% m3 ` Y- J
/*
: T0 }4 F& j/ _" V Plugin Name: Site Wide Notices Plugin
) m5 q+ F c, E8 A6 e Description: Adds a new custom post type for site-wide notices.- ?2 F2 X+ A& h |; Q3 I8 V
Version: 1.0
$ e6 |/ R2 e: s Author: Your Name
7 R0 d. Z% e& f# N; e+ V$ l- @ Author URI: http://example.com2 H# Z; P J# G- z. p. ^
*/
! F+ f* S+ R% P( Z+ {
- J% M% P7 R& C: p+ W0 V // Add plugin code here...
' ?, T$ p5 @; [7 d2 D1 W( i1 g ```, S1 O1 T) ~1 @ l# O. c) w3 Z) X
! l5 q8 E8 R3 H, c 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
9 O, F# `# \% u# k+ T6 J8 F- X* X) C8 T. a
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
v3 { d, G m% M2 F4 t1 N; d9 F
4 z0 z! s+ e3 ~/ Y6 ]" ~ ```2 }$ s, ] X2 e0 t1 s' T0 r
add_action('init', 'create_custom_post_type');
: R" P( W, w4 d! Z! L function create_custom_post_type() {
6 _3 w/ `% f8 c+ ], P) P $labels = array(
: d& R0 T$ z1 T- W$ D7 d 'name' => 'Site Wide Notices',
1 V% r0 f+ M2 X 'singular_name' => 'Site Wide Notice',
% S1 x- c- i, I 'add_new' => 'Add New',
% z G; x+ Y( R6 u6 P 'add_new_item' => 'Add New Site Wide Notice',
. O. L+ {0 c, v/ Z9 ? 'edit_item' => 'Edit Site Wide Notice',* A @3 P5 V/ K, P
'new_item' => 'New Site Wide Notice',
2 _/ H, R7 y6 t$ _8 L- f6 Z( x* f 'view_item' => 'View Site Wide Notice',
- |, W) s' p8 G" t4 l- _# q9 A3 G8 K 'search_items' => 'Search Site Wide Notices',
2 c) z ?2 e5 I+ o& l& Y 'not_found' => 'No site-wide notices found'," K7 j* Z) b1 }: X- V/ J0 C
'not_found_in_trash' => 'No site-wide notices found in trash'
+ s& ^" u; V2 J% Q3 K1 z' n4 x );0 I" O5 p2 ^0 t$ A1 `8 L" Z
. ]" L# b+ G7 P $args = array(0 f. j; h9 w6 G& B& a/ ]3 W! o
'labels' => $labels,9 _+ W( f/ p0 \6 m8 u5 _7 C
'public' => true,
; }2 s" B2 d9 W( i! C' ]7 E 'has_archive' => true,' e7 g# s% d# m" u: i, W% o
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
! [) ^8 o: d8 A/ a* l$ O5 j* w 'taxonomies' => array('category', 'post_tag'),0 D1 {& I* a9 @' G, |! }* d
'menu_icon' => 'dashicons-megaphone',' x, E1 S& _8 @2 |
'menu_position' => 5,
( P% C. I0 E. h3 n; A" {# | 'rewrite' => array('slug' => 'site-wide-notices')' u' d4 A8 v5 F ?* _* f3 N" L, \
);
5 t+ a, z) y. ]4 `$ e* D
5 ], d8 \0 Z# { register_post_type('site-wide-notices', $args);
) E( ~3 Q) X3 h/ O* F! x) ]# I }
1 V4 e- T8 W8 A3 h% R( R ```) T' S0 s! n" R
- v; k/ n/ \5 a1 b1 ~* N
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
& y2 F! ^/ d8 U/ L; m& O7 G0 B" h
8 c4 q6 g% j, i |* R/ _3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
0 Q; w+ T7 d( K/ r! ? [# W+ _/ i! g0 S
```6 ?& H" Z7 M4 F( K! @4 K4 _
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
. G: A! R/ z/ x$ E9 Q function add_site_wide_notices_boxes() {
- U; l3 r8 y, P, m1 ?) ~ F1 n add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');2 w. @- V' b0 Y6 [5 O! U* t
}7 s) s. O, T9 }0 e4 y
8 s( K: ^( n0 V
function notice_details_meta_box($post) {, a2 q d6 m0 W
wp_nonce_field(basename(__FILE__), 'notices_nonce');
* j( [5 }. N, w $notice_title = get_post_meta($post->ID, 'notice_title', true);
$ U7 k% q! ]8 I# r0 u2 C8 g $notice_content = get_post_meta($post->ID, 'notice_content', true);- q, [ D# a9 l5 Q3 Z4 x1 S
?>
- X7 `# I0 O) J& A( S, ~ <p>
4 t( m2 W- s7 n4 } <label for="notice-title">Notice Title</label><br>
4 L* w( f5 L; I, |: @$ | <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
: p1 W/ |1 |7 `: v! t |! S </p>
7 l0 L0 o2 U I' q/ U/ h( h <p>
7 ]3 }4 k! K" _" U7 } <label for="notice-content">Notice Content</label><br>
6 e' K* c- ?( z3 C; Q <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>2 |: N2 _% m- O: V. }. S* B
</p>
/ B' N- U, [) F+ r7 y <?php
5 A2 h p2 E3 V& S# s5 N }
! u: r1 k3 Z! h# ^- l% T3 D- c) [
4 q5 f! D k( f! s2 ` add_action('save_post', 'save_site_wide_notice_meta_box');' ?4 v$ K5 V+ G G
function save_site_wide_notice_meta_box($post_id) {
5 _2 z: C! ^1 G; l) S$ R& D- S if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))2 ]0 J u( |0 o2 Z. h
return;( Z8 W/ Q y T9 R
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
5 S/ `$ g8 Z6 h1 ^( Z4 q return;
8 Z% j: T3 `1 i; q7 P7 N2 b! O3 G9 p& N. X v' f3 W4 s
if (isset($_POST['notice_title'])) {* [" Q+ B; c3 e: d, X- N
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
B' X; y9 Q2 n& [+ g }- L/ K: ?" g8 w+ F$ C
if (isset($_POST['notice_content'])) {
. u B R( W. a- s, V Z update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
& D% E6 ?2 q/ s! |5 C3 e }3 |; D4 S) D) P, ?! g: x
}; R% v7 T; y9 {
```6 f5 l+ ]# o* y4 c3 }
2 C) \# ?/ X2 e+ c6 N) s8 n1 M 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。! @& s4 Y, o" G& A
# F# G. P0 Q* J# N* R, T/ \
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:. D& \) r9 K [0 P
7 j: r& i5 P( I V/ T
```7 R0 r' }+ U. q2 p( i
$args = array(3 t: e- D' A: S, J$ U" P
'post_type' => 'site-wide-notices',
+ n5 d j0 |; c/ ?- k 'posts_per_page' => 3,
( u( ` y6 B6 R& o 'order' => 'DESC',
! u5 _ R" w' `# H& x6 Z 'orderby' => 'date'+ O1 S p6 Y+ O5 K
);! I( V6 B+ ~- Y+ I) U
$query = new WP_Query($args);( Z3 k2 Y# U, f0 K/ X6 g( n
if ($query->have_posts()) :
6 ?6 ?6 U/ `7 l3 ?' j; }0 J: Q while ($query->have_posts()) : $query->the_post(); ?>
' [! Z& y; K' v# y; P <div class="notice">- h, ~7 S* I- x9 U& n, j
<h3><?php the_title(); ?></h3>& N8 b, B$ L! F# S
<div class="notice-content"><?php the_content(); ?></div>
, s7 ~# E0 _ t5 i </div>: W# ^8 R7 R0 s U4 J1 |
<?php endwhile;/ L0 Z/ W; K/ D7 ^5 ]1 n( m; Z
wp_reset_postdata();; c5 ^! n8 K. Q/ g
endif;
D6 g$ q2 E8 c( Y- ^; { ```
& o: ]7 y0 `# d2 ^, ]7 v# f8 `
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|