|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
: S" x9 u% s, Z/ b, @ s2 b* X/ n9 j2 U0 q6 M, a
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。8 Q, H5 n% \5 B. {1 @' W7 H
! I$ [: }* \3 @以下是创建自定义插件的步骤:
" I5 `8 M' @% @, K7 P/ x$ D' s$ K. \4 H o4 K
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
9 w4 W# a& t9 V* u; X) U9 c: _! p' \" I1 Q0 W& T
```5 u8 G5 Z/ f. q% o$ q
<?php
}0 {! V: W' t% b+ ^* J; ] /*2 F0 \* @ I% a
Plugin Name: Site Wide Notices Plugin
/ O' M/ Z+ W: ~5 t4 k8 q Description: Adds a new custom post type for site-wide notices.
+ M- P2 T7 }* N6 B: V- e Version: 1.0, X8 I- v' n) n% R' N% R
Author: Your Name
" f, v& M( U( U6 j" p. X Author URI: http://example.com
' K7 r4 }5 O9 J+ L */
$ ~* Q7 A e! e5 g; Y! n+ `' ^' T z3 p F
// Add plugin code here...
! C( X: }! ^1 \7 u ```
" \) c) y) a3 _( i
- a: _/ G1 ^7 d' G$ L1 ?7 G 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
0 v) J8 x' ?% x2 S' o$ `0 h( Y0 n* X6 T9 b' b! ]
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
6 `2 E/ @: N" w. m" E# R* f7 X, _9 e9 g9 q0 P" G0 b, ^
```( I6 ]3 E! X; c0 I. H+ ?0 T1 V7 Y
add_action('init', 'create_custom_post_type');/ l* A+ l. y, a- e2 S( C& t
function create_custom_post_type() {
" x& P) b; x% q$ K& ^0 r1 J* U $labels = array(- j- r. W1 b+ h4 U7 J) _# R
'name' => 'Site Wide Notices'," }4 L {; c& l. q7 E5 s( a- {
'singular_name' => 'Site Wide Notice',
& J. V& ?6 o6 H5 Y. n) T! J7 ~ 'add_new' => 'Add New',
1 A. @% ^: }% i$ A) G6 m; S* H 'add_new_item' => 'Add New Site Wide Notice',
5 d$ Q5 e! F8 M& O4 i' Y 'edit_item' => 'Edit Site Wide Notice',
# J/ u2 v: ?+ S e! Z8 f 'new_item' => 'New Site Wide Notice',
; F: [5 A* D; c" C, _. }- E1 E" H 'view_item' => 'View Site Wide Notice'," k$ S# _7 q5 h g$ f
'search_items' => 'Search Site Wide Notices',
, `% B+ x4 v! G+ J* H 'not_found' => 'No site-wide notices found',
, p; J; t. ^; S; g 'not_found_in_trash' => 'No site-wide notices found in trash'+ w& P0 r( x \! W7 i
);
T, B% ^% ~5 N; E
& D( f8 ? o4 z4 L5 k1 i" I T $args = array(+ _: x6 S. V. N& w5 v
'labels' => $labels,; }+ n" r7 d0 N' m* m. N0 Q
'public' => true,2 e+ T1 _" a& l6 [8 A
'has_archive' => true,! [1 Q+ S8 Q- x6 M
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),) Z6 h. C8 `2 G, d$ c
'taxonomies' => array('category', 'post_tag'),7 M4 v2 H* C2 @* u' N
'menu_icon' => 'dashicons-megaphone',
" x+ l" c* V$ Y8 E9 p 'menu_position' => 5,+ o. k' S1 h o" S* h l" E) M
'rewrite' => array('slug' => 'site-wide-notices')
- Z7 y* C) i" G) s- j* i7 O );" M8 J2 q% R- L) @2 r
% e2 U! O* X; D0 L6 j a5 r register_post_type('site-wide-notices', $args);0 [+ X9 `6 [) o, ?- I
}
. a& Y) |% y( G ```% ~0 ~: v" | m0 |6 r1 l5 E
: ]4 H8 I; a8 ^8 b 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。1 y2 J# p* k8 J9 E6 s
6 h: k0 ?% L. @' y* s
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:9 d4 Q# X E6 h, M/ }
$ I% b& k+ Y% o6 h, x8 { ```7 f/ B& {: V+ \ G2 E' }% N
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
. J7 h7 r( E6 s* d$ C" H function add_site_wide_notices_boxes() {1 c1 K n) \/ [0 D; _; j
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
$ D: [9 E# i8 r* }0 v, k }; t+ s/ |1 [# G, n, f
5 K5 i8 O# _% @3 U
function notice_details_meta_box($post) {
0 Z0 p. b. \& A' N5 b wp_nonce_field(basename(__FILE__), 'notices_nonce');; \, }; a+ }& x9 [
$notice_title = get_post_meta($post->ID, 'notice_title', true);2 R- t% V4 w3 E) t* Z
$notice_content = get_post_meta($post->ID, 'notice_content', true);
+ W( j- ?- E6 q6 [# E. x5 m ?>4 |/ H8 u8 [6 G2 h
<p>, ~. O$ v3 V( L' t/ Z6 ?5 `
<label for="notice-title">Notice Title</label><br>/ }6 A3 q& V) A9 L+ s8 K& k' ^
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">6 s( K! p j% C1 G3 E8 Y
</p>
0 W* j0 Y8 `, y9 }& W* n. u <p>: I% W) o0 l2 L; ~. F( z0 T
<label for="notice-content">Notice Content</label><br>
- X$ h: w8 Z. g. s7 z% [ <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>6 e: Q1 W. @4 C% r2 ~% O" {
</p>
! x6 \# R. A& S( N* _ <?php! z3 o5 _: A: U) Q ]
}
, O6 j& F' x5 h# c: P
- R j3 {, F3 H1 v( t1 w! N3 l( ] add_action('save_post', 'save_site_wide_notice_meta_box');
( M# Q O5 t/ Y function save_site_wide_notice_meta_box($post_id) {* z9 C; s- ]6 G6 ] s2 G
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
3 H+ q& I* r3 t return;
$ v o' i k' b& P" t; D if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
' ]" L* s+ o7 i' ^1 x return;
3 ]( b3 _( w) p; A! {
! ^; V; Q% Z1 H) q" [8 z4 q if (isset($_POST['notice_title'])) {
" l j. B3 F; r' D. O update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));) h& r) i! ~: g
}+ q, U" g1 j" B$ p4 ^/ z4 ~( {- {! R7 Y
if (isset($_POST['notice_content'])) {
, Q D. G7 r3 N3 l* ^6 L6 ` update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));4 r, i' S, L+ X8 g
}- B. N5 _. I2 U% S/ n9 |! h- J) A" z
}$ h1 q. z$ B) ~ b: U
```9 C* o$ F5 C2 B0 G0 e
) V" l/ \# ]9 I' r5 ^ 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
7 e6 B; @( I- F4 n! i$ R5 o2 e9 s" q
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
4 d8 e% G; P2 u' M' P
1 X0 q+ X" H5 O v) i. u ```0 r1 P; Q o7 R2 z0 _: V8 p) f: Y. [
$args = array(
4 j1 n, @3 G7 R+ P1 y' G 'post_type' => 'site-wide-notices',
( F5 X9 x6 i5 \. _0 q9 n 'posts_per_page' => 3,. F d3 h# e! C$ M
'order' => 'DESC',
' t6 G. E7 z. F3 `. H2 ] 'orderby' => 'date'
& H! X& ~: H) ~/ k: V* l );/ k: F6 B+ w( d2 O9 ?
$query = new WP_Query($args);8 s$ S4 D5 c- p4 ?4 o) C# L& [
if ($query->have_posts()) :
; V! s1 i0 `4 L. {, [4 b( m while ($query->have_posts()) : $query->the_post(); ?>
1 i4 ^: u$ L: O( [% T& b <div class="notice">. L9 E& B4 {# e! x, k$ T0 t' T/ a
<h3><?php the_title(); ?></h3>2 @ Q) Y j/ j1 f
<div class="notice-content"><?php the_content(); ?></div>$ H# }2 C: h, M7 b
</div>
& `, M* g0 D5 t0 Z <?php endwhile;( Y W, E# Q, [; g2 A$ Y
wp_reset_postdata();
* [( ?7 L7 D+ a" C& x7 z, U$ j endif;
- Y4 y, H& u) D: P ```
( t: [4 H: v* {4 V! g1 s7 L, z, [6 j( ?7 w2 ]8 J+ H1 g
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|