|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?8 v3 w: B6 F( P
% |' u* [- y) q, I如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。3 ~6 A& }0 A |2 ]
% M* K' t( z U8 v
以下是创建自定义插件的步骤:9 W9 H# T1 r, T& \: K9 j
& `5 M. Y5 M+ o* [
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:* ^6 `" f' R! E' V' {, R
" M7 l) k- k5 z( |5 y( x
```
* A- \% W# T1 k$ R& g <?php
- Z+ ~" t9 @, Q8 Y2 w! u( O /*
/ s s+ ?9 L6 Z Plugin Name: Site Wide Notices Plugin
* t$ u, x4 Q" ~ Description: Adds a new custom post type for site-wide notices.) e8 s$ G# K o9 q$ W, Q# @1 z
Version: 1.0
1 y( p! ^; _) Y" q2 h Author: Your Name! Y8 g! s# `1 K4 z4 a$ q* i6 K
Author URI: http://example.com
, m i$ m3 ^& L# W */3 z: l& k$ W4 Q* H% |: E$ [0 d5 D+ y
" @2 q/ Z- H% q2 z6 C // Add plugin code here...
- x0 i. S& h" C5 c3 Y5 g ```
5 C6 ?% S, i2 E7 _3 B, z) F
; n+ v7 N( G, ^( F; s" V9 Z 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。$ {, Y/ `! x7 r" U( f: g% `4 q
* O' q, u, a; {- {- w
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
* b+ U7 l$ o' q6 {" D( e
0 A& h+ }+ r6 u! b ```* H* x) o6 M# I; L& f; W
add_action('init', 'create_custom_post_type');
" q# {# ` A6 L" p% q) `' B" o& k function create_custom_post_type() {
+ z9 o. r( b2 i5 Z# B# m& A $labels = array(
8 _# K; G% C! S2 ~7 Q 'name' => 'Site Wide Notices',
# l8 w) d0 B1 \. F' }' E 'singular_name' => 'Site Wide Notice',8 k. h) j: `) q7 F
'add_new' => 'Add New',2 t8 Y+ Q: Q& ?2 Y) ~, p
'add_new_item' => 'Add New Site Wide Notice',
0 P1 }8 B( K2 }& f2 p 'edit_item' => 'Edit Site Wide Notice',6 T$ `7 V3 k3 O5 A2 G' |+ P
'new_item' => 'New Site Wide Notice',
0 ^( b5 j* q6 u+ E+ I 'view_item' => 'View Site Wide Notice',
1 N+ c+ v" p% w- z } 'search_items' => 'Search Site Wide Notices',
( G& g( v6 Z/ `1 L, T 'not_found' => 'No site-wide notices found',# B/ F0 `& u- F; e
'not_found_in_trash' => 'No site-wide notices found in trash'! S; j! u8 K3 P6 q
);- s. D' v: J$ D# m
! b9 p+ ]) V% z- o$ P1 }2 U; B $args = array(
! |3 d8 L1 R. R+ A, b, {! m! F( e 'labels' => $labels,
$ z w2 M v5 p x1 @2 G3 P 'public' => true,
* o' g* w0 t6 N% x& K- y/ q/ u7 B 'has_archive' => true,
8 Y, S0 s2 m& U+ d" f/ K2 Y: y 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),$ \. x3 a) w: G2 [* v: n/ T) c2 x( v
'taxonomies' => array('category', 'post_tag'),9 z, i8 [# t5 c3 p) e, k e
'menu_icon' => 'dashicons-megaphone',
' O+ ^2 o% C9 W 'menu_position' => 5,3 E% v- I+ T; i S% K
'rewrite' => array('slug' => 'site-wide-notices')" t6 e, D: d: z* [5 d
);
& E: B9 I; B5 K f8 B
. Z- h' F8 b; U4 H register_post_type('site-wide-notices', $args);( j4 Q! i5 u2 [% C
}
2 R& @5 r' A4 ^- O ```, p; T$ n9 ]8 C- I, p- U1 f0 N
, s5 w% }6 d t H* `7 Q6 x
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。: B0 |% s% T9 U7 I5 w
# l) J5 X7 k+ ?3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:+ [' W% O# Z: Q" V' R
9 d, A) {3 b% I* S+ i
```
: v# C% L$ M' [9 M* p7 U add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
, _1 j8 P5 F, ~% A1 C# e2 _- H9 `" O function add_site_wide_notices_boxes() { `/ u4 o: q- ` z$ l
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');) ]9 N6 I& E8 J' M1 J
}
. c6 f) u5 X" j; f* G
/ _2 D3 t! X2 n0 w& I8 K function notice_details_meta_box($post) {
/ S* j: L Y8 y wp_nonce_field(basename(__FILE__), 'notices_nonce');
' \! y# m) T$ g; V O $notice_title = get_post_meta($post->ID, 'notice_title', true);0 S" W; E9 I$ w4 d
$notice_content = get_post_meta($post->ID, 'notice_content', true);
8 q: f$ Q; l" s8 ?/ k ?>( a# z$ Y8 ~- U$ Z3 m3 v I/ D
<p>
$ [1 d8 K* d% z. M <label for="notice-title">Notice Title</label><br>4 `* j2 w/ M" q+ M2 d
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
4 a/ ~4 F& B% R! F0 }7 M. u </p>
, P) f! I8 w. c2 v- o8 m$ s <p>
/ n' M! k% q. W6 K: @ <label for="notice-content">Notice Content</label><br>
0 t) a4 ^6 ^# f <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
. z) ~0 ^* [$ V* T4 x- Y0 d </p>, X, y) p( |& }& @
<?php6 |5 z# V0 `* P0 i+ T
}" m/ M7 v: q5 Z) ]& |: V7 U
6 g) ^ V: Q0 F2 G0 v
add_action('save_post', 'save_site_wide_notice_meta_box');
% Q3 p% q- H6 C9 u function save_site_wide_notice_meta_box($post_id) {/ g+ W4 V- n, b% n" p7 U
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
3 @/ e7 W0 p( |, q. L return;" v* L2 P% S% T1 N, B5 D# Z
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
* @5 L! V8 L& t9 H; z- J& j; F4 Y return;. V8 X* j, @7 L1 I* `" t2 G
1 X) r" @' g8 |- C if (isset($_POST['notice_title'])) {
$ D, `& Y: Y2 D# N, @' X update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));* A' ?% m2 a' T% Z3 J* B% c
}4 Q2 Y( K( H! }7 i; P
if (isset($_POST['notice_content'])) {
/ g4 o' S2 n! d. L( M9 j3 V update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
! [) i9 S5 X d3 t5 r3 A }
: ^; i9 O0 j s }* ~/ K) P0 o# B2 y4 F4 t( c p" k6 B& A
```
' F" t8 s$ G# a3 v
+ t: \6 q" X& M& p# ^ 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
2 j' j0 _- ^* S) b& K' y' X2 q5 A, A7 i1 I& R' C$ _
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
' z$ u8 @( i( Q2 R
@0 ~* R) M( c) P: B' {, G3 e3 H ```
: Q5 D% n. s1 _ $args = array(* T0 A r+ a& B' C4 u
'post_type' => 'site-wide-notices',3 ]8 @& c6 u" G
'posts_per_page' => 3,
* }' N# |7 I: M/ ] 'order' => 'DESC'," E; J+ v ]( ~- h! I
'orderby' => 'date'3 }" f# M4 r# O9 h- P/ N7 ^
);7 N$ T" ~% k2 b
$query = new WP_Query($args);
) ~4 X& x3 _7 E D if ($query->have_posts()) :
3 B' T5 j9 a C: ^) q& o' V' u while ($query->have_posts()) : $query->the_post(); ?>
: ]; W# {6 J* @9 e. h: U4 ? <div class="notice">8 |6 Y/ V* s8 E; f
<h3><?php the_title(); ?></h3>; v( |* I7 S+ B2 X
<div class="notice-content"><?php the_content(); ?></div>
7 F4 v; j. M/ H0 d% q4 r </div>
, Q5 i( o0 ]9 c6 k, ^- }4 o* N, Z <?php endwhile;- [5 r7 f" N6 D# Z
wp_reset_postdata();
: p! K$ K! E6 h endif;) V7 y" N' ]0 P
```0 J T: @* [9 D! V
( v+ s, i, o: G7 K" D
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|