|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
1 b5 J4 V3 E; A# t0 B/ e' \ G& i% N$ y
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
& K5 |/ @3 k( ` S4 p) N" A3 O
" N4 A! u. T7 G: T" D1 b9 O9 c以下是创建自定义插件的步骤:& a4 v. T$ \5 \1 x5 f7 T: p
& n' M$ F; C* N4 E3 D$ c1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:" q# o4 [+ `8 @; D1 c+ `! T1 A
9 [4 h: x: A u1 k# x3 C# G
```; @6 Q8 C8 ^8 K6 s
<?php
: @1 _. L6 H1 J) Y) m- H /*4 o! y7 O+ c2 L0 c; ?
Plugin Name: Site Wide Notices Plugin A8 {, U, b& G2 x4 U
Description: Adds a new custom post type for site-wide notices.
- w6 Y% t& Q, O6 D0 h8 j: [9 n Version: 1.0, M6 U& ]6 z+ n5 k- {( u
Author: Your Name
+ M0 c+ |) f |6 B Author URI: http://example.com
2 C: S; h$ T% T! q; b */2 H- ^3 }7 x8 T" q' Q8 g
$ P% q& P% F) ]
// Add plugin code here...
) S: O$ c+ K" V4 E; J F5 | ```2 w2 U0 }3 b) s* W u$ K! l
# ~; G2 u* X5 [2 [' u5 r 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
* w( u. \7 _! U
% a J" C1 s B$ m3 f2 l/ l. J2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:0 |7 J2 s9 L4 C$ v, x
- v% q# e$ f0 M
```
" X* ?3 x$ X7 f/ L/ S3 v add_action('init', 'create_custom_post_type');
: l# H* B; I# \/ i function create_custom_post_type() {
t: P- K. {0 C" h $labels = array(9 j' c3 ?8 m4 e- W& V$ y- J
'name' => 'Site Wide Notices',
5 c0 E) p5 C; k3 i7 d 'singular_name' => 'Site Wide Notice',
+ t' g* n/ n& ?5 U1 F& e5 `, x 'add_new' => 'Add New',; P: w3 p& b2 C+ K4 m4 t+ C
'add_new_item' => 'Add New Site Wide Notice',( J, E" y' p9 \- w! J
'edit_item' => 'Edit Site Wide Notice',7 U# N2 p- b( W! P, B! B! f
'new_item' => 'New Site Wide Notice',
( E& y8 r! f, I: L 'view_item' => 'View Site Wide Notice',
o2 b- ~ [% s) w' A" a 'search_items' => 'Search Site Wide Notices',) C* Z4 G1 B S& m" k' l! r# m6 h/ J
'not_found' => 'No site-wide notices found',; a. C3 d& R# x2 K
'not_found_in_trash' => 'No site-wide notices found in trash'4 U+ `3 i* U. r1 H* F1 B5 Y1 ^# ?6 ~$ C
);
# p; C# h1 d8 w: w* W# S+ o4 ~
. ^; b# N7 ?1 }3 g8 [ $args = array(2 K8 A, _/ |% c- J
'labels' => $labels,
( K/ {1 S7 V7 L( q 'public' => true,% M; X: N" W( t9 X' S8 J( `
'has_archive' => true,$ B; K* I) C# r
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),. }" B1 p2 C' ?, z5 f
'taxonomies' => array('category', 'post_tag'),
/ ^ z: k- d& c% y+ T6 e 'menu_icon' => 'dashicons-megaphone',; p: n8 i- } a1 P8 C9 M% c
'menu_position' => 5,! [1 l: `5 I+ C) s A7 E. F
'rewrite' => array('slug' => 'site-wide-notices')
4 Z& r/ Z2 g* Y; y- z* M );
8 [) [' R0 ], E" o e+ U6 \' j- m) `% `3 c- T
register_post_type('site-wide-notices', $args);
. d1 I" J5 B' V9 s+ c N, d }
; D Z1 C2 k- v6 G/ R! S ```
# x0 d: e/ x# Z! |, d
1 o6 ?% k2 ]& k8 x3 r* w- \ 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
# q/ p& m/ B, q& d' H* R
! T( _, ]0 C0 [( H! U: g3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:/ B) ~, }8 R# z
, @# O, E' J% o ```
$ h# ^6 o' e, _3 a7 t add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
$ n* p! g& ^4 r$ V9 w function add_site_wide_notices_boxes() {7 q2 c) d$ |) k. C
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
8 e; q' u, X! ~# q. _% x }" {- u; W* a; f1 \6 o8 `
9 z/ [; m+ `% M2 a function notice_details_meta_box($post) {
& G8 A$ b7 P# x. n! n2 D/ n wp_nonce_field(basename(__FILE__), 'notices_nonce');( W1 ?+ k) X, B1 O' D6 W! J- R, [0 [
$notice_title = get_post_meta($post->ID, 'notice_title', true);
: k7 ~( d* a! v$ ` $notice_content = get_post_meta($post->ID, 'notice_content', true);5 }, z8 `$ a4 y1 T& [
?>6 {" K. T' }) u/ J$ z
<p>6 I7 {2 U; f {: U" \
<label for="notice-title">Notice Title</label><br>0 O2 c6 P9 u4 G7 ]* C) j
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
% B7 u7 |$ H) _8 [" i/ g1 `7 E </p>
0 P, N# ]8 }, i, |2 C <p>0 Y; R+ D* E: p5 J2 ~
<label for="notice-content">Notice Content</label><br>/ p% u9 L' B( x" E' o; a
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
; B( @9 B [% F! _: p& o </p>
( E' ^" t9 A" N0 p0 [/ g <?php% Z' L `# w) ]; I: }+ T* w
}$ T* r. X5 c0 L$ | ]9 s
6 s' y$ r% v$ [2 n* U2 O+ B- m
add_action('save_post', 'save_site_wide_notice_meta_box'); E3 `: W6 `$ e6 G
function save_site_wide_notice_meta_box($post_id) {: E4 ]& R0 S( @: \' [/ z) B
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
+ D+ s4 Y! K; L U( P. ^+ E return;- Q/ J" [" v) h' b2 h. G: M. ^
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)* i* t# ~4 O' G5 r" B. J
return;3 ?3 [* U* Y+ W A
d) }' h7 M, i' H- y
if (isset($_POST['notice_title'])) {
@# i3 X+ }0 v8 i; } update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));2 G7 m* ]8 M* i9 K
}0 @0 x1 R3 ?( r
if (isset($_POST['notice_content'])) {% U; I: T4 M6 N$ t; H1 k
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));2 `+ j6 N- n1 p4 j8 S
}
2 x( ^! c" {% x2 n% m }1 x3 _3 w: V' p
```8 G1 ?- `" c1 M$ ]3 T$ Q
8 f3 f- Z- o1 o" Z' N; r9 | h 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。) _* s, \: B, @. k1 I$ P! s
4 \/ T8 g$ Y8 \" i2 c5 _2 P! f1 i
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:- x9 _$ J* q8 d! i
( O7 |: S% k. X3 c ```
( G6 Y* n3 s. J, E5 U3 Z& s6 R $args = array(
, r: C) v. v5 t, Y# b5 X6 v( a 'post_type' => 'site-wide-notices',
& H- h3 m4 w" P( E1 x 'posts_per_page' => 3,7 m& B2 M: d; O# i8 k$ ^ F) w3 [9 h6 V
'order' => 'DESC',$ S) f4 f/ m( M/ H
'orderby' => 'date'
8 s3 ^* I% N. U! i1 Q ); g6 c: @- M' Y! w5 s
$query = new WP_Query($args);
, H& X; Y6 Z) G& y if ($query->have_posts()) :$ W; H, }3 p7 u3 ~* R! X( o6 ]
while ($query->have_posts()) : $query->the_post(); ?>
- E2 G6 y0 m6 C1 F D* z& b% L3 s3 o$ @( w <div class="notice">
6 I2 m0 o$ Y: Y5 Y* ]4 t. a <h3><?php the_title(); ?></h3>+ E2 |# G5 @) \& H, o i6 R( W
<div class="notice-content"><?php the_content(); ?></div>2 e; |8 Y6 r4 Q& \, { S; ]4 X- C
</div>6 G/ {, \" |! l+ ^# O' G8 D! |
<?php endwhile;% n! R4 L( V* f5 H0 x2 a) D; k
wp_reset_postdata();
9 L8 c# K% B) c% T( M' e) G9 }: ] endif;. r3 x8 L5 ^+ [) T. u
```
1 ?( Q3 p( J6 Z" n/ l$ b6 J9 a" p6 K7 n
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|