|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?' C6 K: w9 W# n
8 f8 ?! u& I Z* M如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
9 W# }* y3 R( q) g% V7 }$ q) e) E/ y9 d9 g. M
以下是创建自定义插件的步骤:
$ j) O9 l E# Q5 f6 l2 Y5 a1 q; G- b0 ?* X: h' t' @5 V
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:/ r% o( B6 u- c0 Q' Q. G
- [+ P+ h5 v$ _/ d ```
/ a; ^, {. t' V: a4 t <?php
& k9 n2 i# }: ?' ] /* L$ s5 a% z9 R
Plugin Name: Site Wide Notices Plugin
" E, ?/ J9 f. h Description: Adds a new custom post type for site-wide notices.9 b- K9 j* o/ h8 M
Version: 1.0
, j$ Q9 I) Y8 W$ R6 C& R Author: Your Name
! f [6 L. y7 H/ z Author URI: http://example.com
( O$ e) x2 d7 u */! _9 d9 ^ l( G& U, ?; j) z+ v
+ z5 F9 }3 w' d8 {
// Add plugin code here...8 N, Q. F) {" t
```
8 I4 t# ^" o X: y; f
6 m& N* a7 X8 r. h. P5 w D& l 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。* o- F- G: x6 \
4 C. L4 S/ Z9 ~( ?% n% ^6 e. S2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
8 J z' c7 E& O7 S6 I8 W3 V" n( r# A; ^8 i
```
' K) f6 m0 x" ?7 y4 h8 l; x( q add_action('init', 'create_custom_post_type');$ o7 M) i, _* G& r( `4 N5 ^
function create_custom_post_type() {
, T- _1 l) J+ [ $labels = array(
6 s/ O. I2 }% C# h8 v 'name' => 'Site Wide Notices',$ S$ m7 j: p0 a3 k9 K' n' w; ]% }+ U
'singular_name' => 'Site Wide Notice',
5 V( b4 e3 }1 t. s: f 'add_new' => 'Add New',
" v7 ?& m# k' b0 V- k+ J# S 'add_new_item' => 'Add New Site Wide Notice',
6 d: s* ~; W9 y9 |( l5 x 'edit_item' => 'Edit Site Wide Notice',* k4 z T( N( w( x! x( @$ G
'new_item' => 'New Site Wide Notice',; y+ q3 Z+ F& N/ g! P% y
'view_item' => 'View Site Wide Notice',9 r' `+ J4 v) G6 D! P" a
'search_items' => 'Search Site Wide Notices',5 v8 L* P: B7 Y) _2 ]- j$ Y& o
'not_found' => 'No site-wide notices found',
5 ^) R# i( l _' [- b6 i2 j 'not_found_in_trash' => 'No site-wide notices found in trash'
" L, {8 G. g1 L );
% }5 o0 `% m' I- N8 Y8 U9 ~9 R- K
: B' h% f6 {$ O) x0 X' H O( ?# @% w $args = array(5 C! W B3 r* Q& Q
'labels' => $labels," q1 N4 B; N3 z2 Q5 O2 P
'public' => true,3 W9 h6 R- k C2 s' Z+ K+ p) m
'has_archive' => true,
% L+ H9 @1 n5 }5 K. S2 S) S 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),8 w2 c a+ N9 y
'taxonomies' => array('category', 'post_tag'),
: N; l" w9 X% e" e1 O( S x 'menu_icon' => 'dashicons-megaphone',
) L1 E2 ^6 a% n* s0 g 'menu_position' => 5,
4 \# F h" f E _ 'rewrite' => array('slug' => 'site-wide-notices')4 S$ Z) R# Z% H# g* p C4 J
);
0 T- Y8 @2 T" O7 d/ T! M) n3 v p) {% u0 H" I& o
register_post_type('site-wide-notices', $args);9 O3 R/ Q" P. i, v, W
}
, a) {3 Q- ^' ~& s ```6 K% p" c, f' A5 C
' ?" M$ v+ j+ O' m4 R& d
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
. f; B. w- Z: {& x$ c8 _
% d! w( N, Y- m+ h3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:, J+ z+ f. M! r; t( ?; U# i
* G+ J) N! R# o8 k
```! }. H7 ~! e# f7 b0 O. u
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');% m8 e/ ]; s/ z0 f
function add_site_wide_notices_boxes() {
! Y1 \$ R' w) _( X, o) L$ E( Y* y0 }0 Q5 c add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
( o1 p0 _9 g, p6 W5 @ }
: @" g3 A2 r: a+ J0 o: D1 P3 j1 C& w1 U2 B" L ?
function notice_details_meta_box($post) {; i% `& u! Y! x
wp_nonce_field(basename(__FILE__), 'notices_nonce');
1 ] I' c' v0 N6 G8 K9 S4 y $notice_title = get_post_meta($post->ID, 'notice_title', true);
7 C1 g. ?" x0 l3 y& R' C4 x $notice_content = get_post_meta($post->ID, 'notice_content', true);/ r; d6 s$ S& q% o6 m( M
?>
. I4 p$ j# x# @- h# H% { <p>
" A1 b1 ?7 B3 m8 x7 t <label for="notice-title">Notice Title</label><br>
! Y( ?# D9 ^ s5 x& K2 l' f <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">9 d. {5 Y3 E v) A5 P
</p>* w3 l/ v" t) w" w( |: x
<p>5 v. _( O2 n) l
<label for="notice-content">Notice Content</label><br>
0 i& V, ~1 Y( `7 e$ {+ n- k <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
1 [4 e0 a6 S0 R1 L- O </p>9 t7 r2 @* v5 b
<?php
$ \" w- B6 x6 I6 d/ u4 W* B" L }2 Z+ M0 r' X# F2 \& O
* x; X: Q0 N3 Z$ v
add_action('save_post', 'save_site_wide_notice_meta_box');7 _8 |* H3 [& A' y$ Z7 k" z! U
function save_site_wide_notice_meta_box($post_id) {
, ^" e# }" K; @0 ~% U& L: ^ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))5 L7 \% e, V' i! a* h) S
return;
: ^5 d( a( Y3 _$ y if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
0 x, S4 ]- g: N0 Y8 {+ h return;
- O; g7 `8 ~0 _+ t: X5 Y( p
4 m& `) O1 u2 Q( H if (isset($_POST['notice_title'])) {
7 m5 V7 X; R! C4 D6 ]4 |( q update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
8 }* y8 b3 B/ `: y* ~8 T1 a }
6 {' M: P7 o+ b4 T; h2 { if (isset($_POST['notice_content'])) {) o9 \1 S9 ~7 y+ F
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));. H9 |' o* M- M
}$ \/ U+ v1 ~7 A% [% b/ ?. {
}- g( z* k4 {: I( h, `2 E+ z
```
# ]: \ k0 ^0 x. \: @- n) n/ L' W1 _$ [; @" J: P
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。5 m( X( P4 p6 P7 N# E& p/ d
0 ~, @$ N$ W$ I) g, a/ p5 `$ L; K4 Q4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:6 W2 \ D) P% _7 h
3 e: u+ Y1 i) e [ ```/ e; w) @/ ~9 O# f; s. v9 g
$args = array(
; u/ J# Q9 Z4 {& {+ N 'post_type' => 'site-wide-notices',
. x+ i3 M! D! P' I) k: Q1 w 'posts_per_page' => 3,
# R) w. c$ a; ~! D1 Q 'order' => 'DESC',
; T6 g- C1 P$ ~ 'orderby' => 'date'+ C. ]2 s! i, l
);0 a" Q8 ?& z% c0 P! L5 o# q
$query = new WP_Query($args);
8 c( r: D6 z) w+ x if ($query->have_posts()) :7 ~, l2 u1 W9 E" Y
while ($query->have_posts()) : $query->the_post(); ?>
2 }6 h- J- c1 m! Y# w; Y) }' v- X; Q <div class="notice">. R8 J- ]6 s* e7 K% \7 ?- q
<h3><?php the_title(); ?></h3>
2 j6 @7 K. C+ @/ V7 _2 {$ O* K <div class="notice-content"><?php the_content(); ?></div>
9 ?" K5 d5 r+ ~( k' m </div>
0 j4 J+ W$ H7 J/ r# }9 k <?php endwhile;6 h) d" f! e/ r4 S3 k+ ?: O; w8 x
wp_reset_postdata();
' x m. I5 j& ^, l) y) @ endif;; v+ D% r# T- j4 ]
```1 ?! m2 ?( C5 V8 L8 h0 p# _
+ ? u, m; ~5 D 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|