|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
+ p% X/ [3 E- |( h# o* _0 z. E# n' z! G) i: a& s' f9 m
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
& t3 v, O" {% {- E5 P; X& J: I. m" k( Z$ o# p
以下是创建自定义插件的步骤:( ~, u6 p9 u( ?2 h
}, S9 V, {& h5 G+ R+ }# E% O- V1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
' b- q; b3 J) ?7 p0 C/ q2 e# s+ |0 S! H. i" F9 l5 P
```
) r# ~- {9 ^' i6 |$ C. i3 |; X0 u$ w <?php5 V- l% d+ a3 ]+ O8 k$ e; W3 F
/* S3 \+ \9 Y' l Y1 O7 s( a- ?' n
Plugin Name: Site Wide Notices Plugin$ F' B1 o1 f+ z/ m) E9 v; b% U
Description: Adds a new custom post type for site-wide notices.
3 ?" O; Y1 x. d3 j5 S Version: 1.0
" d! C8 a0 N$ p# _' d: w Author: Your Name, y2 D) F4 Z" {, n
Author URI: http://example.com6 {3 y. u- `7 _1 K' v5 h
*/
' T/ N; D5 G5 ?) i( l: U" M/ a# ?1 U. ^; o
// Add plugin code here...
~. H4 y+ c, u; c- c9 R% j ```
) g! ?1 x* m! h) j4 C5 w. M, v7 {' J0 r
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
% A, H5 j& O6 Z: }! A( c3 y/ h! \# c1 e" b/ E) w1 j( d
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:, z& D- p# I" k# D/ U
$ r8 I# C: f2 K: W) J$ ~ ```7 [( |1 ~: z, R! @% r. d) O: ?, r( d
add_action('init', 'create_custom_post_type');% y! {* Z. r3 N& ]: v" T
function create_custom_post_type() {
9 X+ T6 t4 y/ ~ L $labels = array(3 H* R9 k5 Q8 ?5 c$ d4 N4 `& [
'name' => 'Site Wide Notices',
( x+ `5 c8 t& n8 t9 v6 g 'singular_name' => 'Site Wide Notice',- \4 ]2 k- `: L* k
'add_new' => 'Add New',8 p7 L: g3 |+ V) t, a
'add_new_item' => 'Add New Site Wide Notice',- J: x8 b8 ?) S$ Q6 L
'edit_item' => 'Edit Site Wide Notice',6 ^; |( n* K2 z- [6 |6 t
'new_item' => 'New Site Wide Notice',+ f5 i9 B/ N4 x
'view_item' => 'View Site Wide Notice',
8 u8 O( s4 Q5 `$ J 'search_items' => 'Search Site Wide Notices',- q0 g$ _1 c4 l! l& }% C
'not_found' => 'No site-wide notices found',
5 ^+ H, I! B+ K7 `/ } 'not_found_in_trash' => 'No site-wide notices found in trash'
. G1 n% ]# w _# h+ S: q );
5 I( Q* C) V3 u; V8 E1 r
! q8 C+ b; I7 A $args = array(/ j/ T9 H8 ?; [1 R- t
'labels' => $labels,) W& m- D! T+ Y3 f C7 `$ ? _
'public' => true,- U: _% Z( D$ t/ W- \6 f4 A/ E
'has_archive' => true,# ?1 } |8 s) T) I
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),# r& R6 T _$ x/ G$ s
'taxonomies' => array('category', 'post_tag'),
/ ^. D8 j. K3 X 'menu_icon' => 'dashicons-megaphone',
$ u X( y% d7 @2 G" j: @/ d 'menu_position' => 5,4 v; ?) M$ o M
'rewrite' => array('slug' => 'site-wide-notices')
; B4 W& d: S2 {. C );
) M# N; l q" x" l, o) {% {
# D5 ~! e, V5 U7 y" s! I3 b6 f register_post_type('site-wide-notices', $args);
; ]: ~7 {* @5 t$ h: T4 N }
, E8 ?& q! X5 e' v* w; p" J" z ```
5 J8 ]3 m7 o; P1 T; S) v Y' k& a5 j1 S8 ~9 w+ r
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
' {# c% P m) m6 C# f" I4 N8 J* Z* d! ~, T0 g% f! O) a
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
, Y# I4 D& b' i }# _; `) ~6 d" i9 z% W* a+ b y# k9 Y, c7 S* W0 z5 ~
```3 ^7 @/ p1 i5 G) ]2 b
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');1 j! A( a: t. ~5 z. }
function add_site_wide_notices_boxes() {
, {& p. B1 f6 O# x add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');' o& n8 P' W; D; n
}4 ^! U$ z/ E) V; |6 o
2 r+ i# D$ x7 p! q. a# S function notice_details_meta_box($post) {
, ]6 j+ C# `" C. `1 i9 G wp_nonce_field(basename(__FILE__), 'notices_nonce');
2 Q+ d: X% a8 `+ N' p4 U3 W' ]- A $notice_title = get_post_meta($post->ID, 'notice_title', true);7 T" ^5 a) H" Z0 [ X' J) t
$notice_content = get_post_meta($post->ID, 'notice_content', true);% U$ \& }& w) q7 N
?>
& U' r! Y" G4 @1 f <p>
( _6 X/ _( _6 }$ {( a; B2 Z. P0 C! z* F <label for="notice-title">Notice Title</label><br>+ E8 A5 l+ M! u" c
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
0 r: c- W5 v& ] </p>
% {& ?' R1 h6 F% k! }6 M6 V+ m <p>7 m6 `: t4 i5 n( U( n
<label for="notice-content">Notice Content</label><br>
% T" G( @- F) ?2 n+ B: l' g- C <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
' ~+ p1 b. q- @3 H2 ] </p>
- h% Y$ f4 i+ M% ]" M <?php7 j7 \+ Q4 m+ n' ^/ z! K) m3 i
}% q; Y% X" B2 e/ x
: T) \; w: t% ^8 `
add_action('save_post', 'save_site_wide_notice_meta_box');
9 R5 G- L# c& w( |( u" O function save_site_wide_notice_meta_box($post_id) {. y9 H2 e. R T( p
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
! v+ k5 g* d% {* G return;
* X7 x+ `# I( ] if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE): f4 }: k: L! c& Z! B
return;
. o* L; s$ M w) |+ c" Z
4 m! ~. O' y- G3 `: p# i; `! m; w7 F if (isset($_POST['notice_title'])) {
6 K$ y( v; z5 R% T: @4 x) o9 K update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
4 T" V+ ]/ S" z }
- s8 B' I L+ M4 W$ h if (isset($_POST['notice_content'])) {0 E P1 y" m3 X& d
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));. R" F/ M2 G7 G; p/ S: N; p
}! H1 r* y) _. i2 d) C' N0 ~
}
% l J% p7 x) |# B' v) o) ^3 _ ```
, M' Y' v- S; k# A" _$ K1 Z; j& Z& F _& Y5 y) L
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
0 l$ d, J+ z. C9 ^/ B* B' g" y. ^( Z, z6 E7 m
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
3 d9 g+ W1 d9 x+ P; z9 T+ [. r1 x- W5 }& x8 s. v- V9 {. A
```
9 Y7 d+ B* r2 r! J' |! y K: a6 N $args = array(: a) n9 h. k. [
'post_type' => 'site-wide-notices',4 p6 y3 v q3 [& {; L
'posts_per_page' => 3,
+ p/ R5 o0 d% x3 D1 w 'order' => 'DESC',* e. g4 L$ Z" C1 _, m: |
'orderby' => 'date'7 Z* [2 V M( i! K1 B0 L
);% T/ F1 ~2 ?5 U4 Z" S; |5 ]" a' F
$query = new WP_Query($args);3 t2 A9 g7 m' Y6 b- t5 M- F
if ($query->have_posts()) :7 v! L V; h. z9 i: r
while ($query->have_posts()) : $query->the_post(); ?> G8 p3 s" {- \/ L9 x
<div class="notice">$ D% ?1 x3 T- S; A# x' \* U) H6 B7 w1 _
<h3><?php the_title(); ?></h3>3 v* `) ~, Z$ a
<div class="notice-content"><?php the_content(); ?></div>" [. E4 ?- g4 a0 d+ T
</div>9 e" t9 ^6 s( v) Q$ V N3 \
<?php endwhile;# s: L7 X! v% }- z4 R7 a
wp_reset_postdata();( `# f9 v( W f$ C8 k6 J7 m+ V
endif;
0 r/ N; ^( H' A" Z m; z2 ?* g1 U ```( H) k. }6 ]; D! W$ l8 I# J: `& |
! ~ p- G/ A, i3 a
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|