|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗? R7 O W# q: z/ |' p3 g8 R! W' K8 F
5 G/ f) Z7 V$ o( q2 z7 Y
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。6 b- Q$ Y, M8 B$ j
8 n4 J/ w# a, ^1 ?以下是创建自定义插件的步骤:
0 |1 B R4 O6 @; Q9 B
$ s" k7 u- o" ?1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
; V6 }" T: P% W* R. Y' E' U) K+ o9 {* _+ O& G
```+ X6 W. K1 T8 G h; {
<?php
1 r% ?5 [/ h$ W) o: O. e- b8 i# f/ l /*
$ E) R% e. A% T! n O+ ^ Plugin Name: Site Wide Notices Plugin
, L: Q; a; ~$ H& F; B& | Description: Adds a new custom post type for site-wide notices.$ e% d0 s8 A+ S. G) R3 F4 o$ ~6 s
Version: 1.0$ x4 _( [8 g' D( w+ m; q) v" O
Author: Your Name, F2 L3 \' w0 I6 T) h: `
Author URI: http://example.com
6 u7 b, X" z& b */
3 w+ B5 P8 W B6 \& r* f% v# m) L6 X7 e, Z/ }% \9 Z1 D/ R
// Add plugin code here...
3 K/ V: X B2 Z ```
+ s" [ \& U2 m5 h& k) D
5 P) N+ C( j) ~$ j: O: F u 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。3 x2 g1 a* O- z8 ^4 \, g
+ P9 B: p0 N$ i2 q' @, r) L2 A x2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
( i% h! H8 D, h2 o ]- ^
0 Y: U9 x7 {1 v" t5 _, d ```" `, C+ k1 K% S# `
add_action('init', 'create_custom_post_type');: j+ b- y9 r2 O- T8 i- V
function create_custom_post_type() {* G9 t1 [- \+ F6 G" ~6 s6 b8 R
$labels = array(* C( `& ?6 c( _7 s+ X3 a0 O
'name' => 'Site Wide Notices', n( N4 A3 H9 n8 s' Y) {6 }8 P
'singular_name' => 'Site Wide Notice',
$ l% g$ D2 U3 r( M: k 'add_new' => 'Add New',
; [2 k' G4 `! b 'add_new_item' => 'Add New Site Wide Notice'," |7 m' i' w/ t. m* R
'edit_item' => 'Edit Site Wide Notice',6 B/ _/ B$ Q4 Y
'new_item' => 'New Site Wide Notice',) M3 t' g5 x, z3 Y: Z4 A X
'view_item' => 'View Site Wide Notice',( U n- e H. q9 X+ K
'search_items' => 'Search Site Wide Notices',
# k/ Z$ |1 J9 N( Z 'not_found' => 'No site-wide notices found',. [! h0 q4 R% L* {. G" e
'not_found_in_trash' => 'No site-wide notices found in trash', B$ P; v2 R! @0 p( Q! K5 |& n
);; X) a) M S4 I) T! x+ e0 S
; g1 Y; s) }2 K3 m $args = array(
4 h+ ? M# {+ q5 Q6 V1 B 'labels' => $labels,1 h$ ~7 X3 L1 Z( ^1 F- H Q
'public' => true,
4 T% d' a* Z8 l- r 'has_archive' => true,- _: ^7 n6 I3 Y1 `0 k
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
8 b8 V$ V- _9 D3 d6 H9 e 'taxonomies' => array('category', 'post_tag'),! x4 U' p& z2 a4 ~! L! \: G
'menu_icon' => 'dashicons-megaphone',
. p7 k' y# \' | 'menu_position' => 5,
5 B2 X9 Z& B4 X 'rewrite' => array('slug' => 'site-wide-notices')( @0 y# r3 V5 ^2 x) h. F: `
);
' |* w3 d P q( d" j* P7 s1 R1 p0 D: A
register_post_type('site-wide-notices', $args);5 W+ P! p0 b/ Y* c8 y* G( l: _
}3 H' G& U8 |6 B$ G' o" k
```
& G/ Q- O5 z# e1 L- L+ I3 F" W5 c+ E0 \% t. n4 E O
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。) Z- V& O# ]2 m$ m' i; z2 W- f( f: E
" t. ~* t. r# U1 g- a* Q2 B3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
4 v+ x8 Y4 ^( R G: f6 l6 _" j/ o8 ~
```
) Z! D- n6 x8 K' ]# b4 T, i add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
' x, o L* x& W/ k0 {7 f function add_site_wide_notices_boxes() {, U" ?* ^; C: j: n' n$ T9 `! K y
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
& N( d( |) s- j- ]+ R) m }5 \7 o* P3 j. t+ E
( `- r3 H+ t( k* o
function notice_details_meta_box($post) {
! {$ h8 m1 J( W0 j I( P- E wp_nonce_field(basename(__FILE__), 'notices_nonce');
1 i% v' U1 ?! X* B# r/ v+ g9 s $notice_title = get_post_meta($post->ID, 'notice_title', true);+ @; m! T4 V) a% T
$notice_content = get_post_meta($post->ID, 'notice_content', true);8 {* Q1 S$ c6 z7 m
?>
4 K, ?1 @6 W4 J' ?& m! X <p>
/ U6 g* C; T" y/ j. B <label for="notice-title">Notice Title</label><br>
) \& o8 N" l* c <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">* t) ^1 {+ F' }( O+ ~( V8 b
</p># ?8 r# S* u! e
<p>
/ n( r; v% i/ E' ~; V- y <label for="notice-content">Notice Content</label><br>
2 t% s3 T* U# X0 k2 q6 x) ^/ \ <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>0 v9 _( a$ [ u# [
</p>* |& }1 v) _4 W6 @* [
<?php( C7 L+ Q. ~6 T, m8 B n) R
}4 @, J7 j+ z \" K7 m" C! r
& m: a: R6 l+ r7 b add_action('save_post', 'save_site_wide_notice_meta_box');
. H, C6 B5 k, i3 u. M2 Z function save_site_wide_notice_meta_box($post_id) {
- e: {" a" P. m. Z! l if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))* R5 Z9 a# K$ U# L& _: u) h# K6 u
return;
! R1 j# w+ b: v: E1 W if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)( |) A$ L$ B' h3 r
return;: {% J5 Z h I: p/ z2 p2 P
6 Z& i5 P8 t: \& z5 N, Z! h6 [ if (isset($_POST['notice_title'])) {9 h2 }; j( l% i
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));8 _1 I/ _8 H) T& n) n
}, U( S% p6 e* K0 M2 m
if (isset($_POST['notice_content'])) {1 @# @7 f5 |5 z6 G. x$ R
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
3 D, E! ~' Y6 k5 B, E( z! f, ?. t: G" { }/ K. ~- d j: h6 A e. i& ?: f
}/ c. Y! _. f# h! T& Q# u* _4 h
```9 J g2 ^& }+ z* C( M* T/ l3 M2 r
& w7 g x8 \4 L3 }. B% U) K
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
+ x: V6 H/ l7 _' e9 a& c
% z- J! y r& t/ `- y$ U: L4 b4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:0 y5 h* O8 @( K
# Z1 Z: [/ v7 z1 a: m/ e7 s4 I ```/ v9 Z* b K; o, _& K# K C
$args = array(7 a4 Z8 b- s! s
'post_type' => 'site-wide-notices',
/ v3 e' P! h6 s6 N/ V 'posts_per_page' => 3,& Q$ k( R( C& _; q$ [
'order' => 'DESC',0 X0 {- S7 N+ i8 N7 b2 ^" n
'orderby' => 'date'$ ?: O7 q0 N0 ]
);# F% f9 O+ A* e( _8 P0 x
$query = new WP_Query($args); Y/ ?1 M3 H7 `2 I
if ($query->have_posts()) :
1 J) Z- {6 h4 O3 u$ D while ($query->have_posts()) : $query->the_post(); ?>
( ~ i1 M1 N5 u! I5 Y8 r <div class="notice">
" I( `; v8 r X: @ <h3><?php the_title(); ?></h3>
) \1 a! m8 B& z5 z$ j [' X9 V <div class="notice-content"><?php the_content(); ?></div>$ Y3 S' y, R t$ {6 a9 I2 X) U5 y
</div>' i7 s4 l6 i# }* v: q- m* Y! B1 w4 \
<?php endwhile;
3 s- d1 M. {7 @! k% e% }9 P wp_reset_postdata();# S+ n$ x! i, j: ]4 O' y' D
endif;
% S3 I5 l; W, X) K! O4 ]- E2 [0 N7 e ```+ `, T0 |6 p+ A* s) F& P6 W% X3 g; Y
6 P& i7 M1 }# P 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|