|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?9 j6 u6 [8 n& ~5 @+ K+ R+ o# N$ c' t# H1 c
. M+ h7 ?. g6 s& D# g如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。. O" u- [6 S3 x
2 y% I2 V, p; v" \( N, I
以下是创建自定义插件的步骤:8 p8 J+ r- i6 k0 d6 S
, t4 v" [. c0 t4 e7 x* v- F% x
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:( }0 V: x+ d8 N; s, a' G
' e' n0 {* M4 i- ^. A, p. V: u, V ```
3 w2 k0 K, q' t* x <?php
* }# X: Y# g5 O% _ /*5 \% N) u6 K4 u6 d
Plugin Name: Site Wide Notices Plugin, S" L; k4 q, J. X2 V
Description: Adds a new custom post type for site-wide notices.
* M7 ?2 ?6 Z- a2 p9 Z& v- r Version: 1.0
7 P2 _$ F- b% t; i Author: Your Name7 j- h" \# w5 T2 s' F' x
Author URI: http://example.com
" ?; }* {" L. A9 c0 k! n */
1 b$ ^, z* D6 y _% F4 |% I! C% n2 o. A6 S4 H4 \
// Add plugin code here...
) l) F2 Y$ Q. R% R5 { ```% }( @( K. z- z, o
! R8 `" Y9 P% z) x
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。$ X! E3 t# h* Q$ l& p
4 g8 ]' U9 d5 C5 T' B
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:) \) S+ p2 E) }/ P
, _7 M; J- ~- } ```' t6 h1 X' m2 i0 @! r4 ]: B g
add_action('init', 'create_custom_post_type');
0 n* _1 P5 U. j function create_custom_post_type() {9 G% L6 n' ^8 ~! f: L
$labels = array(9 W4 O* R4 Y! E0 `+ s& X, a
'name' => 'Site Wide Notices',/ {8 e# Y3 K1 V- }) U- _
'singular_name' => 'Site Wide Notice',
' J( }7 y+ H+ E u% j: i 'add_new' => 'Add New',7 R$ J. i8 _/ m5 y
'add_new_item' => 'Add New Site Wide Notice',; t& ~: Z |- @+ W$ I' m) A! _% d
'edit_item' => 'Edit Site Wide Notice',
& \* k, {7 g- W# P 'new_item' => 'New Site Wide Notice',+ N% g c: J1 O0 g8 B5 P
'view_item' => 'View Site Wide Notice',- g' R9 M: l# C' Y+ B/ f
'search_items' => 'Search Site Wide Notices',
0 s" }% m$ B, h2 s 'not_found' => 'No site-wide notices found',
/ o9 Z' W5 C1 X2 t' j 'not_found_in_trash' => 'No site-wide notices found in trash'
& J4 j* M% H2 t );
4 O1 ?/ R: j# U) Z& d. N3 R% V5 ?. ?0 W1 f
$args = array(
! H- U3 ~# g: v3 V; M; d1 I 'labels' => $labels,. N! u+ t3 {0 N4 Q/ ]) N, v
'public' => true,+ m: _4 B, E7 s
'has_archive' => true," x& @0 u5 S. j1 v3 V
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
1 {$ z9 g3 R3 R$ M: ]9 c, j 'taxonomies' => array('category', 'post_tag'),& F/ r. W0 y3 }! z9 I
'menu_icon' => 'dashicons-megaphone',
1 L$ D* A9 T: e 'menu_position' => 5,1 _2 {. R T/ `: Q2 Q0 [
'rewrite' => array('slug' => 'site-wide-notices')
, y2 G$ V# [* \) P7 W* ] );7 }4 u& H+ z- i# M- A I
* F" l( S9 d; a
register_post_type('site-wide-notices', $args);) B k: X9 X5 x8 Z8 `4 J7 Z# r
}
" O# U1 f- k/ e/ k ```
5 F |9 u; c" X9 T# P0 @3 G5 _4 l- F; k" W' t4 B3 T1 F
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。5 K: k) s& k1 D7 t8 F1 X9 K% d
/ t3 [$ {2 j, d% G& ^# ^% d3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
5 r( x7 t. J# T% m# d" T% c3 w# z/ _% J+ K) C. I& W. @6 l3 \
```; N7 P4 q5 k G1 t ?
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');% V/ I7 y6 {( n# S6 ?, j% G
function add_site_wide_notices_boxes() {- \# h. b. m7 `8 }% {
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');9 b9 Y/ ~+ F* F* T6 e. r
}
, Y- H* B. o) C; G/ ^. L( J
' N9 H$ l8 m' v; p$ V5 S function notice_details_meta_box($post) {. h. Z U5 b$ M7 N u
wp_nonce_field(basename(__FILE__), 'notices_nonce');
* T7 X, A( V9 J6 g/ I4 _ $notice_title = get_post_meta($post->ID, 'notice_title', true);3 o1 o$ R: u* V/ f3 ]* g: I
$notice_content = get_post_meta($post->ID, 'notice_content', true);
" I* l. K9 U. c# K/ W8 b ?> y `2 r% F0 t" X0 {# X& a% ?% |
<p> g" l1 Y: X: z9 M& {, {+ Y1 {0 @
<label for="notice-title">Notice Title</label><br>' u o4 g" i( g- ~/ }$ ~; A) T% r
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
7 p: A/ G- S' L/ b Q </p>
' J: o- U3 K# a& }! m; f5 } <p>
z/ ~/ f3 ^& I1 M! ^) T2 g9 c <label for="notice-content">Notice Content</label><br>" A: O( r7 k d" S
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
5 r. e7 q, t6 v, ~8 U </p>7 Z% X: w/ y" {& z1 |
<?php
$ @' U1 ^; T/ R) T; J6 r3 K }
: Y/ z5 p4 C ?& P/ M/ d
) o1 W6 a9 _8 B- ~# m add_action('save_post', 'save_site_wide_notice_meta_box');
0 b' _2 W% O2 @" I* I* k+ w( x& c function save_site_wide_notice_meta_box($post_id) {3 g. b6 u' ]4 o! r5 c8 t
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
7 b1 |. z2 b; |0 l return;
" J) G. m- h) i" _( F( _# N if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
7 z& [+ @3 ]. M$ b1 u V return;6 v/ r5 G4 u+ p8 {
2 W3 [0 ` O4 S' [% Y1 B
if (isset($_POST['notice_title'])) { A$ C1 C# ]# R" L; V% b
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));' _; j- L% }3 q
}' p9 n3 X# \4 R$ m9 X; l' g
if (isset($_POST['notice_content'])) {
2 `+ ^1 h$ E1 ~' n' U- ^+ _ update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
9 H, u: `6 N; b; Z, H4 L6 M3 h }
) I8 p# r6 T# m }9 {8 t2 x: E+ {/ u# T N( k8 u# U
```+ j/ r0 Y( s3 g
, ]0 L N+ j2 ]1 x# G9 d: k 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。4 `# x2 M0 T7 n8 `7 ]/ ~# L
e- K" _& F2 Z$ \# @" c
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
" }8 m0 T# u. u4 g5 u) ~+ s% l( \" K1 }( u7 L2 r9 u
```
5 W5 k! F- N' N3 L7 v6 x4 w $args = array(* g7 e2 ^5 h1 y+ L
'post_type' => 'site-wide-notices',
; h$ J8 b1 e" T1 R2 A5 G+ T) d5 q 'posts_per_page' => 3,
) F' F7 v9 a# C1 R( C# Z 'order' => 'DESC',
3 b& c# G2 M {( R! h% K3 E 'orderby' => 'date'
5 J* T) @7 t4 K( r: u! s2 z );
; a" X3 U6 K. e! T $query = new WP_Query($args);
/ Y$ p: A. B# t& e6 ~+ y0 t if ($query->have_posts()) : p/ {8 x0 d/ \) J$ ~2 W
while ($query->have_posts()) : $query->the_post(); ?>% D) `" X$ B$ d& y
<div class="notice">
' J! F1 K8 ^" y* t& o' z% c) j$ a4 ~ <h3><?php the_title(); ?></h3>- x. J: M y& d% ~1 Y
<div class="notice-content"><?php the_content(); ?></div>
8 ?' \ [' y" Y, m$ S; M </div>$ n: e2 I+ J5 c3 A% W* Z6 A. o0 g& F
<?php endwhile;; S- f9 `% T- d
wp_reset_postdata();0 I7 x) M+ M, v( Y( q
endif;
* ~' m" {- x& R% ~4 J( ? ```1 I8 ]: O) a/ e9 B9 A: F _
2 {8 R5 r8 E8 W
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|