|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
) P, u* c3 D4 A; m$ O! y) t) ^& ~8 r( ~+ k# O
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
( B2 D, a! C# Z2 i) l7 O; v7 W: a* b; T0 s y; t6 B
以下是创建自定义插件的步骤:
) R0 p4 D( R& [( n6 f" J1 [8 Q& G9 P
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
' O& i9 n) ~, f( L: ^- r, l+ b& o3 b" i
```
; Z- g) Y3 n( S' k* p% ^- U <?php2 Y: {# o$ v$ i& `
/*2 e% w, d# E$ i3 P: S( S+ U
Plugin Name: Site Wide Notices Plugin
# z7 i+ i) o* p4 }" \* C! y Description: Adds a new custom post type for site-wide notices.
1 a2 d' v* A7 P, B; m9 g. ?3 b Version: 1.0
8 w" ~3 f$ ]; b5 x# K Author: Your Name
$ l- h8 B& O! g# f Author URI: http://example.com
: M% d: P, f0 r4 S' j( q+ I */
( v+ ?$ L6 o D6 Z5 t! |/ R
l. h5 v+ F+ ` w" ? // Add plugin code here...
6 C7 ^1 w; G7 u, X w( r0 W ``` g y, @0 w. d( l3 k
2 U) {8 y9 \* {! y( Z3 P
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
" {% A3 V) k! }) `5 I8 f
+ |/ k, I7 I. t7 V5 S% y0 R2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
/ b8 @+ _, s( ~2 v# b, L- @1 M% s
```) v# W9 {' E: q+ {( o
add_action('init', 'create_custom_post_type');9 I* e" \ S/ a+ v
function create_custom_post_type() {0 s% |0 x4 l0 Q8 X0 u4 a/ g
$labels = array(
4 [* x( p6 d' ^( H( L! X 'name' => 'Site Wide Notices',
, [) W5 D$ p- \" \0 l* B, V# z& j 'singular_name' => 'Site Wide Notice',) o: R% E! @7 _
'add_new' => 'Add New',( N v: V8 Y) F5 v7 \& z4 q
'add_new_item' => 'Add New Site Wide Notice',
: z5 C( ^$ E9 O 'edit_item' => 'Edit Site Wide Notice',4 S0 O) q9 E* m7 L0 t; Z; Z; [1 E
'new_item' => 'New Site Wide Notice',0 k4 w& e0 l# i6 F. u
'view_item' => 'View Site Wide Notice',9 x6 K: r" ]. g7 B
'search_items' => 'Search Site Wide Notices',
" L v) e1 u6 g, j 'not_found' => 'No site-wide notices found',
5 X0 f' ]- l$ a5 ?/ J% j4 H 'not_found_in_trash' => 'No site-wide notices found in trash'' q) a) S9 p+ L7 H& J
);% T4 ~, V. d5 u' V% Q
+ G2 g v) V) ?7 A. t $args = array(
! w' k$ ?, A- |. y! s- }) r7 A 'labels' => $labels,
: `# y2 G5 {% M" i 'public' => true,, R; L8 z6 e' r5 o) R' B) W/ B
'has_archive' => true,% D) u/ ^' ]9 o3 w" t1 G4 v! i
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),, k$ [: l3 J( A. m2 @
'taxonomies' => array('category', 'post_tag'),1 K) S9 f F$ A4 z
'menu_icon' => 'dashicons-megaphone',
1 }8 F' ?# x: s 'menu_position' => 5,7 B0 R# ^0 J* Q( e' Y
'rewrite' => array('slug' => 'site-wide-notices')! m7 A$ w8 R0 D/ I6 u" n
);
G: |: v2 h+ X3 |; X$ G% u! J+ m$ k' ]0 D
register_post_type('site-wide-notices', $args);. `4 b4 x2 P" I) n! {9 D
}) q: o3 B4 K3 l9 \$ c: s
```0 d, K$ `9 `# B! ~4 x- v
, U- n* P$ E5 {- I; h# n 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。, o) _5 ~' \2 p6 k
, b* b2 [* b/ o- A3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:* e4 ^; u- \0 P- ?* t
! K; \! M+ D. c. J9 s& @: I ```
# V6 }/ Y* U" [# |0 a add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
) y6 d+ Q* z8 I) x: | function add_site_wide_notices_boxes() {
. l% ]; m- L) r2 N' Z add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');1 |" W5 ]+ A" ^5 b/ i# G8 p9 A3 K
}8 t( O5 r& u0 A w
; ^# _" G' c$ _. P: q! c( X! e3 E
function notice_details_meta_box($post) {9 t+ e, |5 C9 q# K
wp_nonce_field(basename(__FILE__), 'notices_nonce');4 g9 q" A* \- |* l. o. c t5 ?
$notice_title = get_post_meta($post->ID, 'notice_title', true);
5 n7 ~! j% z( L8 _' { $notice_content = get_post_meta($post->ID, 'notice_content', true);
) x0 o" ?8 l$ N; w ?>
9 Z9 D1 a1 T% |% V2 P: q1 G3 E <p>
" W- H# s9 W; U! E5 P <label for="notice-title">Notice Title</label><br>
/ @) N+ Y) d% H) N0 V <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
- D7 D& k+ I3 r& @2 s </p>
! A9 s, O6 Z: E$ d3 m3 z6 W <p>, P. ^! M% A: `, T
<label for="notice-content">Notice Content</label><br>
) x, A P* g# i <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>; \; ^; F% \' b+ {5 g3 N
</p>
! J8 }+ ]0 M* N7 v$ P, K <?php
# u& C& R% N8 q( t0 j4 d }
9 H+ M; W& a: h, \* h; h7 u, s6 R. l
add_action('save_post', 'save_site_wide_notice_meta_box');; V" ]8 a. A7 O `/ ?9 v+ _( l8 J
function save_site_wide_notice_meta_box($post_id) {
5 K! Q# `/ z$ t if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
' @: O P8 U* T9 O# U. Y- h1 D* \ return;9 r6 v. l+ ~/ c: q1 `+ c
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
+ M2 Q2 b X. Q9 J5 Z: {( j return;
3 e; b5 l5 H1 R+ v, m
% p( e7 A3 v* E7 { if (isset($_POST['notice_title'])) {5 B& O8 t5 l6 B
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));/ c5 S- `6 k+ b/ f
}
/ n3 l: X. m T/ W( q$ @; I if (isset($_POST['notice_content'])) {$ A$ p2 w2 d2 k0 f
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
! z8 D- L# Q# n1 X. h% E }* ]3 ~1 q4 f" \! z
}
q# B, i) N( E p ```2 I" e6 a' B7 ]
& M1 u8 D" K8 Y3 o* c. N
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。 e' p# h( L/ B7 n7 o7 T
& z' p3 v( W8 b- A
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:2 y5 n' r, E; b1 w I3 u, S
% m+ `! C/ z9 \: s
```2 Y! Z, Y7 e" |; r! x
$args = array(
4 F! G8 G( Y4 P6 \$ H 'post_type' => 'site-wide-notices',
# H) }# O* A: z- x 'posts_per_page' => 3,# E3 m- w, p2 ?' C w* R4 H
'order' => 'DESC',
+ j& F& u# w0 E" ~ 'orderby' => 'date'
0 D- @/ j: p& F* y );
: L) Z6 [+ T' V/ x5 y! L $query = new WP_Query($args);
* u; M& Q- p' k% Z if ($query->have_posts()) :3 g& ?/ I- t# j' N, s2 g
while ($query->have_posts()) : $query->the_post(); ?>) v9 v4 T2 u! y/ T" J9 F N
<div class="notice">
% I0 }8 c7 ~$ G' T) G. d <h3><?php the_title(); ?></h3>
' d$ n' V- o# K: _: X+ v* X <div class="notice-content"><?php the_content(); ?></div>
! z: S, V0 g- M" S1 O3 i/ i8 j </div> P5 O0 o" y k5 g% C9 R! i" L
<?php endwhile;
% I7 a6 g0 y" j8 i wp_reset_postdata();' d9 n) v# v' @, V' P( E
endif;
7 o; O. I/ x- ~0 T) c, o ```
; a; c- o' p; Q! v+ Z; O% O5 v( T4 o' S4 v, H
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|