|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
; x9 C' k$ p* M; o! r6 |& C$ B3 N+ i |2 \
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。: Y* V4 |7 C8 D% \, h8 B% z! P3 T6 j
) |* d( ^$ p: }
以下是创建自定义插件的步骤:
* X0 p! _+ B6 |& o) n' P6 {3 O0 Z! ~' o) s" F4 z% Z
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
. C4 g, k+ c* ^0 |) ]. I6 U/ ~7 T y' A
```4 s: E N) k {( |, L# m$ ~
<?php
6 Q3 a3 y( ?, ]* `+ d9 L /*5 w0 T$ ~! X$ h7 P" T( X6 q% r
Plugin Name: Site Wide Notices Plugin
' ^# n& i" H% ~/ K$ ^ Description: Adds a new custom post type for site-wide notices., [$ v; ]% `* g' ^, E! u
Version: 1.0
! R$ V8 S# H: K0 s- }, I& R Author: Your Name
6 T5 a' p4 N8 { n; T, E Author URI: http://example.com7 b P' G; }. M- p" `2 v4 l3 X9 d
*/
R" r4 J9 N" o# n9 c B5 d2 y' y2 A$ T3 R
// Add plugin code here...4 Q' Y& u d( D, J. v2 |( {7 a, N
```8 a5 s/ K6 N G
1 A/ T4 q) s. T7 g3 L6 h1 g 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
% B$ e1 P {. E4 e7 w: N. A4 ]- I7 M' F( _
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:8 Z) w' h9 h! k) w
/ w* _ N1 H Q* G8 R ```
# O3 b5 X3 x7 f; _! f6 C add_action('init', 'create_custom_post_type');
" O& j% R8 J" u1 O1 v8 f function create_custom_post_type() {$ x% h9 v+ x7 S/ h* p& B6 ?
$labels = array(
$ ^4 y5 y# ^" o 'name' => 'Site Wide Notices',; w `: J% o8 U6 j
'singular_name' => 'Site Wide Notice',- \4 @5 C1 z. I* K' ^+ X. z
'add_new' => 'Add New',
. p, |5 B2 B7 }* @' f6 W# E$ C4 e 'add_new_item' => 'Add New Site Wide Notice',
7 w$ e9 k6 l5 I7 v8 Y$ p 'edit_item' => 'Edit Site Wide Notice',
$ u2 g! s' T% _. } 'new_item' => 'New Site Wide Notice',6 Y7 z2 T$ f: r+ [ w5 Q- J/ R
'view_item' => 'View Site Wide Notice',9 p2 A) n5 t8 T6 l( E! L
'search_items' => 'Search Site Wide Notices',
# S S" B7 S! Y9 r3 p9 c D 'not_found' => 'No site-wide notices found',5 y" {. S& i, X$ `9 d" V
'not_found_in_trash' => 'No site-wide notices found in trash'$ S2 ]4 w# @# N8 S8 d/ j. e4 L; f
);+ C$ ]+ [' W b0 F& G" B s) T+ H7 N
9 t/ e4 o( h3 l
$args = array(
7 Y& m! z4 N0 a3 S( L 'labels' => $labels,/ @* f( x/ |! c
'public' => true,
1 M+ f0 c$ O+ l5 g- S, p& u8 m' | 'has_archive' => true,
% E% Q8 B" i Q 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),5 O8 E* S% g0 j: `5 ~/ g( w
'taxonomies' => array('category', 'post_tag'),
0 Q- }" \) e) \6 e% B C 'menu_icon' => 'dashicons-megaphone',
+ z% B- p. C) D, m9 I+ l; o0 B 'menu_position' => 5,
( J! k! {/ d, }2 {4 E. ~3 ^ 'rewrite' => array('slug' => 'site-wide-notices')/ ?6 p+ n0 J. o
);& |+ h; o% p7 `" Z
0 C, d! k2 q i5 D( W- ~) x& z1 f$ O register_post_type('site-wide-notices', $args);0 x' t- i4 i( G4 C
}
- U( }9 k# v T- n, t+ z ```* Y0 C3 s. X% F9 d. Y/ Q; l& X5 ~% V
: G" C) Z% u. ~& B* F6 \8 q 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。1 d' _7 c8 V1 m7 A% O! k; L
, Q: P% i5 ?8 L( @
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:; R- J; D. M; k9 G+ `2 z
6 H0 w, v/ O/ _3 D4 b: K- Z- K
```
L- @8 X, w, Z7 f+ S+ J/ @" A add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
$ T4 }+ g" @: Y function add_site_wide_notices_boxes() {
4 m! M) `; O. y0 ?; E- U& Z add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');+ ^- f* [4 K( Z* y
}
0 s; q: P" \" K( l, Z4 \* v6 l1 o' k: S
function notice_details_meta_box($post) {
) M7 u- F) Y, c3 `9 k; z wp_nonce_field(basename(__FILE__), 'notices_nonce');
8 q: \- z2 u1 @' P1 x $notice_title = get_post_meta($post->ID, 'notice_title', true);' [% k" o% L* H/ A( e, c8 x
$notice_content = get_post_meta($post->ID, 'notice_content', true); A: [ x5 v4 X
?>- F3 H, }4 A# `4 u* q, n/ U% u
<p>5 [+ |5 L) r, p6 n
<label for="notice-title">Notice Title</label><br>
( s, u+ n) a" C& }6 [ <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
! C% H0 F) S9 x0 D </p>* J& L: ]) p0 @3 }$ L, y2 P- s( k
<p>$ T# z2 W' V6 b3 t8 U
<label for="notice-content">Notice Content</label><br>
- W! p j# x. P# R" D <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>$ ?/ x4 z% m6 ^2 Z: ]6 ?
</p>% W! }4 @7 J# X9 I& l
<?php
9 B+ J) X4 Y/ p1 u* r& r: g }
1 T+ v$ j$ Z/ T, j f( g
# X3 G7 }2 ~3 j% A add_action('save_post', 'save_site_wide_notice_meta_box');
5 R3 k9 k8 o) R# e. i0 }# D4 { function save_site_wide_notice_meta_box($post_id) {6 D: m. i, ^+ r# Q0 T
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
) \) G) t# p! u) ? return;
' w: ^8 V5 Y) U8 y4 _2 R8 m( M if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
4 m7 |$ `: s+ E return;
4 ]1 {" Y f4 O3 `7 k; r
' d0 _' z# u! v% Q2 s; ^! j+ z if (isset($_POST['notice_title'])) {8 w+ l: X) O6 x# [" u3 Z7 l9 H
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));' }5 F* M, h2 M0 ]! Z
}
) @6 ?0 B! @- T! k1 L if (isset($_POST['notice_content'])) {! K' H+ T4 J- G3 W+ h$ C
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
. r1 G4 m2 _. }- G! M0 [6 }8 ~ }( o: ~4 s0 Q+ ?6 R- k# P+ {
}
# H2 H8 \+ H# T2 a8 N9 i; U ```
* j. `1 k, R! E% X3 g
8 @' O: r* [3 c+ f. Z 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。 B' y1 u" P9 P a
+ `. W# Y. p+ T# {4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:% Z9 O8 F: _! z" n
; v$ E' C. B3 l. C2 U6 q6 s1 i ```3 r8 L( C& ?% Z+ t% ~% h+ z H% `8 D
$args = array(
& u! g2 C5 p. n$ r 'post_type' => 'site-wide-notices',$ n. G3 z; E: b
'posts_per_page' => 3,
# t. {# p( B4 T 'order' => 'DESC',, i% D: n. r+ u8 M
'orderby' => 'date'+ D M, D/ ]- f e* K0 O
);
* `. A9 N6 P' V4 i" j $query = new WP_Query($args);4 F% O4 n+ _# u- ]5 F
if ($query->have_posts()) :
& X2 M" ~0 H2 ~2 _2 ` while ($query->have_posts()) : $query->the_post(); ?>
1 j/ O% @" R, [. M <div class="notice">$ V* X* S7 J/ D/ t* s
<h3><?php the_title(); ?></h3>4 K- l2 A) I! B. m+ c
<div class="notice-content"><?php the_content(); ?></div>0 {# ]& }7 d! q6 N5 L$ p* V% V8 o
</div>- c4 H! S8 h! w( I
<?php endwhile;
3 G1 r0 }9 {/ o O$ w wp_reset_postdata();
; G6 k; x' Y' F7 a endif;
$ b% D0 ]3 q) d5 O. U' w2 P" \ ```$ f0 e3 t. M- F* w0 w
3 J- ^$ M/ W& ^$ t8 u+ b4 ?
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|