|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
1 @$ |2 Q& X+ \5 H- u( M3 Q+ c8 W9 c: K3 I
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
* {5 P( G# v! x. Z) u
4 d& X) g* s" Y9 F8 W9 t' A6 o1 d; H以下是创建自定义插件的步骤:$ [% A' ]& e3 v9 I; T; w+ x5 G
' ?! h$ m: O4 k5 `
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
. {4 w% _' _. A* `! W
0 | @: B$ d2 N* _$ t ```
/ Q8 C0 S0 ^* {4 r <?php
, N8 g* z1 M0 ?* ]) S2 M. c /*% R. z+ N- B+ j$ Q, S/ t7 L
Plugin Name: Site Wide Notices Plugin
9 p2 V; x4 [6 p) O Description: Adds a new custom post type for site-wide notices.
+ F, F! O+ K) M C Version: 1.0- P8 P' Z; v# t8 b' X1 C( }
Author: Your Name
" l+ ]* [7 s2 u2 e- b5 U Author URI: http://example.com, |4 M3 w l& F. ]% l j F
*/
% i' C% k0 [" x+ n/ b& S' p7 V0 n: f! |! q$ H! F
// Add plugin code here...
1 p( M6 x9 ` P* ? ```2 r. \, W* u |# Y
, {' E6 e1 D: p* A9 R 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
7 R$ e; q) C: Z2 u q9 G% P
8 `' x9 Q! O- h Q* q* ~2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
$ |" s. n6 s- S b8 U: {
$ K' N% f. J2 d% o; y1 A ```9 [: o) x5 ?; _/ s# m' n) W
add_action('init', 'create_custom_post_type');
8 x4 g6 r' T( _# h$ _ function create_custom_post_type() {
$ N( W2 _* _8 d$ r) z9 W $labels = array(
% _, p5 H0 D" S4 l6 ~ 'name' => 'Site Wide Notices',9 S( i, i- {* ]2 W
'singular_name' => 'Site Wide Notice',( h% g: l1 {4 a! y( N* s
'add_new' => 'Add New',
2 ^! I- U) Y$ T 'add_new_item' => 'Add New Site Wide Notice',% y/ ^' Z0 T) d1 g2 `
'edit_item' => 'Edit Site Wide Notice',
, C% n4 f) T: g- h+ Z7 A 'new_item' => 'New Site Wide Notice',
8 q5 Q9 ` Y, g 'view_item' => 'View Site Wide Notice',+ p8 I* A! O6 s M c- ^) v
'search_items' => 'Search Site Wide Notices',
; }* k ]; }6 @ 'not_found' => 'No site-wide notices found',
+ ~* V; J) D' F8 K- A B 'not_found_in_trash' => 'No site-wide notices found in trash'( ~% L6 @# O3 G# \
);6 }4 q# ~" |6 e, ]% k+ H n2 j
( s- g6 l1 b( a
$args = array(5 s% u4 u. z x0 y0 x- E
'labels' => $labels,
8 R% ]6 P( D9 a- ?' Y# @ 'public' => true,
/ w2 O3 S; i: f2 q! Y% ^! ?8 p* _ 'has_archive' => true,$ P( F5 |( F* K% l/ i4 _
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
3 s0 }1 F2 z2 v 'taxonomies' => array('category', 'post_tag')," A* |4 \4 G- v
'menu_icon' => 'dashicons-megaphone', \3 f' ` j/ t7 m4 C, j& c
'menu_position' => 5,
( r+ b7 F& U) I' R" X 'rewrite' => array('slug' => 'site-wide-notices')0 V$ q6 R& ^* l" y' z4 w, q
);' v* B& _6 l0 |0 V+ b2 }
/ w% K" Y' }2 e register_post_type('site-wide-notices', $args);6 @: n ~/ y( {! ]
}6 }5 j; ]: p9 t. B- S" S
```) L4 r# k) w) y, ]1 B/ @. ^, r
0 Y% C/ p1 y7 B. M 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。3 B$ A2 V7 Q/ D# p
7 {2 v% x( a) `
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
# Y* A. b1 N3 T6 v3 B* i8 |
8 A$ p/ ?8 Z6 Z) h3 a4 _ ```0 s, Q# @, E% l' ]' M' [+ S
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');; K* _7 Q! M: T' O! b) t
function add_site_wide_notices_boxes() {
' P2 X: d, R/ t! ~- x& n% d add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
% ^, `' l! v f* l+ l" r }1 J" P$ \* }& ?* A+ l3 T
* T' M! x+ u8 f, [, u function notice_details_meta_box($post) {7 o( h0 [$ J' {# f" p0 i
wp_nonce_field(basename(__FILE__), 'notices_nonce');6 ?8 J; {& I( R" s! t8 K
$notice_title = get_post_meta($post->ID, 'notice_title', true);: t$ O- o' Y+ m# Z) E
$notice_content = get_post_meta($post->ID, 'notice_content', true);+ p1 j4 K5 b& g; R1 j$ Y4 {* z8 l
?>
2 C8 M8 i) e. x <p>: F0 x7 {6 @; Z
<label for="notice-title">Notice Title</label><br>0 D7 c8 H+ w9 S- S; h+ n! O. k* y
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
% ]1 |1 l6 \' y2 h9 o8 ^ </p>
" u( \0 s; C }, Z; }4 w <p>
. B. ^" ?. n$ h: _& V# R5 M' l <label for="notice-content">Notice Content</label><br>/ C' U5 o1 X/ n; P2 e3 l% y, V" i
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
8 ~7 `& k: O' e' N1 L </p>
4 k# |, `. f8 @! Y <?php& G* k" q `, X
}2 C$ [; A h n# J; `, O
0 R2 T2 a3 f3 _! Z+ |- |
add_action('save_post', 'save_site_wide_notice_meta_box');* A, [6 `% z8 K
function save_site_wide_notice_meta_box($post_id) {
+ G- w2 d" W. A4 N, l" r9 r3 B( F, _ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
( W5 D- Y! t7 Z4 a# J& u- \ return;
" g1 F O8 @4 \ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE), U. A; s4 m8 b" a* ?
return;
4 o, m6 @5 d6 f# N. K* J: i8 ?3 J0 ~
if (isset($_POST['notice_title'])) {# I6 H/ l# v: Q! F5 {6 }/ C% `4 i
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
6 ~$ W* j- I# d0 {- y }3 N; v% H: X x# k. e3 ?7 L0 G+ X
if (isset($_POST['notice_content'])) {0 f7 d: p+ t$ u1 N
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));, P& ]* U( Q+ v, t
}0 M7 ~* X5 }) u
}
; P! t+ M/ ~0 V ```, h/ U* e: U' K
) i" W4 p& E2 V/ B% I3 ] Q0 `
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
# i% N" V/ c" f0 d- v
0 \: c. R* |( w2 m/ k1 S4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
7 s- b! ^: z2 ]- Y$ \0 v7 F; e# M1 @/ G j
```: A& _( M5 i" g. J. {" P5 ^
$args = array(2 L% W- n8 N7 \! ~" k! Y0 p
'post_type' => 'site-wide-notices',9 R: w) ]( P7 y& X
'posts_per_page' => 3,
4 {. Y. I: b- Z/ {3 L. z 'order' => 'DESC',
Y; _9 R* b/ u( g: h* B 'orderby' => 'date'; h [ P( ?6 Q6 m: F
);
8 z; U1 T) V: z# y $query = new WP_Query($args);
0 D. v# l( i9 h if ($query->have_posts()) :
# ^( M. J3 t, u' p while ($query->have_posts()) : $query->the_post(); ?>
$ V: L5 E9 V, I& F$ t <div class="notice">9 e. ?6 |' K/ [: `
<h3><?php the_title(); ?></h3>% i- C! I* n0 Z6 f
<div class="notice-content"><?php the_content(); ?></div>
) W4 f* O) k3 p7 C, F </div>
/ C9 M. M9 B+ L; U! G# u6 V" P <?php endwhile;
2 O/ }% _# b: _- l3 ` wp_reset_postdata();
' p% {: C' V, l2 N2 u2 T& l endif;* y, v- D8 N" w8 J1 j
``` _3 z3 w( F) |! E1 A9 f
/ r1 c. I. p! B \, X- j6 l 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|