|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
' w6 H& J S9 n5 U8 |$ v. N
/ q& v5 f( `4 p6 z如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。. b+ ]# \9 q/ r: ]
/ Q, M$ h9 R2 v8 E0 v" a' T, t
以下是创建自定义插件的步骤:
4 k' {4 G/ \5 v/ {) {" R W0 D/ g+ W
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:7 b$ ^& f& d- W& t
* p8 f& {/ D4 [* g( B ```
: b' Z( T# J, B5 y) n: Y" n; B4 L <?php
( O/ Q% [) ?3 _/ Q* ~ /*
( l' u- T, U l+ M Plugin Name: Site Wide Notices Plugin3 D1 n& n2 U, r
Description: Adds a new custom post type for site-wide notices.2 M6 D; W+ \/ |7 V2 N, t
Version: 1.03 y l N8 r) z& s# Z8 i
Author: Your Name- \. U. Y6 W: p2 N' i' Z. ]
Author URI: http://example.com' S2 z6 B0 I& f: ?9 H
*/. E$ |, F" j4 y. Q! U4 a$ q- i
. V5 t' ]. F0 V2 w) q: M9 P
// Add plugin code here...; R: s! K2 p8 U( v& ?& P$ L
```
" e k# I" Z8 i6 F) c
8 @- _/ E, k4 H7 b 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
7 O/ Y4 t& l% p
! n8 I& J* d* C7 H0 K2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:/ I | T7 d5 b1 W0 ^
& |" d" E9 d9 i7 H' K$ h3 N$ v( d% j ```7 y1 q* l% q! u) @- } o
add_action('init', 'create_custom_post_type');
& N1 p2 K$ C; }( d* ?4 o; s function create_custom_post_type() {8 \, U: q/ ]2 Z2 z
$labels = array(
3 [ L# j+ A% b z; O n 'name' => 'Site Wide Notices',
: _( o6 e- w, W8 ]5 B3 ]9 F 'singular_name' => 'Site Wide Notice',
0 d0 d6 d+ C/ u: f! P 'add_new' => 'Add New',
) A G8 f* ]7 p; ? 'add_new_item' => 'Add New Site Wide Notice',
0 q0 [$ E) Y% Z0 D* _. A! _% k7 o 'edit_item' => 'Edit Site Wide Notice',
6 s, z4 f' n& K! l 'new_item' => 'New Site Wide Notice',5 @ `9 w: A7 p" E' D% y, B% r
'view_item' => 'View Site Wide Notice',
% |3 M! f8 m# R N0 d6 }5 o 'search_items' => 'Search Site Wide Notices',* I% w c' W5 Z& A, ^1 ]
'not_found' => 'No site-wide notices found',
/ M q4 v8 Y A: ?. N& n3 j 'not_found_in_trash' => 'No site-wide notices found in trash'
2 ?2 L& D: V s9 h6 j( i3 r! o \ );" \" E9 z5 S6 x8 c, P5 M
1 w! Z2 t7 r5 ]. y9 D1 |
$args = array(
, }1 C" v7 Q* g3 ^' ?& ?4 e( P 'labels' => $labels,; o+ L0 f% m# u& H/ _5 a* M
'public' => true,( |( a% P; v. c
'has_archive' => true,- b% f- M1 Z: @. {5 h
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
# q% E" } }- I 'taxonomies' => array('category', 'post_tag'),/ \( ]4 G m. Q
'menu_icon' => 'dashicons-megaphone',# t0 X8 K3 Q* a" [; B
'menu_position' => 5,
- \! u/ L* V- Y6 P 'rewrite' => array('slug' => 'site-wide-notices'); \5 u: z- v( ?( W( G" T. I
);7 g! @- B x, Y+ C, r
; [- a% Z8 H' D1 ~ register_post_type('site-wide-notices', $args);
; Q$ U- q! u" i }
, |+ F0 V4 Z. v B/ X5 n ```1 Z$ l* z0 o/ z! r% P
( o5 r* _; W: X; C* ~& q5 N4 } 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
2 I. s3 z" P' n) F. N; t6 g0 w
' C! i9 A4 C+ S9 H# d3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
! g5 G! Q/ V$ B' J/ n% v7 j$ Q* }8 z9 d5 `% [% p8 {$ |, a/ d
```. m/ k# Y9 w1 Q1 U, O5 L: Q+ C
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');! R4 ^' N' s, @1 h9 I8 y" I
function add_site_wide_notices_boxes() {
+ \" s+ a5 m" {6 ? x. { add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');! M# ^; |( ~" H* x- _7 }! z. L
}# L" K! h: t* z, M7 h
' s) ?8 @( c/ [0 E
function notice_details_meta_box($post) {
2 I# K( @# I6 o$ U wp_nonce_field(basename(__FILE__), 'notices_nonce');) M5 o7 x X4 I/ ~5 G. g
$notice_title = get_post_meta($post->ID, 'notice_title', true);
* j/ H. t2 I/ @) C% ` G/ U5 t: } $notice_content = get_post_meta($post->ID, 'notice_content', true);
8 t( }3 s4 Z* C; U ?>
; Q# I( y8 f. @- q <p>
7 F9 x0 h9 m# w1 B7 l <label for="notice-title">Notice Title</label><br>& q% u% N3 s$ q: X9 } L8 v! Y
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">, Z/ J; ^8 J2 j7 S
</p>$ _# f4 Z, K+ _7 }; n1 h7 r* U# ?
<p>* I7 b9 u2 d# R
<label for="notice-content">Notice Content</label><br>
' X' P2 \' O; ^( Q( f" d <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
! [$ i. {7 p U& C' l </p>
4 a8 X: U2 o& H, o, Q <?php
0 K& B6 n, K- J }* z: X! j+ B: E' [. w/ s
4 ]+ R8 O3 \& W/ T0 ~4 j L add_action('save_post', 'save_site_wide_notice_meta_box');
' K7 {* b+ r/ q$ L3 l4 _$ Z0 c; F function save_site_wide_notice_meta_box($post_id) {
/ A. z1 i$ J' I# o4 x+ W Y7 f if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
; ]- h+ W/ @* u, D' ?* r! V return;4 \3 R2 D# ^ r- `+ d
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
/ j* Z! Q/ T8 ^8 C% l6 G, L return;/ Y& P, G k8 o k' a4 ~3 D$ v- [
$ m* C/ I& n! ?4 k if (isset($_POST['notice_title'])) {7 R! Z8 c; b0 K' H0 o! {5 g# c
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));- ~% L& K7 |" c# M* P1 Y! k9 a7 p' w
}
: |) i9 g5 ` b if (isset($_POST['notice_content'])) {
: o1 H" ~0 V+ V update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));( g8 F% ~2 B7 l Z3 k. l7 j
}
+ A+ }- P6 {' q: g }, }1 g+ Q8 [/ d
```4 k# J& T- c- ] {
- X7 @4 P" E% R3 o2 A) d* z$ {6 N* G
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。* u& c0 y! m, W4 B3 K/ ?* N
& ?- Q2 p* R& d G
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:5 ]# Y6 x2 X. ~. u! r% t0 {! P
2 t% K' E* q$ T: ~. J
```9 c b/ \5 A4 l: _
$args = array(
2 \' [1 V1 q6 g+ ]% H 'post_type' => 'site-wide-notices',- u( L8 l$ u: F6 H% _0 `
'posts_per_page' => 3,; g5 N3 T$ a- z& q" m; L: h% s; C
'order' => 'DESC',+ \7 n' l5 i$ e& \9 d
'orderby' => 'date'
: i( R: k0 j t# E% D8 L& V. S) q );
& r( l# u7 ?. m $query = new WP_Query($args);/ p" C; o$ a f" f: c3 v3 l
if ($query->have_posts()) :
7 ~; a: {8 G q) } while ($query->have_posts()) : $query->the_post(); ?>
: ]5 Q! J, ~, G& \ <div class="notice">
: h5 _; G H! _2 | <h3><?php the_title(); ?></h3>) t' n0 t2 {( |+ r6 e0 Q
<div class="notice-content"><?php the_content(); ?></div>" I) C5 D0 j, |2 Z' o
</div>
( a; q r# {2 }/ o <?php endwhile;
! W H/ w1 z& _ wp_reset_postdata();
1 c& Y8 m' A2 S2 }# @# m- o( v endif;9 R" }0 I! r5 j# }# i! s1 R
```
# y' F0 R2 H0 } h' r+ q% l9 N' H6 c5 S8 r5 z' I* l
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|