|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?. ]; ~2 E c8 A; F* [ D4 {
/ _2 {; C% L) y1 q
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。( c" c( {9 I0 b
! H- o6 p' Q' c" }$ C6 o
以下是创建自定义插件的步骤:
% Z4 q/ w" X+ o( h- w
Y" S& H, {' `- @1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如: J/ H) ^: @: f% `* \8 P
$ T0 x" c. Y4 L) O7 ~3 y9 [
```
2 V; C6 p' r. y <?php
" ~8 Y0 b# D6 |7 m ?2 V7 @' \$ e/ W' c /*
9 \7 y! H0 |% o; o Plugin Name: Site Wide Notices Plugin/ O3 s. }2 S7 ?: @8 z
Description: Adds a new custom post type for site-wide notices.7 Y& c3 u5 ]3 J$ g* m W& J
Version: 1.0
# G8 m/ ]0 J; M* m Author: Your Name- p4 U+ M$ x7 ?6 ]
Author URI: http://example.com. E8 `6 i6 S# e1 Z
*/
$ v# B: f+ V9 c; ~
: C, @$ k R1 W, S" a$ N( J% x // Add plugin code here...
/ _4 G4 v, I# c) u( ]# T4 b# M ```
* l/ B J2 ?3 j1 \! U8 N) J3 ]% K8 X! c; n5 ~ r7 R
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。2 F3 R7 U) ~3 O& K0 m& ]5 B) n
7 ^8 G5 l# @- e7 \' V8 B2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:* @6 Y- q: I. E* T' V8 ~6 X
: t- Z2 T! u' c# w) A7 G4 F
```
( E1 _' B) U. M- [( z add_action('init', 'create_custom_post_type');7 t& I+ C* q2 Y4 h' w
function create_custom_post_type() {
9 ]) I- Z' e1 u1 I; E- m $labels = array(1 y+ B4 P. R! \- {; R p2 K
'name' => 'Site Wide Notices',0 \6 Z) i# Z" I+ f( C4 Y M
'singular_name' => 'Site Wide Notice', V3 L% D6 J# j& l% f+ N
'add_new' => 'Add New',7 P) V+ L9 [: g; g2 D8 K
'add_new_item' => 'Add New Site Wide Notice',
( ?: f: m5 G* m" {' G' ~ 'edit_item' => 'Edit Site Wide Notice',
- f3 r1 o6 c# N0 e8 H1 [/ m* u" j 'new_item' => 'New Site Wide Notice',
7 c% D v: k+ z# d. N0 l" N! s 'view_item' => 'View Site Wide Notice',
) R4 b3 ~9 U) }2 I1 y" k 'search_items' => 'Search Site Wide Notices'," ^ L( E! D# v! F& U
'not_found' => 'No site-wide notices found', x5 N9 }, L% h7 n: r2 j9 H
'not_found_in_trash' => 'No site-wide notices found in trash', @6 a9 }6 D3 H# [$ d
);
/ {& r, C4 C! J2 g' ] v* _9 j( N' u
$args = array(2 k# G/ ?; @3 H+ ]/ o3 k l
'labels' => $labels,% u. S8 v# a: O& g/ W5 R
'public' => true,# |2 o5 V( \( _+ h& _
'has_archive' => true,- [, t+ m: z# O# ~# s4 |. q* Y
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),( T8 [4 I- m1 w% u& {
'taxonomies' => array('category', 'post_tag'),
y! B0 L0 {1 B4 O& o$ c8 R" M 'menu_icon' => 'dashicons-megaphone',9 e4 _3 R: s$ T1 ~ L& d
'menu_position' => 5,
9 _$ Y. M4 }9 |- } V 'rewrite' => array('slug' => 'site-wide-notices')8 v5 W" j: {3 |' O+ l
);
; }5 ]6 s p4 N% \( r
6 c! [: M4 {* {" a register_post_type('site-wide-notices', $args);9 K8 {# m& @/ v$ |
}% F, l( t% n/ D/ f$ X2 _
```8 [. I6 R: @! ]% V
{, v+ i5 p4 ^2 W 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。' A8 f: b5 Q: {( W% ^4 b
! @( Z; U& o6 b
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:$ L; H0 L. t: j2 L
& c9 I7 R! w4 C) F ```. o# y7 ^5 R% @" J
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');! M. V5 s3 T5 I
function add_site_wide_notices_boxes() {7 L8 p+ k4 g+ S# ^" R
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
, w( T' O! @6 h* m; ^* j1 _ }
4 j* o. b3 B, R" o$ h
/ O# k/ e3 B% |' l4 i function notice_details_meta_box($post) {
l, @) T5 d) b, [. D: H% ] wp_nonce_field(basename(__FILE__), 'notices_nonce');+ ]$ u1 C+ e* |& I% Y
$notice_title = get_post_meta($post->ID, 'notice_title', true);
( T, u- M* n; W2 Q $notice_content = get_post_meta($post->ID, 'notice_content', true);
& P3 J; ^! f, F# \, `' W3 Z ?>+ V! Q l& T, U/ @. C
<p>. b4 |; a3 ]0 x! O3 g9 }* t! q
<label for="notice-title">Notice Title</label><br>/ K9 }) [) y! x
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
1 Z4 O6 f/ W' ]3 \, @* F8 J+ i </p>
- b3 ~1 B! V L& J: W7 a5 l) g6 e <p>
7 F* K! G# J% a <label for="notice-content">Notice Content</label><br>& ]. p/ P7 R* d" j$ k0 J# A
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
9 k4 d5 }2 g' {# q. Q5 E </p>
4 H4 U! @: K" ?, H' ~ <?php" s: {7 j# \4 B! o3 C
}' N$ Q" p! _# E
/ r# ]9 @5 l n
add_action('save_post', 'save_site_wide_notice_meta_box');" M4 o' k( \) W0 B/ {4 |
function save_site_wide_notice_meta_box($post_id) {
! h0 N5 I- x, w' g$ H( O3 u if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))0 C. Y/ a* i3 i6 F$ c; J
return;' l+ Y! G' g# j4 L1 K# ?
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)4 L: z# n {3 X L7 b. ^" @
return;- G, K" T5 S* m
A% o" S3 h7 o0 K8 y2 O if (isset($_POST['notice_title'])) {" V. e" b. w1 m; e8 A3 T
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
2 l0 z# h) G8 t1 x* V }& ~, [% u6 M2 G9 o- o8 j
if (isset($_POST['notice_content'])) {
% H8 Q3 O% o. K, |% j update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));9 S! n ?; P5 ^
}
0 l/ b: ~4 ?9 w4 Z- G! F }9 z$ V; o. A2 D- w3 o8 ]
```
8 \: r& J! U0 V. C+ ] D |# G7 k6 a2 N4 Y! i; W/ C
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。, z2 ^/ {: y4 u% F
6 I- H; r% ?% e8 u4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:8 L3 P$ j" M6 Y1 F; L
# ~: z9 l ? N# K( U
```
* `& P; a1 y9 G4 R0 H- O6 a2 F $args = array(
( H7 E$ ], W t- J. e 'post_type' => 'site-wide-notices',
' T6 P+ K, n$ S9 i2 ^$ W 'posts_per_page' => 3,8 i0 D; \- d9 J, P- S* n0 F
'order' => 'DESC',7 K, k+ m7 i" n! ?3 y
'orderby' => 'date'
! O# |; L$ R; X, z) A- t );
9 X/ g2 Y8 A" b1 u6 u $query = new WP_Query($args);# e; S6 J4 L4 L6 k* G
if ($query->have_posts()) :1 m$ x7 I) J/ d, T! a
while ($query->have_posts()) : $query->the_post(); ?>/ {& ~: G& b0 A! P0 H
<div class="notice">
; M0 e6 P5 h$ I8 N2 W <h3><?php the_title(); ?></h3>+ Y" `; K. v9 C2 ]
<div class="notice-content"><?php the_content(); ?></div>& h3 W6 ~4 m( F/ s% o
</div>
* ? A5 M( M/ G: G" v' w <?php endwhile;
+ b& U& f7 b( ^* q3 e8 J3 Q wp_reset_postdata();
* c P( _5 o, y5 l3 V8 Y( e endif;
2 l! [5 c3 ~$ C) g8 h5 K6 w ```
) v7 r2 M4 T4 t a- ~
D6 O) o' l/ [- v4 N# c" } 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|