|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?7 U/ o: y0 H+ i" K6 Y$ I
5 f5 Y- _! }( {- }如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
, c$ f% ]4 j8 [" i0 |
r: j, s5 ~4 F; o, z/ R7 Q以下是创建自定义插件的步骤:' h$ N! [6 _' v8 A% S
3 M; J" }1 V9 L3 L+ ^1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
5 _: ~, {$ [; E7 i( f0 a* F9 k* ~3 f0 \$ {
```
% Z% l2 |! Y8 M5 z3 u <?php
" E ?; K Q9 }- m9 n1 s1 x /*$ C) D% v* H6 [$ t8 E& _* v' H
Plugin Name: Site Wide Notices Plugin
- {( U; d1 r0 \1 Z/ |$ y& s& @ Description: Adds a new custom post type for site-wide notices.4 ~ g4 Y5 t S+ r3 {4 E
Version: 1.0
' s4 Q3 K' V+ u8 c* t" C+ e! j8 a Author: Your Name { K0 o3 ^# P: k. j
Author URI: http://example.com! h' t0 G& ?; q ?0 }8 X3 O
*/
% y6 e# a7 k% H# O) k, `9 ` k) _& @( o. r7 ~
// Add plugin code here...
) L9 N3 g: w* p* R- K ```4 D) _' B& o6 L1 k) ~
1 n- o8 d# Z! p9 g" s- i$ U
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
) v) {9 E% }& Y4 {7 s$ z( N! D& y2 V. `" n8 o
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
: K8 D$ P8 f' F2 q' y: K! u: E4 i: m* J" o
```# r+ m9 `* ^2 g3 P1 g* d w6 g
add_action('init', 'create_custom_post_type');9 H% Q1 d# b$ x4 S8 ]
function create_custom_post_type() {
* b9 V1 {# V" |; ?. o2 F, R$ H $labels = array(
5 U3 Y; k( o" P7 \% U0 z 'name' => 'Site Wide Notices',
6 O/ T* H) W' Y) T5 W* Z 'singular_name' => 'Site Wide Notice',- V9 w8 B0 M2 j, M
'add_new' => 'Add New',
8 x3 {+ z p% q9 |3 Q& r 'add_new_item' => 'Add New Site Wide Notice',
; f1 i/ \, }5 D9 @ 'edit_item' => 'Edit Site Wide Notice',4 b& J3 P4 V {' a
'new_item' => 'New Site Wide Notice',8 R1 g1 h# t0 |1 C1 |
'view_item' => 'View Site Wide Notice',7 N* }$ u, @' F2 _' S
'search_items' => 'Search Site Wide Notices',; U2 S j Z( c
'not_found' => 'No site-wide notices found',
1 H1 j( T! n5 r 'not_found_in_trash' => 'No site-wide notices found in trash'
; c" D# {+ R; R2 n5 ~6 f );- U \; O- F% A) J3 g2 Y
7 r) u) ^* L5 E5 ]8 h& V
$args = array(# j: ~& L v3 w1 H6 I4 j; k
'labels' => $labels,
7 ^0 t3 ?$ c! H" F3 F U 'public' => true,
/ |% R# `+ _2 z a5 [5 l 'has_archive' => true,% s* R- Y) D6 U2 C n6 w
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
3 t! s: s& }! D v 'taxonomies' => array('category', 'post_tag'),
0 }7 d. l( H, p# F2 O 'menu_icon' => 'dashicons-megaphone',
' I/ f L+ r4 Q- f 'menu_position' => 5,
' C/ r( b2 E e# b/ d! g3 k& A 'rewrite' => array('slug' => 'site-wide-notices')! m& j j7 S, w
);5 g! w$ r7 Y, o6 H+ `
: Z$ E/ f( ^- S$ ?3 m
register_post_type('site-wide-notices', $args);
, K3 k& J- |+ u }
- g* N% X' q* M" ~+ b+ z. Z# T$ P ```: K U8 _' u& ~( M. O+ a9 F
$ D& T2 o9 d: R% A" X
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。9 ^' m. g9 }/ L7 X* r) X
1 a* `8 F# ?3 }' {. ^; v
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
X( U9 p# D: ^9 P; j2 b8 v* \- w
' [! l3 k4 ~' r; w& a" A ```
" x9 |0 B: }& ]' a( Q8 f) A* [ add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
- K, M( N4 S1 J4 C( f. [% N+ z$ `* r function add_site_wide_notices_boxes() {
6 l. ~) N7 y2 E, b) H' Q L add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
" \3 ? [' U( f+ D" D2 d, P }: @" ?- c; B J) j5 z
( T* {# i% K8 r' N% L# @
function notice_details_meta_box($post) {6 c8 C8 [. I/ j$ \5 ?" L' X( s
wp_nonce_field(basename(__FILE__), 'notices_nonce');
' a& L h: ~5 w $notice_title = get_post_meta($post->ID, 'notice_title', true);2 [8 m8 g& r5 h5 C
$notice_content = get_post_meta($post->ID, 'notice_content', true);
; `4 x% R$ M2 [8 A- E8 D7 l! P ?>
) @( ?7 h3 F+ {5 u8 w6 Q# l <p>9 g+ {8 y. u: H; c* J
<label for="notice-title">Notice Title</label><br>% }0 d( Y9 w. Q; [! o
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">" N* V ^* o2 X# c1 h9 `( \! C
</p>
* _7 O- x p8 }+ k2 `' t+ \ <p>
+ `# q! _" V5 N: b& p+ ^ <label for="notice-content">Notice Content</label><br>
+ }8 w# R) \# E Z& a <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>" B. D) U! A/ i. R) _( g
</p> _& l0 F7 o& b. g. \: y& M+ l3 f
<?php
& L- m! }4 T$ W, Q* M; F, i }: U' R# p9 x/ D
8 J9 o8 E& \' x
add_action('save_post', 'save_site_wide_notice_meta_box');- |8 N/ F9 @7 e9 o/ m
function save_site_wide_notice_meta_box($post_id) {
# G, z; J* ^1 N4 Y6 y if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))/ Z5 `3 o8 \! y3 _* s# Q
return;% Z" t7 g% D7 @, ^. ]7 h, s
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
. i* U+ T" ?( W3 T. M return;& |$ E& o9 [( t6 c# l
, O3 l+ D' n! e- P d7 G if (isset($_POST['notice_title'])) {
; y/ a) _/ T, m& t7 c, \, M6 q update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
8 K2 [( f2 V R- @# w8 t3 u }! h1 g& x. D$ O7 P o3 y0 k$ @
if (isset($_POST['notice_content'])) { @; \$ g9 O X. x; y5 \
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));& C* V0 r3 p7 Y& k* i
}
. U) |1 O! G- u% V( ` }! B; o8 L0 }" S
```7 ]( j% X3 v. Y1 F
- `- Y5 I% h& m! G$ I- ]9 X. q
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
/ S2 q' m3 ?) R
9 u1 ]& ~, E, k7 z+ T1 G0 h4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
5 U6 e7 a7 t0 }8 n6 H/ G9 Z3 V, v1 b+ i6 B5 N* y. O4 D
```
4 P- Q# b4 S# p0 r $args = array(* x, ~& R' D, n9 @
'post_type' => 'site-wide-notices',% V, z% o0 u. s! @, B6 f( B
'posts_per_page' => 3,
8 T( E' g7 {5 j7 W+ j/ {6 \3 \: ] 'order' => 'DESC',
6 K# s% r; ^4 p+ H8 b8 l 'orderby' => 'date'
2 J! u% A- T0 E" K. w8 Z2 q0 X );
* N5 K1 l ~+ A5 z3 J $query = new WP_Query($args);
: `1 _- h# f6 F8 V! F! ~ if ($query->have_posts()) :
% f7 x" ^5 y7 q- ~+ k! ^* { while ($query->have_posts()) : $query->the_post(); ?>
& A' C/ r: i) d) f5 V <div class="notice">1 J) p' ~8 i m3 Z& ]
<h3><?php the_title(); ?></h3>8 t8 K2 w$ x i& A; B- I- j& h% ]
<div class="notice-content"><?php the_content(); ?></div>
" O) W- E* f# F' g </div>
( Q/ {. s: u* q$ U <?php endwhile;5 x# |4 {' \- ~/ k
wp_reset_postdata();
8 X0 X e( B/ K7 v endif;3 n9 }$ v8 {+ |4 i7 d# V
```
1 g; P% m& M! x" A
5 N) ?/ H# L: f0 m; u% ] 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|