|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?) M, H r2 M0 ~7 E
$ t0 l1 \: k7 N/ V如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
& }" P7 q/ h7 G3 f3 N& I0 x; L6 i
* A9 x% l& K) v* a9 a5 {' e9 B以下是创建自定义插件的步骤:
( `& V' [4 m; }" @: @, x
: K# U$ G9 ^% }4 L# B; z; R1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:% H7 D9 @/ Z* J, F6 {
% G# |! ]" t% q ```
0 H0 w. C1 {9 f# w <?php
/ F/ I7 V; u0 `/ `, G /*# [3 F- g) B* Y0 y" X' _
Plugin Name: Site Wide Notices Plugin
* a- q, }! t! \/ O( h& D# r Description: Adds a new custom post type for site-wide notices.
; Y. t. b- c/ N9 [0 C Version: 1.02 b- P1 ^$ T f7 @" x
Author: Your Name' T ]- M1 j' e9 x0 ~( T
Author URI: http://example.com
% q& k+ C! Q* v; I8 W7 o6 D */0 }" M1 H f% b0 T3 A/ N& x
2 c! M3 Z$ p, A( Z. m I5 z // Add plugin code here...
* J5 \3 ]; n, m* \ ```
6 ?5 o. o" k) `5 N* ~; h4 E# x2 {; I K* [; W
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
: L+ `( U8 O# X g/ ~9 o( _$ b, p% b2 X- }0 ~
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
; s, v( ~ R/ l7 Q: B9 }/ j! A
% j* E& \. k1 O0 t; ^' x; f ```7 [, ?) B/ k2 T) ^
add_action('init', 'create_custom_post_type');$ N" E2 \, g$ W5 G
function create_custom_post_type() {
& \2 d$ ?2 w; @2 L* N& ]4 Q- _" f" G $labels = array(
1 b5 \" `( D/ m9 ~- G7 N3 ~ 'name' => 'Site Wide Notices',! t) [5 ]& l- N. M9 c- @
'singular_name' => 'Site Wide Notice',
! C! R0 X) t, Y T; C) D) p 'add_new' => 'Add New',
/ q! n4 p' E+ g# ^ 'add_new_item' => 'Add New Site Wide Notice',
4 }0 h* ?/ V) n+ R* J1 y6 G7 } 'edit_item' => 'Edit Site Wide Notice',, x& y! N0 ?7 I9 _: q) t1 k3 y% k5 c
'new_item' => 'New Site Wide Notice',$ k6 D+ ?" n" V+ U
'view_item' => 'View Site Wide Notice',
4 m+ W C" H; G4 n/ _, j" n 'search_items' => 'Search Site Wide Notices',: ~3 F/ |3 v( d( }+ x! b
'not_found' => 'No site-wide notices found',
7 U9 P0 c# R/ e( J/ ^( k 'not_found_in_trash' => 'No site-wide notices found in trash'# @ F( e- I6 ?* k) C( B; L# s
);+ u4 B& \" B; c# M/ E; A8 E( j4 j
; S' A3 _1 o N $args = array(0 p# C8 ]0 a4 h5 Q
'labels' => $labels,# V- \3 G: I' H( V
'public' => true,
4 ]& D7 z7 a. h, D: x' l" \$ Z 'has_archive' => true,
2 u6 J. z/ Z" Y7 y. {3 m6 R* G 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),( @! ^1 ? g3 ], v1 q
'taxonomies' => array('category', 'post_tag'),
) |2 J' s+ ^( I7 Q 'menu_icon' => 'dashicons-megaphone', V' J: u$ C9 d# ~. u; w0 A% q1 }, n
'menu_position' => 5,6 {2 B! o( B3 y7 Q8 N( P
'rewrite' => array('slug' => 'site-wide-notices')% p- r. T( L4 h/ p1 R
);
3 Z/ B" C$ U9 f/ y
: B6 b K0 H7 h8 H8 Z5 l register_post_type('site-wide-notices', $args);/ v) L& E5 H7 H: N. C- u$ [
}
% g3 u r5 N' K) j c0 d/ v: a ```
$ R# e+ B1 f/ ?0 m/ [ h% `) ]# P: q1 I# w0 y9 s7 k4 V
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
5 r2 C% L8 o+ S
! |% ?0 L- |, k |; |1 u6 \8 |3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
, E# y. s7 p! z% T
`' @/ B5 y% ^% J5 I& z; | ```1 Y: E5 N+ T1 j T6 l+ n. I
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
- h8 H; T; D9 g4 m# U function add_site_wide_notices_boxes() {7 i, @$ O3 j8 y" U
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');* x( |" G7 w# p: L/ M9 O
}* g4 J% X$ l Z' Q9 B! h6 [
0 ] v( G" W% L% h7 r0 Z
function notice_details_meta_box($post) {' I. w1 n- n- @. m- p8 I5 H/ |3 D/ v
wp_nonce_field(basename(__FILE__), 'notices_nonce');
6 j( ]0 p- H+ _ $notice_title = get_post_meta($post->ID, 'notice_title', true);
5 P( _8 I- n. R1 L5 G# R $notice_content = get_post_meta($post->ID, 'notice_content', true);, ^& r* ^/ {" a
?>& g; F7 y! c+ {2 s. O
<p>! r. j; E- [: ]' r
<label for="notice-title">Notice Title</label><br>4 @' t- V p) R. k' s' _
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">+ C0 Z2 Z" L8 r" }, S7 s( E
</p>
( F: t4 W; J* K) N* ?9 X# m' t" h- j* J <p>$ k- M. I& H4 f& }9 X- j8 Z
<label for="notice-content">Notice Content</label><br>6 a! N& c; v" L2 i2 A
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>% r. B; f3 s: t6 A& G
</p>- {$ ~2 E) J7 X+ o4 E$ t
<?php8 `5 [, E. C2 z' v! J: ^6 v. o
}7 t s8 {# [( w( o5 V( }' f
+ c. c. Z5 W* W/ J add_action('save_post', 'save_site_wide_notice_meta_box');9 L/ r4 a' h9 ?- E) _
function save_site_wide_notice_meta_box($post_id) {
- d: ?2 g' ]/ i if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))6 j) y }8 w l6 j- k! q+ h2 c# |( z; W
return;9 z# Q. N8 y/ P- V( \
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
% s1 B( |' k+ R( M) D return;
+ S1 H3 V2 E7 J# f/ A. j4 G7 X; }& f9 d& w" Y& ^
if (isset($_POST['notice_title'])) {* r) a5 F! }) F% {: Y+ S; Y
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
" s2 F6 V+ N- s! m! I }; v4 Y# `8 ^2 V) ]1 w* C6 e5 ?
if (isset($_POST['notice_content'])) {
) B; {) O4 h8 Z3 r( |9 x+ D! o update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));9 ^$ B$ r7 J- m) Z- z. u" o3 y* X2 u
}
+ q- z% y; T* o- { }& l9 x- y6 J- g# n: Q4 H' b u! H% q
```* X( o/ n9 S7 R8 q( `7 T
' f3 ?. F: h! N0 t% Z& i 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。3 A4 ?+ U$ I: T# ]- _# U
7 N, u2 N& g2 e$ L7 c4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:6 K: S; d6 p# ]& b
0 R: [% V+ L5 ]: v- k( I- D! v ```" `3 k. Z; v+ D) [
$args = array(! c. p9 ?7 k1 a1 f$ p) p
'post_type' => 'site-wide-notices',, ~3 p& e. T8 P
'posts_per_page' => 3,
+ |8 }4 P0 V' k0 }! P; R 'order' => 'DESC',' J/ h, S; v9 t* s' n) T3 C5 D
'orderby' => 'date'
1 X* @2 p; ~" ]8 K );7 t x' V: e" Y; D5 G
$query = new WP_Query($args);) \7 ?2 I7 u* s& G2 B( ?
if ($query->have_posts()) :
* k5 h6 R2 o9 d A7 J, Y! A4 S( i while ($query->have_posts()) : $query->the_post(); ?>: A0 @$ F0 H+ X9 N" i/ @
<div class="notice">
7 q6 E" L, [% h# M1 c: \ <h3><?php the_title(); ?></h3>
- @' U7 r+ h6 q$ _* r5 ?6 ? <div class="notice-content"><?php the_content(); ?></div>
. P9 |& z' }3 F3 Q5 f/ p& s' c </div>
& H$ r5 \) H( ]! D4 W$ m1 z& I <?php endwhile;
- Q0 ]/ z3 t: K. o7 H2 w1 Y wp_reset_postdata();
% Y( ^# o& _3 E6 v endif;, ^$ F' g; @( M6 B! i% u3 p# s
```
% h. Z# B, C/ O+ W: x' B& J
6 T6 W. P. A& `, W' r1 E! i 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|