|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
, R8 x$ ^4 D* C( y# |1 n2 o% g8 o& j- c2 s B9 ~2 f1 {
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
) n; s. o! D9 A( Z+ g/ R
' _2 G$ M" z- V9 h/ V; n, Q8 [以下是创建自定义插件的步骤:0 j* o c0 y$ n- l
5 e( s0 i( k3 e
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
' H1 |! Z5 {' M
/ I' p* S' U( w9 P/ l$ F6 P ```! a' ?/ \5 W0 ?% i4 C" S* S# [
<?php1 |1 k* x% N# e* L
/*3 ~+ D( N: J4 B) Y3 E' s5 U5 W7 u: M
Plugin Name: Site Wide Notices Plugin
* J' V( ?, |9 h Description: Adds a new custom post type for site-wide notices.0 D% W U& D/ t! ?3 b2 l+ u0 E; K
Version: 1.0
" \2 S& `& }& R( k& o Author: Your Name
4 N/ \4 R; D; n& |3 w" g! H Author URI: http://example.com
3 B9 A# h* K8 m) v+ K, M% ^6 J */5 W1 t) ]7 H' l
6 g Z8 }$ U9 d" X0 _
// Add plugin code here...2 T/ H3 h8 A1 H: ^/ f
```
* h/ O" \: I. z. b. K) z$ ^$ {% w- L/ b. Z/ o# a
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。+ e X4 |8 q6 a# x# s# x. `
7 Y1 }( X1 W! S" h% k' U; h
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
" A+ C ] G6 j8 k6 K" O, ?
$ c9 e2 ] i% `% C8 T; | ```$ Z+ R6 c8 u U
add_action('init', 'create_custom_post_type');
. k6 m. G5 e, l% A5 B9 G& G6 M7 x+ C function create_custom_post_type() {2 z; e* D+ i. t4 N( g' `
$labels = array(
. ~6 F3 a* r/ c7 M 'name' => 'Site Wide Notices',, x- I$ Y' ?9 C- U3 K' z/ T
'singular_name' => 'Site Wide Notice',4 w/ |5 M( v! c2 y! ^- F
'add_new' => 'Add New',
3 _1 Z0 \& `" Q! I7 Z 'add_new_item' => 'Add New Site Wide Notice',$ e3 w6 f6 Q5 N- S( N# B
'edit_item' => 'Edit Site Wide Notice',
! o+ j( A: l- V7 B6 S4 Y; v 'new_item' => 'New Site Wide Notice',. h5 X' e1 K% t8 I. d" y0 O2 n& y, i
'view_item' => 'View Site Wide Notice',
6 n% T. Y1 F% H% g 'search_items' => 'Search Site Wide Notices',
+ C7 X- \4 t. n3 H 'not_found' => 'No site-wide notices found',
, z# N# ~, v3 {. F' Y# M: m1 q 'not_found_in_trash' => 'No site-wide notices found in trash'
' G2 ~* s) Z U' S- t( z3 b. s. a* P );4 t, w6 p L& r3 u5 `- Q
' _ A; [6 t O5 ~7 b
$args = array( y3 S% G) a" F" m! Z
'labels' => $labels,1 I1 r+ o% z: g; z7 i4 k
'public' => true,! P7 O: H' \( c: i% n4 x3 p
'has_archive' => true,$ t9 Q; b2 N3 o+ ]/ Q4 Q5 B3 |# G
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),3 F8 r; X: l: ~' v
'taxonomies' => array('category', 'post_tag'),& K% g+ e4 j4 g" [4 B% J
'menu_icon' => 'dashicons-megaphone',
7 Y4 K+ {8 n! f 'menu_position' => 5,+ C/ R! o c8 j h# v
'rewrite' => array('slug' => 'site-wide-notices'). [- b- O. x: L( T0 B4 K
);
U l. |. u* A, ~& ?8 o0 Z
0 e s6 u! D* A | register_post_type('site-wide-notices', $args); J7 _8 ^& i2 M D
}- J; t1 h' w2 }8 |" R: g$ G; u
```' m, L- E8 m) t y0 S. T% U$ O
0 O! n" @% @3 g# l# P# t8 L
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。+ H1 l; W0 C- I. G% B) `
6 K( x9 }$ s2 g7 @5 t4 i4 {/ f$ X
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:+ g+ j y: b' B3 _& ~: } f1 q; d4 F
5 p$ S' t& @/ I( p ```$ Z: F% A6 e) H9 J
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');1 @# j! |4 L) U3 ]5 T0 i
function add_site_wide_notices_boxes() {4 F' A3 `; {# w9 B. N4 {
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');& h0 h! p5 u! H
}' C; H6 c6 D9 G& r2 x) ?
$ A% u+ C# h- J' I+ T function notice_details_meta_box($post) {; g ?# b/ N5 ^$ Q6 w
wp_nonce_field(basename(__FILE__), 'notices_nonce');
. s7 C- j2 F% [3 h9 k1 P $notice_title = get_post_meta($post->ID, 'notice_title', true);
2 Q2 g# r6 c3 d- b $notice_content = get_post_meta($post->ID, 'notice_content', true);7 N; V' }4 r1 y4 Y2 Q
?>
7 g3 j' s$ \6 O% i <p>
2 X# D6 ?' n- T& A2 D2 h0 K <label for="notice-title">Notice Title</label><br>! U3 Z k: _* k; \, ?* p7 U
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">9 o; c- P: \6 C3 F4 f0 ?! c" _
</p>2 {* { n- l5 p# N
<p>
1 I1 B! _1 S# h4 y" m9 d0 Q6 o <label for="notice-content">Notice Content</label><br>
* G- t z4 P/ b+ t <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>9 f+ q$ H6 D: P# K0 |
</p>
, J6 ^, e; N9 n$ x) s% E) a* v <?php. u. \4 ]! J7 Q4 V2 R, b
}7 H/ q. F, O5 ]: D( I% ^
# N. e* v* Q% h2 N add_action('save_post', 'save_site_wide_notice_meta_box');
' l6 L* l! D0 F! t6 t% f' l2 U) D function save_site_wide_notice_meta_box($post_id) {
) l/ {& m0 ]6 m3 u% \' h if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
) L4 ^3 |' ?3 T; Z( I/ j! E return;
( j; w( e5 W$ w& F% m if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
% Q' l" }8 P. w: Q5 |, e1 W9 w$ g$ L0 x return;
( E9 j, C( q, W/ Z7 _$ @" c9 u
' n$ Z5 t$ r9 l, Q! C( p$ l if (isset($_POST['notice_title'])) {
- j: p9 ]( x0 ^/ V8 L4 q( Y8 X update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
2 [# }% u/ C0 N- ~ }
2 q; \8 l6 h1 l& B if (isset($_POST['notice_content'])) {
' o& \$ n4 U# v7 O( a% |6 B0 I! q update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
& T+ u6 J& q- @) m t$ ] }; |7 S0 e5 f- Z9 w; C
}" n9 g$ R y9 c! c& H m
```
! V, Z8 e6 I8 Y# T e" z% o( ^
% W$ q" E: \4 m/ A* ?# Z2 K3 H0 v 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。9 \: D4 Y% @4 Y( M2 _! r
7 t' g, k- D* s4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
7 `; t' ]0 p3 G& s, g
# S/ l8 m) D4 R( ~' K* H& A ```
% h( k0 q5 h# L $args = array( q8 q1 O. T& u" ?
'post_type' => 'site-wide-notices',
9 M9 r* d: L3 @$ z/ M& I5 M( @# d 'posts_per_page' => 3,' P5 b; [+ `/ N" x* b
'order' => 'DESC',
- a/ {# H, J; k& b 'orderby' => 'date'% H7 F+ ?/ m5 _# z/ r( B ?4 T
);& A4 ?/ w9 `( G2 z
$query = new WP_Query($args);- G/ x2 a; K1 |( t4 Z0 ~
if ($query->have_posts()) :
f6 d! A, V) q( s9 U% C' u while ($query->have_posts()) : $query->the_post(); ?>
! h- N# T6 C. c* y* t$ F$ x <div class="notice">
* G6 g! h. P. d9 Z3 j <h3><?php the_title(); ?></h3>( U% W" i( H* l( W
<div class="notice-content"><?php the_content(); ?></div>( Y; k6 R) [5 u5 m% C
</div>5 Q& ^& J8 I% r; A
<?php endwhile;
~# l# ^7 I# F' o/ d wp_reset_postdata();
7 S7 n# U4 K/ V* \4 [4 } endif;% T& o$ l* \! w c
```4 z3 T# o2 z1 k, I, }6 r9 F
/ L: h I" a9 `( T1 w5 w+ D 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|