|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
( h1 O6 Q3 B2 S* J+ C9 ?7 H; a' L# e6 Y& _# h
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。% y. ]- A3 o8 l" }0 \, S/ S" |5 H& Y
( M& }* [* @/ [6 h$ `以下是创建自定义插件的步骤:
% v- @1 ?; w+ M5 `1 b, G
2 e4 p) M v4 ^1 z1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
# }6 C! g: y; w9 l& d4 P* W! K8 E$ b5 s) H" P
```
! i. k7 i g1 a( u. y. J$ T <?php
/ l3 N. [* J- ^$ v0 }4 U /*' n: S' J5 u9 O( i, Y
Plugin Name: Site Wide Notices Plugin5 t6 B; Z8 o2 H d" G; U6 V
Description: Adds a new custom post type for site-wide notices.0 _/ ~9 B4 a$ j: c# x8 Z1 R9 T- _
Version: 1.0+ S" w! F6 ?" \. w
Author: Your Name B3 g$ q. y7 w. `1 z
Author URI: http://example.com
& W h7 E" R7 m- U* t4 W2 A( h */
' Z1 Q* e1 ^+ u6 k; n C2 S4 n: g& s3 W1 {# O
// Add plugin code here...' a( h- {! p& m; a0 ?' Y9 F
```% @4 @; L- N% `" u1 j1 g' v6 |& ^
3 ^" v4 g+ f! O% T: R6 r 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。2 i: x2 m$ ?+ y+ S F0 d' a
( ^" {: \. I$ ?! x }9 L0 ` r2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
9 @( Z! e# x' t$ n, {) F
8 S& b0 b( N/ u' q: N: ], G ```
6 ^7 h8 ?) z3 m. _5 g7 T add_action('init', 'create_custom_post_type');
' F1 |) p, E o" m C( m, D function create_custom_post_type() {' ^- G% s7 T9 t1 D, i+ b
$labels = array(7 Y, ~' ~0 [- n
'name' => 'Site Wide Notices',
5 h% o! u4 o2 x8 G& e$ o; ] 'singular_name' => 'Site Wide Notice',
- P# k7 _/ n! q: B 'add_new' => 'Add New',. Y& d7 f1 G+ Q" v! q* z
'add_new_item' => 'Add New Site Wide Notice',
4 i# C2 p- r9 b. l 'edit_item' => 'Edit Site Wide Notice',% h1 |$ d+ z# `! ~6 B6 I
'new_item' => 'New Site Wide Notice',: e( p+ L# F9 c+ B1 `
'view_item' => 'View Site Wide Notice',% ^4 e" A" s1 o$ D
'search_items' => 'Search Site Wide Notices',
; h. Q7 C3 S) F6 G9 h& T 'not_found' => 'No site-wide notices found',5 ~+ S3 v, G9 m3 X
'not_found_in_trash' => 'No site-wide notices found in trash', }: D+ |) l1 h. {# F! D' g( Y
);
" M' b% Q4 P- _; b" L
2 N4 x5 I6 b$ f* u# L# r9 N $args = array(
2 l2 ]9 m7 Y5 ] t3 r5 y9 m 'labels' => $labels,8 ^) R0 P- }& p9 G0 W
'public' => true,
% T8 X" I3 X* V 'has_archive' => true," E: }& P% b2 }+ w& q+ C; M# ?! N0 h
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),& M& p: S3 s+ X& z( P
'taxonomies' => array('category', 'post_tag'),
$ ?9 O1 c- p( G% j3 G: e0 R5 D9 J9 O 'menu_icon' => 'dashicons-megaphone',
/ ~5 _* C4 N# ?3 s 'menu_position' => 5,( M3 ^' v% D/ k+ r" [
'rewrite' => array('slug' => 'site-wide-notices')
# E1 e' i' e8 A );' v. k Z) C, L0 T( A, Q! q8 ^
; g+ J3 S1 \6 O J% g
register_post_type('site-wide-notices', $args);
( X D8 c4 ?. h3 v0 @ }4 B8 }! H: J o0 S: A) f5 b2 t+ y6 V
```' G7 f [/ u. i
0 g- m1 G v$ B0 m3 t0 L
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。* t5 M/ i- J" \' o" [
6 B7 O% t& F2 i: ~
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
2 B, A% f6 N. k, S( _# |% I o) X& ^. R, N+ G9 R, ^& X. M
```( y6 K# ^: q" d2 Q) v; y
add_action('add_meta_boxes', 'add_site_wide_notices_boxes'); }! ~2 ^4 n4 F& D8 J
function add_site_wide_notices_boxes() {
, y5 `4 \1 [ R add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');+ o2 Q# ?4 q1 o1 j
}
7 T$ q# F- o# `( O! i9 l' f. I7 b% t* M. Y; [( C
function notice_details_meta_box($post) {# n" Q7 t3 C1 ^5 d
wp_nonce_field(basename(__FILE__), 'notices_nonce');
( a: |6 t! e. s7 C% t/ }" C/ s; p $notice_title = get_post_meta($post->ID, 'notice_title', true);
/ a$ V& D2 J/ S" a/ U2 y $notice_content = get_post_meta($post->ID, 'notice_content', true);
( C# q: r( p8 W ?>& Z) Y7 b J7 M6 |! ~. R
<p>
) k; l' C, G2 s4 f <label for="notice-title">Notice Title</label><br>
4 t" k" }% L- S E <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">6 e* b, T0 I: e" p! C1 f# S1 l
</p># d% f( E4 n. W+ t6 K1 m
<p>$ e# n# X2 N* B
<label for="notice-content">Notice Content</label><br>( x5 S% d, S2 t- ^* d I: `/ s1 W
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
3 x+ S6 [6 p+ K% `6 I3 ~! X </p>
% [0 G1 N1 g0 N0 J <?php4 |; J) o1 O1 _% w* [# X1 P
}- \) {: v& e" f- B: e
( }6 `# y5 x# C: \* F5 R; I2 ]+ i
add_action('save_post', 'save_site_wide_notice_meta_box');
( r7 Q! L6 h0 T- d- h function save_site_wide_notice_meta_box($post_id) {( Q; z: F* C% w( W% L: Q" W8 v
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
. Z! p7 j$ p$ |( c" t return;
! A7 n5 ^3 T& h; J1 Q/ s if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE), P, d" }0 O: O9 d: U2 ?
return; a/ u) h, x5 O9 d( Z! ^3 e
/ u# u( S7 D6 r- J& s0 {
if (isset($_POST['notice_title'])) {, e; K' Q7 I5 g3 \# v
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));5 |' o! u. b! m% f0 `
}
A h; d! ]2 X" y! ~- U if (isset($_POST['notice_content'])) {, T9 y" ?" R! T6 A- Z0 |: F
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));$ @& S; q* U& |2 r, m
}- ?- q1 R. B3 z
}
* y/ T* O0 m, Q0 T ```
4 z% T# u; a2 {. m
2 ?2 B+ X! G8 g% O9 l 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。( A7 S) p$ G; p* y1 h- ?
% ^' \4 w( o7 c( A! K! I7 W1 J4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:' M% @. \9 k' V3 m, O c$ k( a+ l( e
# _- m6 |$ y$ J& B$ T ```
9 L" U/ Q0 V' V4 W: a! R4 M $args = array(% O0 v4 O3 a; G
'post_type' => 'site-wide-notices',
% `2 i/ P9 i, u0 k 'posts_per_page' => 3,- g( R, B8 c. R! D
'order' => 'DESC',/ U" _8 P; T; Z
'orderby' => 'date'- z: Y+ d# D7 ^/ S
);
?" }6 P* v+ T1 c8 p $query = new WP_Query($args);3 |2 G; H: `% a8 a8 M
if ($query->have_posts()) :7 ^7 x7 a5 r2 p& T) P3 `
while ($query->have_posts()) : $query->the_post(); ?>
( s) H+ m; h2 H: X9 R; C3 N <div class="notice">
. I3 j. S6 n2 F+ } s) b% n% V <h3><?php the_title(); ?></h3>
8 z) V. G, w! z0 I/ n: y+ ^ <div class="notice-content"><?php the_content(); ?></div>
3 ~; z5 f, O' v* I; y( i </div>) S# _7 c" \3 B# o/ O, Z5 c
<?php endwhile;
* z+ K: u* j# d; d, x9 ]( }& l wp_reset_postdata();
0 |! M1 j# ^9 Z' }2 j endif;: S3 C T" F, X7 x, {
```
* P" \; ~/ Q, ~- _ G8 t& l# y% N# U7 v7 |. W5 P
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|