|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
. M% m# Y+ u8 A' y
' [, A0 F& ? ~# I如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
' w# q/ e; r8 u" {/ o0 S2 h6 l: |8 p4 f* B. b
以下是创建自定义插件的步骤:
, W" j* h, r9 }' m" T% p b& X ^" p2 F. x* E
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
0 {8 d( U0 }8 o1 p! t( o' _$ W3 C7 x
```! J6 k0 [: O+ l7 t# _4 V3 b
<?php
5 ~0 F7 k3 M; C7 _% J; {2 M' ] /*
0 m+ t y( H, q2 j0 W Plugin Name: Site Wide Notices Plugin
( }; U- g3 u& T2 _" S# `' Y Description: Adds a new custom post type for site-wide notices.4 m" i- ]8 ^* P. }6 i0 Q" q
Version: 1.0
8 |( \( \$ p) t. w0 D' j7 } Author: Your Name
) a& d7 ~0 Q% K6 @' H Author URI: http://example.com: F h2 o* S8 K" f4 ^( }, X
*// o- b* w. n/ [. C
9 S" z: t. c* f- d6 b, V9 V! w // Add plugin code here...
* a3 [" G f$ q ```
& X B! L# X. R; o7 z' M! H( u8 P. F3 T
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
2 Q5 q4 E. L _1 H7 V i1 D% n# O5 W( N% o& T% B
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:' T/ y0 S& }8 |& u- K$ d
; Y; S6 y" j* U: k( ~
```. p, _8 r8 j2 G) ~# w' A4 O! H
add_action('init', 'create_custom_post_type');
7 B; q7 A* Y! u) ]/ D* x9 g+ b function create_custom_post_type() {
4 W( R, A6 p4 e- y $labels = array(' |! q4 i4 a' O
'name' => 'Site Wide Notices',
4 T1 u( F2 \4 [ 'singular_name' => 'Site Wide Notice',
! b: v0 L/ h# w: i' u7 {; K, u 'add_new' => 'Add New',
# X9 `8 E6 B+ ^* ] 'add_new_item' => 'Add New Site Wide Notice',* S% c: `6 J- ~% H. @# B- f/ ^
'edit_item' => 'Edit Site Wide Notice',, g# \) O. E# g! D
'new_item' => 'New Site Wide Notice',
1 B4 h- j+ i" ?6 a0 b$ E; s 'view_item' => 'View Site Wide Notice',# ^: x" ~# [% n
'search_items' => 'Search Site Wide Notices',
# n0 a' v* Z7 g) g) x P J0 } 'not_found' => 'No site-wide notices found',
0 f$ @ P2 i, [( |! S, a 'not_found_in_trash' => 'No site-wide notices found in trash'
5 {) C0 [, ]+ s' w) Q5 }! `0 [ );+ e3 Q0 o5 u5 e: R! W4 P! X1 Y
3 f4 o" i% l: c( e0 q $args = array($ S: ?! A" s, X7 V" B! X7 b
'labels' => $labels,8 Q* \& e/ s& G
'public' => true,
7 e8 B |7 W: M3 N( S) h 'has_archive' => true,+ F6 f0 f8 w1 X. Q3 s2 N
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
2 n5 V! |% a0 t6 j 'taxonomies' => array('category', 'post_tag'),, Q8 G0 |3 h+ W/ M
'menu_icon' => 'dashicons-megaphone',
1 e/ W. C: `. P 'menu_position' => 5,
$ X- g A8 ?( W# ] 'rewrite' => array('slug' => 'site-wide-notices')
! u) z7 ?7 x+ n: o" U0 b; h/ R6 ^ );6 Z/ q& {+ z* r# R. s% \5 }0 d
- K' Z% a! S7 g2 Y- T
register_post_type('site-wide-notices', $args);3 @6 I2 O0 I. F+ u, v0 O. w- B
}
# U! J# R4 M( H7 T2 i* K3 m! u ```
$ y5 `$ d% _' p; a3 d+ ~+ ` @# U" D' n8 H, E' a, }7 ?/ y1 h
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。5 w' F/ t/ _( G1 _; |
. ~$ f7 l4 S2 I3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:; c! w; Q5 ^5 U+ H" o
. R& L* I" l9 {* t
```
$ A' I2 W& i( M add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
' w- T+ X1 {, V2 g! N1 k) O function add_site_wide_notices_boxes() {% {. e. Z, u& |0 n
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');3 u7 p5 c3 y* h" u6 u& u% X
}
3 v3 g7 o! H4 u0 j" s% @3 R0 |! j! R& G; J3 A! K" i4 Q
function notice_details_meta_box($post) { _5 L7 H- n; y) O
wp_nonce_field(basename(__FILE__), 'notices_nonce');) h# y3 r ]4 ~$ D7 J. d9 ]$ x
$notice_title = get_post_meta($post->ID, 'notice_title', true);
$ F) r+ e1 W0 F: F) t f$ j $notice_content = get_post_meta($post->ID, 'notice_content', true);
9 c7 l/ g& l% M) j' v: v' k4 f ?>
0 u5 @+ i8 v6 y <p>- O% k) P8 g l3 }. \7 I3 F/ \
<label for="notice-title">Notice Title</label><br>! o$ E; W3 w, l4 H' D8 X9 j- ~0 z
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">' s, g& L) ~ x' S+ n9 g8 W7 z
</p>, S% P! Z6 u; M% V7 Z: }
<p>; d4 }5 Z4 r, J: ^& w
<label for="notice-content">Notice Content</label><br>8 L% k5 q. Q7 O. p! b2 D: E
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?> i) ^+ W+ ?9 d# u+ J
</p>
5 A: ]7 ~/ c7 }3 m& L( ]4 U2 e9 G# D# G <?php
+ k( ?: c, S4 Z }" l: r- o1 \% a8 O$ U, `' x
4 p, K' t' V* V add_action('save_post', 'save_site_wide_notice_meta_box');- r# k- p7 B! E. {
function save_site_wide_notice_meta_box($post_id) {9 l Q8 G2 o \4 A5 T- Z
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
' \, M8 {$ }- i0 P9 N return;7 E# {+ D" G9 o# `
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)* U0 W4 ?3 t/ o2 }8 D5 A. m; g9 \" U
return;
* H* v: E9 _6 r8 I* [6 L+ K
# k |( L8 _: w% m if (isset($_POST['notice_title'])) {
& a# G$ n; n( e! C) D: S! I update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));! z" C% l' G5 h. w+ |
}
2 F; m8 b9 _* D% k3 B6 n if (isset($_POST['notice_content'])) {
- i* Q! t; p( v) Y" Z update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
) b5 s' a+ ]0 Z8 Z0 B3 g }. n. V& ]( t" J& K+ o- o
}
" a& E9 c; f; F+ X8 H ```" g- T _, `& a ]( U% r/ e
7 W, ^/ w8 \2 y% u2 _ 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。7 W8 J" t) c. f! x5 \
/ y8 C3 P6 Z$ u* {# e) k
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
8 I- q% V( g4 O8 o( U! q4 h5 k- u8 W% c! t2 ]2 i5 |
```. K( c& d+ T1 L2 `
$args = array(7 F* D8 v! m# @
'post_type' => 'site-wide-notices',) V" Z) q( @ D
'posts_per_page' => 3,* n% y: I+ p+ i1 n) C* y) M4 |
'order' => 'DESC',
0 h# X# \4 r' |* N" f+ p 'orderby' => 'date'9 }/ d) y/ E) c+ z; d8 R
);6 F. ?( V, q& c; K, |
$query = new WP_Query($args);
8 v I0 \! p, q$ v, |$ ]' R! n if ($query->have_posts()) :! k" J9 c5 N) I; ?4 w3 K U
while ($query->have_posts()) : $query->the_post(); ?>. Q; N* e) W! u$ M
<div class="notice">6 j+ _5 x* ^7 c" E+ C3 f. M4 k
<h3><?php the_title(); ?></h3>- r3 v" x$ j1 d1 g4 C5 b4 ?/ \
<div class="notice-content"><?php the_content(); ?></div>6 F, s* |& p, L, V5 d: G b
</div>
5 b/ ]( b4 B( K3 p; l; {9 n) }7 P8 _ <?php endwhile;
+ W5 p$ g1 A6 `0 d% _% s# u& p0 ~ wp_reset_postdata();
E1 F7 f* m/ g. L x endif;
4 k8 x2 ]- G a% m ```
* S/ K# ~: J, ~" O) K6 m
1 ?7 [ W; V3 U. k2 N6 U6 y1 i 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|