|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
- ]) |: Q2 `& B' y3 n5 B. V5 K, u9 h
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。7 Z2 R6 @, K. ]: _0 c5 }
* P0 G- `7 }' U# W% H5 |' `: l
以下是创建自定义插件的步骤:, b1 Z4 d, g) L
; r& t4 ^" h3 I5 E% {1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:. o6 V4 {% \4 M* d: O( Q4 h
" l9 n) D/ h( [ A
```
5 }/ P+ H8 N$ h7 p4 f <?php# q$ ?, a7 u4 o) Q
/*
& u5 M/ i% W% R# G( V Plugin Name: Site Wide Notices Plugin
5 V2 `% n. b+ {# Z) K Description: Adds a new custom post type for site-wide notices.6 |* _, h; G! n6 @2 ]. q: O& F
Version: 1.0
7 d7 ~0 i' U1 a5 D% k: i' H Author: Your Name
6 A3 x, M1 f! L Author URI: http://example.com% j! G# x& x( E4 s1 ~
*/
- B. H/ F7 l) ?; {: W
0 A; R5 x6 q7 B% K // Add plugin code here...
. t( P7 ]0 i# g. x) t9 d( G0 A ```. H9 Y& X% I1 h; W
: q0 Z4 G+ f) r; Q* n( T$ Q
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
- ]8 `; ~2 l3 f1 S8 Q1 F r. s9 C7 @2 l
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型: F) W0 B" V* @% Y4 J
* g- E/ X: M& N
``` H' S9 B) W$ G u& ]
add_action('init', 'create_custom_post_type');
; y9 Z2 M5 }, M& e* Y! R# c L+ q function create_custom_post_type() {
8 W4 S, R S7 n- O; u' x $labels = array(# i# g4 i- u1 {9 H8 x$ L! @
'name' => 'Site Wide Notices', }* \; X; x+ K& O
'singular_name' => 'Site Wide Notice',; t! v- q7 X; {; u6 W0 V& X; O
'add_new' => 'Add New',
$ ~+ S/ T. z! e0 N( B& l% F( ^$ `$ B6 Y 'add_new_item' => 'Add New Site Wide Notice',
4 X7 Q5 U3 D! B6 D. G( v1 Q 'edit_item' => 'Edit Site Wide Notice',7 c+ ]- }2 K1 o# D9 ~
'new_item' => 'New Site Wide Notice',! K5 {" D0 ~+ x! I0 u" u
'view_item' => 'View Site Wide Notice',
$ q% w; e) K5 N# x- T h 'search_items' => 'Search Site Wide Notices',
+ x* Y4 G0 D/ m: m5 L 'not_found' => 'No site-wide notices found',
- \. Q# y* w: u( Y/ p+ V 'not_found_in_trash' => 'No site-wide notices found in trash' m" f1 s1 V: f0 Y# f, w3 |
);
2 [) s# v1 D- I
' d. d5 l9 D- P2 j $args = array(
/ Y- e; a7 u) @: d- q 'labels' => $labels,
3 s& W \3 Y& A6 ~2 e 'public' => true,! E4 k0 c; S' S6 ?
'has_archive' => true,+ b* }- G3 d B8 B
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
. O& l2 V+ Q% @4 g$ q H0 F 'taxonomies' => array('category', 'post_tag'),
3 r/ `; x6 o1 Q; X* J3 u. m( }' y+ m1 F- C2 ~ 'menu_icon' => 'dashicons-megaphone',$ |: x b+ v, O, z# T
'menu_position' => 5,
6 _2 o8 Y; f% Z8 n* X f5 n 'rewrite' => array('slug' => 'site-wide-notices'). z4 p2 M F2 x! W
);
' r3 ?1 F3 }" N$ q* v" E9 v6 E
+ `5 _& k& c1 E4 S, X0 L; d2 }. \ register_post_type('site-wide-notices', $args);8 x) O* i3 H. U3 v7 z
}+ v* S U2 J4 R# X
```
2 b0 y1 Q h# @7 I5 N
# f! m( R6 L& @" Z! {8 }) W 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
1 c& T" P& p) D. }# W" p! G4 O6 U4 T0 \& a8 |+ l! E* ?
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
7 B7 ~+ A$ a" V: [$ \" b5 a7 ?# x" g: S. d' R3 g. m j: w
```+ e3 m6 M' F# z
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');( P% O4 O* }9 o" ~; E* N
function add_site_wide_notices_boxes() {3 c% ?/ V% _6 S) ^7 o* ~
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
$ z1 I/ X- A Q9 ? }- \+ U$ d( [% v# o
9 _6 j: w, R3 ]3 t/ ]
function notice_details_meta_box($post) {, M) E3 @7 |: k y) {6 R7 F
wp_nonce_field(basename(__FILE__), 'notices_nonce');7 B1 G; S. Y+ \! f1 }! v- h
$notice_title = get_post_meta($post->ID, 'notice_title', true);
( B' z+ z# |7 w$ y& ~ $notice_content = get_post_meta($post->ID, 'notice_content', true);
5 B7 y' m f1 a) @- h ?>
( N; c; {) ~9 r) c: o2 V <p>" Y) Z `, H. x& R7 e
<label for="notice-title">Notice Title</label><br>3 W+ s- l0 e/ B( z- `2 {, ]
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">1 g# Q/ K v" ^) u9 T, s: z5 E
</p># K0 p& E' ]/ X) L E% @
<p>
( b! a8 M2 r2 Z' K <label for="notice-content">Notice Content</label><br>
8 S) l( ~% B6 t9 U) D& R <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>' f( O2 n% [& R$ ?. }0 ~
</p>
7 h. R1 M9 r6 x1 w <?php
/ y5 z! Y2 {( H' k }
7 w' y- y2 e# t ^
, k' w" w2 }; k/ l" r3 Q0 j add_action('save_post', 'save_site_wide_notice_meta_box');4 L" F3 P o' B2 ~7 Z
function save_site_wide_notice_meta_box($post_id) {
/ V! w5 C1 h# t6 u& P if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))8 T- o- D/ O$ ?
return;& h! M; H$ A3 Q Y
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
: Y O* M$ G4 W# u3 ^$ X( b return;6 @4 P6 F' H. T8 G
* H1 u) A: A9 ~5 T; ]7 C
if (isset($_POST['notice_title'])) {
, G. j* [$ ]" g" t; [! n* Z9 u update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
4 w# e5 X0 |) g" Z2 l }
/ a) R: }2 v7 M% l% q if (isset($_POST['notice_content'])) {
% @+ @0 Z# t: _ C update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));# e$ j. s9 q" l- b( u; V
}
3 h3 L$ Y3 G/ \9 `- ~& I4 X }, T2 @) O0 a Q5 h0 R6 h- Q
```
1 O6 P' J) R8 L( i8 ] g& @4 x" \# X: W- Q/ O; ]6 [7 G
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
`& p: p+ [; U) d' r1 I. U3 r8 T
/ l. P* M3 I, i5 @, @# _; ]0 _4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
- `* a: R; _" X3 ~
& h' X; t. L( G ^& D, ^ ```
3 T+ V) P6 _# Z) s2 v2 y" n $args = array(
5 ~ U( V# n+ L2 N5 v8 F5 x 'post_type' => 'site-wide-notices',
5 e, s- ?0 a4 a* [1 f! V 'posts_per_page' => 3,9 r+ }) ~ |0 d0 `6 ~2 x4 o
'order' => 'DESC',
k& u' N" W/ F! A% ~$ G$ G$ b 'orderby' => 'date'
$ D9 Y; r* O- N; l2 m8 M );# p5 v. W6 M; M- E* Q1 I8 J0 [
$query = new WP_Query($args);
" u# ~% O. T! V* J if ($query->have_posts()) :
$ ?% K5 @5 ^* B while ($query->have_posts()) : $query->the_post(); ?>
1 k" A! s* s6 v& L8 k- e+ W! K <div class="notice">- K& P8 c/ Z/ N) @9 B
<h3><?php the_title(); ?></h3>
. w: r# M: Y( N' V5 s <div class="notice-content"><?php the_content(); ?></div>
( n( f3 c, D6 P% E" s; @7 o! } </div>
7 \# z) H! N. c8 ]# t <?php endwhile;
. q/ E d# F+ y. B! k wp_reset_postdata();/ c# L8 c0 W+ |
endif;; e* @7 p' M: S. o% G. j! `
```
; h& t& h! H, p# L, ]6 ~- T# u& _( C0 x7 e. t9 N1 v# R3 z
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|