|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
( I7 M: s8 t" I/ l `" y9 E
" I: g1 g% N) x! F1 A3 ]9 s$ d; t如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
) C: U8 L" B8 k
$ {( r$ E9 q3 y3 H. o. I# U以下是创建自定义插件的步骤:0 n. x0 @! S X4 |( {# u' M! I6 `
" c% N: G2 U: y) k( S8 u% |& z
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
* _: M* |7 D J1 V% e" Z
; _4 {, N5 E1 S* H" | ```' _ n7 o% n5 ^) L7 L
<?php2 a' M- N4 K" R
/** V2 [; @, e% D9 \1 X F" b
Plugin Name: Site Wide Notices Plugin
4 b; E2 v( j) q9 I# } Description: Adds a new custom post type for site-wide notices.; _* I& s( F- T$ D; X) j+ \
Version: 1.0. x# Y6 l% t" Q1 q# J
Author: Your Name
: N+ X" L) T$ W! _ Author URI: http://example.com/ |" w6 G. [2 h# H& v3 {6 j B
*/4 X0 ]4 W3 V6 K
6 X A3 Q* G- n+ W" @
// Add plugin code here...
/ W# ?8 a9 E7 O- G9 s ```: y8 K5 ~( E# r5 g6 o) ~7 Q
: |0 o8 X9 X9 g. ~
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。; e: A4 n, G+ P* s( o1 ?
4 r6 H+ C6 U6 `4 C+ x$ |$ T, h
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:# _+ I& E/ u0 g9 z3 `, A
9 N/ @, H8 z; B* z2 J3 }0 Y
```% W: \- Y4 U: J& d# T
add_action('init', 'create_custom_post_type');
7 Q/ p; _6 ~; e7 |6 p) ] function create_custom_post_type() {! G6 d. D ^( m& p1 t# X
$labels = array(8 R N3 o$ Z% @, ^
'name' => 'Site Wide Notices',
; F! f! ]+ i+ X0 t- O( P4 ` 'singular_name' => 'Site Wide Notice',- ]& A3 ~& `7 v( j: A7 ~
'add_new' => 'Add New',/ X: B, z$ I0 U3 d' Q
'add_new_item' => 'Add New Site Wide Notice',- m8 s, P! `& J* Q+ Z; j- e" Z' n6 }
'edit_item' => 'Edit Site Wide Notice',& ?1 m8 P- v1 j9 f
'new_item' => 'New Site Wide Notice',
5 @8 \7 o6 V, h 'view_item' => 'View Site Wide Notice',( e" U# e& p& N+ m+ H7 U9 l
'search_items' => 'Search Site Wide Notices',
6 E; V' ^* x, O) r# W 'not_found' => 'No site-wide notices found',0 `/ Q4 r! L: J: K5 L
'not_found_in_trash' => 'No site-wide notices found in trash'
! H* |$ S0 c5 j: j9 e6 g );
6 w( J2 Q3 y0 D2 t
- t* L: j" k! o( _% p; t r1 Z $args = array(
7 i1 I N/ h- ]: V- G! L 'labels' => $labels,
+ P- U( @8 s1 k/ S/ d 'public' => true,
9 h% r( r1 @ @" o! ?/ i# Y9 | 'has_archive' => true,
$ x" A9 N% ~& M# U( ?; j& [ 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),7 n: b- A. z r
'taxonomies' => array('category', 'post_tag'),
' s Y% C) m& b8 Q6 a! B+ w 'menu_icon' => 'dashicons-megaphone',
* J o9 H) u; T 'menu_position' => 5,
3 c& d% s2 Z: T8 Y! R 'rewrite' => array('slug' => 'site-wide-notices')
- f! E- W' _1 ]9 { );0 d; U' v2 V4 o- |4 j2 c
3 ?; {; u$ v2 g8 G- I
register_post_type('site-wide-notices', $args);
$ y" Z. i. ]: ]) R7 J# ?, v3 E }$ W8 T2 C" k9 T
```
& w. f3 r% t) x( _9 e# i+ _( y6 o
, F J" C) x j) N" H, e 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
+ L p7 f$ `4 t6 e$ _- [3 M% N* i3 Z2 M) m& h' F. \( Q9 c
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:( P; t+ t# {+ A2 N; }
9 a$ ^4 Y: l0 n" ~
```- @1 g5 z8 I: d
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');, P @# e* h- v( p
function add_site_wide_notices_boxes() {4 {5 e( @ h* P& e# _2 O8 D3 G
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');' ^" P. m; P9 Z
}" F0 Q) t4 H$ i4 Z
, C. r+ t- \3 D) s0 J8 @+ m: S7 c+ k function notice_details_meta_box($post) {
) H, k( z* G0 c- x& }* F9 Y wp_nonce_field(basename(__FILE__), 'notices_nonce');; d" X0 [ |# ` n
$notice_title = get_post_meta($post->ID, 'notice_title', true);$ K e# \7 @/ V
$notice_content = get_post_meta($post->ID, 'notice_content', true);
) F, t7 M- i6 {1 c ?>; P- W6 L% ~5 |. D' ~" }( E1 s
<p>
, H" ]$ N2 L9 U <label for="notice-title">Notice Title</label><br>
) O: Q$ j/ d0 ~8 U <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
7 t, ^# f% A: i6 L3 Z# l3 i& t </p>
0 G3 V% b* O& ]% D! M, s <p>5 x; T' N- Z. J6 C! V
<label for="notice-content">Notice Content</label><br>
2 G, a* |' h' i4 S& N <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
' A8 H7 {1 R0 r </p>% k) X: r: ?( U4 ?& G; u* q, Z7 M
<?php6 _/ {% z8 R1 ]4 ^0 O( z
}
, s1 ]) l" i0 p0 w% c
" P/ x- E( N5 C8 J8 Y" U1 A add_action('save_post', 'save_site_wide_notice_meta_box');( {; q/ l$ n( g3 @7 R( B1 ^* P0 y
function save_site_wide_notice_meta_box($post_id) {4 |& ?2 ~; ]* U7 P& x
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))5 j k3 R9 ?& w# \ o; a
return;
1 ]9 Z- D# c& H, z if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
$ O4 T+ b2 e5 n b return;& O. Y0 d" s. E; n6 x' I
4 T$ R1 v7 ~' F0 w4 ], |; N5 P
if (isset($_POST['notice_title'])) {
8 M' K. }6 o0 B9 q+ w4 c update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));/ G$ i# }- p% U) p: t
}$ U# d) c8 |! M! S/ e- W
if (isset($_POST['notice_content'])) {- E9 x4 G3 q+ L! E/ d' Y' S5 Q
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
3 P6 I/ W+ a! Z. l: g8 [ }* E' @% p4 h7 _# H
}
5 H9 ]/ c# d" D* u% _ ```3 g! T7 D" H& R9 b
% P' r8 D u, S0 J( x1 e
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
3 E! E: T& {9 b p, b' q- a8 Y& s
$ _: Q" \, t, ~ D1 a1 X+ y4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
. N$ W) y5 A5 b' v' z* }0 f* x
% O' Y( A7 x( O ```* S; H6 }% p" z
$args = array(4 Y/ S5 \: K! V) i2 ]0 {. S. g6 t
'post_type' => 'site-wide-notices'," l/ U/ g/ i2 o
'posts_per_page' => 3,& V. Z0 M$ ?' F5 Y) n
'order' => 'DESC',, s" r9 Z+ @* U0 s% c
'orderby' => 'date'
+ V: O6 b: Z5 T+ F );$ f O1 n9 B" b, K9 W7 o: e
$query = new WP_Query($args);- h3 A5 u2 R5 O( u5 ?9 u, }
if ($query->have_posts()) :5 s0 f" m7 v. d5 h5 S( j" ?
while ($query->have_posts()) : $query->the_post(); ?># |9 E; Q2 c& a$ }- |
<div class="notice">+ g- o! |( M5 g2 h6 ^( \. j
<h3><?php the_title(); ?></h3>
- q5 W3 U5 S3 d9 o9 g, S+ B <div class="notice-content"><?php the_content(); ?></div>
* @4 y4 C+ M0 M: \, Z </div>
8 m. z. a2 E* w( V* B% H: X6 b <?php endwhile;
3 t- }0 w6 ^% N. H2 o9 p0 E6 g wp_reset_postdata();/ p5 B+ O" N/ e$ @; { G
endif;
1 Q! M5 B. i" \. r5 g; G- j2 F( w ```0 }( I6 l, v- t& ]& t* `7 D
! Q8 Z9 O/ j; d W' q) `
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|