|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?8 U% T; N: ?; T6 [
+ d, w" S/ T0 B+ U, G7 u$ f
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。' ?" n$ n6 `" m6 N: ~: S# Q
& K) ?2 e! Z0 y$ C6 m
以下是创建自定义插件的步骤:, c. P1 V; v! e
5 u) O" W1 u+ [( ?1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:+ G; m5 s7 ~# G1 p9 d0 J3 X, K
+ F% U: P/ O( U+ i7 i; d0 h
```4 [) ]% u1 w$ D3 }
<?php* v6 D- o& R ?5 G: W
/* [ V! X U7 Q: n' I; u" t
Plugin Name: Site Wide Notices Plugin
9 p2 i1 U# x1 P- _% l9 L, i Description: Adds a new custom post type for site-wide notices.
% w1 q u t* p8 W5 w" W3 O Version: 1.08 g; c- D0 I) ~; o) S
Author: Your Name
$ ]9 _4 j8 ~: w1 A Author URI: http://example.com
# k# ~: W8 ?& x" I; V */" j$ U) Z/ x" s
1 A& G, `- P* i& ?( C. T1 i. i7 ~3 \+ ]
// Add plugin code here...9 T% q+ f; D4 o9 {2 P
```0 m& i) L. J1 t3 d" X
' m' @9 D: {/ e& \8 t) [0 b
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
' N$ J7 z2 x- ]9 j' _4 _( w: W3 ~7 m( v. n [* ]
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
" t, L7 H3 G- w
1 ^/ q3 Q& g) I" J ```) U; x5 d9 T, {5 S* N: n5 |0 W
add_action('init', 'create_custom_post_type');
f E; D) M# j0 O( P2 u function create_custom_post_type() {
( v: p. O4 ]- z; S- ^ $labels = array(
' X" ^% P6 k! n. w9 j 'name' => 'Site Wide Notices',$ U; {0 R' s1 s* y Z% D2 R
'singular_name' => 'Site Wide Notice',: I+ ^9 P Q. Y8 F: f
'add_new' => 'Add New',
& K% x* S Y F; K ~6 X. l0 Y5 @ 'add_new_item' => 'Add New Site Wide Notice',
1 o, T G# ]' s0 r6 u* y9 o; w 'edit_item' => 'Edit Site Wide Notice',
9 h& [6 F* X7 W" S 'new_item' => 'New Site Wide Notice',- t7 z d7 v l j: [, D/ N8 E
'view_item' => 'View Site Wide Notice',+ p! R' L) R. w6 O6 S( k7 Y
'search_items' => 'Search Site Wide Notices',
) c' q5 O3 B: a. Q9 y 'not_found' => 'No site-wide notices found',
7 j Z" N2 J. v; f! [ 'not_found_in_trash' => 'No site-wide notices found in trash'/ ^2 b% {) N& b9 y
);
1 M7 H8 K& y2 i: C8 S% @; q) \# G3 S5 C l
+ h, G/ \6 K4 J4 M $args = array(
" J5 Z: Q2 S) O7 \6 ?% o 'labels' => $labels,
1 |/ L: v2 { R" g. ~, B; y% D 'public' => true,: Q) \' V' o0 H3 w) [( Z0 Z
'has_archive' => true,
- ~/ C, I$ m, a$ A( ~! L 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
* r# v3 Z7 ~9 L& d( L 'taxonomies' => array('category', 'post_tag'),
% ]. q' C m0 ^ 'menu_icon' => 'dashicons-megaphone',3 `9 _, m. S6 ]% K7 c
'menu_position' => 5,: {1 ^8 w! I* q. t. z
'rewrite' => array('slug' => 'site-wide-notices')
8 E4 s, x! W' o+ B );
# B5 ? k% b- r
j7 [5 i0 ?1 \) w: a' @5 }$ R register_post_type('site-wide-notices', $args);
- O" F8 f- _8 [3 F7 z } ]* R9 K0 f2 p: a: N5 Q0 _8 [" M
```
" E# y4 U& q0 x' v
$ G! C$ k3 S0 {) c8 N 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。* D5 e! G) c/ Y0 r$ y9 C
s' L( P+ x j& g
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
% K6 ]7 d+ ^7 q( y5 I! f2 u s0 v# G) l4 ^
```
; A1 j+ W% v. [( u! H. }( e* f# C add_action('add_meta_boxes', 'add_site_wide_notices_boxes');1 E% ?6 B1 C/ r
function add_site_wide_notices_boxes() {
) w8 F2 o9 g' `0 O add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');* G4 m& ~! D3 @! @0 d
}: R- g9 b0 J1 K$ B# S' g/ L. k
, R8 S0 ?3 x* e5 }4 o# ^- b# s% z function notice_details_meta_box($post) {
1 {, y' y8 k( Y- \* _9 f wp_nonce_field(basename(__FILE__), 'notices_nonce');6 c* _# T+ M4 O Q$ e
$notice_title = get_post_meta($post->ID, 'notice_title', true);
0 f& V5 C% ]6 K& D" a- I $notice_content = get_post_meta($post->ID, 'notice_content', true);
/ m2 L' T- m! F" V1 B* m ?>
6 L; g" H& m8 R3 k <p>
5 }6 Z5 B u) E6 u" T; j3 ^ <label for="notice-title">Notice Title</label><br>
3 n- J6 A. T& b/ E: W$ t <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
& B* ?& |( F% d4 z( E8 T </p>1 ?8 B: P/ ^4 O" @; d
<p>
6 b, w2 J, a9 s" {' C8 n <label for="notice-content">Notice Content</label><br>
$ k8 w8 c7 A( [; c& H U# U9 l <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>4 ?& h4 o: @3 Z }/ I- J* \
</p>: i) O# ?% i4 Y; R8 e) T
<?php
2 I% x' |' a4 h; E/ Z& k* a t/ ] }& w/ \3 N+ n/ J
- Z) ], m# V. J1 {; ] [$ ~. D3 ?
add_action('save_post', 'save_site_wide_notice_meta_box');% Q; i9 l3 ]) z- g4 G/ I
function save_site_wide_notice_meta_box($post_id) {
4 S4 ^% V3 {8 K0 M if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))' `" l! t. R; D" n6 j/ L
return;2 g8 P& l& M& d( E
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)- w) l1 E/ c! G, l" l0 {2 J
return;' Y2 B i3 _4 F" B) o
7 H7 n O8 u9 }) g1 J7 S, F
if (isset($_POST['notice_title'])) {
' S" i) n0 o! w9 ]# p1 O& T update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
0 S2 [& Y- A& c }
1 L& |# d* b8 j$ I8 q" Y& o* r `9 K if (isset($_POST['notice_content'])) {; j& F) [( a2 q; x# a3 T
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));8 [. l0 `: K0 c* d p/ z0 R
}
7 f0 D% {4 B/ j' { }% D/ u' J* B9 C
```
# q6 t. Q1 F" X* ], F9 h* {1 t" r8 K% q! X
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。6 E; c: x. ^1 o5 _# B% ?/ u
2 d* P! A. V2 }5 M: g7 o, F9 V5 b
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:! @0 X+ f8 u5 E. I$ o) [) _2 D' O
% f j3 s+ i6 e0 U8 e7 x ```
7 ^8 f' { T/ l $args = array(1 \- @! K- n, e6 r* ~
'post_type' => 'site-wide-notices',
Q @$ i. [$ P5 _+ ? 'posts_per_page' => 3,: J; Y3 p2 n4 I: m( {' A* B
'order' => 'DESC',
. y0 h3 x3 [9 x9 N# I/ E9 i2 C& m6 u 'orderby' => 'date'. A, e# }5 U0 k; n5 F
);
, N* E E* b, c$ F5 ?" s& {: y$ o $query = new WP_Query($args);
( n; X4 u6 X1 Z5 O6 F' x if ($query->have_posts()) :& {; [+ g8 L% f
while ($query->have_posts()) : $query->the_post(); ?>
; m0 n, L; T Y3 `3 m5 ~ <div class="notice">
4 g% @2 V/ M# Z6 F+ a <h3><?php the_title(); ?></h3>
+ N9 d- ?5 g4 e7 w <div class="notice-content"><?php the_content(); ?></div>/ _! P2 ?8 K2 U7 f6 D
</div>
' V5 q% g! E: ]4 Z/ T) K <?php endwhile;0 H4 F, r" N! \7 v, C
wp_reset_postdata();6 S6 \8 ~/ H8 c& L- V) B d
endif;7 w7 o. A; I0 i3 R4 L3 ^4 o$ q
```' D0 a0 W4 F' a6 j8 B
7 `( E# k2 _1 ~8 \4 c 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|