|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
4 y* U- a j5 l. n
3 s# q; z4 ^# ]如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
8 k& n% h% O! L0 w* S4 d5 a
: k+ d4 L9 d9 a以下是创建自定义插件的步骤:: Q- X: g1 c G
3 b+ ~! G3 E5 E* K- `" b1 h1 x' C, R1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:$ x! X9 @0 d, q0 E/ C- l
7 o( X" t; O% _! N/ O, [% F# g ```/ N5 W( ~5 l7 w# @/ | w% `6 G
<?php9 q) E$ `6 A0 d2 o3 C$ p y. E2 v7 q' x
/*
4 n% L4 L& `4 s* |. T5 W; @ Plugin Name: Site Wide Notices Plugin
: }% Z1 L; t5 L; \ Description: Adds a new custom post type for site-wide notices.
( k! P0 g3 N2 Z8 G+ u; V | Version: 1.02 O j1 I, U. G, Q
Author: Your Name$ M F# o: u1 v3 Y- K W7 Z
Author URI: http://example.com4 H' g& e/ }3 h: `
*/4 R/ N. I8 V) R
# x8 J# i. I% ]' B* W
// Add plugin code here...3 t8 @- t5 A1 u$ l9 J# E2 }4 w
```+ A* A" q9 t2 G3 x+ f, [
/ j$ ^4 U9 @: D. ] g; \6 s+ x
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。4 `* q" v6 M( ^ {, l4 ^5 J
3 Z7 ?. Y- d+ E J$ M0 q2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
6 v% A2 r% _; R9 V% Z4 x7 m8 [4 b( T$ i& I4 h7 Y
```: q3 Z: O. ]$ t0 C' C* b
add_action('init', 'create_custom_post_type');6 Q) w2 w7 @5 t6 L+ R9 \
function create_custom_post_type() {
* y D, I, a! N- v" X, E, [4 q $labels = array(, y; T) Z) S' h/ a7 P
'name' => 'Site Wide Notices',
- F3 x6 T7 v4 m" t6 E& h3 G( m 'singular_name' => 'Site Wide Notice',; R9 s; m3 k+ E
'add_new' => 'Add New',
* H1 c3 l/ M8 O8 Q0 z 'add_new_item' => 'Add New Site Wide Notice',& [ Q( B# a5 E7 H3 X6 h9 {3 X
'edit_item' => 'Edit Site Wide Notice',
7 C1 _$ n7 G/ s$ g, x% b1 D3 i 'new_item' => 'New Site Wide Notice',
3 ?) {) W3 R% S/ k3 T 'view_item' => 'View Site Wide Notice',
" _$ T" A# T/ K4 x 'search_items' => 'Search Site Wide Notices',+ ~7 s& E; r6 E( F( L* C5 h
'not_found' => 'No site-wide notices found',
5 g8 e7 ]! Z6 m, t 'not_found_in_trash' => 'No site-wide notices found in trash'
2 a% C* f* h- h' _. X5 u% ] );# [8 ^; I+ b6 G3 u$ H; [
k1 z5 {! Z9 b. q8 ~5 F( O# E
$args = array(
( g& L( G3 K; e 'labels' => $labels,
$ \ {$ {% s7 ]; G/ }+ F 'public' => true,: R" ?1 O* e x( X0 D
'has_archive' => true,8 Y6 Q* k/ ~( c+ P$ A3 X+ z
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
0 @ h% w+ c# {! M8 K+ S 'taxonomies' => array('category', 'post_tag'),# Q, a6 n" ^) ?8 i$ T
'menu_icon' => 'dashicons-megaphone',; l1 @: f1 o- n7 X$ p
'menu_position' => 5,
& B: p' p* u9 g1 ?0 J 'rewrite' => array('slug' => 'site-wide-notices')( r) }! \/ Y6 g! o5 z
);& y3 \# u( I. C! f
5 H4 E' ~( D6 Y) Z- P. ` register_post_type('site-wide-notices', $args);
$ @+ B+ X7 D0 P4 _- E( X Q+ F }
% A* U% d, _ H/ [/ I- T2 a! y ```
4 m$ o" P, n# o7 P/ J" L; m/ E" ^* q. o; N
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
+ {: O8 t0 f* m+ X+ s2 d! Q2 V7 [5 F
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:" N N: c9 X" N5 g8 B
! d$ f0 F9 M. k9 Q ```# H- G& a, i) f" f% g
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
, L' _! _9 j8 T9 i function add_site_wide_notices_boxes() {
2 `/ f! f3 n& Y) j& z add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
1 C3 P( U: k" a4 l$ e& G }7 W' Z9 X: k. z. ^; P' Z
& H0 B/ i" r( I! e4 n7 L function notice_details_meta_box($post) {# i4 i9 k- s" Q, J$ i7 i
wp_nonce_field(basename(__FILE__), 'notices_nonce');- `0 r: ]3 ]9 t/ J5 T
$notice_title = get_post_meta($post->ID, 'notice_title', true);( V. P) c% W5 s; @/ ^& t2 W
$notice_content = get_post_meta($post->ID, 'notice_content', true);
( G9 i( n9 s9 G; V C! v) T ?>- Y i) `$ ?1 N# `: |1 v! F' y
<p>' q/ [! u* p7 O2 x) `$ T" n
<label for="notice-title">Notice Title</label><br># y9 n) c* ^6 M# Q
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
7 G/ K( q8 m* K8 m; y6 k4 z- {% c </p>
0 ^/ d. P) g: X) r, O- l' s- b <p>' A2 o5 h$ A, u" B/ [9 n3 b
<label for="notice-content">Notice Content</label><br>
4 C5 I1 [# N4 m& j2 `* N- x$ ~1 J7 o <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
, T0 |7 h' t) q! r </p> L7 h% j4 x ^; u' _
<?php+ `& T) k+ g# e
}
" u0 M; l3 A; j$ h6 v- I) {0 E: P7 T7 ^; Z |4 i3 [0 ~
add_action('save_post', 'save_site_wide_notice_meta_box');
$ F3 `* `# p" |( D) T/ v, _ function save_site_wide_notice_meta_box($post_id) {
8 i: ]- a) U/ a. c7 L9 q if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))" r3 M5 s! \* N% Y1 p3 i
return;
- i2 C5 S! W; p' w$ U if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
/ r( y) [/ M5 ~: N- d2 f return;1 U! [1 L2 N: A. ?5 k/ P
" D, H, |3 B7 n* F- w6 c
if (isset($_POST['notice_title'])) {
# w* A) Y/ ^/ {4 l0 B: i* y update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
; B9 U5 x1 Y# H$ e7 r }% X6 A+ B( k8 }& P; x
if (isset($_POST['notice_content'])) {% P9 I3 h8 S' u* F
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
! b% l7 m( h9 \$ B" R0 r }
+ q! g; B3 D+ H3 v }$ R3 i5 b7 {1 L3 L v0 G6 R( i
```" P; R& h3 `) |+ _" D' y& C4 s
; C; X. m$ U9 e3 B 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
+ c, b: n: b9 o* N. o
1 O+ y( V/ d& _8 g+ h6 ?4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
+ _8 V; Z7 ]) z# o6 V
: |3 f. U: Y2 d0 A3 |, j ```. c. V \( l5 N# j
$args = array(
1 O! w {" c" [; ]/ I 'post_type' => 'site-wide-notices',6 g1 X% O2 w6 }' P% w+ w
'posts_per_page' => 3,, V( Y# a; T& @: x! D
'order' => 'DESC',: |8 P( p+ W' u+ `! [) E
'orderby' => 'date'
8 ~: {: ~' h; x* a6 i4 j );9 M3 a- `1 i# v
$query = new WP_Query($args);
7 h6 W" Y/ y3 ? Z& o7 u if ($query->have_posts()) :
- S3 ]1 v6 j" U" ^4 `# n+ \3 X |+ u+ a4 O while ($query->have_posts()) : $query->the_post(); ?>
* d+ Q ]4 q7 t) u% ? <div class="notice">
; K, Q. |5 X. d: A, _4 \ <h3><?php the_title(); ?></h3># ~ q# W+ W; L
<div class="notice-content"><?php the_content(); ?></div>
7 f5 e; b; ~4 X3 `' t! B* l3 O </div>
1 @! D" n+ V4 ?: Y. b <?php endwhile;
8 g) a3 @& T" |2 H wp_reset_postdata();) D2 c1 l X7 m. ~+ ~; ~ d% s
endif;# \) O/ e' \# |; c
```
8 u# E9 x' F% y
( Z; Y0 R( t: {* y* b% u g& P8 }4 z t 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|