|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
. N0 A& Q! u: g" f8 R ?& w
# j. H+ y$ N6 [; Y" u如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。4 e7 L! K' s9 n
. C, F0 d+ F& x
以下是创建自定义插件的步骤:' H+ F4 x; L4 q* q" l4 q, E
6 g" \5 }' y% S+ b/ b% ?7 [' P* b
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
: W8 x* ^# Q- J- ?5 v9 b
/ M7 V1 O3 _( J+ s ```
# V& c* ~% c+ v8 P2 v" o8 q- Y <?php8 d; K5 I2 ^7 K& I* q# n
/*) E' B; a) E. R! G) z" x- o
Plugin Name: Site Wide Notices Plugin
5 i2 \$ z$ x& Q9 \ Description: Adds a new custom post type for site-wide notices./ H8 B3 T( W7 e
Version: 1.05 f) t; m$ u" U( M
Author: Your Name% I& m! z" F( a# d+ T
Author URI: http://example.com
7 q Y4 a, Q; f# @! J */
9 m- {* \: k8 _8 e7 ^# F- [6 t" a- S! W4 b8 U$ q7 u L3 ?
// Add plugin code here...
, _- {7 \/ x/ B: o1 j# k0 b ```
" L2 U6 I& S! y- v: ]
: g2 m3 v7 W- w 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。3 i+ z$ l+ d( r# y; D
+ J* I+ P& l w: A2 r2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:& `* f, ]% ?/ @9 \; A& R
8 w- h' [, l/ h. U4 K2 m* M6 M
```
/ G. z0 c P: L5 y. k' t& F! B: { add_action('init', 'create_custom_post_type');
1 {9 W4 F5 ^' `$ B function create_custom_post_type() {. b* P5 M, z) a- ~% t
$labels = array(9 c' K2 D" [, W9 k% L! a" d. ^0 R# |
'name' => 'Site Wide Notices',; @7 I+ ~* q+ a. |5 M/ J
'singular_name' => 'Site Wide Notice',
. [ @3 \% t0 C* e0 c6 d: `4 |# G 'add_new' => 'Add New',
8 X- Z, j/ g4 ` 'add_new_item' => 'Add New Site Wide Notice',
; F1 y5 ?4 ?9 E, t9 }3 N 'edit_item' => 'Edit Site Wide Notice',
1 U- w/ b/ `* [3 ` 'new_item' => 'New Site Wide Notice',/ c, s7 g3 E2 |
'view_item' => 'View Site Wide Notice',
: e$ Y" F! _2 o. ?- h a 'search_items' => 'Search Site Wide Notices',
! ^$ n& H/ B+ n+ F* f1 O$ Q 'not_found' => 'No site-wide notices found',8 R: ~3 c& Q& ]- q) ?4 A" d+ [9 r8 x
'not_found_in_trash' => 'No site-wide notices found in trash'
0 \* y* }- d- ?0 b, d5 [ );
" n3 I2 z& G( f3 u/ ^1 a6 W0 T
2 q# N1 I- B4 h; m $args = array(
& n+ c" _* M, ?2 @1 N 'labels' => $labels,0 d& J2 o' N/ ?$ X2 t/ X% ^. i5 I* L/ i
'public' => true,
+ @3 R) F, a& D5 a9 u0 t 'has_archive' => true,
3 T( q3 Q* ?" K5 N2 |) y: @ 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
; h( j$ K* y3 l7 q9 |9 P6 x 'taxonomies' => array('category', 'post_tag'), f$ b( T7 D/ ]) n
'menu_icon' => 'dashicons-megaphone',7 F, m2 \2 g* h
'menu_position' => 5,
+ A, C2 s9 G0 f' |2 X: i2 b) @ 'rewrite' => array('slug' => 'site-wide-notices')
m* U2 Q* c4 }6 b+ I5 ^ );
1 |( o. ~3 j8 e/ T9 j
9 ?2 n+ }5 j# ] Q+ R p register_post_type('site-wide-notices', $args);+ p; t( q& Y% X C% v: e" V
}& e) c4 j. F9 I+ D4 H
```
9 |, r7 d. F$ v$ D+ b. B1 A' T) z
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
~( i3 n7 j( E) r; p1 q1 j; i7 H
: s+ O3 E- U2 o1 h" D; J3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:0 f# l! i. D6 `& ~+ F
3 N6 T T3 e+ z
```
, I- ?/ b5 z+ e0 ?+ D- C! ? add_action('add_meta_boxes', 'add_site_wide_notices_boxes'); T5 ^+ X1 O9 C9 l
function add_site_wide_notices_boxes() {
; j n. D+ `4 u2 \& a/ T7 F add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');1 ~& ]8 I: Y! m, l- C
}
+ c+ K& l$ m1 J) v, G M! N# k) N) Q/ k/ Y
function notice_details_meta_box($post) {& F! m" K1 P3 l* q
wp_nonce_field(basename(__FILE__), 'notices_nonce');6 [9 z2 E C5 f+ O
$notice_title = get_post_meta($post->ID, 'notice_title', true);+ M, V6 V7 d* u2 i# S; ~2 {
$notice_content = get_post_meta($post->ID, 'notice_content', true);7 n2 ?, N# b% p, j+ ~. H
?>
5 O9 n3 U5 ]9 y g3 L1 @' U. I <p>8 K4 _6 o- k m$ K `7 j. l4 O' F
<label for="notice-title">Notice Title</label><br>
3 O. f" Q( h+ p+ @# b# A: } <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
4 w$ y" G) U+ N$ s. _( V </p>5 ~; n3 @) u% O2 c" |
<p># R+ ], }2 v% j% W
<label for="notice-content">Notice Content</label><br>
& N( U$ _' Y0 Y0 b. q; j <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
& D. G: x2 `, c9 t </p>4 M! h+ R# @6 q+ R% O
<?php% S6 J8 m* r0 ^# ?$ d; b
}
+ @4 C2 V# ?2 k- R" \4 o; d, k5 z- ]$ E+ O6 e; x
add_action('save_post', 'save_site_wide_notice_meta_box');6 k1 |" D) T! i2 N5 k/ n/ y
function save_site_wide_notice_meta_box($post_id) {
1 ^7 J9 }- ^, o, W& q5 P4 R1 [# [ h$ ` if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))# U0 D% B6 m% A6 s
return;
5 p8 G& a1 I! c% @! G if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
, U% Y6 ?! l- ~0 Y- c return;
, o! R( A3 `4 n: ]- d
5 t+ u1 b, q% {) `( ~( R if (isset($_POST['notice_title'])) {
, F1 k( _5 m1 O( }* l update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));+ A1 ]* T! O1 j2 d0 @) w) \
}& O' y, Q# C$ h
if (isset($_POST['notice_content'])) {# k1 u/ C* R9 z0 i5 r& b
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
/ n# a. I- v( V; G }
8 s O% H: j. c+ j7 L4 u" n v) l* u }" F. F3 ^& b; R& ]' F+ K5 h; l
```' f; w7 a3 n) n: W
g( k t, ~; i% g# s# q
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。/ G* o' Q: D6 ^6 E. r
7 [& c! Z) C2 w% d8 p1 t4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
3 F9 Z# x% a2 I1 Y% _+ v2 z b) S2 O: ^3 z' }, g% d
```; R5 ?, f" Q" h
$args = array(
; @4 }" [# A! b 'post_type' => 'site-wide-notices',
' W8 q8 Z% j. p+ e' u" {; M 'posts_per_page' => 3,
0 g( `5 g, w, _4 h6 }9 ^ 'order' => 'DESC',
! k) b. l2 Y" d5 e# @4 o 'orderby' => 'date'+ D( M' \7 Y! J8 d
);
5 j7 y D1 p$ m/ c, `: U" P $query = new WP_Query($args);6 U& ]% Z3 e! j$ x
if ($query->have_posts()) :3 h( L2 Q+ ]! O) a- ^' x" Y
while ($query->have_posts()) : $query->the_post(); ?>0 x& a3 Q' C* S V- k4 k# j
<div class="notice">, Z0 D9 J' ^: M" ^
<h3><?php the_title(); ?></h3>$ I# q6 H) L8 ^$ n; d
<div class="notice-content"><?php the_content(); ?></div># |" c2 }1 A; A8 D9 {5 A
</div>
/ |' i9 M( r; e$ V! l9 m+ Z <?php endwhile;
3 h* r& P* l" [' b) l& L. d% V wp_reset_postdata();4 b! K: f3 ?! \# R: \! L- k, V
endif;+ D" g" n0 S: x; k6 @
```$ s% ~3 |% h6 N" ^
9 X+ T2 v- [- Q2 V! e 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|