|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?; B5 z. v! L5 Y1 h* m" C' R. j+ V
9 A% |/ r6 a) o! @2 L如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
; G. E+ A. s' Y2 E
* Z3 I# T% V; Y. a8 x( r+ ~以下是创建自定义插件的步骤:6 S& l, `; U! p3 i7 E
8 r0 @' A" g s$ ^: e( k1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
# P; ]) w( c) I# h9 o( k! S. V) f( s/ D7 f/ W5 m
```
! B: w y; N8 c! b <?php( ~9 {7 o1 |: M$ w$ Q; H# B3 ^
/*
* n9 `2 q, u8 l& u& I3 Q Plugin Name: Site Wide Notices Plugin+ T2 E2 g$ w4 ~7 ~# ?6 D( K- x
Description: Adds a new custom post type for site-wide notices./ {6 J$ {. t: s* Q* F) Z
Version: 1.0
- @% y/ e$ \# u+ q0 D9 N Author: Your Name
& v4 t: y! S) S; Z Author URI: http://example.com/ E8 U! y' y& {+ @ }" M
*/
( }) Q: L2 w; L2 b1 _5 u
/ x% a- _' ^) C) @( a+ } // Add plugin code here...; ^- A" t! }, s3 _ ~: W" K: _8 u; D
```; l0 w* E5 ^7 U% l7 O
! `: m$ P& f9 ]+ T+ @ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。8 `+ d5 {( x1 D8 f: S4 o
9 F b/ u; B+ r/ `, g5 A, E' V2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
3 s, |, G6 Y' l6 n/ E% H8 d
: L) s; Y' @& X ```& X) Q. e+ A) G% F& A
add_action('init', 'create_custom_post_type');7 e# X! x' U( @7 \' s
function create_custom_post_type() {
! U# D9 T7 y. y $labels = array(/ f' Y# Q4 d, k" Q/ u6 a
'name' => 'Site Wide Notices',
+ x; I @& w+ v$ z6 Q# T+ k( D 'singular_name' => 'Site Wide Notice',' e f8 v" k) o" h+ m
'add_new' => 'Add New',
; J: ~3 `2 h! U( D1 _- G 'add_new_item' => 'Add New Site Wide Notice',
3 K# u* Y! U4 c$ H 'edit_item' => 'Edit Site Wide Notice',; v( r# r {' m. t3 W$ }) t: P
'new_item' => 'New Site Wide Notice',+ P x7 _) O7 e3 z/ e4 r( h! e
'view_item' => 'View Site Wide Notice',
, S9 u( }2 b! x3 V8 K' G 'search_items' => 'Search Site Wide Notices',: V. `' n' {' K. ]6 w) W) q( y0 R
'not_found' => 'No site-wide notices found',; Q* A I# k J/ w& i" \! ` C1 E
'not_found_in_trash' => 'No site-wide notices found in trash'
4 b" O1 J& s7 T" j) B- x ); W. w+ d0 a5 U( ~
6 a, S9 L/ ~$ z' q+ ~ $args = array(
" b/ {, `3 h4 [1 Y9 ?- E 'labels' => $labels,
/ E6 F5 s I: w0 s/ p* H/ _ 'public' => true,6 r7 Y( f8 O5 I
'has_archive' => true,7 r$ H& f9 h# k* X& }+ x0 s2 O& s
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
3 C* N) x, c0 O 'taxonomies' => array('category', 'post_tag'),: O/ V5 \7 ?$ H0 X3 w7 o5 \1 G
'menu_icon' => 'dashicons-megaphone',! ]+ L% ~$ ?- E
'menu_position' => 5,( v5 R; x: F, @( B, J: d
'rewrite' => array('slug' => 'site-wide-notices')
" G6 D) P+ t. N" P1 K ?1 s );
5 t+ M2 C# k L# l/ ` a/ P, M# T9 ^' g
register_post_type('site-wide-notices', $args);
7 p2 H, U/ I' e0 E5 u }9 X* Z/ u5 U6 \8 d
```
# h7 y8 P" b" E7 T- a G G: h) r' c$ }7 P/ t% ]- c, ?4 Z1 U
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
: w! F5 I; [9 N4 i) T) k% O% w7 k0 R; v% L! J
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
; s& S- e% s5 U }+ g) Y( d
5 X" j& y6 ^7 B ```
& l o5 O# R1 d' f! p4 y$ G0 I R$ q add_action('add_meta_boxes', 'add_site_wide_notices_boxes');6 h, ~7 N" S4 {% h8 x
function add_site_wide_notices_boxes() {
2 n6 a& X2 \ E$ A3 S8 @ add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
7 \" F" C9 D* K6 B4 s1 R3 Y) ^ }. p1 Q5 I* I! N+ B% ]- P
% V) @, X5 H0 e N3 D" L
function notice_details_meta_box($post) {4 ^4 c1 M7 V4 U; F' c9 t7 ?/ L3 T! A
wp_nonce_field(basename(__FILE__), 'notices_nonce');5 k: l, E; G/ N' G
$notice_title = get_post_meta($post->ID, 'notice_title', true);% v% z( Q" l U- m. I+ n; j( E+ \/ @
$notice_content = get_post_meta($post->ID, 'notice_content', true);% ^' _2 |. J, R. j' J
?>- L; _: U3 _% y/ \# t; \
<p>
/ D6 g, M4 Y4 S- o4 I; _ <label for="notice-title">Notice Title</label><br>0 N5 k# i) A' R- E
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
7 \* H$ X9 ~/ @# |$ W: g( J </p>
4 G9 N0 k+ |8 z, w$ F! h <p>, f: J% c& j2 g/ v& w7 b0 l' j
<label for="notice-content">Notice Content</label><br>2 _$ K1 y7 v8 w, j1 p, L
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>% l2 ^. X3 B S
</p>
! t+ y3 `9 U2 r3 E1 } <?php9 S) G( M3 k5 m4 l4 w3 M
}+ K% V4 g* `+ X( {
4 w7 J) [% L# i+ ?% e8 q add_action('save_post', 'save_site_wide_notice_meta_box');3 G( J# ~) h1 z( G/ y0 ^! `) H
function save_site_wide_notice_meta_box($post_id) {
" i& E5 v. C2 K' o, o; V" q if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))): b" b% H F& I* @
return;
1 e/ Y. d) r# ~' Z# }7 L& _7 }4 d if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)2 O( U+ S q0 s. U8 x& d% r$ K: M
return;
5 e! E- l" i# L$ T0 F
" `; s0 n1 ?3 v- y2 M if (isset($_POST['notice_title'])) {, c. Y; R" e3 @, ~" p2 [
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
1 F) w9 L% ^# k% q4 e5 O9 b }
) n! w7 X9 c+ M* ?8 J if (isset($_POST['notice_content'])) {
. _* j: Z: G( Q6 K; |7 [ ? update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
/ r+ h+ P8 Q4 \ \ }
0 S1 g' \9 ?4 z1 b$ h' } }
. M1 Y7 [) U) }7 l" E ```- \& \1 r9 x1 @9 Y! s8 X
. N( j* @$ V! \! u3 \
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。, _+ L/ y+ L# d" r: T/ }" z/ x
4 a2 N0 }& d9 O* i0 C7 o+ h
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:/ [$ I8 Q. d9 B
( S: ^! b: Y3 ?; { ```
1 s9 l" U& F# a1 \' C $args = array(' u* {0 |( V2 o y$ k, O; T! [
'post_type' => 'site-wide-notices',/ j2 G$ K$ U8 G1 `$ ?! P2 t
'posts_per_page' => 3,
9 q+ a' E n/ W9 {6 l 'order' => 'DESC',
% r# M7 R7 R, E' i 'orderby' => 'date'
j* |* w# _. E0 g );
; |# f% ^# p+ y. c/ O. U $query = new WP_Query($args);
3 T/ U+ U7 V; ^' [$ I if ($query->have_posts()) :9 F2 E) |& n5 |
while ($query->have_posts()) : $query->the_post(); ?>
& P, `/ v4 A' v6 _/ \; j2 _, Y <div class="notice">; A- U4 `: q! p B& |5 V
<h3><?php the_title(); ?></h3>
5 C+ a8 T. Q" c" } <div class="notice-content"><?php the_content(); ?></div>
( J. s. x2 j: Q3 H, f" f </div>9 X1 d1 Q* B& `9 u/ d. v, h- y
<?php endwhile;- ^ M* k( k) y5 u. @6 g
wp_reset_postdata();$ Z" Y3 J* r$ K3 ^0 t
endif;
7 b' m6 T7 t8 ]0 f% M$ ] A* m! u ```
) r! h$ p" \/ N. O9 W* C
1 d' o# [- g9 V 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|