|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?( _" R+ u& O. j E) C9 C% A3 L
- Z9 Y. J6 A8 u/ S如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
' V- L" u% ^& L% [# D5 p) w, a. A
以下是创建自定义插件的步骤: k. a1 d+ P# ~5 n
! T" e" w( _8 _+ @! h
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:8 ]) G. L* Y$ g# v$ i( _
: C: t1 B2 A8 E4 O ```: C5 U: D- R; o- r3 \/ b* y
<?php
- {1 W) B" d) T7 h% z) O+ D /*8 F% _4 }6 a7 x( O/ V
Plugin Name: Site Wide Notices Plugin, Y4 z& K$ f3 `/ o; ^0 d' J
Description: Adds a new custom post type for site-wide notices./ u$ r2 j% Z# F& R( G4 `# }
Version: 1.0
5 N! t0 u( ?! l% O& p Author: Your Name
: ~" a: D4 F8 }; @9 W/ T" ]2 Q. Z ^ Author URI: http://example.com
" C% J% k/ u7 j: C2 _( Q! I2 ]" B */( n+ E; o) s* g, K- N3 U: u
& e. \: b) y) ^' x7 A. \
// Add plugin code here...
1 [9 m7 x% s: P4 l' k ```
% Q$ f6 S( s- g0 V. ]) {6 F+ V4 a2 D+ }0 F9 z% O3 j4 b
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。; M' {1 h7 b* H3 g
9 i7 C; H( {7 f: d2 c4 N x, O( _. J
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
" V' ]- \' E" j; P! G5 L l' S8 `2 P" e. r6 b
```: W. v" x* b$ h. L5 z6 |& l
add_action('init', 'create_custom_post_type');& y! K- m+ x; D" F8 p1 a
function create_custom_post_type() {
# h2 T! M. U$ B$ \ $labels = array(
% D6 e& q' E" ^6 D. n 'name' => 'Site Wide Notices',$ s; M* }7 z( ?% o1 R/ D$ @
'singular_name' => 'Site Wide Notice',
( e0 V( f+ W; b) u& {$ @& s 'add_new' => 'Add New',
( N3 K3 O' G; U 'add_new_item' => 'Add New Site Wide Notice',
0 i+ @. k C, q 'edit_item' => 'Edit Site Wide Notice',5 C/ E8 \3 R6 V% ~) V
'new_item' => 'New Site Wide Notice',
; n ~ u, g0 S3 K3 B2 J 'view_item' => 'View Site Wide Notice',
6 w5 V0 I1 W$ V8 B' }% G. O 'search_items' => 'Search Site Wide Notices',6 O+ M! e2 T8 O. U8 N* }& j3 Z% l
'not_found' => 'No site-wide notices found',4 {2 W: }" f1 m2 ^9 p. _" B
'not_found_in_trash' => 'No site-wide notices found in trash'
# w# t0 Z i& X$ P7 ` );& ]) h8 y; z- A" P2 e6 Q
4 r0 C9 a8 X6 B- V9 _6 U2 R" Q4 q $args = array(
: ]7 @4 e0 O' u 'labels' => $labels,, u1 H# Q. s5 k9 J# @8 O ?
'public' => true,
# l2 O- v" ~9 M4 h n 'has_archive' => true,3 T9 J2 K. c/ h( W0 U
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),% `1 t8 _: W2 l2 ~/ B4 O
'taxonomies' => array('category', 'post_tag'),
+ ~3 I, `, n! j' J6 V- ~. x9 X 'menu_icon' => 'dashicons-megaphone',
* s4 R+ c+ v' T$ ]& I 'menu_position' => 5,: _$ N* {* ] P& {
'rewrite' => array('slug' => 'site-wide-notices')* C8 i: O! W1 p e+ _
);0 Z Z/ o( w3 S' J5 {- |
Z* B6 J5 d4 }0 E/ d register_post_type('site-wide-notices', $args);* i; S) s1 J: Y" C: v5 G
}
/ h2 z8 T" `' O( F ```
$ T! p( I: E1 Q" g3 f% J
+ @6 d$ k5 z, n) o/ e 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。' b/ }; {6 J$ V( k9 u: E- N a
* i4 p! Q+ a- V2 r3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
: L& e9 [+ ^9 o2 E t$ Y5 O- ^, c+ F4 n8 ?8 N3 l* {
```
" K2 c7 Z) F; }& w, ^7 \: m add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
# K/ W% Q8 j( M" E; b) e function add_site_wide_notices_boxes() {1 E$ T2 w3 z7 ~7 s
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
# a0 ^( E' @4 [& G6 A+ g8 X }' o( K; C7 |) n. @8 Z
/ G$ ^! C3 h* F
function notice_details_meta_box($post) {$ ~# e8 E$ J5 }! a3 g
wp_nonce_field(basename(__FILE__), 'notices_nonce');
2 ]3 c$ q$ z. S4 j! z1 O, y $notice_title = get_post_meta($post->ID, 'notice_title', true);
; p5 Y% c$ W+ f }% z4 N $notice_content = get_post_meta($post->ID, 'notice_content', true);: [1 G2 }4 l, T1 F. ]& w
?>
9 r1 ~1 j7 d/ e( B- O <p>; s* f5 a) q1 {
<label for="notice-title">Notice Title</label><br>( I% Y2 x5 I. b6 I( v! |
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">5 {5 C% \3 U/ V" W* v8 \
</p>
; A( v$ V8 }9 W2 X <p>
9 F# l& M/ e5 j <label for="notice-content">Notice Content</label><br>7 o+ @; ^- m3 q; z
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
% D$ R2 r( x9 r3 I9 b1 ] </p>9 c# K* v7 G& m `3 q9 d0 I
<?php
+ e0 t. A" C( F8 p }' m( T& }& C- C( C1 n8 G
2 q/ H' h! w! a
add_action('save_post', 'save_site_wide_notice_meta_box');
$ v- L) R+ N. y- { function save_site_wide_notice_meta_box($post_id) {
; W2 D* l, @5 G% t if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))/ ~, |. @7 J& X' E7 O4 @( V
return;
+ d% N# O0 d' H8 S1 g! o if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
3 V$ I' _' m6 G b/ P ^2 } return;* E2 y* }2 v% A# [
7 D& A! q5 J7 g! h) X5 L3 {& u
if (isset($_POST['notice_title'])) {! L7 x h R9 c# f8 c! O: e" E
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));' ?2 s; w6 T" Z& f/ [
}
, Y9 S: y$ n: V0 ^9 h# c if (isset($_POST['notice_content'])) {; t. K; \0 D6 `+ Q8 \. W
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));6 {( I8 h' y. {3 m
}" ?: W0 f( s: F! Z8 P
}
+ i2 }1 r- F. y! M+ B9 Y" v+ { ```" h) v% l+ E% z& s6 N3 F$ l% |$ C
$ c; @6 ^4 u& A6 j 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
* ?( e& b( i r7 V: F9 s1 p! q5 ?/ S/ r% k: c. m. Q" _
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
. U# M5 }7 m: Y$ x+ j1 o% ?7 ~1 N, i6 U: c! K) A
```- z5 ^$ a( v0 K
$args = array(
) n1 r5 |. o: [3 E n 'post_type' => 'site-wide-notices',
8 e& g) ~5 x) A# g3 h- R 'posts_per_page' => 3,/ W# j& {# f0 x. r
'order' => 'DESC',% B, G8 U8 ]* O; i3 f
'orderby' => 'date'
% c K# ~8 D d- z9 Z/ ? );
2 A% r; V! b! z1 g $query = new WP_Query($args);
+ H0 ]0 [/ G& R' C" l1 p if ($query->have_posts()) :
* `1 T6 s, B2 H while ($query->have_posts()) : $query->the_post(); ?>1 S& ~6 I9 Z4 {& m, P
<div class="notice">( K/ L, E% E4 i% w& b4 F) s
<h3><?php the_title(); ?></h3>
7 b5 _8 c ^$ s2 d/ V: n* @* b' f- u <div class="notice-content"><?php the_content(); ?></div>$ z. u( i( M6 h( N) e9 W
</div>
* T& c: ^# g% ^; i; M3 N, y <?php endwhile;
. G5 n0 B4 ~ b9 s) B( x" { wp_reset_postdata();
. i* W/ z. _: d+ `" m endif;, e9 C s" U5 c/ K n
```: a2 `1 t8 T6 y- _0 `# f C
( E. _, \6 p: p: i) ~
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|