|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
, Z' v9 T/ k: k3 s# J! F# k. M- x& P/ C$ V
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。 W" Z* O9 X* {' g9 }7 J
4 X' L! t0 V/ Q1 c4 g1 [
以下是创建自定义插件的步骤:" U8 I! V8 i$ g' w- y& g$ ~
5 Q4 D$ y) _0 p" T
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
- E* [6 q3 x# P4 ]. h* t7 u6 [; y" r# K. c
```2 }0 [4 P. B) U7 U9 J8 R; L& j/ b- i
<?php
+ X, S1 l* m6 P' _9 u. i6 ` /*1 I0 h- T! _! T" O- {- ?' v& |7 N
Plugin Name: Site Wide Notices Plugin% `1 m+ J9 ]# g; W; R I, u
Description: Adds a new custom post type for site-wide notices.
' O4 ^ ]3 n/ i9 z Version: 1.0: ? ~3 w1 Y- r7 M: |
Author: Your Name
/ g8 v& \$ L" O/ |2 ` Author URI: http://example.com
" ]1 z2 F* e* d/ _ */% y# U) I) u/ l% Q# a# F+ U
% w/ |7 y& f( d8 Z- h2 F$ O
// Add plugin code here...5 h1 x* J2 L" ?1 Q# L$ P
```6 {! j9 z( c" z; n
& q) b& G v5 d( R: ^: C+ X- L 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
. j8 Q, a6 ~! E# ^. g
K0 \$ }( v' I8 p8 H" c( B+ E2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:" q% ?3 y0 h3 C, n; }6 x
6 d! c- N7 F3 i% G. D: ^: s
```" P& I9 I; D' Q3 G. M9 S& ~
add_action('init', 'create_custom_post_type');
# X6 T. b2 \) q# j* f8 v, x function create_custom_post_type() {0 D- h" s, a# @* b" ~$ q
$labels = array(8 L# M( @/ ~; `& t" @4 D2 I9 A
'name' => 'Site Wide Notices',* N3 _2 @9 l- N. ~* Y# r% P0 K. V
'singular_name' => 'Site Wide Notice',
! a+ W, `: W- S# G! ^ G) L 'add_new' => 'Add New',
- \5 l2 C3 w z; `3 l& P 'add_new_item' => 'Add New Site Wide Notice',3 R% k, f1 J" |; X* D
'edit_item' => 'Edit Site Wide Notice',
$ f! ~" C6 |5 y2 i 'new_item' => 'New Site Wide Notice',( B- m) T! {! J2 z$ Q
'view_item' => 'View Site Wide Notice',
. `2 O5 y4 L. c& W 'search_items' => 'Search Site Wide Notices',) f+ V5 o: V' m3 J" A- p. O6 W
'not_found' => 'No site-wide notices found',
4 D1 m) J. T$ m! `, z+ p 'not_found_in_trash' => 'No site-wide notices found in trash'. x3 s+ T- @( N( ]. ~0 P6 j
);' c! ~4 \. {1 k8 d0 z
% ?! C8 w8 g, w6 P4 x
$args = array(, H& w' ^1 K4 X* o0 }4 q0 G* e
'labels' => $labels,6 {" w2 h% E, i
'public' => true,) g# p( r6 N- [& B$ W
'has_archive' => true,0 [, G { Z- y
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
0 N5 Y, r3 n) V8 f 'taxonomies' => array('category', 'post_tag'),# Y& T _- E. I( f
'menu_icon' => 'dashicons-megaphone',& y, K! Z# P6 [* b* i
'menu_position' => 5,4 d! }8 ? r. `1 ?& V) k% P
'rewrite' => array('slug' => 'site-wide-notices')
: l1 [0 d2 \, g* W );
$ @1 c, e" X. v, a/ G v8 `$ M0 h' H# ?
9 c) A; c1 Z; q. | register_post_type('site-wide-notices', $args);
' z; J* O" t( R }
1 k3 F% i" h2 X7 R ``` ~7 H: e2 g% E7 O7 ^& W' a- }
0 H b6 u% }0 D4 O
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
8 w0 D5 C( f; Y( n9 E
: ~3 a; N! f N1 F8 H) o3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:* G5 _$ ^7 g' s6 c `% Q
5 O; m, Q# s2 ^* m/ g$ M9 K) V ```
. r' y f. }7 M6 b/ X' C4 O0 J$ |+ R add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
6 g0 y9 y1 Z1 j8 Z7 T- H/ e! s( S function add_site_wide_notices_boxes() {! C' ~4 r+ c I' @
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');$ h \+ ^ e2 `( x( |: F, `
}
. y! p$ i6 F; f2 T) D2 ~) i
( ~+ S. p' M' C$ w% A function notice_details_meta_box($post) {3 `$ I5 B% {" N1 b3 r
wp_nonce_field(basename(__FILE__), 'notices_nonce');2 g9 P: d1 N) l/ c
$notice_title = get_post_meta($post->ID, 'notice_title', true);9 T# y% L+ y) D2 n9 n1 l4 i
$notice_content = get_post_meta($post->ID, 'notice_content', true);
. }( Y. L8 k: y- q5 s" A' q ?>' n* ~" b' h6 @2 z! N
<p>
/ y6 y+ P, }* I# U2 x! S: l. e <label for="notice-title">Notice Title</label><br>
/ |" a s2 Y: u <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
1 p, Y, _7 o( r; U( O+ b# h4 F4 s </p>; ~" n1 D' _; g( f6 P! s/ B
<p>: { x) ^4 f6 q: K
<label for="notice-content">Notice Content</label><br>
* M% Y7 ]" T* B$ O6 i) U <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
6 F- `2 B- E' i! P. { T2 I" [0 @5 o </p>, W6 e( Q0 e( l, S1 q- T8 X- ?
<?php: a1 U1 b4 [/ t4 {
}
+ Y; ^4 l5 x0 M9 S' i4 Y; R o1 @* z+ N; A
add_action('save_post', 'save_site_wide_notice_meta_box');, I3 K6 [) R# V& m6 M% l# r n
function save_site_wide_notice_meta_box($post_id) {
) t" t0 l$ L+ i8 D if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
& t% [ T9 g8 P* H$ q return;, r$ b! Z& a. j+ Q/ Y( q7 t2 f- G2 ?% v2 }
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
+ x( J1 |8 h7 F return;8 p/ @% [# A7 \5 q6 A @0 m
+ P2 c+ u. q* t* |: k2 i
if (isset($_POST['notice_title'])) {
# h2 f3 d5 R, \7 F A( m+ L: J9 v* { update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
$ w' ?! d* |1 v1 \ }
% I+ X3 C! ^9 g" M0 D2 x if (isset($_POST['notice_content'])) {0 \4 S R- B. y5 R( D) z
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));' P; ?) ?7 X% b* O* g
}
- g4 W1 H1 q: a a: l1 X3 H6 X }" O0 C1 {% u: i7 t
```
_' a+ U1 D/ H i. R3 H/ o. i- d/ \, i9 D" ~0 x
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。# n8 o! _) ]: N+ f% q' A
3 _: B) w8 ?& J0 H- [+ B: l! u4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:7 [$ \$ Y0 v- s# X- C
" }) Z7 x( R2 O" G" F ```
( r! G) B/ M$ z3 [! o& v2 T, B $args = array(% ?3 ^+ L& g. O
'post_type' => 'site-wide-notices',9 j% M! Z/ o: {/ g( [- _
'posts_per_page' => 3,) \7 `! y) P- e0 Y7 }
'order' => 'DESC',
; ^9 c' S1 w* \7 j: O; Y4 l p z) F# i 'orderby' => 'date'
' o* r& ?" H$ g- O- R );
- N8 p" a6 B/ }! W% ? $query = new WP_Query($args);
' |5 o6 x. d* Q( U- w if ($query->have_posts()) :+ T; W) `# }1 `) d5 ^7 l
while ($query->have_posts()) : $query->the_post(); ?>
% g- G4 Q, l/ r! M& ~! g+ d1 g <div class="notice">. z; k" w' V' ]# P. y; X% B( f- V
<h3><?php the_title(); ?></h3>0 U8 B8 N2 Q* V- E1 M: \* ^: V. ?
<div class="notice-content"><?php the_content(); ?></div>" W/ f }2 d1 o# ^0 Y" N
</div>
2 @) c% y' S. p& m <?php endwhile;
+ J2 ^. G, }$ [; W wp_reset_postdata();
. t3 n$ \& i. i4 j. a endif;# ~) m: S( b ?1 b0 Q4 e
```
f% N& s- ?& n! y8 H3 e9 A- e' I& c8 F5 G
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|