|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
D# V' b& r, Q, S$ m) R& f+ y8 q T( a4 N' \6 s5 N& U1 A9 S) v
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
- z( E* P$ J) J" F* W: l% M
0 D, K# B+ `4 g9 c! D6 H) o以下是创建自定义插件的步骤:
1 P! Y- n3 n/ N3 I! |6 L8 S
4 v- {! y5 ~9 A0 U8 L" q' s1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
1 R+ t- _' C* Q/ {/ B0 c9 X/ a/ k
, _9 d8 Z A0 e, L8 m6 Q. a2 j ```
' i: a: W. P# P4 J" t# T+ D <?php* F6 B- J( u6 r2 s. R9 A2 A, g
/*
( w3 u: G' w o5 S* q2 E/ a. Z- _5 V Plugin Name: Site Wide Notices Plugin
/ k; C" y6 t; Y. ^ Description: Adds a new custom post type for site-wide notices.
" G6 Z5 r x+ s x Version: 1.0
# A/ P2 i% {+ a/ s# H# N" s, Z- l) S Author: Your Name9 S7 H$ f& S& u# T% b: E: A
Author URI: http://example.com* v; t3 X; E" Z7 T a
*/# z+ M6 C! |* j% z) b/ i
0 ]3 j7 V' f! l6 m // Add plugin code here...: o! R- E1 N" s+ S
```
! i1 o8 @( \. G/ t
/ Y- M) i6 `2 E* x9 w- z; ]7 b/ g* ], ] 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
/ S, | ?5 A3 _ J7 e- h* V6 {! Y0 H" k: y# A
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:3 g( a! E$ Z& w1 g9 T; o% a9 [
% E7 F' Z( a, V/ i% L, m ```
, ^$ b- J- u9 L! h/ D+ ] add_action('init', 'create_custom_post_type');
2 x$ m: t& n6 [1 {5 N; q function create_custom_post_type() {5 v" ~1 d5 [2 X3 g" g2 h
$labels = array(* A! U$ n- D" D, ]7 P: }8 n( {
'name' => 'Site Wide Notices',
- H4 o2 M* ]" K5 t 'singular_name' => 'Site Wide Notice',
% X5 r! U5 c, U 'add_new' => 'Add New',
' `2 J! S: w' }/ j 'add_new_item' => 'Add New Site Wide Notice',. d1 h p$ Z9 U2 ], r! ~
'edit_item' => 'Edit Site Wide Notice',5 L& a: ^8 O' k! m2 [/ @0 x! l
'new_item' => 'New Site Wide Notice',: o, [. e$ B& ^& _, n7 r7 e! n
'view_item' => 'View Site Wide Notice',
b3 Q# f& k6 o# J; r, t8 N* S. ] 'search_items' => 'Search Site Wide Notices',
; V8 y/ }. t1 v% l7 ^6 k6 s) n 'not_found' => 'No site-wide notices found',
8 W1 ~3 ~' ~- q2 b- D( o 'not_found_in_trash' => 'No site-wide notices found in trash'
1 g" |; x/ E1 ^ );0 t2 y1 A X" i' G1 ^, C
# {- K6 `; o0 h/ \: s- G6 H
$args = array(5 r3 o0 a- ^& T4 t4 \
'labels' => $labels,
& [6 g8 w) p& X 'public' => true,
& n% D7 d1 ~ A, N 'has_archive' => true,& }6 T S1 \. x8 F
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),# a& o% I1 z, D+ ~: t" Y7 f
'taxonomies' => array('category', 'post_tag'),
5 y" P3 R2 w0 ?/ @1 K) S# ~ 'menu_icon' => 'dashicons-megaphone',
- ]+ t3 v) Q, Q4 }3 t 'menu_position' => 5,
" a+ G/ ?! q& z- b6 Z 'rewrite' => array('slug' => 'site-wide-notices')
: M2 j) f2 V5 Y0 K' c );8 k% a6 | Q) Y7 w- X& Q
/ d* W5 ^7 p0 w; L$ ? register_post_type('site-wide-notices', $args);
, _ Z0 h6 e+ Z" B4 G7 X% G" X }
& N0 ^* {/ I6 O ?( }; u' Y" [ ```! o6 g3 J& Z3 L5 x+ g$ b+ C" |
; F, I, H4 z' M, ]$ Y H+ c- ?
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。: u" e1 _9 O5 [9 `* @) d: a5 s1 f
5 g3 ? N/ {2 E, q3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:/ u# U. v8 g, ?( {
0 r: {* H$ U8 I- x6 D6 a5 l ```
8 b) _+ a7 w' k: k7 i5 e add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
' E7 g0 C+ u( M; @/ x function add_site_wide_notices_boxes() {9 e, m7 S3 m7 y: T
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
% H( ^ s/ K+ F }
' v" [: w6 I) H% c+ i3 n' X+ _# z! N9 k9 y1 L
function notice_details_meta_box($post) {
& l; i3 e3 C9 H6 s+ T8 L- g wp_nonce_field(basename(__FILE__), 'notices_nonce');; A8 H) j- [8 t1 x
$notice_title = get_post_meta($post->ID, 'notice_title', true);
5 _% O1 ^ y) U8 H( m# j% ` $notice_content = get_post_meta($post->ID, 'notice_content', true);
7 M/ g+ \- _6 F, [ ?>
% i0 i5 A9 ?, l3 R <p>
) Q1 _" x1 ~$ \, s <label for="notice-title">Notice Title</label><br>2 r' I. S3 W8 w& G! t6 O4 j
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
' k l( d$ |7 Z/ j. H2 B) q4 k. ^ </p>7 M# w! t6 h# `/ |+ [- O; G
<p>
% J4 n& b: a1 D <label for="notice-content">Notice Content</label><br>
1 z) ?( h2 T Y" K! V4 p <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>6 C1 a3 c* m. @4 @6 i# h# q
</p>/ H5 @+ z; o+ E; r5 u, a
<?php
% i, Y, Z: v/ n) I+ | }4 a! L# K5 n, r1 U) V6 Z: J
; Z1 n5 M0 B' Y: O& l add_action('save_post', 'save_site_wide_notice_meta_box');- I& B7 p) N0 R
function save_site_wide_notice_meta_box($post_id) {: C5 h" Q7 V5 Q8 x6 P
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))" W% h- Q: L; b
return;
# g- W/ I) X6 ?6 N6 s if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)& N3 k( h; \) e$ i( `
return;+ ^- p+ H. r# c7 ?# K
. X" k" L5 m$ u
if (isset($_POST['notice_title'])) {
6 t$ F; d$ x8 m/ ?7 C- g" r update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
2 Y u% R8 p$ E& h }7 ^, y, B6 Q! i0 o; Y3 l
if (isset($_POST['notice_content'])) {( \1 ]# F; H/ Q* a( Z
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
- L( }2 B# s$ W) e r }/ C6 i$ O6 X& G( b; h8 @ z# G
}
+ [/ s& {! g% _" |' W ```
5 o9 l$ j# Z# `; m) I7 @- k1 _4 `6 k0 z! ~* v; f# k4 ~
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
: A, T% M6 i6 c- y8 G6 a. `( A/ n* y F, s- e
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
' p0 @, }# O' s5 s8 I4 g: v( \' T7 ?: O5 F. T: S
```' U# g- [. k/ k* B2 [) C
$args = array(0 g9 Z1 y" y# z
'post_type' => 'site-wide-notices',3 _) \& F4 `* I3 w! C ^
'posts_per_page' => 3,- i( `& Y3 B) U: E; {7 U( ~7 A) Y
'order' => 'DESC',1 Z G, e- O3 _5 l
'orderby' => 'date'6 o+ q0 d% }. N' M. u$ ^2 A5 ~( Q
);
! E; `1 s7 W2 \ T9 K: z( ^3 h" I2 t $query = new WP_Query($args);
" {* _* x- K! p# H+ q* L0 v* b+ Y if ($query->have_posts()) :
" x9 {( f8 V2 p) P" D while ($query->have_posts()) : $query->the_post(); ?>
" G# m! p2 ~% r6 P5 H( P1 L <div class="notice">9 B3 N; J* ^- u3 V
<h3><?php the_title(); ?></h3>
% E: k `5 |; w* C <div class="notice-content"><?php the_content(); ?></div>
. S2 z+ ^5 S6 Q </div>6 a s+ \( D* E- j6 i% u
<?php endwhile;/ C7 C: M+ K [) `7 V" Y
wp_reset_postdata();
0 p& a1 |; c* n, a" { endif; ^8 c6 R6 W. x& w! b: M
```$ V1 {" U# ?% \. E
# H# C! X* H, i+ _/ C1 } 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|