|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
# J8 U' T1 t- @$ t0 K. C( g9 I/ J0 v; x* C: s4 I: V4 g
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
N6 _2 g; Q/ g! p+ X+ |6 ~( C8 _+ f; ~
以下是创建自定义插件的步骤:2 C; k( |/ y: p/ F( `
2 T* S" S* }: {. d6 A0 {; d1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
* x$ G; T* \7 ] o! ?+ b; V; k) X6 L
% Y- ]( C* y. ^5 {0 d$ ]) I5 v ```; x b7 G7 {- i& W! ^) t3 C
<?php
& U( C6 c* o) \- u5 b3 E/ Y /* ?% I$ |! @; i N
Plugin Name: Site Wide Notices Plugin$ ]; _1 B" L+ C- S8 J
Description: Adds a new custom post type for site-wide notices.
& t9 Q+ e: S% j! j* j Version: 1.0/ k* K. M* _( B6 l) f8 h
Author: Your Name
+ L8 C0 S' d2 O5 |- e& L. e7 ~ Author URI: http://example.com
; U2 E( Y' ?1 x. G4 h */
# k8 v; {' M3 |+ Z. x, z' t: r0 {2 [/ `5 P
// Add plugin code here...
3 u h# ]% m$ ]8 k* P2 ?3 ]- k ```
/ B4 q R5 ^% ^5 d: o& v- k7 |) X8 Q* w/ A% H' F6 B
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。2 ^" [8 \0 V- b; o5 a8 o
; J) j" }1 G$ T1 w$ T2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
$ p/ R# _, R v% }" c. a5 d: @ {6 C* Q8 T' N( r* G
```
6 t$ U0 d( ^2 {1 J% @4 ] add_action('init', 'create_custom_post_type');
0 T% e/ H+ s1 {$ g function create_custom_post_type() {. [; S; R5 e ~$ S- ]6 ~
$labels = array(
/ m; y+ T. ~6 \4 n b8 f 'name' => 'Site Wide Notices',2 Q) f3 K0 O, r4 U& e% B' x
'singular_name' => 'Site Wide Notice',
G _. B1 @* i 'add_new' => 'Add New',
5 N5 Z1 T1 r- T# h 'add_new_item' => 'Add New Site Wide Notice',+ v* w+ U( P" O
'edit_item' => 'Edit Site Wide Notice',
, ~; N1 ?! m9 Y" v. E5 S( }" l 'new_item' => 'New Site Wide Notice',
; v# e9 e: |3 q; V* E& @" {- J 'view_item' => 'View Site Wide Notice',
6 h2 L5 [- f, \8 Q$ s/ g 'search_items' => 'Search Site Wide Notices',
/ E9 W4 d( H" G+ X% | 'not_found' => 'No site-wide notices found',
4 i4 W8 B& e+ m' g) B# l/ u 'not_found_in_trash' => 'No site-wide notices found in trash'
; Y$ a1 o v1 J/ E );5 e; _) ^4 b2 e9 S- k4 s: q% i
+ \! K2 w) d2 M' q) e $args = array(
2 t$ i% s! V9 a. _3 R3 v 'labels' => $labels,
+ A$ {/ n* M$ b, G* {. t$ H8 C# R 'public' => true,, [+ v4 @- b! ]! N+ N2 o! @
'has_archive' => true,' l% ~. e8 f1 T3 q V1 @
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),, L2 N3 P" A# p& ^# ]/ H
'taxonomies' => array('category', 'post_tag'),0 Q J8 z7 J/ h& r) {! k2 y0 X. ~
'menu_icon' => 'dashicons-megaphone',
8 y6 d- X5 ~: x2 j1 J6 [ 'menu_position' => 5,
; W$ x& {9 G/ S! O& P 'rewrite' => array('slug' => 'site-wide-notices')) ^* s( P2 w0 @1 B g3 [8 L$ W
);
5 R/ g% e% r4 r9 _! Z# e, Z0 |" I# F6 A' n( k+ V9 `& S
register_post_type('site-wide-notices', $args);
5 ?. u8 a# Z. o0 `, q0 j, H }) N- E- _* s; M: ]/ [6 a' V# A
```6 O' t* S' P( E0 T
0 a0 y& N& t/ s% _
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
& Z @, C6 b7 C' O: J
, @# d6 c9 [& {5 X$ g" {3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:, z* f" \4 A' y, C+ h% a7 }5 E
. n6 `9 h. U# ]
```
4 |; }1 U% w) N1 G x( b- B& r add_action('add_meta_boxes', 'add_site_wide_notices_boxes');+ B& U) f3 X5 {; d
function add_site_wide_notices_boxes() {
5 o# h- [8 @# K7 d add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');: J( S8 |) }3 X( x
}
' l" E" m; j, U+ _: w1 v/ S% v
\5 R8 z, D+ h* O5 F# T$ Y function notice_details_meta_box($post) {
9 h% l6 y' }% Y3 J' b5 ~ wp_nonce_field(basename(__FILE__), 'notices_nonce');
2 W) N8 h& G# X x e $notice_title = get_post_meta($post->ID, 'notice_title', true);0 M, }' b, e9 `- N1 @! Z
$notice_content = get_post_meta($post->ID, 'notice_content', true);
( z5 _4 M7 S& M ]0 v* J! J) ` ?>
0 g6 O! z. k2 L' h2 B4 f <p>
% p, v. E1 g' @, r <label for="notice-title">Notice Title</label><br>
* v; h6 k$ b7 U1 S# m* y' }8 _( ] <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">. m' J- j L! p. h/ l
</p>4 x w1 n* R! O3 {4 O
<p>
e6 L* ^- a" ~0 q <label for="notice-content">Notice Content</label><br>% ]& `" y% {- |& { N& h2 j
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>- J1 I2 |9 M0 f& H4 { ^9 J5 i' E
</p>
5 N7 M# W5 O) F3 W6 T; | <?php
k" ]: { ]' c+ e5 E" Q2 Y }/ s' o9 Y: K# J
# Z# `5 G+ E" b$ W( T$ y add_action('save_post', 'save_site_wide_notice_meta_box');/ o" K! b; a t
function save_site_wide_notice_meta_box($post_id) {
5 a0 S* G9 p4 Z: W1 v if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
; b5 G. v2 ~' P2 S: v% ]1 j7 I return;( q5 I5 o% s1 T2 W# T0 o
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)* _- @, Z5 ]3 V+ w" s+ x- Q' `
return;# i8 A) `, t1 Q# `8 v
9 L F8 @7 L" S
if (isset($_POST['notice_title'])) {
+ V1 c6 F6 W7 p! j, t$ P Y! J update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));" R7 V# ~# v! X7 }1 \
}
* Y' ^$ k& o7 K9 L6 m. n; { if (isset($_POST['notice_content'])) {' c/ J/ k0 Z0 ?* k# O, H5 r+ x! R
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));. ]# w) ?/ G. T
}
) Y# `" h7 O! {/ m, J1 j3 Z }* U* J! i( R7 H, }+ \0 w
```
" u, s* u4 H- z0 P/ x& @! o+ z9 B
0 m3 J! N; n+ s2 Y 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
4 U$ ~# l t: M1 J
1 n" U! a& t2 W4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
& `* [( g; e! i6 v2 V6 }3 r8 p9 x G/ S4 U
```
% l2 R# J- V7 i3 | $args = array(" V0 l( ?* }4 y2 s
'post_type' => 'site-wide-notices',, Y% A2 z+ X6 s. T; o
'posts_per_page' => 3,' g2 O- S3 Z! R* A' m( n6 I
'order' => 'DESC',
! t; f& O) H* [& l: F4 i! P 'orderby' => 'date'
3 v# s; d7 H+ A) H );+ ?& \' o. }7 H
$query = new WP_Query($args);
6 r$ y0 A" U& e' D$ y if ($query->have_posts()) :
2 h1 G& g# i# K. E/ s0 Z% q0 o while ($query->have_posts()) : $query->the_post(); ?>
. b" x; B& I/ p1 E( Y" N; q <div class="notice">
, s, {8 p$ }: L$ n" o( e <h3><?php the_title(); ?></h3>
) Z5 |, V! x; K3 [7 t {& a <div class="notice-content"><?php the_content(); ?></div>
- E+ e9 N/ f: n* `4 } </div>3 e2 f) x/ @/ Z( ?; Z
<?php endwhile;- \, M. b K0 Q$ D
wp_reset_postdata();! x- \" s6 b$ _7 ?4 c! i# R
endif;7 \: Y; e. Z& n2 r/ \
```
- u' J3 A- i& U m+ u h' K( K \, w2 ~7 c- V$ n- M
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|