|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?1 m. {4 D; P, o) u) z
" W+ z: G) C, g& {, K- N
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。9 C' P% J. O" J8 {1 }" k v9 K
) S$ H* i4 Z. | {( j$ u以下是创建自定义插件的步骤:( v$ g4 Y* V1 H1 w) V Z
2 w7 P3 E) u8 ] Z- c! U
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
! j) X1 N3 A# G3 m# m/ b$ `8 O
```- w2 P: r5 u9 d
<?php2 E( c- s7 x6 R0 `, W
/*
8 E9 X' O2 S3 Z5 A& B } Plugin Name: Site Wide Notices Plugin
: R* k; W6 t- E Description: Adds a new custom post type for site-wide notices.$ a; |% i& ]# h
Version: 1.0
: A. b) `3 n, H8 P Author: Your Name
5 ?7 V# P: _9 t D% Y( B% y& F Author URI: http://example.com
! |9 L/ R7 ]/ L, }; Q8 J/ B5 i& [8 R */7 K: H( \1 I% F( H3 s. _+ e0 e& I
2 r5 Q: F9 k! q; l: c& ^4 @ // Add plugin code here...& \. ?9 |7 Q) ~ |; N) ]7 N
```% `. a8 j* x& M% e8 F+ _
( Y. R* y% B& ~ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。% B4 K) ^6 d5 ^
8 I# M3 H: h& u2 \3 F2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
( X' Z- N2 @. }$ t# f
Y1 @5 W7 b' B ```- Z3 O6 b/ b: k9 t5 q4 B" m
add_action('init', 'create_custom_post_type');
; B4 S2 w$ C) @7 e function create_custom_post_type() {
p- g- Q( n* l, l2 a: }# M& V $labels = array(! F% o( v3 e% {/ x- S) \
'name' => 'Site Wide Notices',9 z- v3 c. T; R
'singular_name' => 'Site Wide Notice',
8 e9 v9 k; Z. o) S3 E 'add_new' => 'Add New',: q- [5 F1 {# @) _; M
'add_new_item' => 'Add New Site Wide Notice',
; \, h: u B2 y! b+ C/ s 'edit_item' => 'Edit Site Wide Notice',
) v; { v. x0 c1 G! Q 'new_item' => 'New Site Wide Notice',/ {/ O8 k* {3 J5 F* u
'view_item' => 'View Site Wide Notice',1 x( y" U8 b2 z+ X2 @# Z
'search_items' => 'Search Site Wide Notices',# d$ y2 i5 t# A q/ a
'not_found' => 'No site-wide notices found',4 \: p4 y' ]' l! y. j8 u8 }7 z# V- _
'not_found_in_trash' => 'No site-wide notices found in trash'5 P) @% n5 A! \8 j9 e8 b
);
+ W d# m* q; o0 w' e' \# B( j2 x7 G9 Q/ a6 k% o' n3 b4 ^1 [( r7 a
$args = array(; k% B5 N, T1 D# C! y$ X* C' {
'labels' => $labels,$ P# A; P; e7 U( B; @+ E8 b7 y
'public' => true,- u$ u' P9 l* e! C2 d
'has_archive' => true,2 ]2 B+ W% t$ u3 C- ?' W1 h6 D/ j
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
n4 I7 P2 D; r6 T& o: F 'taxonomies' => array('category', 'post_tag'),
' T, K/ t8 H# o 'menu_icon' => 'dashicons-megaphone',: p3 e& Y+ [) t2 Q* [$ A# L* r8 H
'menu_position' => 5,, c: d& | M$ S7 r7 r
'rewrite' => array('slug' => 'site-wide-notices')
" ]; B) J$ k( r+ [7 G8 q );
" d: c2 o; i- V1 [/ k1 D
* S6 ?$ \: V- b9 q. N. ]" w register_post_type('site-wide-notices', $args);
% i; g. C+ x. A6 b$ `7 y }
0 C; Q1 ?/ v/ B( m% x; t, B+ c/ q ```
2 U' M% J# n5 Z" `2 Q' F) z$ X' z& d$ n# @" G
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
! j& Y3 a S* G; }
8 ^5 q; U. I6 S( x4 _6 u3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
3 s4 X: F4 J& ] j
3 r# q8 {; i+ b; @" Y$ `$ v( W ```: k# ]2 v7 n, D: W7 M1 R
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');6 A$ q3 c$ K4 \: m
function add_site_wide_notices_boxes() {
- H5 f' X1 i5 H+ w) m, h add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');. ]6 W1 W) x1 @/ B; w5 o
}
' z- X% O! ^4 Z7 J$ [2 K1 f2 @! d! N7 ~" O
function notice_details_meta_box($post) {$ }0 f$ V# F! A1 U$ }, ~; n- G" v
wp_nonce_field(basename(__FILE__), 'notices_nonce');
! X! x3 w8 d# R$ Z0 ^ $notice_title = get_post_meta($post->ID, 'notice_title', true);% c8 r1 a$ y* K
$notice_content = get_post_meta($post->ID, 'notice_content', true);% n* M" A% q; N, {6 I" Y
?># l/ l4 h1 V, [5 l' U
<p>! }" N: b5 Z* Y. W" l
<label for="notice-title">Notice Title</label><br>( \- K1 }! }+ n$ d) s3 t9 g
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">8 U) ?& q/ G: L$ M3 F
</p>
8 N+ }# U3 ~5 j. @/ R3 w) I- o <p>/ c. Q) s J. b: R
<label for="notice-content">Notice Content</label><br>
4 J3 E/ w. Z R- {/ d$ l: s; { <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>. R+ p! Y3 z5 g& Q1 n5 b1 v) F6 |
</p>
; ], f* e3 }: N& e <?php1 P" H8 l! _6 c
}
3 u( o+ G- E: w* d2 S- n3 k3 |, n
add_action('save_post', 'save_site_wide_notice_meta_box');7 _% O h; P9 o( g, K0 w5 k
function save_site_wide_notice_meta_box($post_id) {
5 \8 m6 m* U0 A' E2 ]$ e if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))0 c# E; ]3 {) f- q" l: `0 ]
return;
) W# S z& {& P0 r if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)$ n7 t, ?; \& H5 }4 D V. p9 X
return;8 h) c- s* S0 n1 @
( y9 V# x4 j# A$ G! ]8 Q- [ if (isset($_POST['notice_title'])) {9 E& w" d- i. a+ }
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));3 C' D" X! {' p D. `
}
8 x( i4 `# C. _5 A: n) | if (isset($_POST['notice_content'])) {$ i8 ?) m% i$ @3 X/ B
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));# }' a) l% F1 {7 T
}. p7 V2 V6 W6 K0 R4 Y
}
7 {" U6 E2 Y9 B9 G; ^: d+ Z* J ```: m- g7 f5 w$ W* N0 a1 ~
/ w4 G) M5 m! J8 v 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
1 s8 N$ a2 [ H0 y L6 W! F
4 v" o8 r6 x; k( V$ a5 _4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
9 f4 B/ Z( i! F- U; V, D3 u/ _- t8 A& `
```
. ]' ~* ?2 o. {; z& \8 g& c1 E $args = array(. H1 V: X. d1 a3 ^: D
'post_type' => 'site-wide-notices',6 C G( S3 V1 d9 s: v
'posts_per_page' => 3,
% e' h6 U6 G1 f 'order' => 'DESC',
$ e) d/ V1 ` P6 c. p 'orderby' => 'date'
& g4 \ w, |, [" R );
* G9 h9 r6 Y/ d% P# y $query = new WP_Query($args);% N: z$ P+ P$ B2 a* G. m
if ($query->have_posts()) :
N7 D& K) I h5 ~3 q while ($query->have_posts()) : $query->the_post(); ?>5 B9 P: x' R" |: @2 y7 ?0 n
<div class="notice"> T0 [7 W& r& G2 w+ a) m) Q- D
<h3><?php the_title(); ?></h3>
/ e5 j: W# d; @% Y) {1 y w <div class="notice-content"><?php the_content(); ?></div>9 V7 x2 ?- _" u4 O
</div>
5 T/ ~: I; L! ^. f <?php endwhile;$ B+ t$ X$ |! r$ x
wp_reset_postdata();% c2 h9 _" r$ ^# l
endif;( [9 O. L) c% D
```
+ d, J7 k! N: f* h% b7 \! N6 s" I- c
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|