|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?+ ?9 T# \- ^, z$ ]6 p
( ^8 `: T1 V( ^3 ~! h如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
: R8 B" ~8 P# N Z! y+ S6 E4 O9 H6 s
以下是创建自定义插件的步骤:
! O) c+ h; z( j) G- T$ A* r2 u
) p% i0 @$ y! v6 P1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
1 x) O3 I, d$ X; [& a/ w2 m0 K& Q p7 u8 j- S4 C" D
```7 f E9 c0 [8 S: M1 ]
<?php2 L) p7 ], U6 \, B
/*" Z7 n: e" M* r# o6 {, M. Q2 e5 t
Plugin Name: Site Wide Notices Plugin
/ y# C: ]2 t8 `+ [ z& u Description: Adds a new custom post type for site-wide notices.7 ?5 S) b+ O0 G' B# p x3 p
Version: 1.0) j- |$ }0 P6 s( w; _% {. n
Author: Your Name" y- ^* u/ V' L
Author URI: http://example.com/ \- D) l: S" h
*/
+ f; T/ N- w, u
- J+ S' R; {, A) E# t! Y1 K) q o // Add plugin code here...
3 i" m/ o1 A( e) V( N+ M1 `* I ``` o% g( f: h1 V( ~( [1 ~- P
+ ^* Z9 R$ Y4 o' Q
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
/ F3 _ c' x9 |9 {
$ d5 N. F" r) ~. a3 B0 G1 n7 a2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
/ l6 t) ]: v! H: r6 X5 l" y' F; W; `+ E' L9 ?& [
```
1 `* I$ I+ n' } O s( K add_action('init', 'create_custom_post_type');* ^2 D+ C6 a/ B
function create_custom_post_type() {
# f3 K4 e+ k1 q $labels = array(
; x9 u) k2 h+ g0 E$ u6 a 'name' => 'Site Wide Notices',
" R1 j# u5 u% W5 m 'singular_name' => 'Site Wide Notice',
' G; g0 C1 T9 }2 H7 T0 h$ y! g, V 'add_new' => 'Add New',. P) C6 T, q3 a1 T" N0 T
'add_new_item' => 'Add New Site Wide Notice',- f2 |" y6 Q1 s3 U+ g3 u4 J
'edit_item' => 'Edit Site Wide Notice',
$ k" M: l& h2 m 'new_item' => 'New Site Wide Notice',
5 J# m" |/ M+ x. H. } 'view_item' => 'View Site Wide Notice',
3 J* }/ _3 v# i: N$ ^ 'search_items' => 'Search Site Wide Notices',
% D6 x0 c5 g8 ]/ b. p' J5 M5 j 'not_found' => 'No site-wide notices found',
" ^& ?. C K# k) |% @: c5 g 'not_found_in_trash' => 'No site-wide notices found in trash'1 \! d' z- e' t& E9 b3 x( n r
);6 q2 @# y! y7 Q& O* p. i. i
: ]; S9 n, T0 N7 B
$args = array(
. t8 l2 E2 U' m/ s' _ 'labels' => $labels,% x0 U* J$ A6 y' _
'public' => true,
5 `9 \& D; K! Y" @' H 'has_archive' => true,
- ~. `+ r, J1 g% I& b6 m5 R3 s 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
1 i) {) _2 P4 S# O3 G Z 'taxonomies' => array('category', 'post_tag'),. A8 J$ f& [2 `) c0 c1 ?, @
'menu_icon' => 'dashicons-megaphone',
) j6 O: {$ K' T! D 'menu_position' => 5,7 V+ Z# r' P+ A
'rewrite' => array('slug' => 'site-wide-notices')8 \) e! }: j Q8 k. ?( b/ | F
);
7 y1 `, x* ?8 o: x' k7 e" z# W; c( g3 R0 F K' o; `! A
register_post_type('site-wide-notices', $args);
7 k+ Q6 x! K) U, L2 _6 D }
3 |- L& V: v9 K/ ^; q: [1 x/ { ```
6 }8 D4 ?9 _* S& m) ^3 l) c# ~( x3 t D) E5 t. p# \( t ~: v
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
9 D0 v* A8 L @+ ]. N3 I" _# u& S1 E- j
0 {: n* F' g; O8 }" e$ _+ t3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如: }; L7 x3 X% |/ E' K
. @2 v- z4 R& [, y o
```6 b7 O. l. p/ H+ |5 z
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');* ^# k2 b& P; T0 p: u! e! u. r
function add_site_wide_notices_boxes() {
0 S; t. R- A' e1 X# G/ o add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');3 e) Z. F* ?7 w5 X& q- f! p
}+ b% H3 a+ a/ i2 W* A, Z3 ^( g
, ]1 I' _5 @+ W function notice_details_meta_box($post) {
2 G' l4 ^$ X: A, r wp_nonce_field(basename(__FILE__), 'notices_nonce');! p# j& R& Q3 t
$notice_title = get_post_meta($post->ID, 'notice_title', true);* [2 L+ M! W" [
$notice_content = get_post_meta($post->ID, 'notice_content', true);
4 f! M# R( u3 c# ^- l( [ Q ?>
# j( j3 z1 n- z( e. | <p>3 f9 p2 U5 H! o$ R0 o
<label for="notice-title">Notice Title</label><br>3 t0 C2 h6 z- y8 W0 r
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">- n; E6 C- x1 |; Y2 z# G
</p>
2 d, t* ` `4 U1 z <p>' s. c/ d/ [$ ?2 _7 d: t
<label for="notice-content">Notice Content</label><br>
3 ~ C1 f6 _6 c+ R% @. o' ^ <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>% o( N3 m5 j1 J
</p>
, I: { k6 I) B5 B% r. [ <?php6 ?5 e |7 t# P; A* I9 V2 F* T; q
}
4 _% h" A. x' O( H x) r
, y$ o! t1 @* {: e add_action('save_post', 'save_site_wide_notice_meta_box');0 p" r4 D; {( l
function save_site_wide_notice_meta_box($post_id) {
8 p9 f* s6 x; X if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))8 g6 d' ~3 |7 g) l7 ^; [) d4 d
return;
) V! G8 ` A; Q4 a if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
- i+ ~5 J" [6 x+ T return;$ e. B' y. Q) a* r
# p% ^8 ~9 T8 |- ?- N if (isset($_POST['notice_title'])) {( K* G; {; J8 A0 I, M
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
8 V! ~ X8 e( J, h0 y6 b }5 S6 V5 B% s) O' U+ z
if (isset($_POST['notice_content'])) {! K: T q2 a% @* }
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));, J6 y. |# [3 m" ?1 T; s
}4 w8 M! |4 ?1 f" ^" |5 B4 W
}4 Q+ Q0 a, M! p2 |" @; @6 Y
```
/ e- A4 E3 q/ z) v" m3 i V3 x. g4 X- z) _2 {# _2 q1 j
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。, T. o: O. C5 Y! t/ F3 E
8 S4 I$ c( H6 o6 ]! x2 C3 ?, \# P
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:1 W2 p% H8 q1 }( h0 | X3 Q
9 F: e9 r" x6 g$ z8 [* V ```
& [: W- H' k- W9 p, ]! c2 ?3 s $args = array(0 k. n8 C" T" ?. L
'post_type' => 'site-wide-notices',
% V+ Y' e! i" q i1 K8 S! N 'posts_per_page' => 3,
( ^+ V& q- Y. C 'order' => 'DESC', C# G" R$ j5 K N* g7 T
'orderby' => 'date'+ X2 Q) ?4 z- `7 D8 i; ]) D( G
);
( P2 ?: P$ I0 m/ N. b $query = new WP_Query($args);' h4 _5 F! o: P9 y. O: R2 M3 f
if ($query->have_posts()) :
3 }$ q+ E) Y f* s; ~ K while ($query->have_posts()) : $query->the_post(); ?>- W8 r6 q; n4 o3 ~5 [- L
<div class="notice">
3 }* g, J" E8 M4 N7 z' g5 H% _; w <h3><?php the_title(); ?></h3>
* x# C* o2 p# k <div class="notice-content"><?php the_content(); ?></div>
7 d$ Q! y p- p8 m- M7 [ </div>
5 g$ S9 R6 _' E1 ~7 K$ ]1 _ <?php endwhile;1 ^, o. Z7 Z0 n; s( K$ k
wp_reset_postdata();) E7 o8 c8 H' |) L% q$ p
endif;
/ N$ E l' i$ Z' u ```
' [$ |" |) U% x' p4 L9 Q* S! f: t+ i* i& F. a
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|