|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?1 B' |3 V' @, Q& u0 [$ m" @
1 C, G. u4 k) l' U
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。" s( K2 C% h2 Q
4 j2 }) `1 X3 J$ x* `; D以下是创建自定义插件的步骤:
, V: w# ]7 B6 v2 N- F1 b' [2 V* h5 O' W$ j: N
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:; ]; m% e x& {
- o6 H7 [# P9 I$ T8 n ```/ f/ T4 b F7 s9 c
<?php
; Y: E* [0 x2 T5 g5 p /*
) a- |0 N/ {2 z6 b' S7 g( P Plugin Name: Site Wide Notices Plugin
2 N. H4 V/ Y3 {7 o% \ Description: Adds a new custom post type for site-wide notices.
# a# E, ?' v/ _5 L& r$ R& Y! B Version: 1.0
r" L' L# D' r3 ^8 Y) F Author: Your Name* ^! ~9 V! C: i7 \5 n
Author URI: http://example.com
; j c: R9 G) ?0 N. s8 u- D */
) X+ l# G4 y! d" Z( V& h" ^" B5 @: h2 f) G u
// Add plugin code here..." ^; {. t& w' z
```& S! ~0 u' n/ F! _4 Q
! Y; j- p% O! i& w 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
. u9 e' |) y6 T j1 G- r+ w1 ], V- ]( K/ m ?8 O: a
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
4 z1 u5 m1 Y8 w& j; X' x) v
; r- e* `' t$ q! ^- f- @ ```
1 |4 h" @3 Z i2 s add_action('init', 'create_custom_post_type');6 t5 w1 d. d, f, D8 l
function create_custom_post_type() {
q( B: C* R/ l+ g" I+ w& G3 z $labels = array(, O) `" f; {. h
'name' => 'Site Wide Notices',2 Q& N. `, ~( w4 Y0 R! v3 H
'singular_name' => 'Site Wide Notice',) b+ n) Q. `( u# O$ n) c( V, L
'add_new' => 'Add New',
2 Y9 D+ A2 ^( p( T- R 'add_new_item' => 'Add New Site Wide Notice',- Z( \8 V2 G" u" d" ?" d5 J& A" p
'edit_item' => 'Edit Site Wide Notice',2 Y0 x: K8 L" ?. ]# x
'new_item' => 'New Site Wide Notice',1 F) w$ n1 ]& I+ H9 E Z& S
'view_item' => 'View Site Wide Notice',% H4 X9 ]- b, w8 g
'search_items' => 'Search Site Wide Notices',
; ?. @. Q! l8 k" w8 q$ z 'not_found' => 'No site-wide notices found',9 n) h& l8 ]) l6 h
'not_found_in_trash' => 'No site-wide notices found in trash'
]$ N, x/ M' V8 h );- S, p S, w0 r/ Y, o. f* G
$ p. ?( c( {# }- F/ f& F! i $args = array(
4 P) E) M1 e- Z& O9 ] 'labels' => $labels,1 w1 }& S" C# J7 j" P- S
'public' => true,
$ `4 L- q* E" O1 C; x/ W 'has_archive' => true,9 K; G: l* o- P' Y
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),) ]- f/ r3 @) P* C# S2 N0 b
'taxonomies' => array('category', 'post_tag'),
" K* M. ]7 s- u" J; l 'menu_icon' => 'dashicons-megaphone',& b2 W" D7 `$ J' H+ d
'menu_position' => 5,
8 C5 F: \8 g9 h9 [+ h- Y 'rewrite' => array('slug' => 'site-wide-notices'): b* M- d' d1 b5 `
);8 U3 E, W, |. }7 F
" P: w# ~% m1 ^9 u8 u
register_post_type('site-wide-notices', $args);
5 n5 @3 Q, ?. @# x4 q }& p% \7 z6 v' Y y5 d9 q
```( F0 L4 m0 l/ e) @
) V# S9 o6 |* l3 c 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。8 [4 {) {6 q8 A1 _/ K
4 Y+ [2 k& w2 W/ v2 D
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
/ v# g. p0 p5 @( J* F; U2 K0 s
& }; N/ k: r$ K @* n. d: i$ o ```
: u6 T2 R9 L5 l: h5 N1 z add_action('add_meta_boxes', 'add_site_wide_notices_boxes');' @; H+ v' p3 C" S9 [1 g
function add_site_wide_notices_boxes() {1 [4 L( F2 _: G x; Q$ m( T
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');1 m1 T' D1 r6 v& N' @( }: K) F
}
6 U6 @1 [; }( V' F0 C/ P$ }- K! b: l/ c, l6 d5 n1 v
function notice_details_meta_box($post) {' g+ s: k; _) o/ q. ?! ^- m
wp_nonce_field(basename(__FILE__), 'notices_nonce');
+ P, G/ @1 o. P; z $notice_title = get_post_meta($post->ID, 'notice_title', true);
9 P, E' Q- Z; R$ o3 X/ ^* l $notice_content = get_post_meta($post->ID, 'notice_content', true);
0 o' F3 H4 F3 Z! F0 W ?>+ J$ T- l$ G1 O" Q
<p>
8 y9 ~5 p) C+ o- m e <label for="notice-title">Notice Title</label><br>
3 c3 |& Y# s- ]5 J2 W! V8 v- i4 Q <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">* d' a, _1 `; m# N
</p>
2 O8 \% u2 p: X. x/ d# H- H <p>
" ^7 z$ Q) q" N" e <label for="notice-content">Notice Content</label><br>! r/ h% q3 o# g5 w, ^3 N9 }2 l
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
( R L( Y' Q2 i3 P; Y2 X </p>2 T2 J" l( \, ?7 C' i2 w
<?php
; S5 E* _# B. k0 w5 i0 p }( \7 y) T+ g5 e$ l( q* @, C7 y- a6 ]
. t; S' y) w) c8 j2 L, }) o# V add_action('save_post', 'save_site_wide_notice_meta_box');
, o/ E) a E2 J' W0 t function save_site_wide_notice_meta_box($post_id) {
7 y! n$ I& B4 J3 d* x% F if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
, {: ]. a: Z: o- h4 W return;) {' b, W* X& @* g
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)# ? ]! T. E9 x5 I, _$ _, [, m
return;6 c' t% V0 Q( c
$ d' ]' c; @ l/ R; K) f
if (isset($_POST['notice_title'])) {
5 p5 f) r% D3 i$ Y' l& k1 M update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));/ N; I" L$ f2 y
}
# \" p* _1 g* r8 i+ P if (isset($_POST['notice_content'])) {- @2 b/ g* M3 ?8 `! x
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));6 u3 n# m* j% }& O' E
}, `+ i" Z0 C2 ^& V _
}$ i. G7 n$ W: z1 \+ L- r7 Q D+ P
```
% M1 W# X6 a7 @, J7 z
% u# B! N1 n5 \1 e3 n1 s& B 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。$ p0 | ] ]) i4 a9 T7 |5 V" k
- `0 E: _% [6 F" }# o" _
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:" q) S D+ S/ S* V
+ U( c. ]8 K& L& P5 |
```
, n! b- R# \5 [5 \ $args = array(
8 H# r- [* P( O 'post_type' => 'site-wide-notices',
) n/ r/ p8 b9 A' H+ d/ g 'posts_per_page' => 3," n+ C9 e8 s4 @3 B
'order' => 'DESC',, }6 ^) J- J* y9 K
'orderby' => 'date'5 h; Z ]& Q, g" V X8 M1 t3 O
);
/ w6 c# E3 C' P o G, K3 @ $query = new WP_Query($args);
8 S) Y! N9 g0 Z# Z8 r+ i- J if ($query->have_posts()) :
, }2 |' z! H" o- o$ R, d: U3 ~ while ($query->have_posts()) : $query->the_post(); ?>
z7 I* m* H2 |& D <div class="notice">
, w" V6 J8 ^) M, m: h& T <h3><?php the_title(); ?></h3>$ u- q' X6 \3 R: W+ u
<div class="notice-content"><?php the_content(); ?></div>0 M3 W7 M* J) z! h
</div>
0 v; l6 ]5 E8 A8 z8 { <?php endwhile;
3 o2 P2 c( o/ d4 I. N wp_reset_postdata();
, N2 z$ ], ^$ D endif;
! G! s4 |. X, F6 ^# T/ |" U ```5 w1 _: o1 c( G, i- l" k& l
4 M5 ~$ |& X: ^- e' S 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|