|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
$ q$ t0 R; ^! }2 C
" ` g" |1 |' _5 g如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
% @/ h8 G4 p, ? q' M! x0 l7 ?3 }6 d2 ~( j3 ]
以下是创建自定义插件的步骤:
0 R% S" q. c( l. ? q4 `7 n
& K* i6 u0 d& j3 A! U1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
6 D. T3 g0 a( w' G# e3 i$ v; y( d; Q' o! d5 D0 z
```2 {. m6 Y- y7 {7 L4 A; g }/ I( W
<?php3 R0 v; ~+ q0 R% H) T6 U6 c4 [
/*
( c: A& V9 [6 f+ y# h7 a Plugin Name: Site Wide Notices Plugin3 Z8 h9 v; h5 Y2 m5 N
Description: Adds a new custom post type for site-wide notices.
9 e, |2 U8 y6 E) P' | Version: 1.0
5 i/ }% G( B6 _ Author: Your Name
4 f0 U7 s' a) x. \3 S2 H Author URI: http://example.com1 z6 N# s' j- Z) s8 r! w) C& Y) H
*/
( I' y6 W/ y6 G5 u! n6 W2 y _! \7 A. [0 u
// Add plugin code here...
8 f9 u# \9 i# [0 Z) d ```/ |: I. P B' F( P& e. [
8 h+ g% U6 ~5 l0 c- e. `
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。 \( J8 c8 }" B4 v5 ^/ j
- z- u" V1 T' v. k1 l
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:( v" Z+ Z4 v9 W! v9 N& P; V
7 {6 U* }- R7 E: w4 h
```7 J/ F) ]( V2 `+ U) E( C) R
add_action('init', 'create_custom_post_type');/ T7 H$ a6 v6 O8 e1 f; t
function create_custom_post_type() {: N5 C; o% w# e; |
$labels = array(& m6 k' d/ \6 s# p, m- t9 G
'name' => 'Site Wide Notices',
* B2 p# ~$ i! | 'singular_name' => 'Site Wide Notice',
! f6 M' r& B4 E$ G2 I2 g 'add_new' => 'Add New',
8 j" B6 d* A( m1 |* p( u 'add_new_item' => 'Add New Site Wide Notice',2 s4 y! [/ B1 p3 M7 }. X' u. ?9 p( S
'edit_item' => 'Edit Site Wide Notice',/ j# l% ~4 P# i6 Z8 ~% f" F! N
'new_item' => 'New Site Wide Notice',
/ D2 ?5 u& o6 E1 g 'view_item' => 'View Site Wide Notice',
! k8 ~$ m" {7 a1 L2 Q 'search_items' => 'Search Site Wide Notices',
! k& y; s8 I, A6 @ 'not_found' => 'No site-wide notices found',2 T' d: ?7 }9 Y! x/ x
'not_found_in_trash' => 'No site-wide notices found in trash'
% ? \8 x9 B6 t: t% o );
6 c q {( X) k% s/ D: J3 x2 R- f5 Y# y+ O7 N
$args = array(
% F: r0 \) k5 a6 v6 _2 M) m- j8 ~4 H 'labels' => $labels,
+ ^' I# A( M7 O. |/ S" k0 v3 r 'public' => true,$ u$ E4 D: ~! t+ s4 P; {
'has_archive' => true,' F$ C/ I) F: \ W `7 }/ V
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
2 |! g9 r S/ h& r/ H1 z 'taxonomies' => array('category', 'post_tag'),% Q3 L$ f/ r% `& H
'menu_icon' => 'dashicons-megaphone',
. o) {& r; x ?( y/ W: { 'menu_position' => 5,% L* J6 ?* r6 F& b
'rewrite' => array('slug' => 'site-wide-notices')
5 u+ q- ~& ^6 `7 P$ c );" h, h1 g0 w( a9 z) \/ F
9 S6 Q) v" _1 x' ], C& W3 }# \ register_post_type('site-wide-notices', $args);% G0 l# s. K9 [9 X! q
}
0 }% o. F# z0 { ```
% r1 h: d' o. x6 U2 e, U M0 }8 m# \
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。5 M" R) h) B' W' p+ s- x7 a
) v+ E' M8 w6 Z4 I% c8 w3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:3 Q- p9 S9 r, A0 w& @
D) u! N' a3 r3 K7 }$ P" C ```
7 j1 |: e0 b' g/ m! c: t9 B add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
% y, e+ D7 h" o; e+ _8 F function add_site_wide_notices_boxes() {
1 \+ O: t! ^! q, C add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');9 g' I8 J1 w# X; G
}: j6 h6 Q& }4 l. _% n
i9 R8 y$ e/ r9 D$ z X
function notice_details_meta_box($post) {6 t5 b$ I* c6 L$ w8 C
wp_nonce_field(basename(__FILE__), 'notices_nonce');
% X+ P3 J& K4 a/ F: w& u$ A! h @8 W" \' c $notice_title = get_post_meta($post->ID, 'notice_title', true);
: ^" G, u1 ?7 A; g $notice_content = get_post_meta($post->ID, 'notice_content', true);$ M: u+ v( `+ ~' R* O* B3 p' k5 f
?>
2 Y7 [& q# J/ H' n2 m$ M <p>% S+ ^* r- w2 y. @1 w! ^4 c$ Y* v
<label for="notice-title">Notice Title</label><br>' d( y2 n8 a; u! X# P
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
& |7 y! t4 g, }/ w2 s: ^5 ~( Z </p>! K" l8 Y" h7 v3 }0 B/ _! ?
<p>
: b# q$ h, d/ V+ H) N4 z+ \ <label for="notice-content">Notice Content</label><br>
f+ Q: p) v* X- F* X <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>+ h. L" s& k. e
</p>
! a# L0 T: Z7 R9 Z% ~3 J <?php' {# k- W5 h3 V6 n; q0 Q1 e* A
}
: D8 \7 o0 W$ `: h/ U" X
X: y" I' q3 _( D+ Y- F add_action('save_post', 'save_site_wide_notice_meta_box');
, C* E' e! I- h# D5 y" { function save_site_wide_notice_meta_box($post_id) {( y/ D0 r8 f V0 V$ u: X8 \. U
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
1 {- A9 M. ?2 [5 A( c+ ~/ `7 ^' [. Y- S return;
* Z) Z) B9 ], d. v& D if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
( [) U" W, C3 ~( e7 _+ V return;
5 I" k% ]- @, [# b' c, I3 d% |8 h+ {$ h5 w
if (isset($_POST['notice_title'])) {
! Z: n* z/ e0 B0 o# e# R/ ^4 k. g update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
6 W5 ~1 ]/ Z: B6 a3 c* \6 ~6 } }
# F9 W/ y) h8 s% t. A if (isset($_POST['notice_content'])) {; O4 b& f8 j7 I7 k9 Y
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
4 ]4 J7 U. Q) D }
+ K' Q2 p6 T+ ^! o/ C }
3 F4 P' v& b+ `6 O* ^8 I( p ```
4 k/ C( Y0 w) T# w5 y0 s/ E( d( i$ ~% a; I! C/ r. k
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
3 ~" @; h$ a; l2 Y) X
4 U) Z# p. |8 L/ r# X9 c4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:1 }: c& Z9 M, \! L4 O1 O" V
$ L8 k3 t8 r( \5 ^# S ```
+ b3 r! h! D6 w2 i0 K $args = array($ o4 _" t6 T4 N+ ~" E. K
'post_type' => 'site-wide-notices',
4 C U9 [ j5 h8 G7 E 'posts_per_page' => 3," v- h0 b: {/ \. }# e' {
'order' => 'DESC',
$ k; v" G8 Z2 V- z0 l5 B% L- x2 b 'orderby' => 'date'( M0 x7 |3 Z5 j$ E5 f& _# k7 J
);
0 c* ]7 f. {2 c; Z. G8 U $query = new WP_Query($args);/ f- |, d5 j: F( ^
if ($query->have_posts()) :! p: y {+ U$ m1 G R. @7 e
while ($query->have_posts()) : $query->the_post(); ?>9 T! K1 A1 X5 q
<div class="notice">
- W' `: T7 [* l h <h3><?php the_title(); ?></h3>
4 `0 Z3 q. ?$ p8 U( j5 ]- | <div class="notice-content"><?php the_content(); ?></div>
9 t/ W0 Y! w7 X </div>
3 H6 Z; d: ]( \1 N4 u" X' ~# Q <?php endwhile;4 [! A/ p/ P5 _" B+ _" d( ?
wp_reset_postdata();
1 U3 k Q' M; t3 h5 K endif;9 \! Y0 b1 x3 @" d( x
```
4 I8 G9 `# _$ g; J$ I$ Y b9 t2 b! h/ [- r: Y; q
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|