|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
H; s3 z9 G" Q
1 D: g; v1 }% c' ?) _" X如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。8 T, E7 ]& Q7 ^3 v4 O, A8 y7 A
0 m1 a% V% N. P: _7 P. ^( r0 n
以下是创建自定义插件的步骤:
# E4 X% c X5 F8 W/ a
% ]2 n- p' `+ ]5 ]' T' k1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:9 I3 \4 W1 v5 L R/ }2 j$ a* s
) Y" y1 g' @: @* T( ]' k ```* B- N" W$ T; @- @6 I
<?php
+ z+ E. ], C9 @4 n: E9 Z" ]0 t3 I /*
' {# V) H' L% |. v' q Plugin Name: Site Wide Notices Plugin
& T3 p4 ]* \' X$ g( @ Description: Adds a new custom post type for site-wide notices.. t3 j- m6 l- H4 G
Version: 1.0
% J: K0 k! T2 J8 a) v) r+ K Author: Your Name' S/ i; b2 {$ t
Author URI: http://example.com
3 q f* a* o% y W4 S1 ` */
' s. u4 s& O' v1 o, W- w4 A# ~( e! c$ f
// Add plugin code here...
9 s; M0 j: G5 }2 U9 S ```
`8 ^3 r1 ^, l N% g$ h; A6 n2 A9 B+ |" r; T$ C" r4 @! g
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。* y$ a3 Z. K; }/ {* u7 [+ K
, @* X+ ~8 |1 Q: F$ P. A2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:5 I: I( c; g4 d3 t! m% G
# U0 j" ?' `7 s1 Z; |
```
/ ~0 {4 v; h( L& G& M5 r0 C add_action('init', 'create_custom_post_type');
0 y+ `. b7 W1 S function create_custom_post_type() {0 Y% c; }9 a4 m ]$ D6 X* K1 v
$labels = array(
. d1 R6 `& h1 X( F5 k- n& u' N# B 'name' => 'Site Wide Notices',5 t; ?. O, O& _# N+ z. Y$ S- u1 a
'singular_name' => 'Site Wide Notice',& ^8 m, s" K- B: s1 K
'add_new' => 'Add New',. N) @& f$ c6 m7 d: f5 {
'add_new_item' => 'Add New Site Wide Notice',
0 m$ \# Y2 K) W- u1 u 'edit_item' => 'Edit Site Wide Notice',
+ W$ N# _+ x8 V: T5 O. R 'new_item' => 'New Site Wide Notice',$ L" I- b5 n- _( s
'view_item' => 'View Site Wide Notice',9 p% j$ [& n8 g8 P
'search_items' => 'Search Site Wide Notices',
6 }( N6 A4 u1 k ?! @# O. a 'not_found' => 'No site-wide notices found',! A; b" }5 \! w$ F/ J! P! M
'not_found_in_trash' => 'No site-wide notices found in trash' { R5 @/ }' d0 z
);
$ g2 {- c! B8 ]( x: \( x5 S5 Y) E
4 g/ Y* Q; }/ L. u+ `# q5 q& }+ y $args = array(4 R1 D; T% y1 Z* P$ w% T& K
'labels' => $labels,
' \5 l# }. V0 p4 g3 d 'public' => true,
) O0 a" t# |1 }% M+ a4 y* ? 'has_archive' => true,7 E! B& E+ n }2 f5 m+ s
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
a( Z3 V1 j9 E9 L [; }. [ 'taxonomies' => array('category', 'post_tag'),
9 ^: x9 T; O% a) s; h) U 'menu_icon' => 'dashicons-megaphone',& L+ e: }# I0 x# J- {
'menu_position' => 5,/ i, B+ t2 H6 s9 f3 T
'rewrite' => array('slug' => 'site-wide-notices')
1 j3 }/ R i% W0 D6 \% c );1 O: |0 l& e/ v$ d" }2 X: \
" e6 d$ S/ K9 p0 I. B0 M3 r$ @ register_post_type('site-wide-notices', $args);
5 k9 O7 ~( p- h. F/ J$ h1 ^8 R" i }
$ V- W2 |2 o7 b6 P3 ], }1 U9 I0 F ```' J7 x% _) s& H% Y$ N! i) W4 q& \
t0 Y6 W9 U c& D% C. \7 |7 J
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。- G2 U2 x8 k5 f) k2 i
/ q! p* r/ Y- r. y3 s3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
( W) N' P- E/ D8 J( q& V" \/ o
9 d! G7 j+ j, w u" u ```0 A) u3 L, `3 E1 ~
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
* l" E [0 V8 J9 ^: q3 {- K0 m function add_site_wide_notices_boxes() {. K& L; j7 K8 y" V7 ?
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
: K" O: J7 u5 i a* q1 ]! { }
) G* A5 l& H$ B# P
- v, i" W; Z& J function notice_details_meta_box($post) {9 p" m8 z% G$ R
wp_nonce_field(basename(__FILE__), 'notices_nonce');
0 G' z7 y5 n0 b5 C$ M* x $notice_title = get_post_meta($post->ID, 'notice_title', true);
: X+ R, C% T0 n9 @/ y $notice_content = get_post_meta($post->ID, 'notice_content', true);" X- N5 x) _1 |8 ~4 ]" E
?>1 e) w+ B- v$ F8 x1 n' o$ j
<p>
9 y; a* y. K2 H* M6 T <label for="notice-title">Notice Title</label><br>
6 M! P+ e- r; ~ <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
' h9 s0 M5 K8 ? u& A; v6 F </p>
: l0 }+ Y& [- e) P6 l* c" N& X <p>" H" ?% b& K- p6 G& V: W" v
<label for="notice-content">Notice Content</label><br>! C* M3 M: a1 y4 B4 T4 W
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
2 O! X i8 V+ ~- E% |* a </p>
0 Y7 ~, f- W& j+ s: z5 i <?php& a8 S. G. I! `
}
# Z- I, Z. ~; O8 P
: }3 Y/ K2 Z: [6 E add_action('save_post', 'save_site_wide_notice_meta_box');
' [( }) j) Y7 i5 ] j; B& o* m* u function save_site_wide_notice_meta_box($post_id) {
4 S- B8 ]" c o( \- c if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))+ g+ F7 R: O. T
return;
& ? u# [' F, ^% ]8 M" U if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
y7 j- s9 ]1 ^7 J return;# L# q8 J' e! D L$ e8 F
) T. F6 T7 `( d+ P, X9 Q0 b7 d if (isset($_POST['notice_title'])) {
0 N* |7 ?4 c t) U update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));1 Z: ?) s6 W$ i
}6 C0 X g; q1 l& v! W! W9 o- v. [
if (isset($_POST['notice_content'])) {$ @( d6 H$ x& r. k* x" U! L
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
5 A8 L7 N: d9 n+ V K }
1 q' D0 O" r M+ c9 g. A; N }
. B4 i; q( f( _7 ^ ```2 u/ H6 o* X6 c& s+ j1 g, b
0 k- F) F( E' ? X 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
" g/ w1 D2 e/ |0 P1 D9 ~( ?# E1 \' n$ u& j
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
+ l; l. G) j# y. f( U/ R, x. O& w2 M
```
/ y3 n! h+ W. W8 T7 g- Q $args = array(
" n9 n7 k, J. R! | 'post_type' => 'site-wide-notices',8 @5 A% f R& H( r- E
'posts_per_page' => 3,
, s- T2 S! ]( l 'order' => 'DESC',# s) n( l% ?! ^1 e
'orderby' => 'date'
2 G" r2 R# ]& c. h/ w );1 E4 g5 c, I% h, w+ t8 g
$query = new WP_Query($args);, R- \1 \, M; B0 e
if ($query->have_posts()) :
# ?/ l7 y2 E# y& o while ($query->have_posts()) : $query->the_post(); ?>
* e( O) v$ `) a1 F* t: E5 T <div class="notice">
# }' C0 _5 Z+ y1 B <h3><?php the_title(); ?></h3>
6 G% u6 w$ _" } b! n" j <div class="notice-content"><?php the_content(); ?></div>
: z% e% p& ` Z+ {0 Y </div>
" x5 e) i3 p& m& M+ A' c <?php endwhile;8 s5 Q8 w8 Z8 S1 x
wp_reset_postdata();
! Q2 ]- }& B& K" u+ J( U endif;, a7 y- b2 v' W6 x v
```
2 ~$ w) y# d" h: ^/ I) f6 }3 ?* U% I i' x1 v, V4 L
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|