|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
; ~; q C' ]5 B' _! i3 n5 f+ L& Y
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
5 j/ s% B/ d5 i Y
1 |& U% n. m" j A) L; S, ]2 j& w1 c以下是创建自定义插件的步骤:1 {/ M' _, l# [; {# O' d
3 L# H- y! R. ]3 I @( D- v
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:8 {, Q6 W9 w! q* Z: G
& R$ M/ t! `+ l' g5 S3 X. L ```
7 ]+ O0 r' Z& w4 p6 X5 p/ Q <?php5 B/ V* t0 ]7 \
/*
_7 a' h4 j0 c0 Y* e+ r9 w Plugin Name: Site Wide Notices Plugin
( T# I* i0 K) j2 Q. f: _ Description: Adds a new custom post type for site-wide notices.
' m6 Z6 ~. k$ m4 `6 b; v Version: 1.0
& b2 m$ q9 q( x! T' n9 _; b Author: Your Name
) ^. V9 M: w4 q8 g9 `/ z Author URI: http://example.com8 F! K7 b& ~! o7 C6 Q" Z
*/, ] \: ~- L; {! K G
+ C3 G) P* E) z; ? // Add plugin code here...
6 P! ]9 b/ s8 Q, f$ P ```
6 V E8 | W: ]7 y+ X$ l4 C- |8 y, a
) |8 D0 ~# v# O 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
: y! }) m! |5 g
6 v9 P4 P: K% ~* b0 t( T. J2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:# e1 j h# E: H% x# D
/ V7 D0 w/ {1 ~
```
& j0 n/ {" u: G9 z& F7 F; K6 u. m add_action('init', 'create_custom_post_type');
, \/ \ B1 o" R% I3 y. H- I6 f function create_custom_post_type() {
7 y* G+ t4 c0 K $labels = array(
* \# U, Q" _; T$ L# Q5 w2 h) _+ L 'name' => 'Site Wide Notices'," C: V8 A! T: Z2 H6 ]
'singular_name' => 'Site Wide Notice',
, |# I. e7 Y+ {* d 'add_new' => 'Add New',7 F+ U+ y1 j4 N5 H! O. w3 ]3 a
'add_new_item' => 'Add New Site Wide Notice',% t. b1 T. ]# _
'edit_item' => 'Edit Site Wide Notice',+ L2 W0 l) }9 r( b- G
'new_item' => 'New Site Wide Notice',
# [% i. f0 O+ h9 B' _ 'view_item' => 'View Site Wide Notice',
% m L+ A6 r. b" d" K 'search_items' => 'Search Site Wide Notices',* I0 V1 W+ c& K+ X" f- }4 }
'not_found' => 'No site-wide notices found',
* `6 P8 q B5 J# o 'not_found_in_trash' => 'No site-wide notices found in trash'4 N. i, i3 k& G8 ?6 {2 T. E
);2 Q+ ?) g1 v; p0 S3 r( Q; N- F
3 A$ R8 ^/ o& c( `
$args = array(
" C7 j4 w( F( F" p 'labels' => $labels,
6 p* Z1 v. _' o9 {: ~ 'public' => true,
% B# q9 j: o, j3 K 'has_archive' => true,
4 [+ v! m7 H% A+ J 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),1 b, M4 ^: l2 G p: L: a( Z
'taxonomies' => array('category', 'post_tag'),
( K& z9 u Q/ O2 y8 b 'menu_icon' => 'dashicons-megaphone',
2 ~3 }4 T3 L' { M9 o# c: e 'menu_position' => 5,
# ?4 G$ \4 \% @; m5 c 'rewrite' => array('slug' => 'site-wide-notices')
0 H7 J7 d9 A, S. i% t/ o );; Y' a( S' r2 H \% l/ V
- [0 P- s5 x4 P register_post_type('site-wide-notices', $args);, _- g! c; X' f6 I4 O- }0 c% F9 V, Z* G
}; z. K' m2 ?/ h, ?! v/ Z
```7 }1 D S4 J. C- K& u9 t
+ X% ^) s- d+ ^/ V
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
$ A5 J; X* z: [# h6 `
5 i( L, Z9 K) j& u( ?3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:9 \! L$ }( e# L, }8 e4 P( S
$ f' B7 Q. @# D ```
: F, ]$ i4 [: d add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
7 Q# c7 F/ Q9 n- S5 ~6 [; I function add_site_wide_notices_boxes() {
' L8 r5 O$ _/ _) F% [& `: a& V add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');2 y2 P0 [" |& x9 N* a. S
}
4 b1 x& S& ?5 R* W- @. C
8 P: I. B9 s- c" x4 h5 x. P function notice_details_meta_box($post) {, k6 A0 H0 o4 m) Y2 q% e. ~
wp_nonce_field(basename(__FILE__), 'notices_nonce');6 `9 ^" v% E0 T$ w
$notice_title = get_post_meta($post->ID, 'notice_title', true);- j1 c; s% @4 G' s& L
$notice_content = get_post_meta($post->ID, 'notice_content', true);+ o; q4 Q3 h* y; S. ~4 ?
?>
9 k9 {% ]' A8 ~ <p>
1 t9 h1 }3 ~, j, u <label for="notice-title">Notice Title</label><br># A$ q) [; k% S* F
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">2 m \ ?( x) M5 m$ T; a! e# {
</p>
1 n( o d/ E9 U/ P2 @3 d7 a <p>
" t$ D I/ |5 m+ J2 Y' H <label for="notice-content">Notice Content</label><br>
+ \- c7 L( w( p <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>! V: Z$ }8 U2 }% F
</p>$ d! y! S7 j" f% D) u/ g; d/ p
<?php: H; J$ H/ d9 v$ p. v1 E3 ]
}2 _2 T _# Y' ~: z: v3 O; {
4 z7 s2 @# X% F add_action('save_post', 'save_site_wide_notice_meta_box');
3 z0 }5 H% [; B D function save_site_wide_notice_meta_box($post_id) {$ x4 K' a# |; s9 H! n
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))- K; ~' c& }3 g7 |& K' K
return;
, @/ `, V! x( E+ E if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)2 \- M( ^ p, r0 O
return;
! u$ s% D) m- i Y, Z2 H+ V
& {- x9 O' x5 q% W if (isset($_POST['notice_title'])) {+ Y% N8 m5 N- b4 x8 I6 Z4 {
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
5 r b i; b+ p6 f; _ }+ Z$ P; u, f" H
if (isset($_POST['notice_content'])) {
" A9 [3 Q) [5 ~" j9 E1 s2 {' ^ update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
+ \+ i W/ ?9 b( s) C- E }
5 {0 u- T+ U7 Q7 I }+ v3 X( n' B4 R7 O$ R q
```
5 F+ T1 D; O" K6 @: g- X, K+ H! C r0 o$ Q; n$ r2 Z$ Z; o
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。0 t5 E3 ]0 W$ z' ]' M
6 S3 z6 I" ?. x! }. D6 n4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
0 a: y c( i/ n3 j$ R1 |, Z
, x4 ^! X" K) E& b$ N& I4 X ```
8 g. W' H1 {6 b8 z $args = array(9 f, e0 c7 A- I4 k9 y9 l* `) F
'post_type' => 'site-wide-notices',
) v" U! ~) b% W( ~8 H6 L 'posts_per_page' => 3,
* e3 ] w8 r5 a4 d3 f 'order' => 'DESC',* ], U! o8 | `6 Q
'orderby' => 'date'
: l- ~% g7 m! r: f; A8 X a" A3 ?# ] );
4 [: e& u- F8 H- \ $query = new WP_Query($args);
, E( {0 d) ]! J7 R+ l5 j if ($query->have_posts()) :
, x* C0 D$ Q+ @; Y* r. \2 `4 d while ($query->have_posts()) : $query->the_post(); ?>1 G# w' `( s k ^
<div class="notice">9 t% ?# T$ F6 B! m7 c
<h3><?php the_title(); ?></h3>
/ w1 M. q# }. B# a8 V! \0 z$ Z t <div class="notice-content"><?php the_content(); ?></div>& f9 U% j. E9 u7 x
</div>4 v7 b1 \0 X H6 @# t2 Y8 n8 b
<?php endwhile;
4 @# Q2 L k, P2 }" w wp_reset_postdata();
! J; E9 Z$ B& Q) b! b endif;$ i6 f5 [5 a9 Q0 V8 I: H: B
```
1 P" m+ v" q+ b5 p7 q& A: S* @8 @2 z7 G5 X8 l. }
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|