|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?$ n" Z) u3 c) z- Q* Z; J# ~
2 ~% A% a: R& o5 Z# W2 L, s4 K如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
0 `2 p6 W6 n7 M* r5 P- W/ ^3 c4 C# o
* C9 M; @; h0 G以下是创建自定义插件的步骤:9 U' W! P# L# `% ^* p4 k
: t' I( v, ^2 y$ x3 e Q! S. D1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
' h4 L d$ ?# I; e; ^
1 I0 I* k, T" Z# x6 D) O' \7 ] ```7 B* y- |7 }% X3 i. W
<?php
k$ P% W* I4 [' N /*
% v. J8 t, g! |0 z9 Y. @ Plugin Name: Site Wide Notices Plugin/ V% |# J- k( w) L
Description: Adds a new custom post type for site-wide notices.
$ b2 T; _' [! @0 W( B4 T Version: 1.0
; F/ H- h& z; d6 M Author: Your Name
0 P. l% @/ C- ?0 U Author URI: http://example.com0 b8 z0 z$ R* Z% C4 Y
*/
# B. h' X$ P6 F3 {0 p) _
# o, x& n2 P N6 T, T // Add plugin code here..., X: u# q. d( h0 U9 Q
```3 e; L/ o0 }6 b& |
3 j' R7 x$ ~6 e6 g, W
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
7 e) p6 J& M/ `
* k& @9 K( J0 E% M9 m8 v2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
$ U3 {. |2 E7 ]8 u$ V' ], u4 A+ S. g9 R. Y% o
```2 r" H; r! o" D5 ~
add_action('init', 'create_custom_post_type');: K( ^) m' d3 ~( c
function create_custom_post_type() {# y# l8 P$ s# A2 x* R
$labels = array(0 {# C; Q% _- D* o- w J0 ]8 b
'name' => 'Site Wide Notices',
( {& S/ p# r6 a& ^. `2 F$ e 'singular_name' => 'Site Wide Notice',$ B$ ?/ h' I/ T, j2 \& b
'add_new' => 'Add New',
! V/ w, I% I; \: Z& v/ }. ? 'add_new_item' => 'Add New Site Wide Notice',0 L& [ |5 B! n% C
'edit_item' => 'Edit Site Wide Notice',# p8 `: o' w& @; w1 M
'new_item' => 'New Site Wide Notice',
4 E3 [8 V, k+ U0 Z, b# x( g 'view_item' => 'View Site Wide Notice',
! v. d! @* a8 i) v6 ] 'search_items' => 'Search Site Wide Notices',
/ r: D4 S" s, j& U8 a+ S 'not_found' => 'No site-wide notices found',( y% M$ y' j" \( ~% F# {- h
'not_found_in_trash' => 'No site-wide notices found in trash'5 d8 P6 Y) F/ |
);
9 u9 J$ d0 v5 Z: N/ d2 @ O' v2 h( ?1 I8 D9 U l6 A* y
$args = array(' E! t$ p/ M; K7 z
'labels' => $labels,' h0 \ u L3 s5 e5 I" N
'public' => true,
- H2 S4 t5 B- a& ~ 'has_archive' => true,
" {2 h6 d2 B$ A 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
: K; _$ s, `" P- Q. T2 G4 I7 x, N) P 'taxonomies' => array('category', 'post_tag'),& r" ?1 f: M" W( }8 m5 K2 d& Z
'menu_icon' => 'dashicons-megaphone',
7 s$ U2 m n8 c3 w$ s* s 'menu_position' => 5,, W* ]. t0 {; m, ]% z/ j
'rewrite' => array('slug' => 'site-wide-notices')
' g2 W) ], i8 ] h );; f$ R' f2 P* r& t# L
% A) R; u: L3 O, p3 s# D/ ?
register_post_type('site-wide-notices', $args);
6 r: s7 G: ` a+ j# b0 u) p) H! V$ w }
3 r9 L4 G6 K) \ p6 k2 M ```
1 y1 _' d9 y: V. k4 ^: a
$ D( M6 ^" h8 R0 t0 T/ A1 w 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。# v4 d0 A0 T$ `- d; g3 V0 C
! H* |8 `/ U8 O% E' I! K3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
5 ]8 l( c/ p; S& K0 X5 l1 y2 t+ A4 X, p% x
```# u, R$ h4 N& Z% s( X
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
5 Y/ U0 P4 A ?6 z4 N! v2 f6 P function add_site_wide_notices_boxes() {
, d3 K! n/ |8 f+ Q add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
* o2 W. l/ z. }3 Z! G2 d0 J3 P }
5 W9 W0 v9 G) r: ?- p& R& f+ v9 N* S% n$ ]
function notice_details_meta_box($post) {# N3 O% U9 |! z
wp_nonce_field(basename(__FILE__), 'notices_nonce');
6 o2 |7 [$ }" |5 Q* J, { $notice_title = get_post_meta($post->ID, 'notice_title', true);6 l( ?7 E3 Z+ P2 e z' {2 P! {
$notice_content = get_post_meta($post->ID, 'notice_content', true);
8 O: ?7 [0 _( ]* ]3 \4 b k5 y8 k5 j ?>; A7 Q% [$ F: A `, `% R# N9 K
<p>
9 P: p9 o, Q) h# H3 ]- [2 N <label for="notice-title">Notice Title</label><br>
5 \: ~$ s- Q; x1 W* j! K! } <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
9 a% Z0 n q5 ?5 L) i </p>1 [- u" y9 I" X' N3 @# B: _
<p>
& ?, `' I" Z+ G' }$ {( h <label for="notice-content">Notice Content</label><br>
! j! @- }1 J, c2 `+ q' C* |* g <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
, [5 O$ r" [8 z+ J </p>
) C: Z# L) J, w0 x& t7 Q% } <?php+ w/ h* B+ j9 ?6 J9 z4 |' Y
}; b9 J+ X! a( H# N1 e
: B- w& c; L: E
add_action('save_post', 'save_site_wide_notice_meta_box');
0 J5 X. b2 p; ^' w function save_site_wide_notice_meta_box($post_id) {2 s" [" c6 Q t! w
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
1 G5 B1 X% u4 E, J- V return;. E6 h2 d! ^& u1 q- I: s" G
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
5 f& R" V& `& _3 i8 |. p0 u return;8 U* h' F* S7 [
. H4 p# J; a3 |6 I' B7 v. p
if (isset($_POST['notice_title'])) {2 X: f1 f. k1 h
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));1 Z2 \* S Y! q- y
}
; S6 ?* Y& L" o4 Q7 ` if (isset($_POST['notice_content'])) {
5 f* }4 P* O" A O& @$ R% T q Q update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
9 C$ p% ^5 S9 S5 R }. }4 W3 y: v6 N+ I5 K$ G; y9 |( }
}! W$ n( Y7 _, k' ?( R5 |5 [
```# p+ L+ C M. Y7 q4 K( X1 F; p. b# S& `0 D
- x, l, t, P8 e7 {5 S 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
6 m: S$ `3 P0 c( H
' V$ n0 T& s0 ?8 }1 n4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:; D, E% p7 u6 B, ^/ g, M1 Y4 o( A
/ }( o9 ]) k- y& Y# ]" I
```* e5 T& l0 r* `3 v# e5 F- d
$args = array(/ w( F& p- v# Q% t# \% B
'post_type' => 'site-wide-notices',
1 J3 i+ S9 d7 B- ?* q& L 'posts_per_page' => 3,
' b/ ?/ {3 M; r9 L0 X7 j# H" x8 o 'order' => 'DESC',
0 k; g- U5 T$ G5 t; Q7 G* g$ f 'orderby' => 'date'
1 S1 t/ t$ i6 J; c a& }& N, M8 i$ j3 N );- |' F1 z: I" R3 G2 i6 U2 O
$query = new WP_Query($args);. G5 F+ t- y# J8 n k+ \, J
if ($query->have_posts()) :
( B) i- s. {$ K6 V6 P; k while ($query->have_posts()) : $query->the_post(); ?>
9 S# {8 |0 l9 @3 U- Y <div class="notice">3 \6 [7 b8 |" y+ c) p. p
<h3><?php the_title(); ?></h3>
C: ]) M: E W% {2 s ^ <div class="notice-content"><?php the_content(); ?></div>) e4 M) d: S2 s$ e% t$ Q. r% n8 Q
</div>
' h: P0 U ?' s1 k9 V6 ]5 \ <?php endwhile;
" U; W5 y* u8 `2 H4 z wp_reset_postdata();& k; e7 b: S( T- L
endif;
/ g9 \ I; t' n' } ```
9 A( [" d7 p' j; M% _1 ?
# P {6 Y$ s0 ^: m 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|