|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
8 `- J' ~# m/ W% b0 ~" t3 `" G' f4 i, m) p! r3 w& m
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。3 z' b' T k# {1 b, c) d8 c5 i* `
( O1 n4 H" R* t3 F
以下是创建自定义插件的步骤:2 o& Z2 E3 z, ]2 y* j
' W; g; X; x( l) R$ {. \1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:. R1 \3 ] L( ]9 Y, R
7 v6 P6 v- a1 ]& R8 w ```& f% k8 R( L8 w. M4 f# U' u
<?php# f0 f! q5 w" \3 A0 e
/*
9 n" A" i1 Z; a5 ?* E Plugin Name: Site Wide Notices Plugin! f$ U5 j; j% D: Z
Description: Adds a new custom post type for site-wide notices.
1 x: X9 s9 f7 z8 [8 d [ Version: 1.0* \7 z! Z5 P8 I9 [' t" Y E" S7 d
Author: Your Name
' ]2 y8 _* g0 i% s/ \ Author URI: http://example.com
1 Y4 W$ @! f. Y */8 E# R m) ]# X# }5 R+ ^
! _0 a( b+ d# S) M3 S // Add plugin code here..., T! ]9 L: e- [; A0 o; R
``` e2 |* R0 K& m; [
T' u4 |& G8 j: u5 m: B7 E
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。9 h2 n8 f) [9 ]3 d4 _
% R. S! l4 s) W2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:- A3 i2 {2 ~% J* n" j& g3 Z( c
- M; K2 @8 T, S- m2 U/ ^. \% T" @ ```9 C! f# ] k3 y7 A! V; h. O
add_action('init', 'create_custom_post_type');
1 B- m B2 K/ J9 \/ o0 S function create_custom_post_type() {7 j0 M( w; x x' _0 N
$labels = array(5 k! I1 _) }/ l) b/ ]- U
'name' => 'Site Wide Notices',
5 s! L: K& e; n% t1 Y0 b 'singular_name' => 'Site Wide Notice',$ y; ^3 b2 I0 z' d. z$ {. ~# e1 @
'add_new' => 'Add New',
6 r# Z8 n) x% A 'add_new_item' => 'Add New Site Wide Notice',7 w7 Y: Z8 o& t: x
'edit_item' => 'Edit Site Wide Notice',. z1 L+ ?# [0 _3 ~5 B4 B
'new_item' => 'New Site Wide Notice',1 s- l n: f1 M( b# ~# c
'view_item' => 'View Site Wide Notice', `; r3 w* B3 L. t% F
'search_items' => 'Search Site Wide Notices',
+ P2 r8 m s, ]7 a) l' c 'not_found' => 'No site-wide notices found',0 y: S" r" }4 \ G3 n
'not_found_in_trash' => 'No site-wide notices found in trash'* i6 I$ j! W7 A0 b
);
. Y# ]" a! V& ~0 M' P0 I* V
" k# K+ L, I: R $args = array(
' O2 Q+ m$ G# }/ ^. F 'labels' => $labels,9 x* ~# E& Q1 N' g) o( T
'public' => true,7 y" ?$ i0 ]9 u: W
'has_archive' => true,
, q# t! D# e9 m8 j, |/ G 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
! r! b6 k) ~. s5 ^+ K2 x 'taxonomies' => array('category', 'post_tag'),, G! W0 Z: C6 D: l. T/ j& j
'menu_icon' => 'dashicons-megaphone',
- Z) o: M$ ]7 a7 ] 'menu_position' => 5,
$ O5 I! c+ D! N) F' N 'rewrite' => array('slug' => 'site-wide-notices') c' U3 R9 K6 j
);
- z; }) G" W& H5 l4 C3 z' V( {
8 b- ]( g6 U" [. e6 F+ I register_post_type('site-wide-notices', $args);7 m/ V3 Z6 ]; K+ ], F, f) n$ Z
}
3 X- N" q1 o( T; D' i* L/ U: I) j5 Z! y ```
* F5 K$ p0 n% z! g' z+ T: S; } a! W
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。3 ?% u c _, P9 u# o3 k
0 J' \5 u# Q) t: n; B* X3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:( s4 T1 b. Z) s8 Q2 K2 ]8 \9 D+ L+ [
, V; E+ b( I$ w% {4 ^' D ```
2 V/ a2 M: W1 f- b5 D- @/ Z* C add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
1 X3 m3 f5 D) p) D( B$ p0 y/ [2 i function add_site_wide_notices_boxes() {, q1 d4 ]4 k8 h3 x' _
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');3 F7 X; g, e* _7 w9 Q8 ^
}& F; `. q9 b$ b+ v" P
9 y/ J0 E6 w! _# b0 I. k
function notice_details_meta_box($post) {
* @5 `5 \+ }/ u% c0 N wp_nonce_field(basename(__FILE__), 'notices_nonce');; ]# C3 j9 f; f6 D# ?; E
$notice_title = get_post_meta($post->ID, 'notice_title', true);
* l! p0 g* Y# e8 _$ D7 C" V- g $notice_content = get_post_meta($post->ID, 'notice_content', true);
1 p& a8 q( M& G/ @+ t" b ?>
5 a- f" C% b$ U9 w$ A, { <p>
4 x5 x( G5 N) x! h: f8 @5 @! n <label for="notice-title">Notice Title</label><br>
9 y0 Y3 |- T8 E <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">6 ^9 }. v( R2 ?8 F7 t: p' W
</p>4 B: M$ ?$ H1 h( g' t; l
<p>
# c i# N) S* R. v <label for="notice-content">Notice Content</label><br>! X4 G( Z2 T% |- w; M
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
$ e a# d' n! G" s# V7 R& @ </p>- D& K. h( _5 i
<?php* t% t5 d0 e. f' W1 E6 } d7 `
}$ x9 S! w7 k: e& q$ A) K" j
6 U2 q! D( n2 t) H add_action('save_post', 'save_site_wide_notice_meta_box');/ z) W. L4 m0 v. S* v& N. X/ S) }1 C
function save_site_wide_notice_meta_box($post_id) {, r2 q. ]$ z/ ~
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))) P$ q/ X$ G! t* }7 ~1 w8 L
return;
" A' `9 Z" M1 O0 q' u- Z- D if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)( F: t4 U! Z9 U$ Q& O' Q5 E7 H1 p3 |
return;1 u. [6 Z8 L H) p ^& f7 {. f
( p+ I; e1 }( Y& t' D
if (isset($_POST['notice_title'])) {9 Z+ G8 A+ ~1 \, l- i
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));+ H# X4 ?, M" }8 D F, R9 T, T8 E
}
7 J% Q& Y9 U+ D9 i; i5 g# o0 T if (isset($_POST['notice_content'])) {
3 v+ b0 e+ s6 @ update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));, J4 g! y1 @, F/ M6 J- J" \$ }# Q
}
" K9 P+ {6 i9 ]$ Y c+ ?2 l6 r) X }" E. c: f" Q- J/ B5 U3 ~7 O" ~
```
$ \' ^5 Q: D- E* z0 W3 ~
1 D2 o# h: y. B1 C 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。5 X1 M4 n2 {/ Y5 F
' O Y; @9 b' j9 d' d' c4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
) G2 H5 [6 I5 _( L1 M: S- J8 u' }
5 }. G! o* K2 @ ```+ j/ ]$ M- K B0 Q' E) z' S- {/ s
$args = array(
3 n: G8 `: Y/ k; p 'post_type' => 'site-wide-notices',9 g6 Y7 C4 ]7 `
'posts_per_page' => 3,, I) t& u* v6 Q" l* V! ]
'order' => 'DESC',4 ^5 f2 y3 ]6 I7 {' l4 H; m
'orderby' => 'date'
4 H2 n; M$ v4 i0 \4 ^- s2 _7 K+ P );- ^3 P0 j1 M* e% e" Y- A
$query = new WP_Query($args);
' t9 ^( ~/ i1 r6 a: g if ($query->have_posts()) :0 |- v8 n) [- P: V
while ($query->have_posts()) : $query->the_post(); ?>8 C9 F$ U& I" q4 W
<div class="notice">
$ [9 Z& Y9 K4 J9 b <h3><?php the_title(); ?></h3>
% U3 m2 T5 O8 B* {0 I8 c+ X' R <div class="notice-content"><?php the_content(); ?></div>
7 k: R& ?) P9 F" n; O$ } </div># u7 w$ H! b& t( |1 Q9 ?: B
<?php endwhile;
. z) I0 k }4 P- b wp_reset_postdata();
3 ~" S* ~$ I# k. s. C. R0 B& U endif;( t: n+ x# X! j
```
- m( Y1 J& w1 J
m% E0 w" {+ ^. L) ]4 c8 P o& x 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|