|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?. f( [! v' k0 i
$ l- z1 S. |9 u) H' G' [如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。* q& @% S" o- [1 G& q
8 r5 q1 d V$ O0 i: p
以下是创建自定义插件的步骤:' C- {3 z# P4 W# _- i! U! t
% `+ N' U e" h0 g1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:) i& e- B8 O. x! Y/ K
2 w5 |! N3 ~3 N0 W
```
; p& v1 W% _$ y/ Y% C- J <?php
( ^; {8 F* ?. |% f: ?7 { /*
' s% ]# M% T# ?& ^7 a5 w Plugin Name: Site Wide Notices Plugin+ c8 m. P5 [6 h$ o$ [5 U9 s
Description: Adds a new custom post type for site-wide notices.5 c+ w8 ]0 V5 g$ I+ C7 e3 s$ T
Version: 1.0
* N% z* C# X. y- i" w, Q: A8 h Author: Your Name! Z- d4 X+ q; G+ L( {! ^* E9 v
Author URI: http://example.com
. I, O- U3 u3 s! Q( N! w8 o( L0 s */
* \' S+ x& l( q
+ s) f& D6 Y) X // Add plugin code here...
- U* r/ n. Q0 l6 J a1 a2 K/ ^ ```. M2 j9 ]) r, _
& U r- t7 v1 q0 ?# R5 v
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
, H$ `( G- _' P5 G4 Q' u/ b' |* S, J1 H8 Y' P: L. r
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
5 J k: G/ O; Z0 B& R: o! o
- h9 w2 n% U' A0 C ```8 M6 f- g, y( s6 f8 `- u) U6 o% h; V
add_action('init', 'create_custom_post_type');
# Y/ n2 l; W( d) ?2 c6 F function create_custom_post_type() {
5 K5 j: V5 d; S# ]5 \9 X' k $labels = array(
2 V4 a, t) O2 r) x 'name' => 'Site Wide Notices',3 i$ w! K, s9 h d
'singular_name' => 'Site Wide Notice',# q& W7 v- Y4 s3 {7 n4 N: T5 W
'add_new' => 'Add New',' q0 b& E* \: A5 \% U
'add_new_item' => 'Add New Site Wide Notice',
1 b* R" P) W* H' i7 Z 'edit_item' => 'Edit Site Wide Notice',
3 x) a! _6 [* w) T' w 'new_item' => 'New Site Wide Notice',
* f m: ]2 I* P' t9 Y 'view_item' => 'View Site Wide Notice',5 l) K: m( N3 Q+ P" \( a, O
'search_items' => 'Search Site Wide Notices',
/ `7 a/ r1 i: L8 X" C9 s- ` 'not_found' => 'No site-wide notices found',
2 d0 V# \4 Y! x' O }/ @ 'not_found_in_trash' => 'No site-wide notices found in trash'7 i6 m. P" o( T% D) t! ]$ R( b
);4 }# b) J' \3 S& H: u X
# P M: Y& n: `) j2 I2 V' [
$args = array(
. i$ e' Y! Q4 n% x: p1 y( I( V 'labels' => $labels," W1 Y5 ^! |, {
'public' => true,/ r) |' }, h2 c. U( i) ^' K; q
'has_archive' => true,3 ^# l3 A6 t4 j: W; i
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),2 B* R; @2 d! ]% Y- n7 g
'taxonomies' => array('category', 'post_tag'),2 {( {) P& ^/ \2 @& X' z
'menu_icon' => 'dashicons-megaphone',
+ H) _" U* F. c: j) O" L 'menu_position' => 5,
2 I0 |* g0 X4 l# C 'rewrite' => array('slug' => 'site-wide-notices')
, X- Z( u) N$ }. [, \" R! j7 y- W+ o );% ?: L9 ^* r! T6 M* w6 C9 Y
, C4 ?2 b/ `5 o* A) X3 _3 q! p register_post_type('site-wide-notices', $args);
& X5 M9 ]2 \8 D# D }
$ [3 d, ?- ?7 b ```
! R) G! @. u. b' E% O
# a( z6 h: T4 C7 V8 Y1 o3 ]7 q% X 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
" }* S& F8 T8 N5 {
% }8 C( k2 g7 U4 X+ t* q1 A6 a3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
9 S; y, N1 ]% _9 M: K( _4 E& a. C: F5 z: E, v2 i. [, d
```
5 O& w% v8 s# b+ `! O add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
' U( t$ L- q% [; n: R: l4 _ function add_site_wide_notices_boxes() {
' C, d2 t# o! p5 ^! w3 n& ^) z add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
9 q# J! X* y3 V( w+ J) w }1 b% k$ a0 s8 ^- w
: }5 Q3 C% E0 ^. s, K% J function notice_details_meta_box($post) {% s; y# h/ W) f; ]# ?
wp_nonce_field(basename(__FILE__), 'notices_nonce');6 G: ~, A0 g1 `6 X: ~5 n
$notice_title = get_post_meta($post->ID, 'notice_title', true);" `4 }# E8 ?1 P! \& _8 d5 s
$notice_content = get_post_meta($post->ID, 'notice_content', true);
5 q/ O! |: x0 r: C0 I ?>
# a$ l, X8 ]. @/ H" y3 @& [ <p>4 W8 H3 Z$ M+ h0 C. v
<label for="notice-title">Notice Title</label><br>0 I3 W5 y' P! F8 s! `& s
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
7 A8 a: A! @6 q </p>. v) D6 }. S) W5 r! @: q! c: k
<p>% ^5 ~4 Q9 `9 W! v
<label for="notice-content">Notice Content</label><br>1 R( }6 o; f* n/ {& O
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
& q0 _$ e! V# [- g( k1 {1 c </p>
. \. Q- o8 V" H) L( F <?php4 F1 r) q4 a+ B% S# y% u& l
}# A' o5 I) r+ e4 V
9 k2 n/ l2 t( G* |( c- A8 S
add_action('save_post', 'save_site_wide_notice_meta_box');
5 B& L; v% T* P" Z! k function save_site_wide_notice_meta_box($post_id) {. ~/ O$ }' F8 |6 [
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
. Z2 b4 n, l/ g. n! F return;3 U" ~4 G" n2 V8 O
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
7 s' I' B) g0 Y# W0 y( m% `- u; j1 @+ } return;3 U8 |) j4 o4 Q% ^( Y$ C! X
" ]" d* z7 ^; m) c6 o4 Y
if (isset($_POST['notice_title'])) {
( p: K' o# U+ ~6 ~& r7 g' Z) ~ update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));- c" }' ^0 [3 o. l+ ]6 {6 G/ W3 U# @( ^) ~
}$ G7 @, `. M+ |, L" C/ R4 w
if (isset($_POST['notice_content'])) {
1 m( g" Y. A0 O* A update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));, R |) E1 H- U' T. V
}
; {4 ^9 w, }7 c9 N ^# f0 V }
( r$ @' @( l F/ z" b ```" W# g4 a5 v( l' c
( m W, F) @/ _ T" W0 ?
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。 v* M# f; y6 k Z
/ y9 q( ]' d& k; B3 f2 C6 L5 @4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
* c4 P4 y# R, f$ O) P/ y7 V- p) d, G7 f9 o( K
```- y7 `+ A4 L" ?: A
$args = array(1 u5 W9 q4 C3 O' k; u
'post_type' => 'site-wide-notices',' u% h# O" g; M5 N- u
'posts_per_page' => 3,
" Y+ \: f2 z* g l 'order' => 'DESC',4 ?2 @ v7 C" W; K$ F& L
'orderby' => 'date'
5 Y) ~! X/ Z, d' b );, V2 A- ]: j) e% c' E6 A$ B, ~/ s
$query = new WP_Query($args);
/ I* @6 Q* U% p" b) @' y- h1 ~ if ($query->have_posts()) :
/ l0 O7 k( L; h9 N3 D0 E: ~ while ($query->have_posts()) : $query->the_post(); ?>
# l- W$ l8 c* Q0 k& y5 t <div class="notice">
2 x4 Z L0 `; P; Q; q1 @ <h3><?php the_title(); ?></h3>) k9 T0 T2 S0 {7 @$ {) ?& a
<div class="notice-content"><?php the_content(); ?></div>
0 J3 m. T3 e$ l2 j </div>
+ r! t: L, z2 m( q <?php endwhile;7 ?9 y v0 I. X7 G- D% R! \
wp_reset_postdata();" f/ ]4 ~3 o- ]
endif;
1 L0 Z% V, d% H" V$ E, D ```, Y% _6 o' [7 T( C( R0 t
5 p4 |# J# M$ v, U, f, O
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|