|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
2 [4 V( r5 ?1 L% y- [& w( [1 W3 W; m2 l$ @: D5 A
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
, }. }$ o/ D; A" z& P- Y7 f
- w# N) x# T& D1 P以下是创建自定义插件的步骤:4 k, h7 E5 r) v
# S) i [' g' ^. K- I. H3 }
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:4 x1 f1 l% g" C, w. ^6 P
: }2 v' }+ l6 D1 G1 t& ~6 `& K: Q ```4 P7 W( }- c w6 @+ @4 C, Y
<?php. z+ ? _; E) l8 M1 O. o
/*
' _1 G6 A9 H9 {" ^0 R& B. A Plugin Name: Site Wide Notices Plugin
: z/ e- J" I0 @$ M) o6 y Description: Adds a new custom post type for site-wide notices.
8 c5 _5 y2 Q* B \, h9 J0 O! t Version: 1.0
( b- ?: a" Q* ? Author: Your Name
; b( O1 i( X6 z2 @: q5 w; O1 s Author URI: http://example.com
7 F! B* w7 @9 Y/ P */+ o+ J; a" O2 ]8 _
6 X( i8 A( g& G/ t
// Add plugin code here... y: a2 K% B2 Z1 O" _
```
$ M4 g6 Y$ ?# S% `4 W0 |& N/ z8 a6 f& l6 G# D
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
# D! `8 q1 W& @1 t3 b( a
! c! b. z- X$ D9 ^. E2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
! h9 q6 Y( E" A
) y3 m7 i; r" u ```
6 o# O; c7 ~2 J9 G7 {: T. B$ r add_action('init', 'create_custom_post_type');
, j+ Y5 `. C! z7 d5 A function create_custom_post_type() {, ~, D0 V3 l) M+ z5 V4 E7 {
$labels = array(
, s$ I, ?8 F$ O$ B: a) | 'name' => 'Site Wide Notices',
0 Y) m! \! x2 A+ ^5 F& k% T 'singular_name' => 'Site Wide Notice',: ~# t3 Y' T+ {2 E6 o, l0 o$ I! d
'add_new' => 'Add New',
. ?$ B% V7 {6 b& X% _! H3 X0 v 'add_new_item' => 'Add New Site Wide Notice',
! f0 T5 Z' R1 e4 ` 'edit_item' => 'Edit Site Wide Notice',4 p+ J; ?* I; m5 l4 N0 G/ D
'new_item' => 'New Site Wide Notice',4 m) i |! _. O- L# {
'view_item' => 'View Site Wide Notice',
8 W q6 S& ?. y" r3 { 'search_items' => 'Search Site Wide Notices',( V# |9 T6 ^" @5 s# c
'not_found' => 'No site-wide notices found',
' m9 P1 u; X0 L/ S* }2 D* q# B \: } 'not_found_in_trash' => 'No site-wide notices found in trash'3 [9 S' V; X+ @# C `
);1 `6 N# l. p7 ^0 m% y
' z9 T% \$ _0 D M' M/ f
$args = array(
[# v" x7 Z/ P; p9 Z 'labels' => $labels,3 T$ D% i! y0 T
'public' => true,, z! Y3 Y9 O9 R, e
'has_archive' => true,2 S% p) q# L" ]" x
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
5 |; N' {1 L- m( D 'taxonomies' => array('category', 'post_tag'),
/ v9 _( t; Z# R& W5 { 'menu_icon' => 'dashicons-megaphone',
; [! ] n2 V. s 'menu_position' => 5,$ ?; F4 i7 J, j) E* S: L H
'rewrite' => array('slug' => 'site-wide-notices')
0 w+ }. I0 T' f# p& O. v' E );
- J% }# w' ?, k8 O! `" D
2 O( f) b0 M; K register_post_type('site-wide-notices', $args);
8 v! x9 a& {* Z+ h, ]2 t }
' u5 p4 |: o0 w F" l8 V ```. o% B4 W% |& ~- }
; c. C7 g! g9 ? H+ q& Q
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。2 \+ C, e, Z8 Z+ L
+ p. _& f0 x& i& C4 {* e
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
- x) Y3 F; f1 A( D* a* z5 V2 j! ~8 p7 O! L; H0 H6 C# G5 B
```1 A* `% K' {$ N% p3 [) Z
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');( r' w2 Z2 m" y/ B/ C# `
function add_site_wide_notices_boxes() {3 y+ J/ B9 Q! Q+ t, o) I+ j6 z' ]$ [
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
. G: V4 V; q, g! T }
; O0 m2 ?/ U1 p5 Q3 a3 x. i+ E; r. ]9 r2 d) P6 y- Z# b4 [
function notice_details_meta_box($post) {
" u4 Q# E. _% B wp_nonce_field(basename(__FILE__), 'notices_nonce');2 u" w1 X+ w7 {; ]
$notice_title = get_post_meta($post->ID, 'notice_title', true);
- F1 K! P7 T9 E8 M' j $notice_content = get_post_meta($post->ID, 'notice_content', true);
# J) I4 J1 n- k6 D( m1 O% { ?>( ~5 P- D( W( y; M8 v9 y
<p>
6 S9 H8 d4 a+ S <label for="notice-title">Notice Title</label><br>
1 {4 r; {, m0 k" a6 f$ E Q7 T <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">5 w8 G5 s) y$ n3 j% H, s+ H1 W
</p>
! U4 H9 ~ d6 q/ {# r9 A <p>
, N# J/ b" x {* P4 _ <label for="notice-content">Notice Content</label><br>; A, y" F# }8 H5 b
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
1 l7 Q9 I3 j2 d# E1 x </p>, F: D0 P" c! K9 S
<?php
' n" T4 a% Y' }, J q }7 \$ _: ~# F; J: C' X/ G
* d+ R9 A$ G/ H" M5 U9 N' v
add_action('save_post', 'save_site_wide_notice_meta_box');
2 t7 G, ^) ]1 I3 e$ q$ h+ p function save_site_wide_notice_meta_box($post_id) {
1 v5 f- P5 I$ r7 `( D if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
6 A* K- h/ n8 D( q+ t5 { return;0 e5 L, q% V$ k5 [. c5 f: f
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
" R/ ]8 ~6 P% \% i2 F( S6 E return;5 m ~8 F3 n0 _2 k, Y5 s3 t( R
V9 _* \: l5 s4 I9 K% k
if (isset($_POST['notice_title'])) {
9 E( ]5 _ _8 n4 y9 h r- p5 I update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));$ L7 ~) H: i# E* j E+ e
}
M$ Y9 b& H' C if (isset($_POST['notice_content'])) {
" q% ^7 E$ u* o% T0 p/ N# q$ R update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
+ w) F; Q( l; k/ b% ]- z+ ~1 F2 c% L }, E9 ]& b9 W: ]/ W
}% ]/ X# z& O }2 G+ G/ D' w! V
```9 `* {; ]/ J& G `; r& ?
$ e& @! L8 q5 b/ N/ F% @- } w
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
" V& i& d9 D& _ e) J
6 E6 q1 l: z7 G( U |4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
, Z+ n7 ?7 s2 f
0 A) ]- s8 w* s S ```+ T) F: s6 M6 g) p- G
$args = array(1 ]1 x3 i- ]2 z' A3 y
'post_type' => 'site-wide-notices',. ^: d0 E2 n: U9 g
'posts_per_page' => 3,
7 q2 U8 C; G+ M 'order' => 'DESC',
6 K( e, r- z/ c( o- v5 [& l 'orderby' => 'date'3 p( R7 x7 a8 W& q) A# `
);
3 G) g( P# g; _6 _ { $query = new WP_Query($args);
7 }& s; x, o2 w if ($query->have_posts()) :0 o$ L- g3 E: G
while ($query->have_posts()) : $query->the_post(); ?>7 B2 U0 Z8 j2 t- k1 w
<div class="notice">
! c# p# e; t/ K+ q' B' d, f <h3><?php the_title(); ?></h3>, W/ Y ~) Y1 Q0 V) `5 C2 J
<div class="notice-content"><?php the_content(); ?></div>, U( m& c7 l/ m% N# i8 f6 I
</div>
1 F$ K$ b, a7 B5 K% R2 W' }' c5 y) `, _ <?php endwhile;; @ J/ x; O2 x7 U; z! ]6 }% R
wp_reset_postdata();
" U7 V5 F: Y: q% Y" I endif;; }: p8 O/ G" c4 o* \: V
```2 d- G( c/ b8 C
7 W1 R# Z/ a: e0 u1 X" y+ E
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|