|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗? ?) q! z/ Q/ }6 c6 d
# X* M( S2 \. ~9 ~ T. P1 ]3 N
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。' e& S( H% m Q: Q/ C
: ?6 Z- N+ v" a. a& P3 [
以下是创建自定义插件的步骤:
1 I( w$ h( M* O' M' P5 C
+ l- F' V, A; i E. S/ t! C1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如: W" H, w6 u4 _! h" A
4 l7 C. y0 a" H ```: c. a- ?) X+ G7 O' _% J
<?php
' j; \$ o' ~% Y /*& u! V* i; p4 d/ C* M# h) u' k
Plugin Name: Site Wide Notices Plugin( H9 Q- h8 B1 D9 Z/ | }
Description: Adds a new custom post type for site-wide notices.
* G/ b2 l q& o9 |" u& K Version: 1.0, P- m+ g; @( m$ a' ]6 e
Author: Your Name' b) G6 Z; T- {6 g% e
Author URI: http://example.com
" b/ n0 B# X7 d, ^# @" H! r9 T */3 Z! K# X) B4 |
* m! o# ~$ o2 [ // Add plugin code here...# O1 R$ f, \8 e$ H! V
```" J9 }6 {2 c/ w! D( C2 s
& L5 `. A$ l( s6 C, i3 A. n9 g
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。9 p. l* k# v! @
4 ?5 m% c2 \! [) ^
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
% ` l! S; t1 p4 t$ v* j: R, j+ w7 i, w& a8 k8 {
```9 s2 r5 w1 [6 m4 k) S3 k* z" b) W8 o
add_action('init', 'create_custom_post_type');
! }) ~: c9 j e8 h2 a4 D% d5 G function create_custom_post_type() {7 f6 s3 S4 D( G: a
$labels = array(
5 N( f( z3 F6 e5 S4 x) c2 b; F 'name' => 'Site Wide Notices',9 [ F, S9 R$ _$ c: j7 m; P
'singular_name' => 'Site Wide Notice',
( ^( u6 J y: ? a. p5 G 'add_new' => 'Add New', _! N! y) f2 m
'add_new_item' => 'Add New Site Wide Notice',
3 U% T! ^6 u3 \4 v/ V& i" Y/ N- z2 u 'edit_item' => 'Edit Site Wide Notice',
6 a. C- r* d- f 'new_item' => 'New Site Wide Notice',, P. g0 N* j0 n- J
'view_item' => 'View Site Wide Notice',
: p3 Z5 W! w" p; w1 ]- k1 Z, F 'search_items' => 'Search Site Wide Notices',' x: B* Y( l; r- l; a/ o& v E* S
'not_found' => 'No site-wide notices found',
t/ B/ ?! m# N7 e- V( B 'not_found_in_trash' => 'No site-wide notices found in trash'# n; B3 V/ U9 z& }9 w
);
) d( U, c' {/ j6 t% H8 m+ O7 K; i& u7 X7 Z2 m
$args = array(, q3 @1 B% q) H! ` A
'labels' => $labels,4 u* n7 x9 C2 u* E) z* n" S( B
'public' => true,
8 H; n( b/ \6 q Z; ?) S& M0 O 'has_archive' => true,6 D9 b' }+ T1 ]3 ^% X
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
6 m6 B; q0 ^3 D& A- d 'taxonomies' => array('category', 'post_tag'),
8 M% f- {0 T/ g5 b 'menu_icon' => 'dashicons-megaphone',
. X: ?, n. |! F+ M, Q1 W' E 'menu_position' => 5,6 a" b. u' p N+ i- k
'rewrite' => array('slug' => 'site-wide-notices')! T e! q- e% l2 m
);+ p5 y9 b+ G$ e/ Y# g6 ]" [
/ T; F# ~2 W8 }$ d8 e" L. `- `
register_post_type('site-wide-notices', $args);5 A" ^! P( @1 j! m* i2 i5 K5 ]
}9 u; L- A/ C: b( k$ Z3 `+ t
```! C/ K% j5 `: W! ~7 y& W; Z- y
$ \! }0 ^# W5 | 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。1 X; T7 M7 w! |6 l/ A* v3 w6 @
7 J& Q6 H* e+ f+ x! \" P' N0 C3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
* h$ ~, a) C% }( q7 A
3 e8 I* m2 P& L/ i7 A2 I9 O ```! A$ D' z/ \/ G8 A. I4 c. A" ^" O) b7 P
add_action('add_meta_boxes', 'add_site_wide_notices_boxes'); Y/ x2 }/ X c0 [7 _+ l; Q' y
function add_site_wide_notices_boxes() {
4 Y$ m7 O- G% x& W add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
^7 B" d4 K, R4 ~ }' }3 v* I, y- G% m( H( D6 ?8 L! ]
# Y8 W$ ]0 h6 e) Z7 V1 I% q' J
function notice_details_meta_box($post) {
9 H2 N% M2 A! g. p% y- z( Y wp_nonce_field(basename(__FILE__), 'notices_nonce');
8 M0 o j: y8 z5 s5 S/ U/ I $notice_title = get_post_meta($post->ID, 'notice_title', true);& z. i& J: j' v0 r1 i
$notice_content = get_post_meta($post->ID, 'notice_content', true);
5 q( h: v& ?2 {4 @, I* f% B ?>- ~1 Y, Z/ d; y. G
<p>. f$ G) ]% e1 l5 B$ H+ q8 v% c
<label for="notice-title">Notice Title</label><br>( }: X w4 r4 m. Y5 b* v4 e; Z
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">: B) u( c6 k7 U
</p>9 @5 u$ H5 u/ M, N% Z0 |
<p>0 s( G F' O0 h2 e
<label for="notice-content">Notice Content</label><br>
& @7 W9 D" `. B/ P <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
" L0 T! l' }$ O& C, l: m </p>
9 y# l3 F6 Z* v- u+ f, \' f <?php
( t i! R. B9 x+ j, |- t }2 x- S4 e. Y: r
7 e5 m" j( t: L$ n+ p6 C
add_action('save_post', 'save_site_wide_notice_meta_box');
# y; V4 T: d- u X function save_site_wide_notice_meta_box($post_id) {/ I8 [2 |( N" J: R
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
! Q* W5 p7 P3 b8 d return;
/ [% \* c+ [$ t" `, q0 M if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)5 k! P" X( K, T# G: i1 g8 ]
return;( t" d$ j A) F7 W6 D N
3 z* d7 j; V1 @/ B. e- o& h
if (isset($_POST['notice_title'])) {% d4 h. I ` J9 S
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));" M ~/ d K/ Y& V
}
+ F# _3 y) J0 O: {& G if (isset($_POST['notice_content'])) {9 n2 p; H- J* T+ A" W9 u" J3 Q
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
* G" ^$ U: i2 u8 Z }
. |. W3 \; |) t2 v8 o- t0 f# `) G }! T8 ]9 ~; c. o _
```
0 o! {; r8 u" S- _: H) B7 P" |
7 v n; V- p6 Z2 ^1 F8 Q T. c 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。& D! h, [2 `8 F+ {" v6 p$ y6 @0 k) L
! P- k6 v; z# H/ l! R3 e4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:5 q$ [: o) d J' j
( l* Y0 a9 k. {+ c- J ```
+ x( S6 s/ [8 T( S2 n6 c( { $args = array(0 f1 H& u( R9 b
'post_type' => 'site-wide-notices',: Y: J- e% E& ^* L' t
'posts_per_page' => 3,
' z$ J2 ^* c# m, |8 m 'order' => 'DESC',% J4 {+ `# j6 P: Y* j$ D
'orderby' => 'date'
: e- z- e' D% V1 b W* P );5 V! M: {! |/ [9 t0 b: `2 i z
$query = new WP_Query($args);* U2 p q% r9 ?
if ($query->have_posts()) : w5 ]! v3 F" _! |; T( q
while ($query->have_posts()) : $query->the_post(); ?>7 F9 |6 o6 D0 r- @
<div class="notice">
& A- R. w. m. ^# ^ <h3><?php the_title(); ?></h3>
: E% r& D I! G! T <div class="notice-content"><?php the_content(); ?></div>, a v% C5 G1 `/ y5 F
</div>
. Y/ P+ l# s2 a; U; { <?php endwhile;+ R- B& o8 q. h1 J. ?+ u. s7 M
wp_reset_postdata();9 Z0 o$ l$ ^: ~. K. m
endif;
: n1 F/ P! j0 J5 ^ ```9 k( R7 o' q, S% i" d/ F6 a5 |
- b0 E6 D6 w& n! W' ]' ^! Q# ^- U
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|