|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
4 G0 ^! a6 s* k1 P% B C
3 @. T. |* I. S3 V$ e6 `- {2 z; o0 ?如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
& W3 y2 a4 j* J+ W6 e0 ?
% y/ a0 D. G) I以下是创建自定义插件的步骤:
M! b# i4 x/ r6 E' r) }6 [4 @4 v" q, I
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
( @$ Z0 j' s% R8 ^1 c( c/ k8 ?
. k. j8 `7 r& o" x/ ? ```; S5 A4 P) b' x8 o
<?php: @# }1 l" ?& ?0 v W
/*
3 G9 C5 b# {% C Plugin Name: Site Wide Notices Plugin
" A% [( s% }9 F5 x. p# {9 n Description: Adds a new custom post type for site-wide notices.) f, u$ |$ H1 @/ E) z. l
Version: 1.0* j* u7 v8 Y0 n% G' M, N+ B
Author: Your Name# k, f; f& h8 K# s: s
Author URI: http://example.com4 D7 u H# `+ \2 r1 N9 L
*/6 w9 @. r3 W8 U, e5 d# W* r& w
e7 d8 }5 q! ?- V, ]' p( K
// Add plugin code here..., D4 d/ ]. D3 o4 P+ N- H! D
```
/ E8 E0 F" ?5 C' P! {/ d3 K8 M0 q6 z- S9 \- X
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
# _/ _7 e0 u" M+ g9 }4 V8 o* h
7 u( i. n% k- H5 d3 }. P! }2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:6 N. B& F, L$ l, I9 w$ f' ^
( i/ Y9 f8 V& W! P ```4 s" A9 ]' R7 |( U6 {" P
add_action('init', 'create_custom_post_type');
! e- h; i3 X" t- | function create_custom_post_type() {$ ]8 S+ a5 Y+ M# e. H
$labels = array(
' b# v! ~3 \- g; E/ b 'name' => 'Site Wide Notices',
; q+ `: r/ r4 R/ _ 'singular_name' => 'Site Wide Notice'," F9 @2 Q$ g1 p! u0 C8 Z
'add_new' => 'Add New',
& |4 h& r( Y. i2 l% I' K: F+ g: ~ 'add_new_item' => 'Add New Site Wide Notice',
6 J0 h& r$ U2 I0 W 'edit_item' => 'Edit Site Wide Notice'," Y) t: v( d0 h0 V) G
'new_item' => 'New Site Wide Notice',4 U6 @: ^: T2 |( z
'view_item' => 'View Site Wide Notice',
, V& M( n; s. `, g$ L; c/ | 'search_items' => 'Search Site Wide Notices',# y& ]" }! @- F2 a' z# m& c
'not_found' => 'No site-wide notices found',
- w; i4 y7 f) V' Z( p+ j 'not_found_in_trash' => 'No site-wide notices found in trash'3 q& w+ K/ `7 k- e
);
/ V2 }# H/ n1 k* r
' ]$ g4 r" c- \1 @: { $args = array($ f; Y8 B( d" h. r. D L3 g
'labels' => $labels,
, z. u, e& Y( B( T: l( @8 ~& P+ c 'public' => true,' K V# g9 k& I5 ]! R% Q+ _ H
'has_archive' => true,8 Q6 S# C1 F; o) ^* z
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),. {! G& N, D. M8 w5 ^# ]7 _
'taxonomies' => array('category', 'post_tag')," Y N* b, W: o% |$ h/ g* J
'menu_icon' => 'dashicons-megaphone',
2 R, L8 k; t( }* p+ M 'menu_position' => 5,' e. Q4 _8 v W! w2 f5 u
'rewrite' => array('slug' => 'site-wide-notices')
" \) t) C" g' N9 q# p );* s( {# n9 }+ K! \* n
, U, V- U& W- O; u register_post_type('site-wide-notices', $args);2 F3 `% l) R) B0 Y0 N
}3 O9 E4 {1 G$ ]
```
% H3 P( K" ]: C
, ]" v' e) p( j 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
* q6 k1 z% I; g, X1 P5 a
# n4 {% U$ Z( r: w7 [2 g8 b7 k$ j& H3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:% R( c6 j1 G, l( y4 E& m
& @1 k3 J8 j4 ~6 c' H
```& @( _6 ?) ]6 r: D1 C) \
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');$ {/ ^# a$ c5 R( e* @- \
function add_site_wide_notices_boxes() {
& H5 Q9 Q1 m3 a& j( O add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
! W1 v% a/ }8 n2 g7 D7 G }$ n2 K6 V# w( S2 {0 l/ Z) [
^4 K6 i' u+ Q0 G function notice_details_meta_box($post) {
1 ~) B- c/ \2 k wp_nonce_field(basename(__FILE__), 'notices_nonce');/ x$ I: D, l( j7 i( s! D3 \
$notice_title = get_post_meta($post->ID, 'notice_title', true);
L& j3 M# K+ x& N $notice_content = get_post_meta($post->ID, 'notice_content', true);
/ C4 u- D' Y. l3 R4 C2 ?" ~& I7 m4 G ?>
% {8 |- |! L, J5 U; x <p>
" z2 `3 n" s E <label for="notice-title">Notice Title</label><br>! {( s9 k* u3 O3 E( D
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">2 L( I) Y0 F7 y! @* b
</p>
- X9 J' G( A+ A' `! Z4 F <p>" w; f+ W% ~4 u: U
<label for="notice-content">Notice Content</label><br>
r Z$ |1 O1 R8 @# f <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?> h! q/ K5 y/ P3 `& h) V% u4 u
</p>
5 R5 M2 z( D1 b! V1 U <?php
7 D9 K7 b( n6 S* U4 I+ Z/ @3 L }
a# {4 m' @" a" d8 n# h, g3 E7 L/ n9 p) r: B7 |5 ~
add_action('save_post', 'save_site_wide_notice_meta_box');
2 v- a" }: |) Y3 ?0 X. z function save_site_wide_notice_meta_box($post_id) {+ ]" y; b3 U, S" W* y- A; D: ~, F, ~
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
. z! S& L5 N# v6 `3 } return;. o9 O. |( I0 r* W9 \7 U+ x
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
6 {& O( H4 l$ x6 a+ [5 t% s) G& { return;( ^2 y, n! X+ x5 u
2 P _6 s, v; z- d
if (isset($_POST['notice_title'])) {
/ _( a G) D1 y0 K( u' l' } update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));& X; X- ?% S( A: ~4 X3 B; a
}
) k! p+ G3 J9 B+ R/ N if (isset($_POST['notice_content'])) {" s" K2 G! B6 O( w* L5 T6 }, z
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));0 s6 p# R& |' m* T0 M3 F: `0 |
}
& z/ w3 ?$ B7 g, R* v }
, T5 U3 _8 x* Q- E9 Z9 A: G ```
8 _" a* K3 J0 N8 V, \$ S8 {3 P( I, Y3 o. C" q- ~
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
+ d: K9 h& Z) W! V' \8 T% @: H& [$ t" ?4 k+ }
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
5 S# E$ M0 P2 x3 Y
1 q, Z7 }! p# j! Y% I5 N2 ?# X ```
' m, k& K, `- m1 p6 H& m $args = array(" f) E/ c1 i$ d/ P" }
'post_type' => 'site-wide-notices',$ m: B+ q6 c+ V3 D/ q
'posts_per_page' => 3,
" ^' R& I t* Z3 Z& V2 F6 F 'order' => 'DESC',
6 g+ a5 E0 |4 `4 B- K2 ` 'orderby' => 'date'6 F1 E; S+ l1 A7 j% |) x) |, Q/ L
);
: s% h6 x7 w5 T7 M% }% j& Q $query = new WP_Query($args);6 s# a k) A$ T0 S" L7 a
if ($query->have_posts()) :$ x/ e' q$ R* T, x
while ($query->have_posts()) : $query->the_post(); ?>( S2 F( @% r9 V, z; N
<div class="notice">) G; N7 r. P2 S4 [/ |, t
<h3><?php the_title(); ?></h3>5 x" S/ Q9 @! E# _
<div class="notice-content"><?php the_content(); ?></div>
/ e8 s. O; K. q5 c3 n" V </div>0 S! ?+ m9 m1 |2 z" E
<?php endwhile;" c9 N3 ?, M5 v" p, f9 W$ M
wp_reset_postdata();
8 P/ I3 t; @; b; }; ?; ^ endif;
3 O ^% ~! j# w8 t0 C ```( U# A1 H6 P1 c5 ^& P* @5 J
# J" T" q! h9 d$ c5 _$ v, s) g1 P 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|