|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
6 I6 Z M% C! D9 [% z$ M, a8 X n. y2 C+ f$ {7 Z; c- A& P4 A
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。# T+ G& L; {+ V( P7 E
2 H9 w/ p' O1 e, W/ l( N
以下是创建自定义插件的步骤:
0 u* P* P7 ~! H1 Z' U" J9 M
# c3 p# d) c% I1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:, Y. E9 M/ D9 ]. b& v
5 S1 d0 W5 W0 c2 t; O; u: n; Z ```& l5 h5 q' h8 P% a& Q
<?php% h- h# X; n/ Q% \2 g
/*9 l7 C6 P3 n+ u# l' C! O
Plugin Name: Site Wide Notices Plugin
+ a$ B6 T$ u( b9 u4 p" O2 [: J Description: Adds a new custom post type for site-wide notices.
, y2 U; h% }( S, o Version: 1.09 p- v+ [" A: Q: \4 P3 y
Author: Your Name
6 i: k7 n" B% j5 l+ y/ \- p8 A Author URI: http://example.com0 O* Y4 }6 E; v: F. w
*/
3 I* |# f2 I4 `1 ^: H
- E6 n Y9 |" y% e* b6 I# J; w // Add plugin code here...4 x; N+ x" f8 l- Y8 N
```2 M% M' C% V( p. K& u
/ c3 M$ b4 q3 n- F
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。: V; k, B$ l x* M- j' R' r. R
; v _2 a( M' ]$ r- I" w2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:+ b5 N$ e9 Y! w! K, o
" E- J0 k) _5 j. I4 [ ```
- g+ e3 O# v& Q* u" j! Y add_action('init', 'create_custom_post_type');
$ L3 _2 v2 f: _0 k5 m, `3 r5 ? function create_custom_post_type() {
6 \* _: `3 g* N) H) @ $labels = array(
8 q- Q% T( H5 i$ K 'name' => 'Site Wide Notices',
0 J7 n: O' X' T4 j! B, [ 'singular_name' => 'Site Wide Notice',
: G$ U# m/ w& x" U 'add_new' => 'Add New',/ ^5 A' @ L$ O% n( |2 l
'add_new_item' => 'Add New Site Wide Notice',1 Y/ [$ T/ R( ^0 E& z
'edit_item' => 'Edit Site Wide Notice',
- l" l9 {( O/ i0 ?2 S& G 'new_item' => 'New Site Wide Notice',; q, ?2 z& C% |) z8 l( m. i5 \& x0 M
'view_item' => 'View Site Wide Notice',# U9 p/ E5 k, ]3 G1 B/ a
'search_items' => 'Search Site Wide Notices',
# e! o. z; g$ |3 p0 `" K: ` 'not_found' => 'No site-wide notices found',8 F$ d4 Z; T" r* J) E
'not_found_in_trash' => 'No site-wide notices found in trash'8 q. M q7 H" w" ?4 m1 T
);8 [5 A! [& ~% U0 f3 o( V. h4 a% a
6 Y% S1 b5 m6 B9 ] $args = array(/ F- R1 x' Q4 j
'labels' => $labels,5 A2 Y( j; N5 G' m5 f' |8 P
'public' => true,+ u) N7 g1 @* a' b
'has_archive' => true,
7 \& X( X, j9 r2 r$ O% R 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),: T9 W$ O7 r6 H* M, b, q6 r
'taxonomies' => array('category', 'post_tag'),
* M- h% G% \' ?* |7 R3 Q 'menu_icon' => 'dashicons-megaphone',
" s M- Q, |3 ^ 'menu_position' => 5,: x- U) X+ S8 U: z
'rewrite' => array('slug' => 'site-wide-notices'). f! Q, L+ [; r5 g. f
);3 h. h+ q c/ k1 \- A+ g. y$ \
+ e, Y! y/ _1 \% w9 w9 T
register_post_type('site-wide-notices', $args);
$ X4 @# R B7 z" h }
) H" R& H: i3 c' N- W ```* O( Z2 D/ l( m" K3 Q2 H2 Z. M
: l# V% f& Q5 E4 F6 E0 U# y% p3 _ 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
( {6 N: j& U* ^( Q; D! y
$ S) I) ]. D) b$ X% {( ~9 |3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:1 Y; p) l+ c! l/ S
! {4 G) w5 u2 T% z7 J ```
6 N: R4 W! i, ? _- F* {/ m add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
+ G& O: Q6 M: T/ L. L8 H function add_site_wide_notices_boxes() {
! ^# U! s. L! N( O add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');: f% R, h3 |# W3 @
}, ?- [) ?, l( o S( G; @
9 J' }/ `5 R4 E3 j5 i% U
function notice_details_meta_box($post) { ^+ O* i# U9 ~$ ^
wp_nonce_field(basename(__FILE__), 'notices_nonce');
6 H" J& z1 h, X7 _ $notice_title = get_post_meta($post->ID, 'notice_title', true);
& G k4 o2 G1 B1 }4 L+ u+ T $notice_content = get_post_meta($post->ID, 'notice_content', true);( E/ k4 P7 m' T% b: @: }
?>1 D& c2 |) f; |& _
<p>* s% r7 r# X( `7 X( K9 x1 p6 }
<label for="notice-title">Notice Title</label><br>! H t7 q# c) n# n m2 A9 ?5 r+ i
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
% Z4 B& a7 C% f3 l/ x </p>
2 F9 T. y, W2 K* U <p># u4 k A! K( C0 L
<label for="notice-content">Notice Content</label><br>2 v+ f0 W( w% F: B8 E" u; }5 u
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>' b- ~8 Y( f. A3 d2 Z1 r
</p>
; j5 ^& }, z+ L/ l; j <?php
8 o$ n/ C! |3 T" P$ N' n2 j) I# j } \5 w" ^" c, V
6 W0 m% h I( E
add_action('save_post', 'save_site_wide_notice_meta_box');
2 d) l/ L3 H s% | function save_site_wide_notice_meta_box($post_id) {9 R8 p. b i$ ~+ m6 ?5 }
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
2 I8 `* {3 E' O/ z( D( w; a return;
+ b- t \/ Y/ ^8 n if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)1 B; N+ M: y1 G) v- F) ~
return;
8 R G. n& W6 ?2 q
1 J3 Y3 y& J" ~ if (isset($_POST['notice_title'])) {
* p9 S+ d! c" R: R" a update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
2 v2 b7 \( M! ]" D }9 ]2 X6 u6 h" Q9 y
if (isset($_POST['notice_content'])) {/ e0 B- K/ }5 b7 X8 u3 {
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));, M& S7 J) s3 s! J) M
}& V) K# i! D. l6 x. f/ {
}% V& k" b; q- w" t' r: D% C% O4 b
```
( t( X6 }$ y3 k, O1 K4 f1 d& `8 J4 A- S
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
, |+ |+ K- ]! T* k1 A
' q1 q4 D* H- w4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:+ P- b" k$ i$ M1 g7 C* Z# {( q
1 C- z! J% t2 B* T% i
```9 U. m" `4 Q. h" S
$args = array(
, K0 ~/ p6 H8 \8 t, L' J 'post_type' => 'site-wide-notices',
4 E0 Q1 h% _9 @) {! H6 s# o( t( K 'posts_per_page' => 3,0 [/ A: D) N( Y1 s8 f }; Y6 r, h; g+ s
'order' => 'DESC',# i" B8 ^% e' ?/ s a9 `- v
'orderby' => 'date'/ r$ ?0 a9 U% h* x. Y) w
);/ j6 s% _( x$ e3 ^5 v
$query = new WP_Query($args);5 N. y8 R$ |/ u
if ($query->have_posts()) :: v7 g) d k) [, j8 |5 p# ~
while ($query->have_posts()) : $query->the_post(); ?>, z; A( ]. h1 ~$ m, w% U2 L Q5 d
<div class="notice"># \8 ~1 n9 |8 w/ ~8 V K
<h3><?php the_title(); ?></h3>/ G3 q! e) f8 N
<div class="notice-content"><?php the_content(); ?></div>
5 T% K/ `" }9 U </div>
6 S+ h- q+ |, c6 T# m( q <?php endwhile;5 U1 C" y J: U' k/ P4 Y' H
wp_reset_postdata();
2 h3 `- e: B! ?% f c2 W. ^ endif;+ v5 `+ j$ D1 J& k9 \6 a
```' y0 n; U6 K1 ~( Q( k
( s% |! @4 \+ e1 f* s 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|