|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?1 m9 `5 A4 _! h a
) D+ V3 z" T$ J8 K9 k如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。. S7 K# I) }. ]2 \' L0 [2 k9 W
- J% w* G/ K5 ?$ {2 B+ S以下是创建自定义插件的步骤:
* F0 ?0 S* G' m: S
. h& X3 Y" w1 {: j1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如: h- _8 _% B- U4 x) v
: b" j" _% \) c \7 l8 k7 L) ~! E ```
. s9 Y) x* o# U- y6 B7 \" C <?php/ R4 N; h/ G7 i7 A+ B4 D! t* `
/*' W5 n. E6 B% F& ^$ P' c: I, S- R
Plugin Name: Site Wide Notices Plugin
$ ?$ F; q# S! Q8 ~ Description: Adds a new custom post type for site-wide notices./ k; @8 l% {) c6 d% [7 S
Version: 1.0
5 m8 ]. I- h% h( h8 ~ Author: Your Name
: N$ `% h- V4 |( L; b. Z Author URI: http://example.com V4 L, A/ @; j2 q! A) J1 l' K- g0 u7 h
*/7 A% {. h4 _- R: O5 S' P
9 }* N/ a4 Z' O8 e4 W! Z
// Add plugin code here.... e1 A# ?& }2 F
```: N4 N( j/ q# s% I, W7 |( l0 Z. k
% P$ ~( q! m- U& h
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。; Q7 M2 Y- g0 k/ \/ b
" b* h" ]+ R& X3 w- s2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
! i/ h+ z* Y% W$ K$ N2 W% Y
0 P3 o- \1 w9 e, ?* F K ```
. q+ ~7 m. R6 i: W; r# f add_action('init', 'create_custom_post_type');+ y; t5 d) I% i' `; i
function create_custom_post_type() {
% P" W6 X. X$ G- {0 o $labels = array(4 n6 V1 ~7 T# u8 R! A! J
'name' => 'Site Wide Notices',
) |0 y5 V( M5 ]! d, q# L; _7 O 'singular_name' => 'Site Wide Notice',
2 C' t6 a1 a, `- @" a( P. R/ S 'add_new' => 'Add New',
9 G( ~* Z8 t0 \ z 'add_new_item' => 'Add New Site Wide Notice',, Y8 w* H6 l0 z+ ^3 k; \$ o3 [
'edit_item' => 'Edit Site Wide Notice',! [7 v2 E9 c: w5 |& d, ]
'new_item' => 'New Site Wide Notice',0 s0 d. n9 v' G% F
'view_item' => 'View Site Wide Notice',
( g' v3 e k/ W& p! C+ @ X 'search_items' => 'Search Site Wide Notices',
) f( ?" P+ p5 B. i& \ 'not_found' => 'No site-wide notices found',& o" E% Z7 U/ a- U6 x' V3 ^
'not_found_in_trash' => 'No site-wide notices found in trash'
7 y# O' _6 p; [+ j8 C );
" H1 V9 z. [" M
, Z6 e; n- v4 T' n; q5 S; ` $args = array(
3 {% e) l3 k/ P ^1 h$ o1 u 'labels' => $labels,
5 E5 F9 ~& t: f$ ^* C+ A# V6 c 'public' => true,
0 s# o$ w& ~6 V5 ^, Y1 g1 { 'has_archive' => true,
" R$ l0 x2 X6 l7 r& r) ~9 P 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),# F- x- W" }* r" X9 L* ]
'taxonomies' => array('category', 'post_tag'),* C. u' u' _7 Y4 |; J9 A
'menu_icon' => 'dashicons-megaphone',
6 F. D5 R# Q6 `' u( G 'menu_position' => 5,( w; g9 R5 Q/ J8 G
'rewrite' => array('slug' => 'site-wide-notices')' \9 d9 R0 {4 L0 ^5 n
);
* e8 F# `! E; G& C1 I) m, C% E
: m0 J( b, e5 {3 ` l4 A register_post_type('site-wide-notices', $args);/ B; C4 P5 e* P+ y! c4 B( b
}, {0 y6 P& Y1 e/ R9 H- R Z
```: k0 w8 v8 W+ Z9 v
5 b4 X* O! _* Y5 ^5 u' Z0 P
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
7 P$ Y# S! d8 i3 d A `2 o+ A+ V0 J. o- a( B
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
1 T* O3 s2 [+ Y4 R- Y
8 |# Y8 D/ A- O7 j+ l ```
8 P8 j4 {5 ~' H" D" \3 \ add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
: d* W, s+ Q3 |! J, ]9 k2 M( ~ function add_site_wide_notices_boxes() {& v, b S% s7 l5 F2 C# T
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
% R- Z/ o; T) d. Z3 Q2 V8 L7 i }! q/ r5 d( N# R2 [- l W: j# i
0 h1 j) O6 l( S
function notice_details_meta_box($post) {5 ~+ z& D: _, H& Z( q
wp_nonce_field(basename(__FILE__), 'notices_nonce');+ `( B" p* r- m) y9 h# e) G! Q
$notice_title = get_post_meta($post->ID, 'notice_title', true);
( }: |" t( v' ]8 h, p $notice_content = get_post_meta($post->ID, 'notice_content', true);
2 h) S* C4 f0 K3 e ?>. x3 B" J5 p ]9 O9 m6 V
<p>1 D8 M0 M$ N7 K) D1 ]: @0 p
<label for="notice-title">Notice Title</label><br>
9 c" b1 M: \, J& h, R+ [ <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
4 B( b+ j- }8 g) x% z </p>6 V# E( z* }" E# s
<p>
" L" W4 p0 g& j8 u; N9 {4 M <label for="notice-content">Notice Content</label><br>( s1 t. t7 {/ J+ N, S- U4 g
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
& H! T2 Q- ]1 u8 H( C4 X4 s' k </p>
$ d( V! D+ f* ]+ ^0 J$ E <?php9 e3 d# _( F/ X, v1 ?
}1 }/ h* o7 f" ]* j1 o4 D( ]/ z" t
' t' l- c5 N( H
add_action('save_post', 'save_site_wide_notice_meta_box');
- D7 W3 \1 R5 t% [% f. @2 g function save_site_wide_notice_meta_box($post_id) {5 c H; s0 a( T; K+ i' q3 t! n+ S; z
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))8 d3 @: S7 G7 r1 u; e% f% ]' r
return;
G! T' f, x0 } D if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
7 |7 V- H' k! z+ M; A$ P$ m2 P1 @ return;9 p( [7 V/ O4 W1 \6 E2 Q' f
) X; f+ F- c/ U" d9 e1 W8 ^
if (isset($_POST['notice_title'])) {5 a- F' p% @% Z$ w
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));0 }/ D; O0 p8 Y* k
}: s( c4 T/ x, }" S6 ?/ x
if (isset($_POST['notice_content'])) {
" ]9 o# ^9 Q4 p4 @* s/ Q update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));6 U. e8 ~6 y( R. X
}
0 }3 z1 b9 ]2 _! i z0 b- [ }
+ M% F/ D2 M3 E ```
: z& Y$ X. S* u2 j7 u$ x" k7 M, k2 ]$ ~; Y" B
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。+ V/ Y* \- `+ r `
6 I& c, h; A" A, S
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
: C* @4 e, k. c: M8 n
5 e: B% v2 P2 _7 K ```7 o( S6 V' o; R/ W8 @
$args = array() q( w; K8 Z6 `* A* m/ G5 t" z
'post_type' => 'site-wide-notices',7 r; S" M- z0 o. I0 e! m* G
'posts_per_page' => 3,
8 m5 J. G0 B4 I$ |8 g+ h: ` 'order' => 'DESC',
! J( H) M& {$ A- E3 } 'orderby' => 'date'+ v/ Q5 R. k! @7 w
);
f1 L4 ]/ }5 A/ L9 O $query = new WP_Query($args);
% M4 O9 C" } B6 d# Q. Q* X if ($query->have_posts()) :
3 q% Q: i7 o+ Y while ($query->have_posts()) : $query->the_post(); ?>
" L! \6 a c$ D; N3 o' x; w: W <div class="notice">- j3 n/ y- B s" y: n0 p
<h3><?php the_title(); ?></h3>
$ i H, D6 B1 p+ x( v5 Z <div class="notice-content"><?php the_content(); ?></div>1 X, ]) ^( ]/ ] k8 w
</div>1 M% q6 U8 Z8 z" P1 M# e% s
<?php endwhile;
0 ]5 U' U4 {& `2 z9 l wp_reset_postdata();" U1 {9 s3 ~; M* W$ ^! _
endif;0 s6 R2 u% E$ ?6 y3 |) O/ ?% ?
```7 \! L8 t" a* X# U- w3 l
: H. x8 I; x# B 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|