|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?$ K& ~; E7 v/ }- ]' G) C4 T! V
6 |4 e" L+ g9 Q0 t9 ~如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。# ~* D5 Y: E f/ @" g2 w/ M8 V1 @: t
3 s; b _1 U- J" F1 w) O. ]
以下是创建自定义插件的步骤:
& {2 s/ P4 n9 ~2 s: z2 a2 v+ D2 Y. _6 D2 a$ l
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:' B( X6 _# e2 c' z* U
W+ H d! X: }' B' w( r ```2 a% K- V/ l. V9 `% Y
<?php
7 U$ ^$ R$ u5 S$ f% s& B) | /*0 i1 y! O) A% v4 W E- [
Plugin Name: Site Wide Notices Plugin
/ F2 L) t" U" U, o! I Description: Adds a new custom post type for site-wide notices.
* L4 k/ W; i" j7 _' m Version: 1.0, Z0 E1 ?- q; n- ~$ a8 z1 x: Q
Author: Your Name
% k5 D7 {) S3 U) [2 @+ n Author URI: http://example.com& ~4 U c( C2 B* [4 y {$ r4 x
*/. `) N. c" T5 T% ?% y2 F/ q
( Z# v4 Y& q3 x
// Add plugin code here...
$ \( u* O2 d5 I2 u9 y9 H4 I5 Q ```
+ R( e2 v9 X" H, r( W8 a4 p& [) T
% ]$ B; `1 q6 q( E ] 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。0 r! o2 n3 U* q( W. B
; j2 ?% J; M, B5 Z8 O
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
- r. N2 F1 H. ^. T4 X/ n
b3 Q3 m t( Z' ~ ```4 c2 T N. B7 L8 s1 z; ?
add_action('init', 'create_custom_post_type');
2 F3 S( t; A( e4 x' f: a function create_custom_post_type() {
[+ j% k; K* W6 D1 t $labels = array(6 m; r8 O3 `7 }* F% g' r7 l; n( f& b
'name' => 'Site Wide Notices',3 o/ @ F3 p d6 M! s
'singular_name' => 'Site Wide Notice',' \2 a' N# S) ^( |' z( y' _# p: h; E
'add_new' => 'Add New',2 h6 c i" e5 C
'add_new_item' => 'Add New Site Wide Notice',
6 q) O; m9 t+ I" d 'edit_item' => 'Edit Site Wide Notice',5 g! {; M+ N- I1 |' o9 {4 G
'new_item' => 'New Site Wide Notice',3 m D. V- @2 \8 s; \
'view_item' => 'View Site Wide Notice',
% l% r x; e) x& _5 Y 'search_items' => 'Search Site Wide Notices',7 b- {+ ?6 M$ z" {4 q) M' r+ ?
'not_found' => 'No site-wide notices found',
: K- t: i t9 o0 R m 'not_found_in_trash' => 'No site-wide notices found in trash'3 ]2 `, f% Y! q3 |
);2 V0 G5 R, b* o1 f/ T0 M
$ T7 y0 O+ { O6 A' e $args = array(# ^3 i. _7 K. [ ~0 R0 c, D, C
'labels' => $labels,
* m+ A% }3 s0 i+ h1 y 'public' => true,/ j" G3 ~0 f) n" a$ I
'has_archive' => true,) C. }- w9 M9 o! W" r
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
9 E% R1 L7 `( g; I E 'taxonomies' => array('category', 'post_tag'),
/ R2 D% M+ F+ m& J7 ] 'menu_icon' => 'dashicons-megaphone',
& t2 U8 {- K# E* ^1 t1 @1 c) c 'menu_position' => 5,
) Z+ R {. a- ~/ j 'rewrite' => array('slug' => 'site-wide-notices'), t5 r; G6 w+ @# |2 Z$ U6 R9 j( j; f
);
1 P4 I7 W, E6 _% U+ ~% e
- F3 E: G. C' V register_post_type('site-wide-notices', $args);
: J# t" Q9 V" j: T1 w$ O/ P }: G! s! K( X8 v4 A
```: A; q1 ~6 y4 h- S% z8 T% y
4 a8 |0 [" d! r, q' \& q2 }2 T
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
, f' m) D3 k% Z( D& p' k `+ A( @* Q6 r; k. _
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
5 Z/ u& P# s; a8 F4 k5 a5 r
$ @* T! q: J$ j) ?9 u! S% r% n* J ```
8 j0 ]0 u! `( P/ g" d add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
' K1 m9 d2 f- x* r+ h: m% e. Q function add_site_wide_notices_boxes() {. `& }: X7 w D' z+ [. l
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
, Z. e5 d; ?9 n$ D( A; \% [2 H6 I9 p }
" a ~3 _5 E6 l! x; z: m- J* D/ z2 |5 f( O' F
function notice_details_meta_box($post) {
( t' C4 |1 D2 y) q3 ]; E: T1 M wp_nonce_field(basename(__FILE__), 'notices_nonce');
' l5 ]; p$ |% S7 _3 T. t7 B1 m $notice_title = get_post_meta($post->ID, 'notice_title', true);
4 j7 j7 p. o2 {8 r7 p8 ? $notice_content = get_post_meta($post->ID, 'notice_content', true);# n8 W7 _1 x. P+ Q! L
?>; K( Z6 y' ]( q' N a* w% F
<p>
3 b `" O% D" U( p <label for="notice-title">Notice Title</label><br>8 L! T: Z, |3 h$ T5 f
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
! B" W4 D6 v2 ~- O9 d5 Z </p>
. h7 A( l8 ^: N& P# f$ W <p>. w& \ y/ y2 R$ B( z0 r
<label for="notice-content">Notice Content</label><br>
0 s, N1 r2 W/ U7 E+ s; v: ^. } <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>+ g9 c8 l9 \' V" c+ [# P
</p>* ~$ r' E; w' A ^
<?php r9 u7 Q6 S3 x
}% R+ L" o- ]' G% N' l- F/ U" ]
8 F& y6 B; V" n0 q. y1 Z) L
add_action('save_post', 'save_site_wide_notice_meta_box');9 S: h+ n/ s3 x$ S) q. h& c
function save_site_wide_notice_meta_box($post_id) {
2 h: s0 Z: {' A- i4 |- Y% _7 h! H if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
: h/ _4 `- j% I+ S, ^; @# v# k! o return;
6 a+ x3 Z8 D3 ^) \% |) V7 G if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)6 b- |% f) y9 ~2 t' k1 _
return;
% Z5 d& }' a1 k- F" |, L: D, E. ?, y' z2 Q& A$ ^8 p& o/ H8 E( P
if (isset($_POST['notice_title'])) {
$ G6 e. n7 j1 `' \, }% v: O. p& f4 e update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));5 v7 A; H2 }2 i0 {% M6 E
}
# ^2 S% y+ u9 }& j$ w if (isset($_POST['notice_content'])) {
* H6 Z- ^; t! v7 H$ H update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));' Z+ Q1 p2 i( `. n" I
}
6 v3 m M2 Z2 L, p+ H+ ` }( J Z: p) o. n h+ I) A, S
```5 i* Q1 \% r1 G) d% x
o+ b" L; y% X# l& q z
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。( D) t* e/ M! F6 l6 X5 K
& b0 I) o4 k7 V+ }- e- w7 p# F5 @
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
5 O0 k$ I% l, M
) ^% i p C+ D2 s' ?* y ```: [' J, b' L. J/ s' U5 h
$args = array(6 i; S8 N5 A* E+ f& X9 F
'post_type' => 'site-wide-notices',$ f6 n# L' V5 Y. n, k
'posts_per_page' => 3,
1 p$ k5 `8 u8 J' O) s" q 'order' => 'DESC',
( {. f7 @( n! q' c% w 'orderby' => 'date'
' Z+ N8 Z ?0 H8 V4 C );8 Z: }, L, ^# H, g6 }- x
$query = new WP_Query($args);) a4 N2 X/ P' L9 o' @5 s6 f
if ($query->have_posts()) :6 c3 ~: r: p2 Y+ G& h0 n
while ($query->have_posts()) : $query->the_post(); ?>) `: h5 l& X N ?# n' n$ q
<div class="notice">
$ ]. f" S/ S1 [5 w5 x <h3><?php the_title(); ?></h3>
8 c5 d; J) L/ _# n( ^ <div class="notice-content"><?php the_content(); ?></div>
; e% `8 ?6 s9 Q! g) f+ f1 e. O </div>% ?' T3 c/ g; q4 F5 L% t Y
<?php endwhile;4 V' L5 l% {4 \5 W& \: h/ f' \. ]
wp_reset_postdata();) z" r( D7 u) {9 X4 J p1 W5 V. ?) m9 G
endif;( U0 s, f" ^- G5 d& @: F' E
```: I7 J8 f+ N. F# s) H2 S* f f& W
|# Q) X6 x6 o/ }) | 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|