|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?3 F* Y* r, Y. u# T
4 y* n- y3 \4 p, x3 E( N N如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。5 B) O9 A- e% B; J7 x% v1 ?: _
; P: Y- |, H5 Q4 n& q
以下是创建自定义插件的步骤:5 ?) `" p/ Y) R
1 l9 B/ _6 q5 {3 t, W1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:! {+ m5 i# g( n( T, t
1 k1 V+ O# h7 T- n1 @& t- q ``` |# y; p( ?. k0 Y
<?php1 P) e3 Z4 N5 n" P0 } |. E* G
/** I2 p& B0 V8 L0 }$ `
Plugin Name: Site Wide Notices Plugin
- k9 r/ O/ z( ]- f8 H" n- p- S Description: Adds a new custom post type for site-wide notices.
: v* c! C4 H# B* x* b ]( C- [6 ? Version: 1.0
* i" j) x( `5 @+ ?- Y+ A. z$ n4 s+ F; Y- r Author: Your Name, N) w$ S/ r' A! V( t; u
Author URI: http://example.com$ ~, z7 W' M. n$ q& }6 M
*/, A/ U' n3 e9 \6 D
# V+ [2 T+ Q3 b3 b9 {, {0 K# U
// Add plugin code here...
# C+ j/ p0 C1 l( @3 @; K% X _ ```
8 }2 z+ i- t3 T. B9 W) E
) \6 P) T: b0 e3 |0 L 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
9 m. x3 m5 l4 T( k% t. z& u- O/ V$ g" E4 I9 }: i
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:0 E) g* m. F6 b! h$ e& H
' O: f Q) C* Q/ t9 b2 [3 K
```
/ w( |- @( ~( r Z" z add_action('init', 'create_custom_post_type');
9 J, W8 u" H0 e7 B7 e6 D' k function create_custom_post_type() {, U, j3 p) E6 l- e4 `9 u, M, ^
$labels = array(
8 ^8 H3 ^: k8 k; e: } 'name' => 'Site Wide Notices',' G Z, l6 H+ d; K- d/ T) q+ f% {
'singular_name' => 'Site Wide Notice',7 A; t, v0 ^4 E" n# ^
'add_new' => 'Add New',% Y* Q. z, {( i# M5 \6 s3 M. v
'add_new_item' => 'Add New Site Wide Notice',
, Y; R1 E7 N5 [ 'edit_item' => 'Edit Site Wide Notice',
7 ~ t# O% }% i 'new_item' => 'New Site Wide Notice',+ F# t' j( p% Q# j
'view_item' => 'View Site Wide Notice',
4 T4 E/ v. {5 J9 Q9 K: ~ 'search_items' => 'Search Site Wide Notices',
/ |7 s3 `! M1 c0 Y, D+ Y 'not_found' => 'No site-wide notices found',% A5 R7 N; R" }; o
'not_found_in_trash' => 'No site-wide notices found in trash'
/ r9 T8 ^5 N3 U );
% s! ]1 J" v5 z$ ^- q7 h, q9 `5 u* D5 ~5 D+ x
$args = array(
* `& |1 j% c- s) |9 u 'labels' => $labels,
a0 w, o# K0 I 'public' => true,
% Q. K* u# S% ^/ h* K+ y5 m* v 'has_archive' => true,5 s! f; I* d* u0 t' k! I
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),/ D* X3 M P$ l5 b6 t* x7 [
'taxonomies' => array('category', 'post_tag'),
B- F+ O! V4 V$ U, i* x( @ 'menu_icon' => 'dashicons-megaphone',
" q- f/ o' X9 L 'menu_position' => 5,
/ |# A/ k t+ X! k" V7 ? 'rewrite' => array('slug' => 'site-wide-notices')
$ B$ B% R6 B$ x+ S, Z# k3 @3 H );$ [% I) [- F) p# M1 \# }3 H$ P
) G$ Q4 m- U: y+ Y' i; A register_post_type('site-wide-notices', $args);' Y* c0 ~& S& E8 k, P# J0 X
}1 o/ s" K8 b3 k/ T7 w# l
```
4 q# D; t! T4 W7 u) h5 o7 ]2 I& Z) F) `; F: d" {; e+ W
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
% v* h: y$ C- F* B0 R1 W ?+ X( D6 [8 l& ?
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
$ D& R" P4 k; ]# j4 e' |
! M5 p- i" V- |% m1 t ```
3 _/ I: r+ ~* H; w add_action('add_meta_boxes', 'add_site_wide_notices_boxes');$ d# p Z( X4 s" f* i( h
function add_site_wide_notices_boxes() {, n" u$ v: Q. [6 F( y0 f& K; B4 i4 X
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');5 ~8 z& J/ L7 X0 I6 K$ k; v# `. g
}
4 @) |* D" d' H. u* j
3 f8 R- m- D: D. { function notice_details_meta_box($post) { b- i0 @2 _; I
wp_nonce_field(basename(__FILE__), 'notices_nonce');
3 u. F5 g" }, C& Q! ^0 } $notice_title = get_post_meta($post->ID, 'notice_title', true);5 Z0 E: y) s. x z4 K5 |" ~
$notice_content = get_post_meta($post->ID, 'notice_content', true);) ]( a9 w4 p4 H2 O- ~+ K. P- N% \
?>7 Z# j3 ~/ L0 ~1 A
<p>$ ~% C' w! l9 z# B5 D
<label for="notice-title">Notice Title</label><br>+ f' z. E9 ^* w0 {
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">) L/ w7 q/ Q( O4 i: t, f
</p>+ r6 B- J0 R8 W. ^/ x' E- w- c+ Y( h8 |
<p>- p, K3 V! `: ~4 K
<label for="notice-content">Notice Content</label><br>4 q# F6 U% O; c8 H
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>3 Q& X. k3 ~4 o; l8 k
</p>
( [: }( W+ i7 b8 f7 [6 L0 o6 R/ w; J <?php
+ C7 S; D6 `% E7 n" t }( k( ~6 n. L/ v" i& \
3 g/ C9 C9 ]4 t8 u- }3 L" d% m
add_action('save_post', 'save_site_wide_notice_meta_box');8 Z5 Z+ k l" R0 z3 `# W
function save_site_wide_notice_meta_box($post_id) {
$ @6 b! a2 f1 d; L& T) U2 P8 F if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))& Y% z. v* q* X3 g; X. h
return;
! R* `" z0 U. z if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)( M$ E; P% d0 T( Q0 T4 X2 G
return;4 ^$ ~- c6 o @( X+ O3 z' h9 ]
4 ]2 D( i: D4 S0 W& h8 ?5 j# P
if (isset($_POST['notice_title'])) {
1 h0 O! w3 u% T5 p! d" H update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
' u- b/ M0 P2 w7 A" I }* q$ N( N8 \: O* E' o# Q
if (isset($_POST['notice_content'])) {
4 @* Z/ `1 ^# V8 z8 Z update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));& w6 G c8 N& T& F. g( l) E5 {
}# |7 s* d4 r& K6 }/ j* V% w
}
0 B/ X- f& c9 Y2 ^$ }$ E6 ?9 c ```
6 D% z4 i: k/ Z3 M6 j' Y
: f$ C i5 D/ V+ J' u% w0 | O 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
! c4 g$ Q2 j9 t8 }6 \$ ]8 G8 Y7 y& d" o) ?! G! b0 P- O
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:+ n: A Q& _0 v `& k& A
$ s( T, `* J5 |" y ^& e& u7 u+ l
```- w5 H+ b G* d- x0 }
$args = array(; F6 m, X# }, n0 {" h" h6 k: F
'post_type' => 'site-wide-notices',
+ g$ Y% l7 E2 e' m1 ~1 ?: P 'posts_per_page' => 3,
6 G' J5 ~2 |- \! U! n b/ D 'order' => 'DESC',
# r$ P( T0 I/ J; B, {0 D' Z3 k 'orderby' => 'date'
% U4 Y# v3 E0 g: D8 {% n4 E );& s* \) g2 o0 L2 [$ z
$query = new WP_Query($args);
% P7 Z" o" t4 n" | if ($query->have_posts()) :4 p$ T2 i9 g3 z5 r( k1 Y6 E
while ($query->have_posts()) : $query->the_post(); ?>
) h0 k! K2 r& a3 C+ P3 u1 p" ] <div class="notice">- ?' O/ t$ j, `. ^) D8 d% l
<h3><?php the_title(); ?></h3>
4 M; S7 M, N0 h9 h: t, C <div class="notice-content"><?php the_content(); ?></div>$ ~7 _ S1 O: Y9 t8 G6 C( j
</div>
$ R0 k/ Q) h( A1 G <?php endwhile;; T! x( W9 K w& `
wp_reset_postdata();
# O+ A/ c3 g7 ]4 N endif;/ H& n# Y. z$ K, F
```
) F- }' |9 R+ o4 {
2 Q( ]" z; {5 ? 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|