|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?+ A' w; D. o4 {6 W% _* S
' U0 p$ v! z) X2 H2 ^$ ]- t7 Y如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。! @* O a" s& D4 Z N
7 k+ X" `! t8 |+ |
以下是创建自定义插件的步骤:
# d4 R6 k) y( V! m' X( d4 x7 }! ^( |3 x! K E
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:2 m& t2 T# h/ q1 a6 N4 V9 o
' R7 W! ~+ a$ U4 D/ y8 ^ ```
2 q9 s" l* R7 Z9 {3 s5 ]$ m' O <?php) A; a* b! P2 l4 }" V
/*: B' P6 i4 M' x1 Y' a
Plugin Name: Site Wide Notices Plugin1 p; i& t0 C& M
Description: Adds a new custom post type for site-wide notices.8 Y% i# ` o- r
Version: 1.0
" `0 z/ @/ B- x; O Author: Your Name
4 I1 x2 i, ~( }8 e& S Author URI: http://example.com% W, L" k+ T4 n! V0 V/ t1 m
*/
; k' m/ ~2 l' U7 t c) K: |. D9 H2 S
// Add plugin code here...! P6 l- N9 z5 Q: S7 J
```- b8 g( ?/ P' U- h
. b+ ~8 ?1 z) ]* ^
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。7 o& |2 {% ^5 r: F
+ U9 a2 a( {4 X
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:5 N( \" X2 O' M2 A+ h
8 }: v2 A8 L) S ```
6 q3 D) M5 n% i1 m$ V- n add_action('init', 'create_custom_post_type');
, c7 q( v2 `# I$ X. B8 \5 ?% P9 r function create_custom_post_type() { j# H5 u! i, `; E+ A: v) L
$labels = array(2 g+ i, }- i7 Y7 |+ K# F3 V$ W6 p
'name' => 'Site Wide Notices',
( I7 }- i1 G2 s/ u$ W" ] 'singular_name' => 'Site Wide Notice',
- D2 ]; D5 C7 u 'add_new' => 'Add New',
5 V2 \2 _, _" z) } 'add_new_item' => 'Add New Site Wide Notice',
7 ?; y* M- @& G* c- F 'edit_item' => 'Edit Site Wide Notice',( o @; a# G% |+ U5 v2 B# p
'new_item' => 'New Site Wide Notice',
, d( x" A, U3 ?: W8 ]# _ H 'view_item' => 'View Site Wide Notice', v+ u S$ `! C8 I
'search_items' => 'Search Site Wide Notices',
3 C, M; s# B% J 'not_found' => 'No site-wide notices found',
. h" ~2 n) C; | H- s 'not_found_in_trash' => 'No site-wide notices found in trash'
) `2 M$ }# q9 f: w% X A& e6 w );
+ ~. T( K. `. ?# ]) P8 A) R7 j/ o2 A" v. g
$args = array(
- l: i. U8 q8 c8 k" U 'labels' => $labels,
; n% d+ q3 z! H( J) h8 q 'public' => true,1 a" e3 I" x. A: [! z0 v
'has_archive' => true,+ f6 S+ g& d. u5 d! d( `) E+ h; T
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),4 b+ p- h# P3 x
'taxonomies' => array('category', 'post_tag'),% n1 }" a; [3 U, d$ ]( u6 s
'menu_icon' => 'dashicons-megaphone',' g% ^, Y! o: K& P/ q G- x" t" s" Q
'menu_position' => 5,
! a1 V* n2 c; _# ~ 'rewrite' => array('slug' => 'site-wide-notices')
8 W+ v8 c6 }% u# j8 `/ c );4 D& |% s; J. K
3 y" u* T$ C4 N# C; f( P
register_post_type('site-wide-notices', $args);
& p, G6 \4 ^& {- z# e/ b }
. y7 {! c: I" V' s( \! L9 J7 c ```
2 S0 x. C6 R0 E7 I- F6 p+ B C! y4 _' B9 e$ Y+ S; ^8 m
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
7 \$ V' c2 Q3 z* G4 G; M9 g. h# D5 t8 X6 Y
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:- M8 C Z8 Q! x; S7 O; P2 m
# H& z3 F$ F ~. b- s
```
. S+ P _ a& J1 d add_action('add_meta_boxes', 'add_site_wide_notices_boxes');8 X; M9 J9 T' m3 N. h4 u
function add_site_wide_notices_boxes() {! O. Y9 k$ F, {. B& r0 G
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
/ X' F7 d% H! |% V }8 W5 Z" }' X( G* D# `# R: x) h4 v
" H0 R# g# }/ N% p: }' A9 n
function notice_details_meta_box($post) {1 |% {/ d; W6 {1 G2 | `
wp_nonce_field(basename(__FILE__), 'notices_nonce');5 D* X" Y b+ y0 z
$notice_title = get_post_meta($post->ID, 'notice_title', true);
/ C5 \! H# ?6 S ^9 g $notice_content = get_post_meta($post->ID, 'notice_content', true);
, M' d3 L9 [7 f2 D5 O ?>
+ j- A8 ^5 j( r9 q) D, p4 q9 | <p>
3 S$ I9 h0 G" l/ u <label for="notice-title">Notice Title</label><br>
2 _; ^0 Z. j' V8 d" {' L( p2 c: E <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">& ~. [3 L( o4 Y5 j; C Q2 Z4 K
</p>$ A4 Q5 v8 p3 X! Z2 W: {
<p>) P& B1 I$ l1 z8 W+ _/ X4 B% \
<label for="notice-content">Notice Content</label><br>4 H8 ~/ |& o: n: l. d
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>- B* o: K) C1 g8 e& R& m/ f A" d
</p>/ h! y H# U% R, p
<?php
, q5 Q5 `* v$ o2 g# ? }
0 t# V2 x8 E. I! K" p
: p) H( }* B z! {) [& a. { add_action('save_post', 'save_site_wide_notice_meta_box');) B& a9 ]: i! I
function save_site_wide_notice_meta_box($post_id) {
u3 C" G" N! \' y if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))); ~" a+ u) I6 H9 a. t
return;# J8 r2 e7 H8 N. r; U
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)2 p0 k" S& a* j2 w8 K
return;1 Y {8 V! o: i+ f# }$ d- p% k" J
# s- x6 C+ m. q1 @: U$ M
if (isset($_POST['notice_title'])) {7 H$ p6 r2 ?+ j) O) ] h
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
1 Y! Z$ o% [4 @; S- B }+ N, _9 F- H% _# x/ z
if (isset($_POST['notice_content'])) {
/ m" ^ u4 s" C- {, ^3 o update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));: V E/ ^' e) r( z: I& V
}) I1 V9 z; C- ^7 r4 r3 f9 e
}
2 R5 s9 E b; |/ {5 O {( ?3 k- Z ```
7 O# W6 k/ E) d2 Q w: f- r1 g6 M0 h3 p7 ~
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。; x' s& ]8 f. x( H
1 @- \. A) Z) Q# b2 B* M7 v. V( T$ H
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
/ x `# t. x: v; w7 M3 a
' o+ m/ e+ Y7 c ```
/ t: T& [1 P& w) A B $args = array(
`9 T: p5 a5 B6 F' u! @! A+ J8 _ 'post_type' => 'site-wide-notices',
2 q I$ \. n" F& R* V! s O 'posts_per_page' => 3,
- r. }3 T/ t" A: p/ W+ ?/ R9 S 'order' => 'DESC',$ O2 U! w+ u+ c# |) c6 ]
'orderby' => 'date'
5 O4 y* v& A3 e );& W; r' H4 Z# ^( c% C5 r8 @
$query = new WP_Query($args);1 ]' J9 z9 h, V/ y
if ($query->have_posts()) :6 \/ v$ a, S) H7 R; B8 j* t
while ($query->have_posts()) : $query->the_post(); ?>! G/ j1 B& D4 N
<div class="notice">
! X2 E9 m" g- q8 u6 @' a0 g: w <h3><?php the_title(); ?></h3>
: X! [3 x; K! y( V3 ^( G <div class="notice-content"><?php the_content(); ?></div>
) t* R; l* r2 P7 }( f6 f5 C </div>
9 @' Z; t* E) N7 T" C1 l <?php endwhile;
* [* s: r% |$ k) {) y/ s wp_reset_postdata();
" q* H* h: z" o1 H' ` endif;
3 \7 V$ h9 w W8 F ```
8 }" o p8 H% ^! c' z- t6 K- i
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|