|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
/ V8 S) }1 F" X& ]4 P9 K" o
; x* z s$ z2 F2 R( c如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。$ M* R* T* [0 z# |5 O$ N5 `# U0 r7 t
7 u$ ]2 |5 s3 G" T8 f% ^0 Q5 f以下是创建自定义插件的步骤:3 A+ V; \1 u) U. Q& n# {; Q
& ]/ e% U! d2 o4 U
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
4 r* B+ |/ Q; Y; N: R
: B- Z( r" \4 |. u ```+ \9 v% e, l! o$ T
<?php& @ { E& W1 M) x _; l
/*0 B/ @3 P$ Q& w6 t# U" u4 J
Plugin Name: Site Wide Notices Plugin. y# `1 j& w- q0 z6 K0 S$ k5 j
Description: Adds a new custom post type for site-wide notices.
( h# O& y. |$ f! C( c' ? Version: 1.0+ _! Q1 h K( z* V
Author: Your Name
! u" M, g! c4 I6 s( l Author URI: http://example.com
, { }2 b* p7 _ */
`/ T) ?5 g# y4 [3 Z1 V( b& T/ ^ a7 V
// Add plugin code here...( J& q F# D Q: ?, S$ \4 J2 K
```, ~0 {3 V" R5 l J, h
2 c$ Y, A- @; @9 l; S; H: f( w 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
; _$ I/ t7 O4 p9 d$ @+ }" q& q; D
! D, ~; c8 O4 U2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
6 ?" T9 s8 |1 A( ` D3 j" m" w# F: O* ?+ Q+ c# k
```
M A1 E+ P1 }* E. L: i# r. h add_action('init', 'create_custom_post_type');, {- n; y: T1 B1 c1 g
function create_custom_post_type() {
: L& l6 S( d. b& G( R2 C' x$ E $labels = array(
3 |8 X4 S3 J3 m/ ], v 'name' => 'Site Wide Notices',9 V& R! H% p& e) w# e: q
'singular_name' => 'Site Wide Notice',5 m4 Y, g( m; r+ T c
'add_new' => 'Add New',+ n9 D# L* r ]3 y; x' C% q
'add_new_item' => 'Add New Site Wide Notice',1 f5 _. u, V8 y' ]9 [$ d
'edit_item' => 'Edit Site Wide Notice',
6 M- i! x4 A5 I7 s. I, G 'new_item' => 'New Site Wide Notice',
0 ?1 e# [9 ?& j9 [) Y4 _ 'view_item' => 'View Site Wide Notice',
: ^1 k8 z; {: r 'search_items' => 'Search Site Wide Notices',
" q, t: b8 }( W6 Z& n# e7 B" P' H 'not_found' => 'No site-wide notices found',
/ d& t ~) L- D8 q2 o 'not_found_in_trash' => 'No site-wide notices found in trash'
7 z8 b8 V6 {9 O6 D. z );) S# w/ t0 M) `2 @: ]0 M
& w/ z$ g- K$ ]. e $args = array(: G3 v4 o: c" y) m
'labels' => $labels,+ L" v4 U" j9 E& G: \+ b3 j
'public' => true,) Y& l; e2 w& H: s$ w# j. L1 Q5 x
'has_archive' => true,
4 T4 o9 F. z& ~9 u8 J+ | 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
% ], y8 t5 R6 Y4 O- K+ L 'taxonomies' => array('category', 'post_tag'),4 R1 u& B7 X3 S2 O/ T
'menu_icon' => 'dashicons-megaphone',0 a; W: q, a! e$ f
'menu_position' => 5,, A, v2 T& k9 l4 `
'rewrite' => array('slug' => 'site-wide-notices')* H+ I" O' X. D/ u) c
);- r5 a( _/ Q! |6 V' ?9 R2 ~+ B9 W
1 n5 a" ?- R: y- h2 t# T2 g* _ register_post_type('site-wide-notices', $args);
. N6 e) z- C* E( v2 I } P6 D0 K2 h$ k( L. D1 ?* M5 {0 P" }
```! f9 P1 S, a- B3 T/ V2 c2 B
# I9 D3 E! m9 }# @5 Z 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。& A2 e$ e( `" @6 k' m& M' G( B
; z6 H G! Q% z
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
3 ^5 J7 m; V; T2 F- j& w* l& T
0 b& [( q' F: S& P' J ```! g* r4 v' O8 f4 j
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');6 h: J( C. @) I: X
function add_site_wide_notices_boxes() {
- p, W4 q$ v$ c0 \ add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
5 F5 k2 x' Y7 i) P }( U+ o$ [2 r2 W K# `* Q
! I6 ]/ L, T. \5 C0 u function notice_details_meta_box($post) {
8 Z$ ^# a* r, d r/ C$ P wp_nonce_field(basename(__FILE__), 'notices_nonce');; D9 G+ k& Q5 g4 {8 p: L
$notice_title = get_post_meta($post->ID, 'notice_title', true);
7 \' \4 c2 P5 ?- G, S h! |3 w $notice_content = get_post_meta($post->ID, 'notice_content', true);
( [0 ^$ p; n$ n- W6 G ?>
, V5 y2 V# J5 Z+ z <p>
9 U2 z* m y. c0 I <label for="notice-title">Notice Title</label><br>% I2 W; [1 g, P4 w
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
# s! K; h8 t7 X! f0 J </p>
. X( S3 Z v4 W6 [- } <p>
! B4 G s& M$ @* T% P5 g( O+ S <label for="notice-content">Notice Content</label><br>( D( F0 b3 T" z* V
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
0 p* U2 k" f) T </p>
! H9 W+ G V9 {. c4 x! S <?php
. R8 E7 d3 E$ r" t }$ G3 J; d2 }7 x' W: T/ ?9 p
" J" M0 p& o1 w. K- N# l* ~1 F* q
add_action('save_post', 'save_site_wide_notice_meta_box');
) g0 K+ S; b# G( j3 W" { function save_site_wide_notice_meta_box($post_id) {- _: U7 E5 p! i* j/ t3 _6 Z
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))6 z9 x6 ~8 Z7 E+ f) I q& k7 ^
return;4 L$ [( i# X. H3 e
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)* C E4 q7 J4 C9 v, h) v
return;7 y1 g! D6 z* e$ z: O! q. A% P
, y2 _6 Q9 \/ z if (isset($_POST['notice_title'])) {+ A3 N9 y8 L/ t, \
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));6 C- n- M& I4 i. E
}6 Q9 n+ M" Y2 j, @) E2 H! N) A" w
if (isset($_POST['notice_content'])) {
: o, j" \! V( V4 Q2 |2 O update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
4 `$ D; t4 g+ L" t+ W$ X }* O) C9 {* ?5 f
}7 u" H5 d3 F8 j3 Z2 Q2 {4 S3 L
```" }! @& i9 ]4 X, c% {
, ~7 ]! F' u3 y' b; F( N 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
m0 X" u3 T! x# i! P8 S1 L2 P& R6 i# y
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
: z0 N8 K0 L$ f; D' r8 ~2 C
, x- }% |3 |' C# h* I t ```9 H8 S* {7 p+ {/ f5 b
$args = array(; n" z4 G! \9 I6 t
'post_type' => 'site-wide-notices',
7 {0 ~+ u$ z$ o9 A* n 'posts_per_page' => 3,
^( ^: g! I3 `: q/ n! ]' g 'order' => 'DESC',
7 Q& g9 _( h/ h/ q1 W0 z 'orderby' => 'date'
: S/ B+ _7 y* R3 N% p );" h& c( O! j8 L9 O1 s! u [
$query = new WP_Query($args);% n2 p# V4 a% ` }. l
if ($query->have_posts()) :
* G$ l% R# A! U while ($query->have_posts()) : $query->the_post(); ?>
t1 T% W4 j. } @5 F' u" |6 X6 l: @4 n <div class="notice">
$ R6 h/ A* a; M. |" J <h3><?php the_title(); ?></h3>
8 f5 r8 E) c) s. }8 w# v <div class="notice-content"><?php the_content(); ?></div>
. [: S3 \0 [. W! j; T4 k" c </div>8 }+ m' X O( [7 E- q; M# }
<?php endwhile;
9 w1 V( Q7 ~( q& g& ^. @# B7 v wp_reset_postdata();% u |) X( P0 S+ R7 k/ Z
endif;2 Q; c' l* f% v% @" _. w2 M, L
```- Y# i9 L- F/ n7 a& {/ z* |5 [
1 X/ I/ \! Y" i5 P
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|