|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?- l R& o+ D) E: }0 i3 b
& Y) v9 ~6 d) }, H
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。. [! K, [- t( D5 E1 V
# p$ P( M+ p. a- F
以下是创建自定义插件的步骤:/ a. c, B4 Z8 i9 V: F% V
5 b& U( z5 ?! d; E- D, R: X1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
& Z5 H3 h; P% i% \
; G( _( }5 P3 P5 i; P- K ```
+ D+ z/ Q. ]9 z- @/ I$ N0 t' P <?php
6 @/ _$ X: u( c! S/ c( u /*& [5 X. i; h6 m, c* Y
Plugin Name: Site Wide Notices Plugin9 a! T0 F! z. V" [0 L$ @- L' f
Description: Adds a new custom post type for site-wide notices.9 E( b4 L1 v, `# s9 R
Version: 1.0$ v- w+ Y! T* M# V5 b$ K" y+ {
Author: Your Name- z( L4 T& X, {, m/ J' G
Author URI: http://example.com3 @ o! i7 M0 f) f, o4 F4 s
*/0 P0 ~$ d& }$ j2 ~& i
3 T) I2 @; f, N/ F# @
// Add plugin code here...
: J5 [) X. g# i$ j4 [3 n$ N' P ```1 C! U7 {' R7 y5 W% g
e- k2 R$ U7 B, h: Y) \ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
/ m/ X }( y4 Y+ L# e! j/ y3 T5 A; U1 D( c: G+ m
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
5 ~2 i9 p8 F, \+ J+ c8 b1 L1 ~# z" f+ ]3 X7 J+ I
```/ I2 s0 P) V. P- h* B. j
add_action('init', 'create_custom_post_type');
, O: k$ l! t" \# }0 [7 { function create_custom_post_type() {5 {5 g5 c' o1 ~ u1 E$ R
$labels = array(
% Y: {/ @& e) W1 M1 r* h3 e 'name' => 'Site Wide Notices',
- D# U% d) ]! }8 w* Q* p2 x 'singular_name' => 'Site Wide Notice',
/ V, S, S* q4 T8 U8 t 'add_new' => 'Add New',
7 w. N% j; C& d" @ 'add_new_item' => 'Add New Site Wide Notice',
) P1 j$ O& s; J) N+ X3 N. h 'edit_item' => 'Edit Site Wide Notice',
8 M0 P2 E% I& w; C7 K 'new_item' => 'New Site Wide Notice',, d+ _1 E/ \4 e* n5 T5 {
'view_item' => 'View Site Wide Notice',9 t$ ]1 ^- C8 j) O) s# k
'search_items' => 'Search Site Wide Notices',4 l, i; E5 ^9 T2 a
'not_found' => 'No site-wide notices found',
& a6 N9 A8 N" L8 W% ~7 c1 q9 c 'not_found_in_trash' => 'No site-wide notices found in trash'. p; {5 n" A# s( F
);' A3 g) v' Z5 s: J
) x( ?4 i% l; B# ~/ z
$args = array(
6 m6 h7 y$ ?7 l/ q6 j; @ 'labels' => $labels,7 w4 D K" \4 A9 `2 O
'public' => true,
# V3 n8 V9 t* [ 'has_archive' => true,
$ |6 w2 m. b0 `& C 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),& c9 x9 R0 R4 o1 O7 `' N
'taxonomies' => array('category', 'post_tag'),; Q: W' n, D6 U4 I+ R) i
'menu_icon' => 'dashicons-megaphone',
% A+ j4 W* E" {8 ^* B# t 'menu_position' => 5,+ K8 p/ Q; I0 A& O" @' r
'rewrite' => array('slug' => 'site-wide-notices')
8 h4 t0 X4 P; H5 F' w' C );4 w; T, a/ N$ }) p
5 x4 H$ V% D2 n. h2 }$ U
register_post_type('site-wide-notices', $args);
7 y/ p6 w% E/ |2 b2 ~( c5 H }2 |" i) j- P3 q
```
! y+ K) L3 `5 w! W) }" c, o( u5 j9 U/ B+ r& Y0 {3 s3 r; r
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。: r& w; M$ @0 I
" k2 j0 p8 _0 q. q3 F3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
8 f$ o( ^! f1 ~* q' R; E+ u" ~- Y
- u% ~; {4 Z+ `* Y# N- W- b: J2 t ```
}7 @$ }* p/ B5 d, V add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
$ f/ e5 u4 T( D. c5 g' T function add_site_wide_notices_boxes() {
; m# D# t$ F, m* }& T add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
: s0 c% _) V2 a! y C% a* u) B8 J: [ }
" j5 R6 S+ Z' F, C" M
7 C% j+ z+ W. k* ~* y6 ^ function notice_details_meta_box($post) {3 m" g0 {+ G6 @* f' i [9 {: `" H, N
wp_nonce_field(basename(__FILE__), 'notices_nonce');. l8 X( B( T. N7 A Z* G
$notice_title = get_post_meta($post->ID, 'notice_title', true);6 N: H# ^6 m1 f; x, l4 C
$notice_content = get_post_meta($post->ID, 'notice_content', true);: t' C: T6 r5 |
?>7 c9 S, n4 q& K5 a# E7 x5 a- z6 A
<p>
4 y# U! ]" ?7 I' p' | <label for="notice-title">Notice Title</label><br>8 \$ ]( U7 X: e+ ^& X, R* B6 {& W) |
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">1 p: F1 d: {" Z8 f
</p>: C- j' a3 U4 t" z! W
<p>! H; d( E" l: I
<label for="notice-content">Notice Content</label><br>' s! K' V H3 R! c
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>0 D* W% r1 z1 j, r
</p>
2 k* e: p) @* F3 Q' n <?php
5 Z5 \% x3 k, P }+ Y+ ~3 W% S, p- h
& f4 P3 f( q8 @/ ?3 c
add_action('save_post', 'save_site_wide_notice_meta_box');
: U( q% ]( R# s4 u9 \4 c function save_site_wide_notice_meta_box($post_id) {1 ]. h! ?; q& |. `8 ]
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))* h( n; S# F" A1 C1 Z1 w; p
return;1 P+ a0 ~% U; D6 a# l4 I
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
7 A7 a* ?! [8 L1 z" A- M" k return;
7 ^+ ~8 F9 F9 h) @3 j
4 P4 W" u2 B5 {5 s& q if (isset($_POST['notice_title'])) {
7 O/ C: ~7 A& ~ `4 U update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));9 f6 G# J3 X$ Z8 T
}
" u! ^! O _2 |" R' E6 B% u& A if (isset($_POST['notice_content'])) {
& q8 w! h4 n& B8 V' z! ^. D update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
/ y! ?& q$ H. i* j' U* z% u }
D" M/ N0 Q: v+ T0 g }
$ i" R3 W ^7 L6 @5 Z% N ```- T6 P: A0 i D5 {4 R4 ?
. v7 s& p7 S9 J3 u) W 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
& |1 G/ s U/ D* y* [$ G& ?# k; E5 z' j. M @) n J0 j1 k
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:2 J N) K" P2 u5 B# k
6 s4 d4 J$ d5 v g. K) h
```& E9 ]$ ]8 q1 U
$args = array(
+ ^. E. K1 ?3 a- U/ D 'post_type' => 'site-wide-notices',0 Z" I; u! }# J# ^0 d
'posts_per_page' => 3,
% H: S3 e+ o2 Y# a4 j 'order' => 'DESC',9 l6 {* @' h$ g, x
'orderby' => 'date'1 T6 @4 z% J ~
);3 Y/ }5 G- s4 d0 ~
$query = new WP_Query($args);
0 M0 f# P% s) j) C4 J7 v) Z3 u if ($query->have_posts()) :! R1 T1 }5 \- r4 G9 }
while ($query->have_posts()) : $query->the_post(); ?>
+ Q0 G3 u% P4 e7 U <div class="notice">( \; d5 {& Z: H
<h3><?php the_title(); ?></h3>1 ~0 U9 W; d& h; M3 ?: L
<div class="notice-content"><?php the_content(); ?></div>
* u- t, v$ n3 W: x) Q( U </div>8 {1 V" M9 t, i7 ?+ F4 o j4 g" a# v
<?php endwhile;
0 \" f8 s4 m! {9 F" _, c% B) { wp_reset_postdata();; e$ v( Q4 ?1 g7 \ z l' f
endif;
& A' F- I, I% K4 h1 b. W* a& ]# x ```
( X4 P- h$ _ V5 i
& d$ j& V; P1 A0 j 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|