|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
: }4 p8 b2 D0 ~. H5 W6 Q
: I; h, X# d* S6 ]1 K& b7 l; P3 V+ L如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
% U. o' g% b7 Y& t3 w
* Q# q! R1 |' W+ z以下是创建自定义插件的步骤:& D# ~/ x3 k5 x5 {# O, h5 y
. a8 D) P1 v( I
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
" n2 a# G, n2 D, ~( e& N! `3 S0 ]3 y% f: x- X+ k5 K+ G
```
& U7 _! K1 ]7 J- q6 A& i6 F `! e <?php, r* |; n! p, r" H
/*7 p* ?) E8 l- e) x8 S3 h
Plugin Name: Site Wide Notices Plugin8 A! i7 P7 g+ `0 s6 z
Description: Adds a new custom post type for site-wide notices.% ^) F) g$ u, H0 M1 }
Version: 1.0
7 T7 v6 B: ?9 m: n$ |7 p0 o Author: Your Name
8 v0 K$ E' R/ r, s% \$ F, ` Author URI: http://example.com
) C1 ]) P. B7 W: ]- @. E */
" Y J/ ^$ Y( i M4 W# C {! W0 n' D, q3 \+ v+ C f+ i
// Add plugin code here...
$ J7 g( l- s& F5 I2 O% Q ```
/ t$ s+ |9 L1 i) K9 L [2 n8 L' ?
9 J: a' a. L0 }: N 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
9 W9 r% F B ?4 b& a& C
5 X4 `2 w8 y# d' K; U9 J6 O# M( H) j2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
- @# |) U% I- e0 `
1 X/ P# v& h7 S/ \/ l' w% ? ```
8 n4 \# a( l5 q8 E% _! \( Q add_action('init', 'create_custom_post_type');
% v4 y4 u+ s+ T2 s# f& ~; p$ y. z function create_custom_post_type() {" O; }" r. S2 q. f
$labels = array(
( L( e% b- g* g 'name' => 'Site Wide Notices',
5 N) A$ }1 ~7 M& f1 i* L 'singular_name' => 'Site Wide Notice',6 N) E- J& @; U1 V
'add_new' => 'Add New',
7 n/ ]: ?" M! `6 m 'add_new_item' => 'Add New Site Wide Notice',
; T* S* b% l3 Y: ^' y5 e' @/ I/ C8 x 'edit_item' => 'Edit Site Wide Notice',
. X7 z8 J$ I Z! o: E- p5 N 'new_item' => 'New Site Wide Notice',9 c* I/ O5 h; d- z, k4 [3 r
'view_item' => 'View Site Wide Notice',
A+ }$ z: ]6 y4 M" A3 r 'search_items' => 'Search Site Wide Notices',4 J5 H/ S9 j/ k* F9 b5 s! L
'not_found' => 'No site-wide notices found',
6 x4 l+ `1 o& |# w 'not_found_in_trash' => 'No site-wide notices found in trash'
4 [3 T6 v& h* ^- Q; e" Z );6 Q/ U! x- `! c% b& V% s$ ~: i
. p, l3 Q1 C s2 w" m- B( t0 I& |
$args = array(
8 m* e3 {( j; P3 p; ~8 q0 e$ X 'labels' => $labels,
! ]4 x2 X; {1 a7 k) A! v# u0 r 'public' => true,; H; F, c$ @: l$ r+ N
'has_archive' => true,. ]- `2 D/ w5 A4 D6 z) j
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
+ [" S# }# l$ B4 u9 O! W 'taxonomies' => array('category', 'post_tag'),
1 S- n K, V4 p- L 'menu_icon' => 'dashicons-megaphone',/ O I% F) W5 o1 H, ?, j3 R/ U6 K
'menu_position' => 5,
+ U0 ^- e! U2 w0 y0 b7 s0 O 'rewrite' => array('slug' => 'site-wide-notices')
4 \$ g! ~( l& u/ J6 |# V7 ] );
% k3 v# G. u! P( J4 o4 p$ I$ ]4 k! |. a. h" ^
register_post_type('site-wide-notices', $args);$ E- ?/ U, t6 }$ i6 [( v, L& G
}, q. K. a8 w4 ]) G
```; J( T! r, |8 h. @/ D7 E1 @8 k( q( L
5 [/ C9 W; E: Y# \ 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。" r( M# V, @7 V! K6 O8 W/ T
1 q) r; H. l Y3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
4 p- Q: [5 s) Q @0 L6 m
) c& y& y: K4 Z/ L% A P ```
; Y5 W+ E; a* {3 ^: a1 Y% I add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
- }/ q" j9 `; q, k/ S* Q' r function add_site_wide_notices_boxes() {6 O6 b# g/ L9 z
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');0 V c1 N, u! n* t" `% W
}1 Y! X$ J1 y8 k1 M0 |9 E; \
, E- v& Z6 ~0 }# b
function notice_details_meta_box($post) {- c' M" N% d3 r. j0 ?# U
wp_nonce_field(basename(__FILE__), 'notices_nonce');
/ U- O" g/ F# n2 ~% m $notice_title = get_post_meta($post->ID, 'notice_title', true);9 L. W. H% R0 Q
$notice_content = get_post_meta($post->ID, 'notice_content', true);
4 h) T4 ^" r5 u ?>
- U$ }' \) g: [ K1 O2 Z0 Z: g <p>
$ r5 ^. s0 L8 i, v; R <label for="notice-title">Notice Title</label><br>
2 g& @3 _, L' k1 \ <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
, G! i: P8 V% d, T" i </p>* k: T% L! m' m8 a( m/ J' m7 v6 B
<p>2 t+ g/ f- X2 A- }' n: }1 F
<label for="notice-content">Notice Content</label><br>1 Q% X2 f2 w$ E( s9 s
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>0 A$ B; U5 A6 L0 _2 Z
</p>1 P! i/ `. T" c# x
<?php
& A/ s: ^! K6 a }- z8 H% x2 k; r. `$ Q5 b# T X9 `
. s( J2 t' g" r7 U+ y% K6 [6 J add_action('save_post', 'save_site_wide_notice_meta_box');* l/ f- Z+ T7 Y2 w% E6 q) S9 w
function save_site_wide_notice_meta_box($post_id) {' F9 v3 x# T6 S0 ~! l& G
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
0 e/ ^: j% S& i7 ~ return;+ y0 k3 @5 k' |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
* Z+ c! h' K% ]/ a, N return;
+ n/ ]6 I# D+ s3 F% g0 y3 v7 q0 K. P# @! I& I& v' H+ Y3 p. X
if (isset($_POST['notice_title'])) {
" K6 V% m$ t) _/ X update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));( f% t+ e& ^* t3 m* E8 G
}
+ v s w" V" x& O if (isset($_POST['notice_content'])) {
5 {% C' A9 d0 G r" x2 V$ ` W update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));. C+ C8 |) b4 F, X4 N+ m$ T( X3 u) _
}5 N# W! Y* [7 N4 F0 B
}
& Z+ l( U0 x- T X2 D z+ u: S$ P ```0 T+ j7 v& Z2 W, b- Y5 v- {% L/ L
. H. Z& f+ I2 ]9 _- c' h
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。8 `8 ?" l) H; w- D
4 o- U' J" k* b
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:' c S# }8 z7 F. N/ i& j) |
, n2 f! j) B/ f* c0 _9 d" E
```! I0 Z+ p0 v( Q+ _9 c F! S- x
$args = array(
6 y; A# \2 [ I 'post_type' => 'site-wide-notices',
% P k+ C0 F; i3 Q M( Z4 g% n) L$ z 'posts_per_page' => 3,
7 v9 Y( I; h) C; i 'order' => 'DESC',, o( Y6 }6 C5 x: ~" i) p# J) o. ?
'orderby' => 'date'' U9 n7 i3 W' K/ _ }/ ?1 A
);
2 Q# P2 T1 Q4 J- Y: f& C $query = new WP_Query($args);4 w O0 t7 b0 x0 ?
if ($query->have_posts()) :# Q( Q. Y4 Y8 D U
while ($query->have_posts()) : $query->the_post(); ?>5 [2 x+ i x/ K" K
<div class="notice">/ ^6 v4 F/ ^* l; t; Z3 e! M2 J
<h3><?php the_title(); ?></h3>: ]5 Z# r3 L8 L9 V4 T
<div class="notice-content"><?php the_content(); ?></div>- r! I, P# v' @4 R+ N
</div>; i* h3 h$ w2 O% m$ w, b3 p _
<?php endwhile;- T" q) L$ E9 P
wp_reset_postdata();
4 b, I1 o2 {2 ]* S- N8 V endif;# H+ X) X0 {3 x+ q
```. ?% z. }4 ^- `+ }5 d- W, P
+ b7 ~0 R2 y0 m& A0 _& b' u
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|