|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?& {1 c# w7 o0 E7 y! @( A& ]* L" |7 Y
1 @* B" E7 Z, W2 v
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。* T0 q7 q; B0 p4 U& X [& C
* g T8 n {6 |8 x& }以下是创建自定义插件的步骤: q& h( J @4 q- ?
% L3 z+ l3 B; I! ?
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:% [' M8 K) r. D
$ h- a9 c& ?' g4 x O ```2 W/ S$ ^( ^8 W0 k5 u) B
<?php
6 v$ i# D' V) o5 x /*
! o. \3 r: b" e' v; w& S( Q Plugin Name: Site Wide Notices Plugin
" T8 c" C* E7 V7 t( _" A* g Description: Adds a new custom post type for site-wide notices., A7 {! Q7 p* K2 g, r6 W: Y% H
Version: 1.0- I; [) p5 R% W% ~
Author: Your Name
+ _, }; F' _2 x/ ?; V; I Author URI: http://example.com
7 Q0 G9 u/ \/ }5 I0 d: d */1 W6 v9 j/ V! ?6 }
8 H" t. j, S0 a t- S
// Add plugin code here...
6 `. ]6 P5 V- M- ^0 Z: a ```# R# q N* Q# y; A9 p
y" V/ B9 Q& m* S( I7 i1 _
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。; x9 z/ q1 e. L3 R6 k$ m/ s
2 Q0 _+ q9 }# }. E& G; @( W' a
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
{7 `6 n' U; n/ ^) [/ f. J V8 m! N9 P5 P
```
7 @3 t0 E. o. J, b6 m2 m9 Z( `$ G j+ l add_action('init', 'create_custom_post_type');/ A- _+ c% ~/ S. |% G
function create_custom_post_type() {2 [ Z) O" |" ~. G+ @) L
$labels = array(
+ ?' ]9 z0 b% b, [8 I Z- I6 \ 'name' => 'Site Wide Notices',7 Y `0 B' d" W. S, M7 n4 {
'singular_name' => 'Site Wide Notice',4 g6 L0 f! B9 K1 N, }
'add_new' => 'Add New',
7 A t* l$ d; l; n 'add_new_item' => 'Add New Site Wide Notice',
! k; m4 R; L4 i( {& Y 'edit_item' => 'Edit Site Wide Notice'," A1 j) y: Q7 B, z
'new_item' => 'New Site Wide Notice',- z0 W5 C% N# D# u. h9 ~
'view_item' => 'View Site Wide Notice',$ r) L! Y x5 P$ P$ p2 f" h
'search_items' => 'Search Site Wide Notices',
6 W: e: r' x$ B4 J/ P# I$ r( e 'not_found' => 'No site-wide notices found',
1 s% ?! H, |) i# v. X% R 'not_found_in_trash' => 'No site-wide notices found in trash'
$ A7 L6 I1 a, E6 o6 q+ e! A );
. l# F. m+ G! O' i
- m. I% S1 D' t% w2 h/ l# T $args = array(
6 a, i( \8 c9 [8 i! R 'labels' => $labels,
% i2 c! b$ \! C9 ?5 H8 g0 E0 f 'public' => true,7 L( e- U5 ^7 y5 Y6 v2 `1 |6 y
'has_archive' => true,. U, ?+ g+ D4 i! v; }
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),2 |" {/ \6 |3 @; M/ o6 M2 Q
'taxonomies' => array('category', 'post_tag'),
$ I! @' j2 f/ B6 W* j 'menu_icon' => 'dashicons-megaphone',5 O2 i$ P9 {) E" G" l/ ^
'menu_position' => 5,9 M; Z. q+ Y( w4 ~* f
'rewrite' => array('slug' => 'site-wide-notices')
: \- w1 E7 R5 U' i" X' v; N2 }2 A );
% c! I& G* R& n3 ^2 p W4 D5 |2 ?- J+ H6 B
register_post_type('site-wide-notices', $args);
1 Z: X) W5 C. }( Z } T- ?! i% A; ] I( Y0 m' S/ `
```
$ q. @' u4 D5 U" s
, c% J' D+ ]. K/ x) c 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。# q- @; c$ U+ G7 h4 \$ V" A2 q
1 y" E% {* Q* d5 j' q$ v3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:6 w. A# m1 M% C3 n4 ]' a- ~+ J# s
8 e9 r" {( q2 @
```2 w5 u* v9 [# U1 ]8 T0 Y
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
% E3 w% G; L% s4 k( l* l function add_site_wide_notices_boxes() {
0 _2 p4 p5 T. A7 d) z add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
# d* p0 _7 ^2 M9 y6 ]+ w6 W. V7 d }- k3 B9 @% p- A5 C" v4 U' q1 h
, B8 [7 Y( R4 m
function notice_details_meta_box($post) {+ s2 |2 H+ B0 i3 `3 R2 t& }
wp_nonce_field(basename(__FILE__), 'notices_nonce');; o* ~/ ?8 T. _+ q# S
$notice_title = get_post_meta($post->ID, 'notice_title', true);) K- F2 `; J* C; o& D
$notice_content = get_post_meta($post->ID, 'notice_content', true);% P( j- R4 y+ S2 F/ [+ \% ]
?>
. W$ r( |7 o5 |$ R8 S* E. I5 l. \ <p>
( n T: h$ Q; _ ^1 O+ f0 U+ X8 Z <label for="notice-title">Notice Title</label><br>
% ~5 q5 S+ [/ h; B5 y3 R& m <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
4 A# W, ^5 T3 B. X9 E </p>
- c' z4 Y( G7 V$ ?# L <p>. I; e7 @+ {: p% m* O& h
<label for="notice-content">Notice Content</label><br>) [7 ?3 y& g3 g( \) S5 M
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
$ n( ]* n% o" H! ] </p>) K. G5 G2 v0 T) l9 ~
<?php: N8 ` b$ D& y% a0 p1 G6 o
}7 I( V/ Z& S9 O
$ C9 y, C4 a, T$ O0 v/ r+ w8 H+ }( Y
add_action('save_post', 'save_site_wide_notice_meta_box');
1 i6 M" m% r- M: r3 a' y5 V, h function save_site_wide_notice_meta_box($post_id) {0 {/ F7 e; V9 j2 N
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))- ]! k4 F" R+ C+ N1 @+ w7 q
return;+ e5 @# s. g# ]2 G6 ^0 l
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
' g. o- Y( W6 p( P$ ^! D return;
K6 c# B+ h% n% ]' J+ h
# Y+ `; f+ s5 o# f. t& I if (isset($_POST['notice_title'])) {/ q q) n4 J4 D( s/ t
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
# t- Q! ~- }$ U: `# J2 X6 g1 w }
: s0 [) ^' k: J% x( S' U: O$ Z if (isset($_POST['notice_content'])) {! K- E- c8 l0 i7 R" @
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
2 r( m: P) E3 ]9 w }. n$ R4 {+ Q$ E1 K% t" T8 v! }
}
% `* `. b2 u7 p6 ?! P! h8 n ```
9 _7 h' T* J2 I u: t" H1 |4 l, u9 g4 c% r, ]* r
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。# k% ]8 d7 a$ c- S7 S! Z
9 b4 ?' a! \+ {5 s( g* x6 c
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
5 q* J+ n: ?& O2 o( ^! ^& Q# Z |0 V) s1 k' g+ W3 ?; Y
```
. r; g/ p* u) a: b& {& i; s $args = array(
" |3 f/ m7 M3 F& [0 y 'post_type' => 'site-wide-notices',+ C/ u9 ^: ^1 t
'posts_per_page' => 3,2 o( T7 a/ J) Q" L
'order' => 'DESC',* s% ^) p0 O0 L
'orderby' => 'date'
?; T3 b' _. T- K. B$ q U );# \; g& n$ f7 W' j; v$ H
$query = new WP_Query($args);# K$ Y- v+ j+ _5 n$ r7 @$ O3 @% ?2 _
if ($query->have_posts()) :
" v0 m% Y. V2 k# l$ t% X' K. b8 f$ b while ($query->have_posts()) : $query->the_post(); ?>
) D/ M( d) C9 u: ? <div class="notice">
2 X& S- Y" ^0 K& d <h3><?php the_title(); ?></h3>2 @6 s( S/ i/ i1 `. u
<div class="notice-content"><?php the_content(); ?></div>1 W4 }: d2 I( y
</div># M, Y3 Q* J1 t& @6 ]+ L* q6 }+ j
<?php endwhile;& D) _' P( D. q
wp_reset_postdata();# n$ t% ~6 J$ b
endif; V b& Q O/ D6 N: P \( w
```
3 k. ?- L3 e g+ @) i+ @& s
& {! n0 @8 I7 O. U' D1 f 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|