|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗? s) v2 A& l! [2 i9 S8 w
* y3 u$ k, e- S _' q
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
/ F% ^* X" S& k* Y0 E
& j% S" M% |/ `以下是创建自定义插件的步骤:$ k" [6 a, a p1 W$ a: D/ R
, V) ^- N# j4 A' I* o
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
! ?; S" D7 ]# J7 R/ L: D$ T1 [6 `; m8 l: L0 u n1 f
```
( U3 J) r7 q) y+ E7 x1 o8 T <?php! G! T3 z- G% d8 Y) X8 R
/*0 m, C3 _7 F; q' R2 B+ w: {8 k
Plugin Name: Site Wide Notices Plugin5 M- u# D' C- i
Description: Adds a new custom post type for site-wide notices.0 o/ P5 v! ]$ k3 ~
Version: 1.0
8 n6 l1 V2 |0 n( W' r Author: Your Name
) O! @3 f& e7 a4 n, F5 m Author URI: http://example.com0 O z7 u7 ^$ J) N/ F+ y
*/; s* t0 H! M" z# F/ [& o
' `7 x0 W3 y8 z2 }7 T2 [
// Add plugin code here...( c4 @3 O# _, k+ d' t. `
```- O$ m$ f9 i; Q: M+ c; C/ Q- v- I! t
$ x3 g8 Z, W" Y 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
9 t5 i1 x- v$ H R" A, I* Z! T6 ?4 C1 [4 V# j3 s* }! E- R
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
/ \& u \* P; ^) J1 c9 D: Z3 h+ P6 s* `' r2 B$ ?+ V* w/ Z, Z. q1 K
```3 c4 O/ B/ W0 c3 L h/ o
add_action('init', 'create_custom_post_type');
, \- s) Z( m/ P( d* k/ c function create_custom_post_type() {
% `! b3 i+ y0 s t0 o1 g* m. X4 [; J $labels = array(
* n" K- y' j" @3 A6 D 'name' => 'Site Wide Notices',
- `! P: [' f* F/ e/ J; e6 @( \ 'singular_name' => 'Site Wide Notice',3 `' {: F! b) ~$ X) d( W# t
'add_new' => 'Add New', a0 ~3 W( Z* ^( `- t w
'add_new_item' => 'Add New Site Wide Notice',
+ v; L1 z7 A1 Y/ U 'edit_item' => 'Edit Site Wide Notice', i$ l$ ?- \+ r
'new_item' => 'New Site Wide Notice',# s b( o6 X3 S" f* B
'view_item' => 'View Site Wide Notice',
1 C: @+ A* i9 l& I( ^% u3 O: | 'search_items' => 'Search Site Wide Notices',/ j: _# e6 ?9 N/ g7 e
'not_found' => 'No site-wide notices found',
/ N% ?; l, X* ], I8 U9 p3 [ 'not_found_in_trash' => 'No site-wide notices found in trash'$ t2 v9 {, X- X* }9 \
);
) h9 {' }- i7 }6 g* L5 Y% B6 k
$args = array(9 v6 B8 S, d: o; Y7 Q. j
'labels' => $labels,
! B: _! l/ N) g* p/ s 'public' => true, ]$ f0 B2 \/ b. o8 H2 \2 @! B
'has_archive' => true,& {; J! z. E2 y8 v# h, Z
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
. o. u. n6 i0 z- y0 X. f' ?3 f 'taxonomies' => array('category', 'post_tag'),4 ]) B: b/ `7 c) @$ Y
'menu_icon' => 'dashicons-megaphone',+ T1 o8 r) s2 S& C4 a
'menu_position' => 5,
7 c- p/ e5 G8 X3 ?# }1 b5 P# J 'rewrite' => array('slug' => 'site-wide-notices')& j1 w0 |9 _2 Q, J% e$ I: @ {5 c7 z0 w
);
( Z& `7 [! H+ C H3 H6 m* s: E' d3 \9 A! _# g, E# h- |8 J
register_post_type('site-wide-notices', $args);
x8 W7 F( r: `) g. v# ^ }+ F8 J0 h1 t7 X$ I9 ^
```+ @/ m, q. S) q# \$ ~
( |" y: u8 p+ H, q 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
) u4 f2 Q0 w J9 Y2 D" ]3 C" _+ F7 W# q8 v& r
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
2 a+ K$ Y* W( ^: d7 k9 T8 G7 N
( G+ x- ^2 A( }* y2 h7 k8 m ```! r# j: ?/ R& F$ e5 F! l; Z* ~
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');% L; Z7 r" N6 H
function add_site_wide_notices_boxes() {
8 J1 z- Q% y- w add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
' }7 y5 t1 o4 M2 H+ W! o$ ` }! F' l6 @1 q3 g! e6 ^8 i6 J
- z& B5 |2 c4 A1 u9 Q: n function notice_details_meta_box($post) {
. s' p" j$ v# N0 b. B wp_nonce_field(basename(__FILE__), 'notices_nonce');
. G$ u6 Q$ i, s1 _ $notice_title = get_post_meta($post->ID, 'notice_title', true);, X2 D: ^+ d: q: j2 I$ |
$notice_content = get_post_meta($post->ID, 'notice_content', true);
0 L8 C+ j8 [, ~% S2 f* H ?>
0 `" w- ?2 x2 i) J! Z <p>
* J J% J& J4 `; Q; [" W <label for="notice-title">Notice Title</label><br>3 O& x& O4 i# K
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
2 B' Y" v) w% { </p>- F! x# \2 D0 h( J/ V
<p>, _# y) ]1 G6 t( G& [9 L3 S, U8 j
<label for="notice-content">Notice Content</label><br>4 |0 _- j' u4 H, C
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
$ h& h) v9 a* b9 \8 M" \1 B5 N </p>( o7 t6 L' ?+ U/ s
<?php9 \+ a7 _' @' r; |
}1 \2 }3 d! ?8 L& u' w
3 P: m$ N3 m% w/ j" B& Y; n add_action('save_post', 'save_site_wide_notice_meta_box');
# Q; a6 c8 |* A) B' q; F; A( ? function save_site_wide_notice_meta_box($post_id) {
" R" y# i) z3 J if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))3 N' I9 l% r/ D1 c0 ~
return;+ e. S6 y" Z4 W6 H
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
% R: Y( R1 K9 l return;$ n& H; c- p) c* v
) l. m8 M/ o7 V2 ]: k, U
if (isset($_POST['notice_title'])) {
: |% y( o4 w: B) o8 v. n update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
2 p4 M$ b x$ j) L- T: C- r- x }
/ k9 \1 G2 C: P# @$ r' i3 h& {( N7 g if (isset($_POST['notice_content'])) {# }8 d( P7 Z; s2 T2 D
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
/ Y& g- z8 s7 p$ `% J }
1 ~3 I" J6 L7 H" } }- {/ Z i; u& B. r1 i$ t7 V0 V6 e; b2 a
```
: b6 t4 o% P4 ~3 I. ~' h" A+ W- g8 P8 M) Z
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
5 [+ ^! x. H' q( n' r6 G+ Y
4 ~; R' U0 l2 L, b4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
' g+ B. |8 {' G9 d0 g5 w8 N7 f# ~. _9 u# p5 _1 [
```
1 |5 ^, l6 H2 x, [& V* Q9 D $args = array(
& D' B. Z N) C& ~) o- L( y 'post_type' => 'site-wide-notices',
5 s3 ?* O6 ], W1 d; a! [ 'posts_per_page' => 3,
2 `/ I& c( M- r5 Y0 J0 X7 K 'order' => 'DESC',
, l$ G: `$ R; ^7 b1 E0 {+ I" ?& N 'orderby' => 'date'' }* k3 b& W7 Q0 z1 Y R! C
);2 [& I- C" {& R0 z
$query = new WP_Query($args);1 f% U; k, B0 H. F7 a3 G4 y
if ($query->have_posts()) :
7 }" p) @' }" M* ^9 K2 L! Z while ($query->have_posts()) : $query->the_post(); ?>' _# P1 W% P- r, N; k
<div class="notice">! ^5 v3 R6 G* ?
<h3><?php the_title(); ?></h3>& B$ w* R7 `' ~8 u
<div class="notice-content"><?php the_content(); ?></div>
( V2 J$ j1 \% ^3 U. E </div>
" U$ X1 H/ y( W0 j <?php endwhile;
8 R$ _1 ]8 T o wp_reset_postdata();
# {9 E7 l( x8 |6 i( ^- M endif; Q. d8 |1 S9 p5 n2 u* `) I0 M
```
9 z+ \, K/ M9 h* D; l) F! L( X' ^7 p0 N
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|