|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?5 t4 k- r4 X! S# A2 m5 F- S
% f t$ y' U: r
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。7 Q8 I, U) k2 T5 V0 {: ~9 c
) v+ ?! L; S K- _以下是创建自定义插件的步骤:
4 Z; E, A/ |7 n+ q1 d" V, z* e$ l$ ~3 e6 O: a" n8 u
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:/ J0 d% N- A$ h: o% p- A. R9 n
/ } C" o) z5 K8 _+ _ ```
+ T. u6 U. H* P# w, }4 L3 d3 B <?php
( ?' R7 J+ S* F) t6 q3 y; G0 ~ /*0 Q' F5 b6 x4 j9 I5 F( d
Plugin Name: Site Wide Notices Plugin. |% G& ~: I, b! o
Description: Adds a new custom post type for site-wide notices.
( \. v+ F6 F( n/ P D Version: 1.00 l. ^" V* ]+ R, k% U
Author: Your Name
* H( J8 F1 M" Y" {8 |, ~5 ~ a Author URI: http://example.com9 _5 m, I8 U' B+ ^- K+ C
*/
" g5 R2 V9 S" E6 L+ H2 t& N% F H
U" L9 s) `, x) X) T. H2 |+ ~ // Add plugin code here..., e B8 v H1 e' y6 P
```
) z% q* N2 R8 W$ S% [) ?& O- P9 i7 B9 C
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
8 D. Z6 j5 V: O6 m) _$ t& b
1 V9 L6 b. I+ }) \7 n+ {3 I/ c* C2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
+ \8 O# j& ^4 y1 i* G0 ?
4 N5 r' L* Y& Y4 V4 \, l ```
6 c- a; k$ {$ Q, u2 I add_action('init', 'create_custom_post_type');( R+ g1 T' @; ^) u. i0 w$ V' A
function create_custom_post_type() {, W5 I* r+ Q9 F
$labels = array(
: B- k0 K4 l, v 'name' => 'Site Wide Notices',+ Y |" [# z3 R( L' }: K5 E) [& U" k j+ _
'singular_name' => 'Site Wide Notice',* D- v/ L5 Q9 G, D- ~! d
'add_new' => 'Add New',; d- R6 w- L$ U4 @! `$ z
'add_new_item' => 'Add New Site Wide Notice',
% Q& s& D! O+ d8 `+ R 'edit_item' => 'Edit Site Wide Notice',. q, T# ]7 o4 m6 n7 ]; |
'new_item' => 'New Site Wide Notice',3 d% x! F+ L$ b3 |* {
'view_item' => 'View Site Wide Notice',1 X/ Z# o% K: u: J" u* r
'search_items' => 'Search Site Wide Notices',/ y/ H/ T2 c1 I; R7 h7 ?: G
'not_found' => 'No site-wide notices found',& X3 o! r1 K4 q# m, _
'not_found_in_trash' => 'No site-wide notices found in trash'
% V, l4 p6 i5 d( o/ U2 U( V );
, e' x2 N/ |7 J5 Q3 f
0 {4 R: F& C' }5 ~- J( q0 x $args = array(! f4 B4 t7 Z4 F% P
'labels' => $labels,4 [0 p0 ~) q0 z' ?5 Y: C" P
'public' => true,) F, G# U4 {9 ?! ~/ @* z+ Y
'has_archive' => true,
8 |8 m, H4 T4 ?: T 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),& K9 R o9 _/ |6 a; j$ @
'taxonomies' => array('category', 'post_tag'),* {* e l' l# o8 s0 `% z u/ w
'menu_icon' => 'dashicons-megaphone',
/ C8 A. b! d% z: b( t# w/ Y 'menu_position' => 5,
7 N" A1 {" D5 z' Y! \ 'rewrite' => array('slug' => 'site-wide-notices') f7 u) Y v3 T: ]* Q
);
7 q+ R$ W3 n: z1 ~
; u# p. i$ ?3 i, R register_post_type('site-wide-notices', $args);
0 W. G1 v& Q$ B }
6 ^7 Z R4 l" e8 }2 m ```* v& r; q) }4 Z3 W' ~4 I6 Z
; n$ G; Q r- i# i: C" w! V$ l 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
! F0 [/ X6 t. [0 h; S R8 _2 g: t
8 z5 b, d0 R9 `4 M; @) @3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
# d# K }! y8 f' s
) P# ~3 \9 @: T2 S; g: M+ \ ```' g. }+ U# ^+ i* M
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');' F% j u* m( ?
function add_site_wide_notices_boxes() {3 l( G5 @+ v9 w* m5 p, o5 q r( {
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');% w- G: L; s: K8 J$ x, G% [
}. W! l) }3 h9 ?! d5 t) d
7 j* x: _; R- y# x/ P* |& ~
function notice_details_meta_box($post) {. P/ q4 c3 {2 A
wp_nonce_field(basename(__FILE__), 'notices_nonce');
' R7 k; V" J0 q l$ z $notice_title = get_post_meta($post->ID, 'notice_title', true);/ m8 I/ D& M9 |
$notice_content = get_post_meta($post->ID, 'notice_content', true);
+ n/ N& v* [8 s5 e5 u ?>9 e5 f' U1 ^ L' p
<p>8 }" Y( i" V( b. h( b2 Y
<label for="notice-title">Notice Title</label><br>+ w- q9 i) z6 H
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
: Z6 ^2 d5 ~* ^/ j6 e T5 x1 G$ _- S5 S </p>1 N# }0 v1 f- ^5 U" B
<p>( F* [/ t3 F$ ?+ E& ^& K! K/ v
<label for="notice-content">Notice Content</label><br>
" j# R# ^7 @* N' z( S <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>+ T1 [+ h' s, Z* C) S; m1 d
</p>
& ]2 S, x) T! Y% j <?php# X* v; O& n, g
} s0 I5 m! x0 }6 k. b( P
0 ~) @' h/ K8 w( s- U" k, q& N) ^$ `/ x add_action('save_post', 'save_site_wide_notice_meta_box');
1 ?: e( ]/ J( z3 @2 y function save_site_wide_notice_meta_box($post_id) {
5 P6 x* r# y3 m$ a" Z5 S. G if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))' v# h9 y* l2 v& R$ H; N' {
return;; i" W/ z1 h4 O
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
/ M; h6 i* c7 D1 Q7 R0 T return;/ `. `# G7 u# t6 u+ x6 S
, X5 p$ j' U! e6 u8 q) L, i if (isset($_POST['notice_title'])) {6 u, _( h. }- Q3 _
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
4 H* ] x! I1 \0 G }
+ \; o# n' F" {3 t if (isset($_POST['notice_content'])) {# n/ w/ Y2 N6 V3 \0 @$ n
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
% I" M. k6 S' b( M: ^+ O- H }. d$ v$ x( _( H* A) X! N
}
9 |' U b B& x/ `. r( f ```( r! g/ ?; ~) V, K' l$ f; Z
! a4 e/ }% N% X) P8 C& c 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。$ ]# K8 {4 I- B, V8 S- } q5 ~8 Y
! S0 C3 r. N4 ^: |# O4 B
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:" c* e) l# U- _3 b( y
+ F! Q& c+ f% s% a* l7 k
```1 B# B6 s+ Z5 O1 S1 k
$args = array(1 G" A# t! Y6 w& M2 @
'post_type' => 'site-wide-notices',
( y4 }0 Y! t$ q7 \+ c/ R 'posts_per_page' => 3,1 ]8 }2 n8 h/ l5 i- E# m' H3 Y
'order' => 'DESC',
0 q3 j" F3 v& t& Z 'orderby' => 'date'% n0 V5 M, V5 A
);* S& C9 D! s% k) G: }0 M
$query = new WP_Query($args);( g7 e7 R: c7 @' Z8 p3 N0 S: m
if ($query->have_posts()) :
+ A3 ] h* G6 s- \2 I( Z" j% m- y while ($query->have_posts()) : $query->the_post(); ?>
! n9 C: g9 W9 d/ p/ z: `6 d <div class="notice">
, H0 l: a# I; ]9 L9 P( r <h3><?php the_title(); ?></h3>0 }6 X! @& h5 u; T K! f
<div class="notice-content"><?php the_content(); ?></div>/ S' x" f2 U: n+ n8 i
</div>, \! i- m1 Y: A2 n( y6 @9 V
<?php endwhile;( X+ _; ^! D: `: L
wp_reset_postdata();6 t9 u) `0 e* T2 Q3 W3 ^
endif;! X: J' @5 y+ r5 h3 Q
```
1 {8 P3 w) z, N6 S+ s7 Y% _$ [* s; y" Z- x- s% Z' p0 P
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|