|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?) i. F& o1 ]& s V9 f
# h0 U" x( R9 L" F/ h1 o& \, H如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。* J& b. f" O# P+ v8 Q$ _+ _$ t7 a
9 q N6 {8 S; T* G0 B以下是创建自定义插件的步骤:
! \; _0 o' v3 w$ S; Z
! d/ m, G/ r6 z( f! S1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
: S* E+ |& [' _# J2 {6 k( e0 @& H% }' @6 K6 d
```
, B" f- O+ }" J4 S' r; N, o1 T <?php
( r; Y( Z: W- {& A' B: E( W( u /*+ `$ ^8 h3 S" l+ O/ M- S
Plugin Name: Site Wide Notices Plugin
4 i+ W7 s9 t; U1 Z) h Description: Adds a new custom post type for site-wide notices./ @- L: a+ p' ?& f1 j2 p
Version: 1.0 q7 S5 ^& S9 H: f8 R0 o' s
Author: Your Name' T5 {! X: [" ?5 ^& t$ ^" o
Author URI: http://example.com
+ k8 F. ^3 A0 @% V3 |+ N- F h */
2 F( h) q' f9 w/ |6 ~6 ~
7 D# A2 k. ?- ] // Add plugin code here...1 _9 G, \6 Z1 S7 c
```
p; _( G8 g) ?, T; B: X6 e4 m
" r# Y6 a$ W8 ^; Y8 U/ ]5 O1 `6 W% Y 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
, B6 T5 U# K" H! w1 `( {3 I" G# [3 B, Y8 G/ H
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
) n6 ]: G% x( ~, q
3 M* ?7 F. H6 o3 _ ```% ~; v( l j- @4 g% N, a
add_action('init', 'create_custom_post_type');7 L8 H6 }8 x/ S1 w
function create_custom_post_type() {
0 t% m/ y) Y; s $labels = array(' [- W% \, R" J: r, x# q8 _
'name' => 'Site Wide Notices',
5 O' x) o" Y) `$ ? j 'singular_name' => 'Site Wide Notice',# _1 A9 z: n+ f4 x7 n+ x. B
'add_new' => 'Add New',+ q" w' q a0 l
'add_new_item' => 'Add New Site Wide Notice',
0 P; D, Y8 {5 b' K# D: V 'edit_item' => 'Edit Site Wide Notice',
4 g" }% | E8 }: u 'new_item' => 'New Site Wide Notice',$ B' k* l8 ?% O- F7 L# N. u
'view_item' => 'View Site Wide Notice',
, n2 U1 i) k: K) p! A8 u& ] 'search_items' => 'Search Site Wide Notices',
' K3 D- y/ Y" V# X% z 'not_found' => 'No site-wide notices found',
( e7 f, h" W! m: V0 D' Q4 u" g/ N 'not_found_in_trash' => 'No site-wide notices found in trash': u5 N4 Z# m! @* s
);" e. a* y/ l3 F( Y; x
( `( s6 W/ y; t* o" [! o0 ? $args = array(
0 D3 c1 R& @9 x' y" j 'labels' => $labels,* T0 E- M" X9 _9 X5 a
'public' => true,: n% `6 B6 O! ]& C4 J1 h. O
'has_archive' => true,5 ?- ^, |0 J/ Z7 E% ?
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
- {8 T4 e* k$ | 'taxonomies' => array('category', 'post_tag'),! t, [. E1 }. n! t) c0 H' o+ F
'menu_icon' => 'dashicons-megaphone',
1 l! K3 a+ ~7 Z1 h, Q. |- X 'menu_position' => 5, f1 N; M n C' D; W
'rewrite' => array('slug' => 'site-wide-notices')
+ d' `3 N0 k& |9 G c/ c1 X );
5 [- B5 `- a- C& ?' n' g& V. E! \+ ?- l& G7 k' C0 W2 s5 O
register_post_type('site-wide-notices', $args);, a* I+ x9 g' W) I0 N" x) S
}" D5 A- n/ B; s' m3 p: Q
```6 `. x0 K( e5 U, ~$ b- G
; O# t6 P5 A" y! A/ ]- P
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。5 Z: C, ^) g- I% j$ h" I- Q% ]
2 g0 Q8 U7 y5 y2 |0 Z' k3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:, c+ s; p% ?( P
/ E2 p7 G+ |8 h) m, g5 _
```. i# N+ x; ^2 m# s9 h( u
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
; @$ V$ h- x3 ~- u$ j' q! Q" L function add_site_wide_notices_boxes() {, K& O" ~5 T& G2 q
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
0 J: J4 B0 R( Y, V4 @ }# n5 ^! A0 C$ M ^, V* m7 ?
/ D. _- Q$ ~/ P3 u- S* C9 S function notice_details_meta_box($post) {- [5 X& {' ^$ Q6 n* G8 _& T: R c: I
wp_nonce_field(basename(__FILE__), 'notices_nonce');' d* N0 z9 I- r
$notice_title = get_post_meta($post->ID, 'notice_title', true);
1 ]: j% i9 {% d# J. [, y $notice_content = get_post_meta($post->ID, 'notice_content', true);
/ \0 ~9 q# f# V" b4 ?( b) N0 I ?>/ u1 _3 w, o3 o0 K$ r6 V0 N
<p>3 t& T' z; ^7 x# A
<label for="notice-title">Notice Title</label><br>
7 _1 u' C6 S: `' I& { <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
- {3 B. H$ v, X( S" f \ </p>
+ \! \# ?5 G5 b3 l <p>! ?! ?5 I3 B5 ^3 |0 _; e5 O! D0 A$ X
<label for="notice-content">Notice Content</label><br>
1 p6 Q$ \; p" t4 i' T q* @- E <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>& U5 O% Y. h6 O0 T$ C- D; @5 d# ~
</p># O5 f! ^% D# k2 N# _
<?php
) \8 D5 O" A/ p# U6 p8 H$ s }6 c0 r* k& _! t
, n- X0 Z6 p" m. A4 }. k2 k
add_action('save_post', 'save_site_wide_notice_meta_box');" h( m6 w) |* D0 |9 x4 Q3 Z; m
function save_site_wide_notice_meta_box($post_id) {
# w* W" {/ e. M$ U if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
4 i1 K' P6 B- m0 N return;
f6 f+ I7 K4 B2 @4 u s) L if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)+ U0 b0 X6 L0 d- O" X% n
return;
4 v+ q- z: g) {; c6 c1 \- H/ t( {: C" `$ L
if (isset($_POST['notice_title'])) {
' J- v0 p0 Q* R ]+ n update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));- |! D$ L) D# P) s4 E
}
) i& ~4 I! G: F, s+ y. ?1 A if (isset($_POST['notice_content'])) {
: P, V/ q; Z& F2 \% D H update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));- i O: v/ j0 R5 h- S& P" E/ u% [
}
! F1 ^: w6 z% d9 R }8 h& t7 ]# a9 V- z' S
```" J4 e$ T* v0 E' S) M
s/ {% E! u9 Q( `# Y2 f
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
' f7 F. Y+ H$ w% U% _/ @0 O) H9 h3 `
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
- R8 R/ S4 D' U l* U5 U' [; v" g( l
```0 h3 G3 ~$ a. i4 w
$args = array(6 x) k' i+ G: r
'post_type' => 'site-wide-notices',4 V2 ?0 o% s+ T3 Q; [6 A* s, q
'posts_per_page' => 3,
, F, t6 s0 }7 X) h5 Q+ F$ \- |, N+ q 'order' => 'DESC',
: c$ e1 i& S" E 'orderby' => 'date'
" d! |$ J: Z0 J0 w, L0 _ );
5 m3 x2 U# e! N( A* O* ?" E, n5 u $query = new WP_Query($args);
8 h7 C" o0 K) U o3 t4 _6 O if ($query->have_posts()) :! }0 U2 N" q6 j! }
while ($query->have_posts()) : $query->the_post(); ?>- ]# Q4 a/ u! f* t9 e4 z
<div class="notice">, o z' o W/ `: {, B, q. ~! i
<h3><?php the_title(); ?></h3>
7 E! G+ c, V8 k3 _& e <div class="notice-content"><?php the_content(); ?></div>9 K* i: d7 V' k& \0 [/ h4 V
</div>& d' N( l/ d) n( C+ s
<?php endwhile;9 T8 n, ~7 K8 h3 _9 g- q
wp_reset_postdata();
+ ?+ B1 H- r, o endif;) ?3 {' }8 O; L) ^
```
( v, T* Y- }7 G& _7 n' s: N
$ f j' x. ]7 r, t& J 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|