|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?1 F1 j/ n4 K' p& [) K: I) L; X- g
! M# n% w% X+ z W( A+ A# E4 @2 E如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。 Z: x1 Z! q5 F8 r' R$ z( c u) f8 ~
$ W( S1 l+ p, p5 e以下是创建自定义插件的步骤:
4 k- V" K. w" c/ W, X5 | M$ @- l* R4 t- Z' M! B7 p
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
9 W% \! C8 x2 u3 d- y0 T8 A& u1 B6 F: g# ]8 Z' I$ S5 S
```+ |1 J0 V) [: r" T6 ~* v
<?php
( L c/ v2 M7 y6 Z# t /*2 _0 `4 o; K D) w4 b
Plugin Name: Site Wide Notices Plugin6 T1 d! M$ A9 f5 R% u
Description: Adds a new custom post type for site-wide notices.
! @. P& a) W+ u* A" X Version: 1.08 \& p0 S6 D9 G0 ^. E. Z$ u: T; s% q
Author: Your Name
7 g) J0 E3 H& R/ o5 B8 X/ S* J Author URI: http://example.com" T$ ]2 I% ^ T# H4 Z% e: Y+ W% o
*/
8 D$ _4 X! a( n1 b$ j$ p/ i4 c/ @/ t) B" s
// Add plugin code here...' F' [! Z1 Z. I" T5 ~
```
5 T7 a- C) v; T/ b: Q, v& e
5 v5 Q$ C2 l0 l) Y2 z$ h 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。# G# o, }* u/ {* g0 b
' A: a0 d2 k& a2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:5 Z9 V, j( a8 [* }( g) h1 ]8 f
4 h6 v4 ?: K1 u) F ```, j) y: y! s* H5 z2 _: e
add_action('init', 'create_custom_post_type');
3 U7 s/ j1 Q( I \: Z function create_custom_post_type() {1 F6 h) ?* `" _/ q; k
$labels = array(. A6 n+ ~; z, J' a* G5 q
'name' => 'Site Wide Notices',% k4 q" ]8 R* C% L
'singular_name' => 'Site Wide Notice',) N/ m: c0 G7 c7 S
'add_new' => 'Add New',
, T# z- W" C k! k' g: a 'add_new_item' => 'Add New Site Wide Notice',
& A! a3 V2 c- a7 \ 'edit_item' => 'Edit Site Wide Notice',
4 s1 L) L. y) |2 A: F 'new_item' => 'New Site Wide Notice',
# b8 ~' J! ~; z. B6 I& L 'view_item' => 'View Site Wide Notice',% N0 Y" K! H9 F& ~# I
'search_items' => 'Search Site Wide Notices',
/ S7 x- }- D1 L; N. ^. E 'not_found' => 'No site-wide notices found',
; C9 T0 o5 }, V3 }1 a7 ]2 K2 e' m4 Q 'not_found_in_trash' => 'No site-wide notices found in trash'' Y Q* I- c% |9 f, {7 N$ D" k! _
);0 s3 f2 n6 G$ o1 p( d
9 }% o1 e8 T7 H2 {: N# M
$args = array(: O; Q7 _0 Z8 x# t
'labels' => $labels,$ y6 j7 Z/ m i) \ Y6 e
'public' => true,- X f9 C2 z! V5 `
'has_archive' => true,
* B( p) D. A T# n5 d8 t 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
8 G9 L P3 i$ ]8 O0 d5 F 'taxonomies' => array('category', 'post_tag'),
7 }5 S7 y& ?0 K1 M# a! G7 ` 'menu_icon' => 'dashicons-megaphone',7 J; J( H. G% r( ~
'menu_position' => 5,# k7 ~. h3 ?' i
'rewrite' => array('slug' => 'site-wide-notices'), k8 {! G. _4 ~, @4 _6 l
);
4 \8 H5 `- b( c. s% K5 S4 E3 D, q0 I/ `1 ^/ K( n9 B: Z7 z
register_post_type('site-wide-notices', $args);
1 w8 z+ u6 q# r+ R! N }; o/ O3 I5 ]- j: b$ u
```8 Q1 U. p: ~" I$ T/ U: @9 s
3 C% M0 y) E! w9 G
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
* y1 t( x0 T* r% Q) K4 y8 h, l9 I0 C- R4 H: K
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:: J5 H5 [2 b& t7 W4 a
4 i% d/ J# w* O: ?$ I7 l
```
0 y' q- W' h, w; l add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
3 G9 R7 P& w3 b6 z8 J( s5 a6 Q function add_site_wide_notices_boxes() {
& j; ]1 |' U# R# ^9 s add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');' }& `" D- A' k6 h6 p6 J
}
: j; t/ j4 ~$ P# J* y4 d
9 x! l e) g$ r; |% l: k function notice_details_meta_box($post) {$ L8 s- A; O$ n7 R" C& @- l" Q6 w
wp_nonce_field(basename(__FILE__), 'notices_nonce');
9 G& Q1 a, P+ W8 I' Z9 w9 { $notice_title = get_post_meta($post->ID, 'notice_title', true);1 n/ l" ? r# @: ]) P. y
$notice_content = get_post_meta($post->ID, 'notice_content', true);
. Q$ h! }8 T G7 s& S& L+ c ?>3 D1 r: X& x$ R
<p>/ g1 _* F9 F0 K, [6 y' ]/ r& A2 T
<label for="notice-title">Notice Title</label><br> K3 r0 Z9 Y& ?$ [. `# X
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">2 z" k1 Z+ q! _5 ^3 k
</p>6 F9 A. j* p- {1 n9 K9 o5 I/ P! ~
<p>. b/ H2 O1 K& s" x* V |* D
<label for="notice-content">Notice Content</label><br>
7 N) p. Q4 X$ [: L/ `$ m <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>6 p$ }( E( Z& g
</p>* R- \7 G6 n# U8 l$ Z
<?php9 F C! h* S) k/ P# b' U# e
}* D* D: }3 i2 K7 r/ x+ H
- p6 n- c+ H8 C" T. a6 J& M2 s
add_action('save_post', 'save_site_wide_notice_meta_box');
5 ?0 s9 P. y+ a7 a" K! I function save_site_wide_notice_meta_box($post_id) {
]2 I% g e+ g9 R; O! l2 ]8 E if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
6 e5 \; z0 S+ I: N. ^ return;
. ]3 K0 b( c' N* g0 q: P( D if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)2 K$ N: f- V, k: _1 H; \' W% [
return;
' O# ~; Q% T2 ]! S' J7 m @% Q: G0 `$ `
if (isset($_POST['notice_title'])) {
2 q2 A0 N: V" j% E update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));+ N# z2 w0 B' U
}2 x5 ~7 L/ j7 q1 [6 p' }: [
if (isset($_POST['notice_content'])) {+ i- s) w8 G( m6 \5 p
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));9 M. M8 d/ N: C& @$ c! @
}. n# |2 A- H2 a2 ?% l5 s4 W/ n
}7 @3 ]% E' P7 ~9 S
```
# s( t h2 A. `# C0 U: R
; d# }( V8 ~% m6 {& v0 | 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
( o+ v% U8 P6 G2 p6 q% K1 P! F2 ~& x- C' D
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
, k0 f' n) K6 |6 k& g/ y% K# _
9 B* O& j- o% |8 `7 z/ v8 I8 L8 L ```% W2 I* L) c. i9 F* N" s
$args = array(
, Y$ J" e: N- z0 I% s1 A E: T% x 'post_type' => 'site-wide-notices',: P% ]+ x2 I% D) g
'posts_per_page' => 3,
3 F( n2 v- ]! n4 Y 'order' => 'DESC',
; e' Y' W; H0 c 'orderby' => 'date'6 _" m! ?+ F6 I* D+ V
);
Q% O" i, {5 Z2 y& j $query = new WP_Query($args);) p1 U; O3 t* S" D5 k9 Z
if ($query->have_posts()) :! q- c0 K* u( _: q
while ($query->have_posts()) : $query->the_post(); ?>7 M6 J7 e2 @5 ?7 k& L: R. d9 y5 V
<div class="notice">
) q, M+ S4 E& ^0 @ <h3><?php the_title(); ?></h3>
0 ?7 l0 O- y$ }9 K# X0 _/ [ <div class="notice-content"><?php the_content(); ?></div>4 L! A/ j% z8 f& `+ v; L3 b# J0 @
</div>7 B4 U9 f7 f2 G2 |
<?php endwhile;* D9 H/ n$ s4 R' v. R
wp_reset_postdata();
) c2 H1 T0 H4 s8 I endif;8 l5 B6 I* P' `$ G9 J$ `; d
```9 ]: Z, Q" s. Y3 g
" O$ u' ^; e I/ R! U
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|