|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗? p/ ^ W0 h) q
- ^) X* q1 x+ Q+ ?如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。# g- ]. O$ L, v5 |
# A* f9 o3 f! ?+ t3 f9 |# ?以下是创建自定义插件的步骤:4 H6 H9 L% g2 t0 g" W
4 u. L/ O6 k) p
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:' l- ]: j5 B" S& e- W
9 S8 ~) [1 g, k# Q6 z ```
8 o3 [% s9 c! l$ e2 \ <?php# Z3 K* R1 ^0 q. y$ I. E J
/*
3 a8 s3 U Q. w' Q8 u Plugin Name: Site Wide Notices Plugin* n: T% E; V- w* P9 X# G1 ?/ ~
Description: Adds a new custom post type for site-wide notices.: {) w" F- ^; Z8 k+ K/ R) P0 l
Version: 1.0
a. n4 |1 F7 m6 p( c Author: Your Name8 ~$ L$ q0 q k8 K: {
Author URI: http://example.com
& R) [: X; B T* Y1 ^ */; Z/ g2 \5 j( O* k/ A& Z
- a: S% y7 R% ^3 E // Add plugin code here.../ ^% z1 [: d% t2 q- Z+ l" ?
```
; p0 D4 e# X! u& H* y. {2 v: j0 M6 U, |
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。' @; \) ^: \" m P( r
* [ K+ n' X8 H+ ^* x5 R
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:/ l% k1 Y! L4 r5 F3 ]3 `. M
2 u0 C# M$ S0 t ```
x$ F9 [; x7 M% a add_action('init', 'create_custom_post_type');/ G/ w; b, C0 M7 t t3 c
function create_custom_post_type() {. K c, {; P4 x, o
$labels = array(
( {; ~3 x6 \4 \# K/ ^ 'name' => 'Site Wide Notices',: O+ n) L# n, p% _
'singular_name' => 'Site Wide Notice',
7 x# n0 H& p: Z. k 'add_new' => 'Add New',
/ b$ Z1 q- |" _0 l# U 'add_new_item' => 'Add New Site Wide Notice',' ]) V( B- ^+ V2 l( `
'edit_item' => 'Edit Site Wide Notice',
; q# j. M" i3 q4 Z% p# C% J 'new_item' => 'New Site Wide Notice',/ D d! Q4 S0 T" n; b( ?6 N0 t& |
'view_item' => 'View Site Wide Notice',8 z& H6 W6 [0 h. P8 m
'search_items' => 'Search Site Wide Notices',
6 Z8 h# z/ {3 u% T 'not_found' => 'No site-wide notices found',
4 g& A% @+ W, |5 o3 r0 n: P 'not_found_in_trash' => 'No site-wide notices found in trash'2 h3 S2 f3 e; T& y( ^
);
u: I% F% l# l; ^5 s/ p3 L6 g& S3 g+ c* e E: K7 @. o. [
$args = array(
% q' H0 m- p; R) p) ] 'labels' => $labels,) V6 J) N( Q# ^5 Z1 p
'public' => true,! h, S! t) ^" p6 ?" P6 Z
'has_archive' => true,, k* i, m0 h6 }+ R+ j' ]
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),9 E& `5 n3 c1 z
'taxonomies' => array('category', 'post_tag'),& `! O0 R5 \( F* ]6 v
'menu_icon' => 'dashicons-megaphone',
& ]6 g( y" z- s: m; @ 'menu_position' => 5,' t# v$ F0 S4 z* t
'rewrite' => array('slug' => 'site-wide-notices') G% ~ G; o: F3 i7 w
);
i0 H. D* l' t7 n
, m% z/ A& a; C# M# X O register_post_type('site-wide-notices', $args);& I4 x: s/ y" l# \
}
0 i# u: z1 o6 G( r; b& r! H ```6 }$ t8 Y& D- \
% C) f4 v& N* @ 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。- p4 V! S7 U# t0 [
" a/ j! v, c( j" M4 G! I) \
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
+ K1 |3 a& x+ h; a! \
5 z0 z2 _7 R U* O ```
! u5 p: s8 S; M' P- A9 |7 c add_action('add_meta_boxes', 'add_site_wide_notices_boxes');" X. @7 u. n+ F- L4 G0 `) C
function add_site_wide_notices_boxes() {
& L; L4 ?) s* E8 B! E add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
5 D( M# |0 p; l; g' Q3 U }
: q) C$ T7 K( K. s( r9 |3 ]7 O& w/ s0 u* s/ Z
function notice_details_meta_box($post) {
( g6 C2 C* W' Q2 D Y wp_nonce_field(basename(__FILE__), 'notices_nonce');/ q) L0 }, e& h" H$ X. U% ?
$notice_title = get_post_meta($post->ID, 'notice_title', true);
9 K0 v7 d( R! ]6 | $notice_content = get_post_meta($post->ID, 'notice_content', true);
" u' ~4 w: L5 ?9 }' U ?>
i8 v( {8 z. d" T9 m" u, O <p>; t" X8 m/ y" P- b2 G
<label for="notice-title">Notice Title</label><br>' O6 n/ W* p* N- k7 f- K& k1 h- o
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">* t- V' f! S! C$ i
</p>8 L% A" l m4 ]1 N
<p>9 w# d; R; O0 D% S0 g' {
<label for="notice-content">Notice Content</label><br>
. t! u. K/ `; H5 a% f8 s/ y9 ` <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?> a2 e! q. L9 m
</p>
* p+ s7 K9 x1 T' v2 }, z* h <?php' r9 j$ Z3 z2 ]- b5 K6 n
}
1 |, u% ^& ?2 f8 d8 v, q& g* V- X7 C. v9 u- [
add_action('save_post', 'save_site_wide_notice_meta_box');- S, e' I" D/ k( I6 P+ I
function save_site_wide_notice_meta_box($post_id) {* O. `; n/ q. t9 R
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))) T* N( b/ e+ @1 J9 u$ u" o
return;' m% |+ B' E5 u) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) I& b7 d0 r2 \) X& A
return;
9 ?3 U" `) X5 P+ V3 l3 o/ o# i- @# A5 u1 s: j# x
if (isset($_POST['notice_title'])) {0 L6 w/ S/ ~- T, M$ N; @
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));0 y6 w9 _$ S6 H
}
1 v- |7 ~1 z0 c+ y: n" S/ O% x if (isset($_POST['notice_content'])) {% E0 g7 P$ S0 o$ K; Q4 k
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
9 l t0 ^# q+ j5 f/ B }
* L: W) P9 k9 L/ y# a; F }. M) m1 b- e* k9 _9 v2 L& u
```
0 L% h; a- T8 k! o2 C* p$ E- N% a& c
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。6 }0 H2 b* n* o- J/ T
# n$ L* e) ?1 @" ?- T; F6 a- i
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:- q( w/ Q' D- F% g# R
, g7 r& p1 L" `/ P3 b; S
```
( E; b3 d3 c3 C X# t. L, D3 r $args = array(
% {: u$ A4 D/ m2 H5 P 'post_type' => 'site-wide-notices',
5 m/ ^' l. Y. z7 j, q 'posts_per_page' => 3,
M1 R$ k1 `: G6 s 'order' => 'DESC',8 J6 T( _' I. M1 l& v, m q( {
'orderby' => 'date'* U+ g2 Y9 F* D6 J* @. `; A
);8 W* @# v c% A: q' B1 R
$query = new WP_Query($args);
( e) d/ e: U4 O4 f9 ` if ($query->have_posts()) :
1 L1 t7 a" s, @2 \* `3 E while ($query->have_posts()) : $query->the_post(); ?>! C' {* X" H* T- H5 y, Y
<div class="notice">5 {" i7 c& e `) B' g* S
<h3><?php the_title(); ?></h3>
8 U5 z3 I6 P' X. c <div class="notice-content"><?php the_content(); ?></div>
5 L9 @5 Z& b- ]* S </div>
/ H8 s. U0 D# m, n2 r <?php endwhile;! I; o8 l/ M3 F3 _' o! V6 r
wp_reset_postdata();
W5 S1 U3 d" b( p+ i6 _0 H1 t M endif;
! {. P# g1 ?: @4 h' _, }- B6 j ```6 S- W5 ?$ i, b$ \! `$ M4 a4 j, c
, a% m# Y3 M' h( C# X: x6 s0 y 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|