|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
4 x$ M6 ~( @; W$ A2 f
4 ~! c, M! Q5 v4 \( `如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
3 T) `5 ^, s" c0 [( v* @: V# I: S; L' `! _5 O8 H& r5 |" u/ W
以下是创建自定义插件的步骤:
7 _: U: V) k& p) {* h4 b: W) @+ F& P% x8 H" |" H( A* u. C" [# s1 S
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:: J; h7 `; i; p6 y; Q1 ?
) Y% G& Y2 D7 x: G @* J8 M ```0 W' k+ p# b# h u4 p( |6 e
<?php
9 x3 G- @* d' r" h& v /*, a; N$ [, |# y4 X+ v0 C; t [4 A* b
Plugin Name: Site Wide Notices Plugin
" t/ r4 ~! L4 h Description: Adds a new custom post type for site-wide notices.
" [, s2 A6 M6 `# G1 j Version: 1.0& _. B, E0 \2 C2 o
Author: Your Name
% K' G* H5 O# Z" x$ ^' @, n) k$ [ Author URI: http://example.com
6 f$ B; j* z0 z6 D */0 y6 Y3 X+ ?- Q* ^* d% f5 x
, G5 c. g6 g3 o7 f8 H/ `
// Add plugin code here..., M; j: L5 x) \+ w% \' r$ w9 ~
```& ?+ O$ p* h$ X) I
2 c. \3 I O6 D; N 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
6 j- Q% G" L" u: d8 Q" ?
( O- X( w m0 \# n4 p( A% i2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
; `7 u+ j0 X* \9 W4 [" o7 ^7 G, c1 `8 x3 [
```( F9 `( A) {3 q- J
add_action('init', 'create_custom_post_type');
0 q, Z2 V- b, g4 A/ K4 H function create_custom_post_type() {+ \2 w$ _9 i& D% E4 u
$labels = array(- R1 F- o( I! }: p) Y# f1 \& `; i0 }
'name' => 'Site Wide Notices',
7 o! [5 [' }- r6 n 'singular_name' => 'Site Wide Notice',
- {" M V/ A. q9 Z. X& P' O P1 `6 L) ~ 'add_new' => 'Add New',8 e% B8 t4 D" F7 L$ v, E5 p
'add_new_item' => 'Add New Site Wide Notice',
5 C! V7 F- Y& a% z* U/ Q* f 'edit_item' => 'Edit Site Wide Notice',6 B8 T3 L5 e7 E6 Z& y
'new_item' => 'New Site Wide Notice',/ F4 Y$ ? z& `# e- ]
'view_item' => 'View Site Wide Notice'," P* I2 ?9 G, z3 ]
'search_items' => 'Search Site Wide Notices',5 H! ?- S9 c, Z
'not_found' => 'No site-wide notices found',
$ |/ O* N2 t+ U+ `3 f 'not_found_in_trash' => 'No site-wide notices found in trash'
0 S2 q8 q9 g# M! ~ );- d9 W3 X) T- \# V
: H/ g/ e/ `* R
$args = array( Y1 A: e9 n2 J# `; w7 w, z. T
'labels' => $labels,8 S' j+ z# E# d+ M" L* b8 }
'public' => true,
4 ]! @! |8 }( {) C% O, L 'has_archive' => true,$ `7 a0 w2 u* ^, a! o5 d. u
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
1 F) c0 t' H, Y2 G5 b, e 'taxonomies' => array('category', 'post_tag'),' e+ x3 N5 v) x* S9 _" I
'menu_icon' => 'dashicons-megaphone',
: d" G* _2 Z( d$ d 'menu_position' => 5,% V; n$ W$ ~* I; ]/ b
'rewrite' => array('slug' => 'site-wide-notices')& j' O0 @9 V! T7 X" K" ]# Z
);
/ r# ^8 x6 d/ i4 }( x, u# H5 Q# T/ ]! I" B
register_post_type('site-wide-notices', $args);/ P( B7 I& {5 p' d. {! X3 W
}3 M: l6 \1 J. h' O
```
5 U$ {! z* K U- H" G+ x% N0 v2 U& f0 Q9 o
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
/ K9 ?, m7 m- h4 o. \* h1 Z* U
* j+ ?$ i. ?1 G) d& q) R$ t h3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
& Y: i# U' ]$ y! ^" c& D3 p9 N0 B" a, h
```0 W# @, q: s. D o6 K3 w
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');6 g% J. i! z6 s) x5 S1 G
function add_site_wide_notices_boxes() {" v9 ~) |4 ]6 C- O }) Z' a
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high'); f6 |8 y4 x1 _4 z$ w, m. L9 K* V
}( F6 G' r8 p. f4 z5 T% T$ B
2 h' {3 x! B6 o
function notice_details_meta_box($post) {6 I7 V" d) [$ \/ `; w
wp_nonce_field(basename(__FILE__), 'notices_nonce');; x, R9 B" ]7 i
$notice_title = get_post_meta($post->ID, 'notice_title', true);3 z+ J1 g/ G9 J+ o9 O9 a- g/ @7 T9 B
$notice_content = get_post_meta($post->ID, 'notice_content', true);+ M* P( T' D; K' n# f
?>" s$ j; k/ `" R; p+ d
<p>
3 a" e4 l- X/ v* y/ N, x <label for="notice-title">Notice Title</label><br>
( g6 t* a1 E+ b+ }" U. x1 V0 \ <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">2 O5 ?3 [! Q7 \0 L
</p>7 s/ G% t7 d$ e9 f& ~
<p>
J* l1 r/ F# h <label for="notice-content">Notice Content</label><br>4 {, c9 m& N0 E
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
) a1 T0 L9 }& _+ b2 d </p>* L" F$ D- L7 _" m$ W0 _
<?php
. D5 `0 Q. M2 {( b0 y }/ p) F5 d1 e& s
$ x; z% Q% m% t5 @4 ? add_action('save_post', 'save_site_wide_notice_meta_box');* `+ K) z, N/ @2 Q3 X
function save_site_wide_notice_meta_box($post_id) {6 P8 \# ^0 G J0 L
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))5 s! F" v( ^$ V% t! C& r
return;8 t8 m( M$ v' k* \( b3 V" o
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)! Q, P6 H+ Q9 A/ K; z
return;, A) N2 [4 e- E( e6 t
9 o' I: ?: h& ]* z& Z! O if (isset($_POST['notice_title'])) {
2 t, p& V- _, @% [6 v update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
! @0 W. B7 v# d( E! f( Y. w }7 Z, e& Q; ?4 v! N
if (isset($_POST['notice_content'])) {( k E0 _9 R0 d$ x# L% X
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));, @, x" u# x. Z7 V
}; b+ R0 a( Y) a+ O
}
! |$ n2 |6 w% d. p ```
( _$ }/ r3 X* Z9 X. b6 Q
, N" ^2 b+ I" y 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。) b/ L0 V8 c8 ^8 I
5 q. r3 c5 A; ^& _* W
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:! p* y) @( J8 B
0 u: r6 o6 |* L7 o
```" G2 ?' n( v) c" D$ Q e0 V1 L
$args = array(: y/ C0 }/ i- n4 e, ]9 O0 Y }
'post_type' => 'site-wide-notices'," M% X; L8 \1 F
'posts_per_page' => 3,' X( i. E, B' G; l- r9 u
'order' => 'DESC',
4 B- P+ N% K4 g! n+ g7 _ 'orderby' => 'date'/ d0 ~8 [7 X/ L: G+ k
);' m& g' Y6 c! A
$query = new WP_Query($args);+ s" D6 g) Y% d5 b0 g& F
if ($query->have_posts()) :. \1 l3 b4 a4 z
while ($query->have_posts()) : $query->the_post(); ?>
: q3 C) m+ j: B0 S* ]; x0 U <div class="notice">" D: ~1 m% z9 o; S
<h3><?php the_title(); ?></h3>9 @) M: Z$ G1 I f8 m- D
<div class="notice-content"><?php the_content(); ?></div>2 t) G8 C2 J2 e; ~' g1 ]3 ~9 ]
</div>
3 [; T- t, G$ S9 D. k <?php endwhile;
! U1 e2 \/ p+ K- I+ m* F6 ]% v/ E wp_reset_postdata();6 k, O( ^0 ?# q( W: v* M5 _- b+ R0 g! Q7 J
endif;
) m4 \, \0 k- o( Y. t ```3 g+ c2 b0 a6 `1 `, V
- a/ A- h! e' t2 W- Z1 \% ~0 @7 Q
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|