|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
) d$ j& u7 `5 O5 J/ l
$ m1 y9 [' `1 R, i$ E5 V, f如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
9 U) B, M5 `( d' V' t: l" [& d) b6 l/ L: ^
以下是创建自定义插件的步骤:
r) r8 M' F" Z5 e$ t3 o: n
+ G6 b/ v" E+ T' V4 H0 @8 U1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
2 h% Y9 k! f3 C# Z7 G: w3 v& D' `3 g: m6 K t# Q& S4 }! y
```
C6 E. L1 j; t" i; N <?php
: O" E/ T+ X" Q9 x( o i% s /*
7 i( b& s9 i! l+ N/ J; V) r, Z" m Plugin Name: Site Wide Notices Plugin) p4 \6 N* P9 D( L
Description: Adds a new custom post type for site-wide notices.; V `0 [' E! Y1 }, t
Version: 1.0
6 g# G P H n) A `9 p Author: Your Name u! y8 g+ ?/ R9 D, S
Author URI: http://example.com3 M9 ]0 M/ U4 v9 d+ e
*/9 s/ u% d8 T& _" r
# b7 n: X' j% t1 k // Add plugin code here...5 M. z) B. [$ O4 e; v+ A
```, j, c8 x+ L% R9 K: c9 Y% E1 P' n
/ s% i8 [ p2 X( V0 Y3 k+ g" [/ F 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。4 s5 k+ u- N( a' y. s& s4 [! c
* n$ ~; S e, s" W
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:: m/ v$ j% E) p# j
3 H' v- g: j0 M. [, h) n5 U6 ^ ```
5 Z: U v7 \- h; T add_action('init', 'create_custom_post_type');4 y% f) r' u+ p7 {
function create_custom_post_type() {' i+ t( y& W7 V4 u
$labels = array(' L0 x% a" C5 v
'name' => 'Site Wide Notices',+ N5 E2 | w0 C$ f$ S9 l
'singular_name' => 'Site Wide Notice',
; U9 c8 f4 |4 j# d. G: l 'add_new' => 'Add New',5 u0 L" C9 k( Q4 E# u& s) |
'add_new_item' => 'Add New Site Wide Notice',4 S. k. D7 H/ d' A, X- m% X
'edit_item' => 'Edit Site Wide Notice',
6 p( E3 N- V! i4 W 'new_item' => 'New Site Wide Notice',. q2 X) B" G7 ~9 ?& d$ B
'view_item' => 'View Site Wide Notice',
/ |- t; W8 z3 q) t 'search_items' => 'Search Site Wide Notices',' k- m& Z+ u$ I+ o {. I
'not_found' => 'No site-wide notices found',
/ k# k& j" p# x+ I$ m& v 'not_found_in_trash' => 'No site-wide notices found in trash'- u: S' D5 X* I
);
+ y, v7 W( `$ [1 i5 x) @3 ~* ]5 t+ W' X5 h/ N' U& r' v3 |0 V8 ^
$args = array(
- o# D* F' }" U+ d- o0 R 'labels' => $labels,
$ a/ j' r9 k" C3 X 'public' => true,: J4 v8 _1 r6 m$ |# }
'has_archive' => true, b' ]( `. _) ^* a: u' V* v
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
* F$ }) Q4 l Y$ ^ 'taxonomies' => array('category', 'post_tag'),
8 l# y. \$ X. n% J4 r 'menu_icon' => 'dashicons-megaphone',( n0 a; S' S3 v( R
'menu_position' => 5,+ ^ S, X- I. T
'rewrite' => array('slug' => 'site-wide-notices')$ d4 T) i6 {, ^1 j/ S9 I
);
7 |6 D0 e( F" a q# r) n7 h5 g% Q9 v. C9 U
register_post_type('site-wide-notices', $args);& P! D6 t; n- o# B
}3 a$ \7 u& v) m1 b6 M1 p
```
' ?+ a4 o& t# x
) h+ w3 K8 B6 }9 U' Y 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。' Q4 `: u2 ?. e9 K( {( Y4 Q7 _
0 J" u9 |3 f0 r7 w; q
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:" ]9 H2 F i$ g* V
0 g3 a# p1 N3 A/ R0 Q Z' V+ b# O ```+ M' ]9 O& C; S
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');0 F* z) k& `$ \4 a& E" {
function add_site_wide_notices_boxes() {0 x- E' R% B5 k7 `1 r
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
1 `/ g. M8 ^) v }7 {, q2 Z2 G; e" n, Y* y
1 {: i' ]# j+ w' M: {4 w0 ~2 n
function notice_details_meta_box($post) {
: g9 V) A* ~* H9 t: j& W wp_nonce_field(basename(__FILE__), 'notices_nonce');/ e' @4 ^/ P6 i6 Q* @8 ?
$notice_title = get_post_meta($post->ID, 'notice_title', true);& I* e' t4 [9 `6 k7 x: \1 G
$notice_content = get_post_meta($post->ID, 'notice_content', true);
; r6 R3 {0 U# ]" H3 f ?>. G2 U7 t$ x6 h% K- z2 w4 q
<p>
" j' a0 }# w' h <label for="notice-title">Notice Title</label><br>! |. d; `* l e- _) O& }1 m$ X2 e
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">4 A6 [& V& O* C2 ]7 X! Z8 p! _
</p>
6 S. C ^: I0 l# `6 ?8 b: K, G' j) ? <p>
# s1 Y ?- e8 O9 m9 C/ [ <label for="notice-content">Notice Content</label><br>
w' v: h! O) i4 m3 t4 G <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
+ o. B+ E' r4 N& _: O, f9 p8 ]2 } </p> ~* T+ R4 ~% T {( ]. U( n
<?php
8 _7 P( j$ f* m/ i }* z2 a5 j f7 u" `; |) S
+ c# o: o5 G3 u. b# ]7 s
add_action('save_post', 'save_site_wide_notice_meta_box');) F# P! n9 ?) f1 g m6 J: p
function save_site_wide_notice_meta_box($post_id) {
/ S8 J) K8 K d& T D if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
( E- U6 S- A. t0 S return;
- v: U, S, q) O% x% b if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
H/ I0 T% X6 w! g) g return;
2 u/ e8 W6 t$ I9 t! g
+ c% v8 V3 y& i8 _" L if (isset($_POST['notice_title'])) {, y4 G9 Z) q- r, `' i7 D0 ^
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));4 C6 j6 m) T0 Q( w% K( w0 X' Y3 b
}; G6 R+ L2 L! @" H8 \$ X$ q
if (isset($_POST['notice_content'])) {) u. \) `9 u# V' N% E& R
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));# v3 I" ]& |' c1 ]& m( L5 m
}1 G6 `/ g3 A7 f- T5 b- Q
}
. X; n7 L1 j% ~ y ```
: [0 T0 K% d- O3 a' o7 U9 _% l* r7 C i) z6 ~
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。( R8 h$ r6 g- Y7 o$ F
% M. E. H5 l# ?. m% m' w, g4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
0 F1 Q2 [3 u: A* V5 ]; V$ S/ N* f$ N9 s& x+ E
```
% p3 K! |: N% Z) U7 V7 O! q $args = array(
- L8 h9 g) \- f! t2 T! q# r 'post_type' => 'site-wide-notices',0 g9 R3 z C( v; u- t9 ?6 _+ \
'posts_per_page' => 3,
- m6 f. S+ U# ?7 j( B9 w L1 D 'order' => 'DESC'," J( `+ q$ y9 B/ R6 Z) b. \
'orderby' => 'date'
, a" F) ?) j& X; S4 s );
/ q$ L# d& g0 X $query = new WP_Query($args);0 P0 q. u4 z/ a5 m3 e k8 w
if ($query->have_posts()) : `# p* ^# f! `# y- M; b! X1 A
while ($query->have_posts()) : $query->the_post(); ?>9 y1 K& Q U* T4 K: O) h+ D
<div class="notice">
& b: z8 q T. V e6 i <h3><?php the_title(); ?></h3>- i! y# b. [' O$ V; e4 H1 P
<div class="notice-content"><?php the_content(); ?></div>
: s* y7 J; \' _( n7 {5 U </div>4 K- k/ E0 U% g" K$ z0 a6 r. i- L
<?php endwhile; V. Q$ E" Y ]" h5 ~
wp_reset_postdata();" t: n4 e: J$ F) m
endif;
+ V+ k3 u2 E( ^ ```' S: V7 X: D* Z! {
5 d& L: ?' E0 a4 ]; w9 R5 S
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|