|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
9 v& u2 m: G, n/ M q4 c4 E5 G# `/ K: j* h( a( O# L8 @' N
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。, x1 O- Y0 P1 i3 u; Z- p5 k
: L! P" G2 [! ~6 d1 |" f$ x! c2 ^以下是创建自定义插件的步骤:
) p) N+ P2 ^; L- K R$ c6 H5 t2 Y( w% n
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:+ u3 |5 P" l! I0 D6 y6 V3 Y
1 G- c* `9 | n; o, _ ```8 T" L' a* b" y; t# ?! e
<?php
* E8 t5 D8 b6 S6 M3 I /*
2 @3 h1 L- l) V& M Plugin Name: Site Wide Notices Plugin( S" q6 s/ ?- F% T4 b
Description: Adds a new custom post type for site-wide notices.
3 i( g; O0 Y# E, {: y; a8 x Version: 1.0- W( v! @* B& L/ y9 O% }! [! \' i
Author: Your Name, S9 p. \9 K; `
Author URI: http://example.com
9 B( ]2 `" D% N% N) j */
; S& f0 ]4 p( j4 b
) V4 e+ m" d$ m& ~5 K( E1 M // Add plugin code here...
1 r7 V; z: ^, Y4 @6 O3 N# Y. q ```
4 K$ m8 T; C8 X2 B+ g* n, U+ V, f: j, l
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
6 d. o. t3 s6 ]$ E0 k2 x
' I* C& ]8 J) O: v+ ?2 g1 Y6 A/ T2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:" I$ o! j9 r8 `1 ?! @- r/ [3 `5 d
) U- T# g w$ R, q$ J
```$ Y2 Z, O3 H9 S$ y/ S
add_action('init', 'create_custom_post_type');
/ K+ a/ x* S$ D6 N1 n3 M- ? function create_custom_post_type() {
1 z- J+ d1 K% o" O3 w1 ` $labels = array(
6 H& T' d- h( m9 {" _ 'name' => 'Site Wide Notices',
5 j, V, g8 B6 F* G- d 'singular_name' => 'Site Wide Notice',& W% r. t9 l/ Q
'add_new' => 'Add New',
* y# V: `* h) x1 i( c, E 'add_new_item' => 'Add New Site Wide Notice',
% G# d' B; r. M 'edit_item' => 'Edit Site Wide Notice',
" S, i1 x7 R/ v+ V7 g3 g) h 'new_item' => 'New Site Wide Notice',
( P& J, a3 t- ^* c 'view_item' => 'View Site Wide Notice',
/ F% i& b- O' E3 G$ C 'search_items' => 'Search Site Wide Notices',' F R' q: j6 D- A4 e" ^4 e* K
'not_found' => 'No site-wide notices found',# ~( y" W- x! G3 U0 |
'not_found_in_trash' => 'No site-wide notices found in trash'
+ `) P, G$ w% r, K" @% s9 ] );
" x) g, U6 Z8 |3 u/ p
! C# v! r# o' K$ c $args = array(% A% U+ p" m5 v+ }0 C
'labels' => $labels,
1 c5 J e6 Z, a- E, \" [ 'public' => true,
) K- [! Z5 H, p' A( ^ 'has_archive' => true,
; V2 k: g+ Y- Z 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
0 ] j$ W0 d/ p$ T+ r 'taxonomies' => array('category', 'post_tag'),# V3 u! G+ V K' O5 e' h- M8 B
'menu_icon' => 'dashicons-megaphone',& D( g$ J- {5 R7 ~2 K1 ~) n; R/ J
'menu_position' => 5,
: u' u( o, f. S5 J4 F$ h* K7 P8 i 'rewrite' => array('slug' => 'site-wide-notices')$ f& `8 C: V, N7 l4 L1 e
);. U5 C% [$ u' V j6 T- I/ E3 y
7 L+ _! m& _+ t9 a5 e
register_post_type('site-wide-notices', $args);9 j' ?# W4 R. L& s
}* X: o( Q1 S2 A( `2 s7 q7 g
```
# E: e; A( T/ X% Y: S$ x7 E& _# ]/ U; f. {& Q% H' e" _
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。' k: H0 `, Q2 k2 Y8 x$ h7 I; g
1 [+ |3 j' ]9 y- u' f z5 r8 Z
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
7 Y v" Y" \, J& H; R1 s
6 Y! I$ U! C2 {- V ```5 j) D4 y% c5 f- s, {2 i; v: D8 D# c
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');' |0 `% p" r' o% u0 V
function add_site_wide_notices_boxes() {
O% [9 X8 L" t& [+ R! o add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
) M& t: ~3 t! @! F$ h& a! c( m }
5 Y ]' N! j2 D0 k8 R; o: J2 _$ T/ B
# g) `+ Q" m, c, i function notice_details_meta_box($post) {* ^0 \# P3 K2 b$ u
wp_nonce_field(basename(__FILE__), 'notices_nonce');
7 o w! q0 P# P, o: x $notice_title = get_post_meta($post->ID, 'notice_title', true);; {6 V+ N6 ]8 k3 l9 N6 m% M& L
$notice_content = get_post_meta($post->ID, 'notice_content', true);
3 Q2 ~1 Q: q7 F+ W9 E! L ?>
0 N) J0 ]% M" K; q <p>
I3 Y% V& }* J, E0 {. e: b <label for="notice-title">Notice Title</label><br>! w, s) m' D& ~1 F$ B- v ^
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
- ~! e' i- P; }7 C P0 b/ j9 ^ </p>7 |7 b5 n9 e. b5 }
<p> G' L% l0 I0 j9 U) ^
<label for="notice-content">Notice Content</label><br>
, F' ^9 K, B6 i6 [5 E- i <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
* w$ ?, _! y; T! i7 j- V0 G; k1 X </p>
) r& {* j7 S3 j( S& ] <?php
: j4 h9 M8 i, `3 P3 ^- X }- v, G4 D. H) w7 Z2 {- F7 b1 J
4 D2 R7 L) x7 D/ a. k$ |
add_action('save_post', 'save_site_wide_notice_meta_box');" h$ o0 T9 w4 r; _6 x" m
function save_site_wide_notice_meta_box($post_id) {
# f# M5 x: {) b8 {, O if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))). c$ {2 L4 W6 a; j
return;
! _1 R5 O/ O6 H% A% w if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
) k* b. d- ]9 T+ z: f return;) f: e+ m( i6 s8 ~4 L
" M% O" g# }5 A: N- O6 G$ e5 p) M if (isset($_POST['notice_title'])) {: y& b9 C, @1 ^. O
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));& M( n$ O4 }) c u' [6 {
}
. j1 d& p5 u- q% k: t6 U% {5 X9 l if (isset($_POST['notice_content'])) {! k, S2 C0 J6 g" D: n# S; J
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));# b7 w! ^% \0 P% x
}
% @2 l% h( `; c }
: L8 X8 q/ h1 P$ u ```
4 m6 G, i& ]3 X: U9 T
( }: O% R6 x, j5 C# U/ P: u 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。8 D+ a E6 S9 ^, x# H) O& z
3 w" v1 w2 w7 t! a2 W
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
% y% B8 P2 w. }& }; i8 C9 P# v( Z% A3 Y! _; V! @
```! m% L0 @# l" S' |# c2 ^
$args = array(
( D' {- L5 U# [! D$ j# { P2 A% F7 K 'post_type' => 'site-wide-notices',! C+ [) [) t: ]0 ?6 [5 F
'posts_per_page' => 3,$ g) V: Y$ c% g. o a8 G b1 P2 y6 S; _' y
'order' => 'DESC',& k; h( x0 i- R. D+ q
'orderby' => 'date'
( P/ L6 N/ u2 ~ );+ ~1 s' d# U/ K4 N
$query = new WP_Query($args);
' G% H' \6 }" p if ($query->have_posts()) :$ e: R9 d( c. c' r3 J$ w, E8 i
while ($query->have_posts()) : $query->the_post(); ?>
( k7 a0 P$ a. I7 L: g <div class="notice">* J U" ?2 l* Q {( K# j
<h3><?php the_title(); ?></h3>
' i" J7 i5 m. E$ N1 R& f, V3 ? <div class="notice-content"><?php the_content(); ?></div>, s! B% L4 i- [1 e% A
</div>; w$ k9 G8 A V9 u
<?php endwhile; v0 _1 c" h; a" T1 q) A3 D* F" j
wp_reset_postdata();- V: L J2 ~1 s. J
endif;
c1 E2 @- N% e, w* V ```
+ O! R3 _3 m' q. ]2 x! q, o1 e$ [4 i
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|