|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
3 c6 f3 v( h# L; g/ \0 b
9 I+ ^- {3 I. k- n' |) m如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
' C/ ~1 f3 x7 \/ d3 d7 o1 w
3 i1 w; M; E- j. \以下是创建自定义插件的步骤:
|- v( n& i- f
! k$ R4 U* F3 E; c% W1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:3 \1 q% c# s$ V7 f+ E1 _3 J
* E$ k: X+ n5 K& }$ _1 ~/ I
```! H7 h9 _* h& Y2 [8 @
<?php
" @& y# g- n) [1 Q* Y) c! M /*
; H4 }/ B! E6 \& c1 O Plugin Name: Site Wide Notices Plugin c: r( V, F2 f4 J' ]8 p# D, L" {
Description: Adds a new custom post type for site-wide notices.
8 l! N8 ?( @" w' g r6 ` Version: 1.0
( P& d0 K G) v- V3 H Author: Your Name
! y/ _% X* R$ K, @2 h, P$ I6 ^( i Author URI: http://example.com
7 |5 }) p6 C) D. D+ w& n/ ]) d */
6 l+ x- S7 B2 l E3 V+ K: S# G# M- Y4 q( m' j: l9 p; ]
// Add plugin code here.../ S$ c0 r/ v3 L$ O
```- L4 x8 i; K9 w, G
8 G- V' e- z/ t. T8 N1 ~! g
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。* W0 N0 {1 j5 V% G) b" r8 C, E8 g
( `' ~7 e1 i# @1 b* r
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
. j. z4 C( i, f
7 i2 ~% f2 h. Q6 l% ]; \- P ```7 n6 n, [: o* J4 N! Z: @- t
add_action('init', 'create_custom_post_type');$ {; e9 C0 E, u9 P
function create_custom_post_type() {
9 M! S9 K4 _/ k- B. A $labels = array(
& i, q2 A9 e" o$ Q 'name' => 'Site Wide Notices',# G9 q- p3 }. V" t
'singular_name' => 'Site Wide Notice',- B& `. a( @( J, s' e
'add_new' => 'Add New',- o: V" {; |' A/ D
'add_new_item' => 'Add New Site Wide Notice',
2 ?6 W% K5 x) K 'edit_item' => 'Edit Site Wide Notice',/ t* ^3 B2 s. X5 i
'new_item' => 'New Site Wide Notice',
: X( Z) e; a2 C3 l7 \5 b% j& D 'view_item' => 'View Site Wide Notice',! t" l& k+ j4 R/ A" m
'search_items' => 'Search Site Wide Notices',
% O; `6 u: H D7 ]( v 'not_found' => 'No site-wide notices found',
% I$ X8 ^9 O8 M8 B( i' P! d8 ?" t 'not_found_in_trash' => 'No site-wide notices found in trash'
; a' q7 b) t7 t1 ? B' f );) ]7 A/ p- q. c3 q
. ]# O3 M- J7 y4 x' d" z+ W $args = array(
3 Y* D# i6 K" O- Z" k G 'labels' => $labels,9 N3 K( k3 J- B% h# j% V+ d3 {, ?
'public' => true,1 y2 j- s. S5 h7 P8 T
'has_archive' => true,
* W# ]6 h; q. A& T0 A 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
- e- Y4 @% f9 |9 n! z, i 'taxonomies' => array('category', 'post_tag'),
1 w6 Z1 ?/ Z8 G 'menu_icon' => 'dashicons-megaphone',
$ G( B$ D# X& | 'menu_position' => 5,, g/ F! Q6 ~+ h& l& b. f
'rewrite' => array('slug' => 'site-wide-notices')
% c+ N, V7 d: E' c1 F );% Q, R0 w; S- I" \0 `5 N
0 ]3 ]7 M `* S; r$ U4 v
register_post_type('site-wide-notices', $args);
5 @: O9 J6 z0 [ ~1 i }
+ H& S! x' [; u5 k ```/ C4 r4 v9 e W) Q8 \7 x2 f( Z
}* \1 u" q( \0 b" f F9 g9 I' l0 E
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。$ ?0 Y& ] {# {; I, o4 R: e* ?
}' J& w$ o- U/ t3 u3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:0 y' s7 d+ Z: J# b
# s: p5 x1 N# h! D6 U9 T2 A ```
# _6 x" {3 k. E8 o6 E4 z1 Z! V/ c add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
; @5 G3 j% G1 U4 j, m9 B function add_site_wide_notices_boxes() {
8 H4 m& P( o7 g3 ?; l add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
; D# i: p) d, k }
1 m8 \. z" b1 c' ^
6 T! v3 o, A% V5 f function notice_details_meta_box($post) {
& m( k- W; C' Q$ W' Q8 n+ _ wp_nonce_field(basename(__FILE__), 'notices_nonce');
' t: ^9 u# D: |/ Q5 P $notice_title = get_post_meta($post->ID, 'notice_title', true);" w! P3 d8 `! z* s% H# R; N
$notice_content = get_post_meta($post->ID, 'notice_content', true);9 i) _) J* l( ~+ O5 \
?>, H1 W" f- A. D5 Q% Y E+ r" Q4 N
<p>7 U2 o" V1 c: v* L- d8 [- A# m1 C
<label for="notice-title">Notice Title</label><br>$ H i4 g' k- e' V+ M' }7 w
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>"> e a" V/ `) L
</p>& k z N( S* @* y; i
<p>8 O d, [. Z* R. d6 J' I
<label for="notice-content">Notice Content</label><br>
! ]/ X" U- r" l" a; V <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
$ b3 z* B Q6 ]* L3 g. j </p>
1 ~ {! N5 U" g+ J) E9 O <?php) H+ c! P z$ r$ B6 a6 O3 K
}' T0 S7 N9 o6 Y4 X
8 v/ H5 o- X6 S add_action('save_post', 'save_site_wide_notice_meta_box');4 n& h1 k% v$ o$ E& y3 ?
function save_site_wide_notice_meta_box($post_id) {
3 F0 `4 k2 p+ F if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))4 g/ Q. J/ R! J9 `# B$ B
return;9 [) ~5 n* L8 @1 M% `1 ]8 ~
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)9 w* Q0 K) v5 b1 P& o: h- Y/ [' n
return;. ]6 [0 @8 d* G( t6 I0 @3 ^
' h% Y8 c& ~0 @1 x B- k4 L; [7 D if (isset($_POST['notice_title'])) {
) L- K6 G4 H( K8 O2 C* K update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));) f) X4 [( T& t2 y8 c% m) d
}9 w0 L" V. s/ w
if (isset($_POST['notice_content'])) {
" q# u4 `. e0 H2 Y+ c update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));6 a' D# n9 o1 E% |1 k- ~, d* r$ E
}
% j% b. ^7 l, \4 g" e }/ o8 f& w) w( g
```8 ?, [# [" d) i# i0 ^% f1 _6 z$ n
% y; o r8 C! ^: k 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
. k+ n& k1 W. P k" K. a9 m; X6 M' X' ^ v8 u, u! ~. Q
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
! l. J: }4 K+ P# V
$ V. J/ Z( x/ B, A ```
: m) A$ k1 i! {- K) L2 _2 ] $args = array(
* ]" s7 H6 W$ z7 F3 i9 Q! f 'post_type' => 'site-wide-notices',
, q8 G* W% B6 n3 a8 Y3 A2 C 'posts_per_page' => 3,6 R+ f W; J" @/ @1 |
'order' => 'DESC',; Q4 \1 z5 x% u
'orderby' => 'date'$ {1 ~' S, y& | c% H0 ]
);
- ]' ~( s, U( K; ?' w $query = new WP_Query($args);# h5 A1 H' Q5 h9 t4 J
if ($query->have_posts()) :
, K1 m: e, C5 E! U while ($query->have_posts()) : $query->the_post(); ?>4 G( u9 O* B# D9 b- k" y0 H1 }
<div class="notice">4 F. }& U# i6 \" i/ E
<h3><?php the_title(); ?></h3>
: P% j( t+ E* J* w, s <div class="notice-content"><?php the_content(); ?></div>" m. L& |7 q( b- `3 W
</div>
/ L4 {6 Y' V2 ?7 t3 t5 q <?php endwhile;
/ k a- i; ?% e) {! s0 X wp_reset_postdata();8 g1 i7 q( n" I8 a2 H, }) R2 O
endif;
( R0 x9 h4 H7 n" V/ X: I ```
: q) V, N; j5 f6 _
4 X0 C+ k: T5 M: v4 e" M. b 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|