|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?4 u% ^/ I+ o( ^( E" h' e8 s0 |6 G
2 l1 K) b4 k3 r- o7 `6 }6 d! N如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。5 v) P3 c/ _' R. c) C- u D
4 K) G' v* U3 [
以下是创建自定义插件的步骤:7 [- D9 y. h4 O2 X. N- S) h0 h
- ` T7 F" {7 q X. U& m0 e3 |1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:0 m1 w1 O2 y" A" v* I3 L* @
- h( e9 e G6 W/ t7 H" n: R
```/ W3 S# i o2 i: ?7 f
<?php
: Q' W. h D; c! q8 o3 R /*: q7 _& k# l9 l7 h
Plugin Name: Site Wide Notices Plugin
" [: b) k/ j4 s7 E B Description: Adds a new custom post type for site-wide notices.2 Y0 _8 m0 f' ]2 y
Version: 1.0
! t5 w& G9 ?) c) C0 s( W5 z7 f Author: Your Name
; T1 N! Y* `( z Author URI: http://example.com L; U) |8 i, \- F
*/# X; D! Z- \9 |4 v1 N8 H6 z
$ O7 _% p% z, h/ N$ u$ A
// Add plugin code here...
|- [- q; i- c: a ```* G8 Y/ C" U4 Y/ X- n& z4 t4 L- q
6 j$ G2 h5 M3 p4 a0 @2 W4 | 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。/ P x: s. n% z% g/ X
" ]" [4 @+ z% p2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
/ W% Z: }0 \% i- f$ Z
8 H6 I/ O( r& m( f+ @" o& q5 L ```
) ?8 m: O, t9 t) \2 @ add_action('init', 'create_custom_post_type');" o$ U- q/ s; E" |
function create_custom_post_type() {
- v8 M7 w! C0 ~& [' T' x $labels = array(
; C( r2 F3 ~% l/ V: @1 C: r$ V 'name' => 'Site Wide Notices',
' F7 m2 E6 u8 o! o+ B( H 'singular_name' => 'Site Wide Notice',/ L+ W6 X8 a; K
'add_new' => 'Add New',
- e" M" x/ z9 W0 I 'add_new_item' => 'Add New Site Wide Notice',
" ?* a& ~$ F ] 'edit_item' => 'Edit Site Wide Notice',# b" X8 [% F0 d* y
'new_item' => 'New Site Wide Notice',
. ^1 m: x( Z0 Y' M" {, M 'view_item' => 'View Site Wide Notice',; g1 S' ]8 Q5 ?/ x: A; ], o# ]0 }
'search_items' => 'Search Site Wide Notices',5 w- Z& W) v- \9 v% d$ u; R& R% o
'not_found' => 'No site-wide notices found',- i. @! B+ |$ Y5 L. S
'not_found_in_trash' => 'No site-wide notices found in trash'6 W! M, p5 g/ P9 Y# M0 \# K1 u
);
4 ~- C" f; G x3 j0 h4 H
" x6 @# x# D+ L/ j' B8 J: R $args = array(6 S$ [% O) X! Q
'labels' => $labels,
1 g8 K5 f0 g0 f& e; C 'public' => true,5 t4 N8 G7 q/ b! M% P5 N' k5 m+ G0 A$ x) k
'has_archive' => true,
. J3 f- B# S4 l0 A! p 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),: I5 U! l' p3 f+ n1 _: H+ T
'taxonomies' => array('category', 'post_tag'),2 W6 y( I0 S& I; t6 l* c; k
'menu_icon' => 'dashicons-megaphone',
$ O0 X; J0 q; p0 B2 f 'menu_position' => 5,1 s2 o0 a! \, M
'rewrite' => array('slug' => 'site-wide-notices')
1 R6 a I8 [" y% ~ );
% y: ]5 ] Z/ S
3 }, T& \( `5 v7 k+ t/ v register_post_type('site-wide-notices', $args);
# x6 F6 z8 V9 D; C }
7 }3 T s5 g0 f' Z; d) X- [ \& v ```
- ?; j2 q+ |' S% ]* K) [* [" k' x1 E
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。# Z$ z8 {- z: ] p8 J Q4 D1 q
- ^! n, S( O# X: P) U1 P. F# `$ a3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:$ d: B1 E+ r# H
) Z5 \( e# e7 A8 s, F z ```% A/ J" S( M3 f
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
9 z5 V6 F) E! y) [- p* z6 e function add_site_wide_notices_boxes() {. ^. u% G) M% A# |
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
9 m6 z0 {. X' y% T }) Y$ q2 a2 a+ X, Q
/ \! Z% z, t' m5 Z6 e- b- `6 \) S5 Y
function notice_details_meta_box($post) {
" ^; z, R; v$ p1 P4 x+ S! b wp_nonce_field(basename(__FILE__), 'notices_nonce');
! s9 ^# f# W6 Q* y* G $notice_title = get_post_meta($post->ID, 'notice_title', true);
5 T& ^* P H7 J9 s8 p0 ~* J $notice_content = get_post_meta($post->ID, 'notice_content', true);, v# f6 h. W5 M& c. X
?>: p; C& r, q+ i \+ J# z
<p>! y# A; f3 W3 H$ y% |8 ^! l* L
<label for="notice-title">Notice Title</label><br>
2 M9 B g6 {* q2 H; [( U( c <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">% s/ N4 S# \9 y. e; \
</p>
1 q- B/ h2 J% q' R2 P/ W <p>. g- B( D2 }& `# p
<label for="notice-content">Notice Content</label><br>
& r, f0 N* q' R4 A0 o% a* G8 C$ _ <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>8 ?4 i6 |1 ?4 B! K" t6 I
</p>* u4 j" @+ m: i8 B: y0 N
<?php
8 V4 p" s, |4 j; S }
/ |; U( Y. t$ O- w
; P+ g+ [' z+ Y add_action('save_post', 'save_site_wide_notice_meta_box');
9 Z* ?$ @7 m4 t5 h2 P2 [! n function save_site_wide_notice_meta_box($post_id) {, \* ]% P: W- E1 B5 B
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))) l& U7 E8 L- o& t! i
return;
6 Q; g1 C/ ^9 B- f7 W- u if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
3 ^: F; i, q# G1 j+ J5 q return;/ n& M3 s! P l4 o O& G& Y# J2 K
5 L% ~' I% n8 D4 d/ V' L if (isset($_POST['notice_title'])) {
% ]! x( N* H4 q% J9 S Z- T* s update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));& q: p0 X' m. ], t7 A
}% G! w p) }8 |! v8 ] F/ t) m6 h
if (isset($_POST['notice_content'])) {: ]" m, s+ T9 J! y- h
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));% q; s7 ?6 @% J9 G
}
9 h9 F+ K9 o" a% X9 K. ? }& C3 a9 {6 P9 j& B- i; D
```9 }. @ O9 t- u) n L/ e
: O( Q% l8 L( n9 v! a# q7 ?) o% U
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
* r* Y) @& h* r `: @
1 p$ J7 Y& ?# F+ e x [4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
- |) G3 p' y: }( Q; H! `+ b% a
4 n1 h, N$ z/ `" |0 m* j ``` n/ O% v; y8 v& p( P" c" X z, o0 M
$args = array(
" S! E+ ^; l: o 'post_type' => 'site-wide-notices',) U) e' i+ u) V3 Y; s
'posts_per_page' => 3, [8 j" v: R) P' f; Z0 X6 G0 {
'order' => 'DESC',
& i& Q! d& h$ H! \7 n2 t1 C3 `$ | 'orderby' => 'date'7 M) g8 [3 q6 j1 W2 D0 ?- z
);5 `# |% S+ J( i! A+ D' ~* m
$query = new WP_Query($args);4 I: f ^; H+ Y% ?4 F9 q
if ($query->have_posts()) :' v) B# A" ?. `1 b; v: B
while ($query->have_posts()) : $query->the_post(); ?># A7 S$ j. L) l' Z2 F; L1 y
<div class="notice">
% H# }/ ]2 o$ s <h3><?php the_title(); ?></h3>
) W# }1 f3 `9 Z# u0 P9 L0 S <div class="notice-content"><?php the_content(); ?></div>
: p9 N3 A$ O. P" P/ \" X2 J$ f9 D5 X- ? </div>8 N/ p4 w% ?' t: T7 l! y
<?php endwhile; }* X, F2 H7 E+ v7 X+ i. r+ J
wp_reset_postdata();
9 O3 N2 t: k/ I0 J endif;* J8 t( ~# F$ c
```
5 j. [, r) i* c( z% x
3 Y8 H1 G& H1 [ 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|