|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
1 ? e) _" N2 F% m# C* W7 p2 [6 j- s' l
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。( Z8 g7 f2 e* I5 g
/ Y% | R# J; n: Y# q
以下是创建自定义插件的步骤:
* l0 ? t! a+ F0 t* U+ |/ u9 E7 Y$ c% ?! ]$ E5 m4 t7 K6 o2 L
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:1 \7 k9 M) o$ W. R4 p
: I1 O( u7 D( r/ K
```% [, `- z- r7 n& @6 [! u
<?php
2 b- v9 P ?/ b3 c: j /*
! O: O8 P! q5 ^% {/ x! N/ K Plugin Name: Site Wide Notices Plugin# r5 l) X( k. h' U
Description: Adds a new custom post type for site-wide notices.
7 n O/ ]- j+ @/ J% ~. m, \0 x Version: 1.0
+ h1 |9 j; F/ z2 I+ M Author: Your Name
3 R% V) S( E z+ @4 r& q& a; j( N0 F$ Y Author URI: http://example.com2 U; h% B5 a4 F+ e; l& A* r) I
*/6 R9 a: ?, Z0 A
7 N. _$ y9 Y/ [9 A5 M6 Y // Add plugin code here...$ L" l, X5 h1 A1 C. o% E
```& o% w4 X8 T/ L$ X. Z+ X
/ R* G% k$ g* o1 H" T 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。$ p. ^0 c, l* i. F5 U* c- Z
8 ]9 |) G( Y$ _8 c& C2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
: n+ G6 z: r" ~ K Q- e# C/ f( F7 d9 z- e) ?: _
```6 o: w& T' v8 F5 ]3 [7 u* x# v
add_action('init', 'create_custom_post_type');6 D- q" P- m1 j& H2 Y* V. C4 ?
function create_custom_post_type() {6 i" C9 O Y+ K* H8 {4 l
$labels = array(
3 b. K3 T: \# m# m# b 'name' => 'Site Wide Notices',1 h; w8 X" ^( e
'singular_name' => 'Site Wide Notice',
1 E6 s! [3 p" \! w; m2 \ 'add_new' => 'Add New',
& W9 q* y- Q7 P4 m& W0 I$ q6 A& J 'add_new_item' => 'Add New Site Wide Notice',
* s7 g6 o% q1 o1 ~. ~ 'edit_item' => 'Edit Site Wide Notice',
; x6 J' T. m, |6 P/ p5 b 'new_item' => 'New Site Wide Notice',
0 q5 Y6 E$ v& V* W 'view_item' => 'View Site Wide Notice',! _5 |( |! |! S6 }; I3 [
'search_items' => 'Search Site Wide Notices',
, D* t6 Y& N8 s 'not_found' => 'No site-wide notices found',
& [0 P& J/ K# r( U+ l. ~ 'not_found_in_trash' => 'No site-wide notices found in trash'
! T" }8 y f6 W F: [5 Y );
; [; m) {& l: c; j- a; {0 |
& H/ O$ e1 J5 `4 ` $args = array(# O/ o4 v- u& h
'labels' => $labels,
7 {; P! f0 ?+ g; E! A, R- d! O! u 'public' => true,( o7 l, [& p! I' x' f& {( z+ V- E
'has_archive' => true,2 B; L1 p* _% M2 |
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
; O# X! {4 f) C" k9 w' A& [3 g+ Z 'taxonomies' => array('category', 'post_tag'),# P4 \) f! q$ M7 [0 Y3 u
'menu_icon' => 'dashicons-megaphone',
# a$ R, o, _% f% f$ \- k 'menu_position' => 5,
/ h1 x3 p3 B, K8 N u 'rewrite' => array('slug' => 'site-wide-notices')2 a0 m8 q' ^$ K; W4 _! i% s+ x
);! v" H. e+ s% X
3 K2 K1 U3 s" u; \: o
register_post_type('site-wide-notices', $args);
, w$ C3 q: y' P+ r+ d9 Q }
- s) t* o1 ?3 k& f1 q ```
- _- u' d/ L+ e9 ]% \4 ~9 H3 k9 h: U! i/ `% k: v& g1 f* y! u& k
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。 H+ b4 ^" w+ q R
& E$ y' r& c! ~8 E/ B1 ?" [
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:3 }& m7 a4 a) g" v* j
1 D6 h8 X* h9 U' ~ \3 l ```/ Y0 c# @. v# q. N9 @$ O
add_action('add_meta_boxes', 'add_site_wide_notices_boxes'); [+ n* L% H$ Q
function add_site_wide_notices_boxes() {+ H4 I! S* _" Y7 \' i
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');& C3 L' Q; G4 d9 K1 W& i# v
}5 o9 N! n: e% ~9 }0 D. X# }
! G) o' P6 x% c) j: z function notice_details_meta_box($post) {0 P0 l2 I& [- G; A
wp_nonce_field(basename(__FILE__), 'notices_nonce');) n: S# G6 [$ j( S
$notice_title = get_post_meta($post->ID, 'notice_title', true);6 E' y- d% z* M: k
$notice_content = get_post_meta($post->ID, 'notice_content', true);
, G$ J% \) @8 d' P( x- ~! \" [# p& _ ?>
$ V' L/ Q4 l) I <p>
3 m, {2 u b* a2 C <label for="notice-title">Notice Title</label><br>
+ `, B, [; @" c0 i, I s y; ~ <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
" x9 [4 R. B* w4 S- A </p>$ I' `' m$ ~* @- a" B" U1 Y4 b
<p>$ |$ O1 G) t v+ Z% b
<label for="notice-content">Notice Content</label><br>
2 W8 I; _' |* }7 x8 { <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
0 |5 O2 u% ~3 ?7 o* @# \ </p>) S1 {9 L4 V" v, w1 F* `
<?php1 x# q0 b% [8 l" i6 ]
}0 y& F, K4 O5 t" }; F
- ?; I9 }- O( O3 k: C
add_action('save_post', 'save_site_wide_notice_meta_box'); i7 X0 V. \/ b6 w' Z1 l8 R
function save_site_wide_notice_meta_box($post_id) {; ?3 ~+ B4 C& D# w7 }9 v4 R; K; u" i
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
' p2 l7 Q" Q9 j, | return;
( P4 o8 Y3 }. F, W5 E+ y3 m/ {5 _ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)6 }' ?$ R0 X5 X+ C+ v
return;* g& V" \7 a6 G5 T p( e4 Y$ [3 d
9 l& e, {* g Y: k! F; A5 y
if (isset($_POST['notice_title'])) {
' \, P2 \- _) |+ ^0 Z' A) u update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
4 Y/ a4 ^9 E5 X+ c( V' \+ O }
/ I5 v' r* w; j& C3 q( t( B8 O if (isset($_POST['notice_content'])) {/ p! l5 k. z$ r0 @# G( [# f5 u
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
3 C, L% q* o, U8 C4 a. O }
* x+ s+ U( }( w* ~+ s+ O( [' a }) H) m" H+ S9 P3 f5 G+ G! i
```2 c/ s2 J9 i1 ^+ b. Y
- d/ r- h6 T3 H4 ?4 u: |2 ]
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
: x6 E! G' l4 ^4 E& L
! e( G1 t' p, R( j* Z& x" t4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:( Z0 M7 R$ E6 o8 q
( h( Z7 v+ O7 q( S7 j ```3 G6 N/ X9 X+ j/ f9 _
$args = array(
5 I& j7 Z& d( g. D 'post_type' => 'site-wide-notices',
8 ?+ C% }! U) g. N 'posts_per_page' => 3,
; h }2 y( r' k 'order' => 'DESC',
: X) [2 N" I) u4 w/ O- Q 'orderby' => 'date' F5 t% i$ T/ E# C4 U3 d
);
6 A3 N, A% \5 W( w $query = new WP_Query($args);; W2 J, s6 _9 i! c K/ a( M
if ($query->have_posts()) :
5 T9 M0 C8 C& F$ B- s while ($query->have_posts()) : $query->the_post(); ?>
% a/ n* e. |! j7 A- F& _7 r4 Q <div class="notice">
- s1 R/ O! R; Q <h3><?php the_title(); ?></h3>
3 |# k: g" W2 v% s2 p$ N! I <div class="notice-content"><?php the_content(); ?></div>
" d8 n+ x: a1 ^' | </div>& w3 E' J3 F* B9 w1 j ?. M
<?php endwhile;
: j( S( I: {. b1 z+ c wp_reset_postdata();& r+ x1 i3 T2 J, \ S; D7 @! ?: V
endif;6 W0 u" p6 g% p1 i- P
```/ L3 |1 ]+ r9 o
' Q0 r/ J' C; d
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|