|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗? J3 t) F, }, n% E! F3 v
8 h# M( C4 O6 k( a
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
, r( ^* o" I) G; X: J6 n
" }) r4 D- Z& D1 I) n6 g# Q以下是创建自定义插件的步骤:
) i( R2 ?2 G. G6 \$ j" n% @ [5 J3 C
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
( r4 c( Y# {4 B: Y9 N. v2 K" D4 c* A4 v" _- ^5 K/ d i
```
6 e: t8 P/ K" E6 _ <?php
. s2 U9 k4 h* F* v& X /*+ u* D, q$ W( L8 p: e: T) k
Plugin Name: Site Wide Notices Plugin
! o( d- K4 m( ~" O, d Description: Adds a new custom post type for site-wide notices.
' U" x; m) q) ]6 i) C+ X( P2 c Version: 1.0$ q {& Z; b' Y$ U: B; I& B2 ^
Author: Your Name
9 j3 i3 S0 s( |& u6 C& K Author URI: http://example.com
/ z& m$ ~7 E7 O- l8 W% x: x, m( I3 X2 L */
) g U0 i( h) l$ E$ L! K$ h5 q b) Y$ b2 \5 v- p* a5 n) _
// Add plugin code here...
: N$ t- l( J) i# l( N ```" R- w$ a% ^( H" Y/ X0 {7 d7 \
- Q. t5 X$ w; c4 t; f0 M* n0 y
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。- u) v6 V& U& J) B8 }& G
3 [/ t# M7 [& z& D1 W' ^1 M. `+ A" a- [2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
) G- o6 g" R. G4 A+ e9 Z; X4 Z0 C3 ?
```
1 F- v; m: V0 I; V" {! r add_action('init', 'create_custom_post_type');
( T. j/ \/ Q, |( n- K A function create_custom_post_type() {
0 A+ G4 i: p' {( ]! L $labels = array(
- O% c8 `3 S" E" X& R( C6 N4 K% j" F- h 'name' => 'Site Wide Notices',/ J7 V* T/ ]) O& H* q! x# U9 u
'singular_name' => 'Site Wide Notice',
3 }7 s& R `0 z3 C8 N 'add_new' => 'Add New',, q* T& \0 q) H! v6 S0 s5 ?
'add_new_item' => 'Add New Site Wide Notice',3 u0 m% q# [. Z l
'edit_item' => 'Edit Site Wide Notice',
g% v/ K3 f$ E% p 'new_item' => 'New Site Wide Notice',
, j# p! i& h& S" n# ?% u( z 'view_item' => 'View Site Wide Notice',
8 P* o0 s0 J' M# F B5 I0 G4 h 'search_items' => 'Search Site Wide Notices',
; p. `! Z+ _8 s5 m 'not_found' => 'No site-wide notices found',2 N M! k7 X# m0 R
'not_found_in_trash' => 'No site-wide notices found in trash'( b1 m) ]2 x! F( A8 y& W. G
);
% H" _# q X( T% c$ p' f; b! z" q; g5 H; P: O V
$args = array(0 r+ a; |" q U. ^# n0 C; ~& p
'labels' => $labels,: l" w6 r7 X3 s( T
'public' => true,* h$ H/ e P3 R+ l: B `' [) y: Q
'has_archive' => true,* u9 C& M8 _6 C, L, Y* x8 j
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),& z) g3 R7 Y) q9 c! \5 Q
'taxonomies' => array('category', 'post_tag'),* M+ ?5 n e. I/ m+ I. `
'menu_icon' => 'dashicons-megaphone',) P- |: ]7 I, ]9 U6 v) B$ h
'menu_position' => 5,
% r2 a$ B/ E+ U! Q4 W3 v9 b7 I" U 'rewrite' => array('slug' => 'site-wide-notices')
+ D) O# e: C$ u! I% }; r ); h& g% r5 z" A
7 f2 ^; C0 R+ Q% Y' M m
register_post_type('site-wide-notices', $args);
0 p. k% r! I! ]) q: x) t" ^ }; [" J$ l- s" T" U
```
3 H- |% w" q! l: d+ B
# S" ~* C- _) k3 D9 H. ~! {+ [ 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
+ P+ h& D" z8 h# B+ J* u8 ]0 @4 a0 _/ ?& N& J
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
* H n/ j+ w% c
5 s8 [( _8 o% W' Z ```- `8 e$ n3 m6 E- c/ r+ W
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
9 X8 l& L! O# N/ @* O4 ~ function add_site_wide_notices_boxes() {! A9 Y9 V4 c( a! A( ~) X7 I0 o) ^
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');2 O) G% W$ U' p% o. L+ ^
}
2 [3 i2 O$ m- i6 C) E f6 s; k5 l5 U6 }# t' \3 `, c7 W, p7 f
function notice_details_meta_box($post) {, e* G' c7 R0 i+ y$ g' b
wp_nonce_field(basename(__FILE__), 'notices_nonce');8 p- s1 z7 b) w2 V H$ x
$notice_title = get_post_meta($post->ID, 'notice_title', true);
# k0 x, g% p3 r $notice_content = get_post_meta($post->ID, 'notice_content', true);
# Y! h; {- i3 z6 R+ o' W ?>) q2 W( M- G: E$ y P' r
<p>
6 L, T( ^' e* v! C <label for="notice-title">Notice Title</label><br>
# I$ Z0 C2 Q* G0 n' P& Z+ t <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
8 J# A' f1 m& W. p </p>9 m0 P& @) [* L
<p>
% _" R% ]- i4 h7 P4 e <label for="notice-content">Notice Content</label><br>; C7 t" P4 O. F+ q
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>! D9 a8 \1 z( i$ M; p' J
</p>
' D1 T7 `8 P: z* U <?php3 C& ^2 E" T4 Y" H& j! B% J- J+ w
}
1 j8 l8 C0 G$ t* S: O, n
, y9 g9 l+ D7 k6 h* ?3 j add_action('save_post', 'save_site_wide_notice_meta_box');3 }0 j1 Z3 ?8 F0 k: Q+ _3 `
function save_site_wide_notice_meta_box($post_id) {
) w/ W5 \0 `1 [ N, a# X2 o" e if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))' K o$ j K! C' D5 e4 e
return;5 I+ z/ Q5 {% i% ?, ^ Y
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)2 o- {4 T2 p5 ^$ A+ A
return;
" l: _# G' Y* n+ g( Y6 Y8 H& Q# [- m# U6 |2 C
if (isset($_POST['notice_title'])) {
, I+ l. @7 Z. f/ U# H update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
5 B3 v4 A7 R$ F9 P }0 x% r5 x5 X4 J" g6 y( Z0 C
if (isset($_POST['notice_content'])) {
( `9 W- k1 J6 [ update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
7 I3 S- D4 R6 } }# u/ `7 J: P0 k) n" w
}7 m. S7 ?! o8 {' N# M% c
```
3 R! f9 S H p& K9 \ [- H! ]% V9 l# z4 |5 e% N
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。3 c9 _# l ]6 S0 p+ Z
' @' G& t+ K2 _2 l' j& A' a
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:) S: r% V/ V, _' ^& C
- ]6 c2 S. K, w' o, }& S# H ```; Z4 y0 a* s* e, y$ u" F
$args = array(: ?' }$ c- Y# h2 S5 r
'post_type' => 'site-wide-notices',8 `' c2 d0 Z- e& k
'posts_per_page' => 3,/ R5 V6 a( b4 U; m
'order' => 'DESC',- o; a' t2 V2 F& V3 o; V
'orderby' => 'date': ~% b% G8 ?( R1 v j0 T- u
);6 Z6 X1 F2 \* ]% X2 O) P
$query = new WP_Query($args);
, m) \* n9 F7 Y8 I+ v9 R if ($query->have_posts()) :0 F I W& G' j# r
while ($query->have_posts()) : $query->the_post(); ?>
6 z- N% ]: s: ~) ~! F <div class="notice">3 j! {; K! |' C1 i M7 a- P
<h3><?php the_title(); ?></h3>3 T5 J& Z# r- ~& Z
<div class="notice-content"><?php the_content(); ?></div>& ~, K l- C ]$ ~/ Y, N
</div>
- j+ L1 r% ~7 r, W <?php endwhile;2 e8 E4 S3 |3 i3 f+ S/ o
wp_reset_postdata();1 U0 D- F5 V9 D* l" l! Q
endif;
( p+ g u3 J9 w. B5 C ```* y+ ^0 U2 V3 H K4 I- I3 n' ?8 T
; m1 c6 V3 b! Z; l* a2 L) \) |1 J
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|