|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
* R' B9 |) q6 f( L0 [
$ D0 M3 H4 h7 N如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。; ]* e. t5 \& ^. i/ i) Y, ]" ?
' P" _' F; q" V$ y: J8 A+ j
以下是创建自定义插件的步骤: M$ \) h5 l3 l
! A; A& T. b3 c, ?' I8 k1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
8 D' J y7 ]5 r3 ?$ k3 D7 V# @3 E- }) ^& y
```
3 `2 n: X# l6 b! O <?php4 h% m8 B6 p \, G4 |" d: {# \
/*% X# j7 m' ]0 R( J6 h# ^- j) Q! G
Plugin Name: Site Wide Notices Plugin
. c' \5 o# [2 R( J Description: Adds a new custom post type for site-wide notices.
! Z6 a# _, W5 k5 h# D+ p" G h i- |' D Version: 1.0
$ ?5 ]) e- I, M7 S Author: Your Name% u; F# q- b& P* K
Author URI: http://example.com
* y) g; Z0 L! y! z* B */, B* `! h4 K: O( u
- _' @* I0 ]2 G* r // Add plugin code here...
0 q6 b3 ^) D* a* I ```
" Q3 Q' D& o/ n' X3 `) R5 F/ ^8 e2 D: F! `6 n
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。. D" {, u/ \6 _
- Y1 D- L& Y, L" x3 {
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
3 I/ Q( V$ q% u* R0 |* W; m5 F: `. c$ T
```; C O. F+ w; x& F/ A2 Y1 U& L
add_action('init', 'create_custom_post_type');
7 k* W! L8 B7 i1 y function create_custom_post_type() {
7 g# F4 T+ X0 T6 K2 S $labels = array(
. q: e/ D Y9 W7 M$ C4 C 'name' => 'Site Wide Notices',2 H* h7 Q6 @ z& j8 [
'singular_name' => 'Site Wide Notice',
4 i2 `% l2 ?3 O" c 'add_new' => 'Add New',$ w. h' j3 y7 W6 N- O; w
'add_new_item' => 'Add New Site Wide Notice',( t6 p. p H6 j: i0 R& [/ Q
'edit_item' => 'Edit Site Wide Notice',9 t9 l' `1 y( ^, i) A0 o, D0 m
'new_item' => 'New Site Wide Notice',
9 g! `$ L7 O; h4 A, l* V1 q8 ` 'view_item' => 'View Site Wide Notice',
; S) X( h: v3 [ 'search_items' => 'Search Site Wide Notices',
7 W: u! R4 C- M9 W# I 'not_found' => 'No site-wide notices found',
3 a: [* H, B" M7 J 'not_found_in_trash' => 'No site-wide notices found in trash', K Y, I; b7 Z7 [
);
, E1 A: g, `) W0 E0 c% i$ q0 h1 p2 @/ X+ b& M0 `: t( r4 j
$args = array(
: {& b# L7 f; t, A3 H 'labels' => $labels,. V4 P4 i8 x; u% X+ |
'public' => true,
; @6 H8 t) t5 Y9 Q4 M 'has_archive' => true,* o/ [% g7 h$ D- n1 H2 R
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
- ^' D. G$ c; a 'taxonomies' => array('category', 'post_tag'),
1 i, Y1 F7 O+ ` 'menu_icon' => 'dashicons-megaphone',
' M, a3 w; s* P: H5 \ 'menu_position' => 5,
( ^- z/ ?3 x! j# B 'rewrite' => array('slug' => 'site-wide-notices')
3 J& T+ _! }( Q, L& d; ~ );# o* @2 f/ h* Q& E$ h9 |
5 K2 X. A: s6 C Y5 h# M3 V
register_post_type('site-wide-notices', $args);
5 n# s' S0 L# B6 K6 W }
% C4 q# R0 I' I0 w) c( q& C2 F ```
6 ]( \1 Z$ T: V: R6 \, v" C l; ~/ d0 U
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
3 I9 g. x9 x3 T( a8 {' I9 }/ N
$ e9 j# ~7 B" _- @, `3 s. Q7 n, [8 k1 Z' b3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:, e/ x. S' d: Z( T& @4 R+ X& [) z
$ i. K8 k$ C, h }( n ```
% w9 K _* L) u* Z4 M) J add_action('add_meta_boxes', 'add_site_wide_notices_boxes'); u4 {: i' L7 ]+ N8 D
function add_site_wide_notices_boxes() {
. h W; v, {( S, Y# | add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');$ B* V% `5 q% J
}
, u7 H: T; I! B4 z# u, L; r4 b* {6 b" b# z# i! s
function notice_details_meta_box($post) {
* ^5 {/ X0 ]4 p' W. h" r wp_nonce_field(basename(__FILE__), 'notices_nonce');
! K7 p5 E' V2 m( H $notice_title = get_post_meta($post->ID, 'notice_title', true);
1 p s1 u/ ]7 X0 Y7 k $notice_content = get_post_meta($post->ID, 'notice_content', true);
1 f4 `& J) K- z9 P ?>5 }- B' V" D. ?+ f3 p
<p>
3 f* a3 C& R6 T( }* o: H( J% M2 v <label for="notice-title">Notice Title</label><br>
6 x' M; t: ^. g. s R2 f- D <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
8 n) p5 e- m9 D </p>
1 O7 z' t* T5 R' M1 U <p>
! O1 e; H: _$ a. P* } <label for="notice-content">Notice Content</label><br>
0 C6 b5 i9 C' M- w4 n+ n8 L2 [ <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
( y; v9 {& r1 d) F, p8 n </p>
' z! \/ n9 d$ s. b; F C- j <?php+ `4 u0 C: t2 \3 {" d/ v8 `
}2 ]/ k( w+ h3 L. q& V/ A5 v
' B2 v9 ]* b) f4 A& M
add_action('save_post', 'save_site_wide_notice_meta_box');- o- R% i4 r) v& M, E9 c
function save_site_wide_notice_meta_box($post_id) {
; t$ @7 k- F; i Y7 y# L) R9 d, m if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))1 I3 k. x1 F' A
return;
% t7 E! D0 o6 b) f5 C6 d if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)6 ]: m/ \( z( Y+ U
return;- c. M7 c0 P$ B, j# O
7 C, H% O! `+ C5 u. B" C if (isset($_POST['notice_title'])) {
7 G, K K. z! `5 G update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
0 P- W9 J! W/ U. Z" V$ O }
% C, @- M2 f, N6 d0 O9 G5 r. D$ L if (isset($_POST['notice_content'])) {
- N4 B+ Y' P |0 N( e' S update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
& g8 T- v! O' C& q }+ }- U: K" c+ Q$ g" m2 i- D/ u4 X
}
9 p% n! ^. _- ]* ] ```8 U& b1 r4 h/ O$ [) p5 `
5 n" C7 [ {) _& T, q 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。. e# z9 `3 c: N* [3 W' [1 e1 H
- h6 F0 g: f; n3 `3 C2 [4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:% [* b1 e9 A {: e. Q8 e. n
( c8 w5 B. Z0 [ ```
# e" Q; h" [( d1 ? $args = array(6 i) Y" [0 a r5 n0 n
'post_type' => 'site-wide-notices',
6 t& V5 h$ z8 Q/ i+ }: d" k8 T6 ] 'posts_per_page' => 3,
6 s. c# w5 ~. n4 F% A 'order' => 'DESC',1 h- ^8 l; q7 G* {
'orderby' => 'date'
+ i9 j+ w$ g: [% u );5 s* j( O* W( `% w/ f3 f
$query = new WP_Query($args);
% [" M8 [ P* r$ A7 `" {, X if ($query->have_posts()) :
2 o7 c* Q/ g0 ^& `; | while ($query->have_posts()) : $query->the_post(); ?>% M* N; L: R, q! d& [' d
<div class="notice">
^0 f! T5 Y6 v! `2 `* t2 E7 X <h3><?php the_title(); ?></h3>
/ A0 w; T7 J, a+ F% L0 M <div class="notice-content"><?php the_content(); ?></div>
! u5 E! n& E" ^; M2 a/ u' ~, V </div>
4 F$ s3 B% ?( ], S! A4 E% c <?php endwhile;) M& Z+ Z- a9 d! _
wp_reset_postdata();
$ @9 w8 I, h4 j9 }$ _: |1 _ endif;
5 C* @2 ?# U& S i ```
) b5 L6 e4 A& B. @% p/ Z7 q( P" p
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|