|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?! P( P9 t3 G; V5 P' X
1 K/ ~6 `$ A% f o& `9 ~
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。, c/ i5 V- r" c4 J+ \$ K' h \( T& t
( J/ n1 C, U1 C& u' u0 m; a% M/ i
以下是创建自定义插件的步骤:! M7 h# |4 \6 v8 `: I& {3 |, R& n! t
7 l4 q$ G' ?4 Z+ F
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:, P$ {# A2 i$ \4 Y4 o
2 x3 b0 k* s ? q& p
```; L6 z5 R' P ]# J& g6 f& K
<?php
& C$ c. \' ^6 L' ~- `3 A /*
3 v; d! @" c9 w) h Plugin Name: Site Wide Notices Plugin" T |; E6 l3 K+ _4 b) h' t3 @
Description: Adds a new custom post type for site-wide notices.
1 x% w2 j: k1 a; O6 Q e) r! N7 t5 @ Version: 1.0
6 Q# i/ \0 n0 A9 E/ @ _' w, D. x Author: Your Name
/ y% Z. E" r5 ~9 u# ^' |/ M' g4 W, x Author URI: http://example.com* s6 \9 m/ v4 y2 q% p8 Y! R4 w
*/( \0 g$ L$ J% t3 _0 o; w4 Y
; w$ C; x& v8 B- M5 E$ i // Add plugin code here...- i/ ?. d1 t$ B( x0 t& c
```
6 ~& y9 N+ E) o7 q! ] ?
3 D" I. w% g) n0 n* _ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
4 ^; P* ?' v" ?
- _5 H+ \/ |) E& @3 i2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:) h* Y- s* _+ ?- w/ i
2 F. p$ V/ X" y: \% x8 L& R# b: A
```7 X/ ?3 ?* \ p' Q
add_action('init', 'create_custom_post_type');" v5 v2 e4 c0 }% C9 @
function create_custom_post_type() {
" t( C: y# H3 z9 G+ t $labels = array(# ^: X" `' c9 }
'name' => 'Site Wide Notices',( D' C; @3 m* y2 v1 {
'singular_name' => 'Site Wide Notice',
" M/ O+ m+ j$ c5 C, ^/ s 'add_new' => 'Add New',
; Y: p% n: Q" R( V, ]6 |" x5 c 'add_new_item' => 'Add New Site Wide Notice',2 ?9 n i! W8 ]' ^
'edit_item' => 'Edit Site Wide Notice',
# a' P$ e& o. M# D0 Q 'new_item' => 'New Site Wide Notice',
* u1 ~! N+ p$ b1 a+ _1 Y- g2 m 'view_item' => 'View Site Wide Notice',
; ]% j( {' O1 k 'search_items' => 'Search Site Wide Notices',
; z& b1 H/ {( r* a2 C 'not_found' => 'No site-wide notices found',$ ]6 c L3 {( ?7 H9 q
'not_found_in_trash' => 'No site-wide notices found in trash'9 O# J! q8 d& m, g+ S J
);
- G+ w' }% q; a0 Z: n
- O4 k8 }+ M# T5 M6 s4 @$ M0 n $args = array(
c6 ~( B( [1 A! B0 [( E3 g0 T 'labels' => $labels," x# |, \. E4 m0 u# |
'public' => true,
1 q8 _- u7 g- ?+ j5 ~ 'has_archive' => true,. L- a3 I P: K- j7 T m v3 Q1 N7 u* u
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),7 `/ u3 y) n) B. l- N6 _
'taxonomies' => array('category', 'post_tag'),
) ^/ l; l! u0 m7 ^' ~7 n0 J0 C" e 'menu_icon' => 'dashicons-megaphone',
! x( c' k. e) [' n7 s( }- z. }: ~ 'menu_position' => 5,
+ j Z6 r3 L8 m( h) I. J! a" y$ U. e 'rewrite' => array('slug' => 'site-wide-notices')4 j; n* ^8 e& T+ r' a9 a
);$ h, B5 Y7 ~! m* ?7 s8 H
$ \9 ]7 i6 q; F- S, N
register_post_type('site-wide-notices', $args);
+ c3 }! m+ k9 W( M P }
" c' m/ h' j, K ```
% O3 E% u7 M) l4 T* `# ~2 G# ^2 `" d4 p4 g
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。4 h0 a! Z9 k N: s
6 E4 t+ w ^& u' I1 |' o3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
6 s3 U; ]' p9 K5 m/ g- g9 J" l* }* ]' @0 I+ x! d5 O) n8 O1 d/ V5 t
```0 u' G" b7 k3 B
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
1 b6 i$ l V5 i! Q6 h function add_site_wide_notices_boxes() {% J1 b* g7 p" ?: E; d( d( U: j% p
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');. b& T1 ~6 U3 E0 e
}+ u2 `1 j4 O* i4 ?
* g! X8 d1 [; Y3 F
function notice_details_meta_box($post) {! n+ A! N: \3 Q8 p7 J. g* u
wp_nonce_field(basename(__FILE__), 'notices_nonce');
/ l9 k% g: ^. ~ $notice_title = get_post_meta($post->ID, 'notice_title', true);
2 R* z+ v: d7 ~" e$ L0 m1 { $notice_content = get_post_meta($post->ID, 'notice_content', true);. [( g1 `! p* m. Z V" [
?>
2 j9 \/ m6 L9 u9 i7 b) o <p>5 q' Y4 ?% n2 p- r8 s: t
<label for="notice-title">Notice Title</label><br>
. T/ `, s; m7 n( A+ g+ k <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">! I5 [( a2 D! `
</p>
0 v1 _2 }# _- p' u! r, @ <p>
) ?+ g- V8 R! u1 {7 \ <label for="notice-content">Notice Content</label><br>
2 j3 [# h; i8 K( w% L( D <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>7 V" c7 M) n p1 x; M0 y3 v. L
</p>
, G% a3 E! r$ Z/ F. P4 [ <?php3 m' }2 \; U, Y r. P- F
}2 k# i) }4 I! F
3 W# P1 w- `8 i+ x1 c
add_action('save_post', 'save_site_wide_notice_meta_box');
& `# x7 D" c$ B. Y! ?) R! \' j& Y function save_site_wide_notice_meta_box($post_id) {
+ E9 T6 [; F; n8 ~ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))8 {/ g8 A" t5 e; @1 O
return;
" d! K- M+ B0 `' I if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
8 x6 `' i5 ^" }6 O2 N; ?8 b return;
! l5 G. |9 v6 X- H. s! [; ]) b# d; b7 B
if (isset($_POST['notice_title'])) {
4 N6 t+ d) s$ ]8 @ update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
* m- H1 u4 g$ P7 H' V/ n \) w j }
8 u; @" R' y' m; t( E) q8 ] if (isset($_POST['notice_content'])) {
/ V: n9 g: f+ w s update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
7 U8 h9 o9 Y' m; k! Y- x2 | } S+ G" A3 e# m9 j% j# M
}6 H3 F& U" w1 H% ~6 y; K7 g; ^
```
) s; T! p9 g0 [0 s, j" Z& {& p z) s3 t: a5 S3 H
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
" ~+ I& j a( p( {! f( g; h
; I# Y) s- c' @, C4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:8 R+ B( i# g0 J- [6 v
# w( N* h, w+ D" O- r$ }; A
```
c& m0 K u6 e3 l7 T& Z% o $args = array(
6 u1 N( c5 j) w" R: L/ i: B. z% O 'post_type' => 'site-wide-notices',8 [9 F' D* x/ a! h( H4 V
'posts_per_page' => 3,
8 H- \6 {# C0 k- T) N! O6 ~! c3 {0 W 'order' => 'DESC',3 j9 |. O! O8 n
'orderby' => 'date'8 h& h7 q* O, m2 I5 v2 _. l
);
( f; p) q. }, _6 ~; I, _; w $query = new WP_Query($args);. y6 y# ~0 p4 p0 C1 \
if ($query->have_posts()) :
4 P# ]# ^6 ?, G while ($query->have_posts()) : $query->the_post(); ?>1 p% z2 Q, s3 |
<div class="notice">! o* [" y( h+ L! a% c
<h3><?php the_title(); ?></h3>7 s9 Q; l1 g6 W4 G
<div class="notice-content"><?php the_content(); ?></div>( t. g6 X! U7 L9 ]% ?
</div>" v6 q! T) O8 G+ m
<?php endwhile;! D! S8 M- ?0 t6 A0 k
wp_reset_postdata();, l- U! n' u3 U# b& L0 \
endif;0 {0 I$ Z! p6 H( l" J
```
0 Q i/ Z8 G. U T( [' `, w$ W. w7 {( E2 `
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|