|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?& I0 w6 r% [. Y/ l; K+ E( W7 ?
1 [4 S0 n. z: r# h7 e
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。9 r% a3 `3 {( u7 \2 ?
+ h a3 @! \2 C/ f7 `' W7 w
以下是创建自定义插件的步骤:
* g. J6 [$ q: @% {3 L( W8 q2 _, u6 U
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
% g4 w) U& P7 K7 u3 i9 b" {/ n
" K0 ~4 I- w, P ```# j1 o' `# | s# v3 ?
<?php* r7 }' \5 k5 T1 d
/*
9 Z" \2 x5 o7 ]/ [' Q Plugin Name: Site Wide Notices Plugin
5 z, d( D* |. ~& ~ J6 q Description: Adds a new custom post type for site-wide notices.
9 f& V' z2 F: k* S. x3 g5 D2 @ Version: 1.0
/ i4 i- }* r0 N/ j' w Author: Your Name4 @9 p+ f5 C+ J
Author URI: http://example.com( t0 `/ A, h( H( }
*/
! Z2 ^( O6 v3 n J% M9 u3 \- v* q7 u2 {
// Add plugin code here...
8 o, y! z( e: F6 g) ] ```
! `/ m L- H/ |- k' V# V- c+ c. x: \5 L' n {+ s& z4 c
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。) _9 F1 b, T, \- k. U
$ g8 E4 m; m7 R3 p, Q
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:9 F2 }- h; k( T1 O: {" r5 t% d- s
, D, t$ F* l: P. a; x+ K+ \0 {
```7 Y* q; `) w3 t' R8 q3 |, `
add_action('init', 'create_custom_post_type');
' S, z0 q7 L8 k% s" ^ function create_custom_post_type() {% H! h# o& h/ w! ]' p
$labels = array(6 L; { y; H" J: j/ x" o# R5 b6 D
'name' => 'Site Wide Notices',7 j3 s. w% T3 S$ P, S+ {6 G
'singular_name' => 'Site Wide Notice',# S7 O& T5 @' ^- b) h3 ?6 `- n
'add_new' => 'Add New',7 V f+ L u' }0 Y: a1 y
'add_new_item' => 'Add New Site Wide Notice',9 u% m7 I! P, j1 y3 j2 n
'edit_item' => 'Edit Site Wide Notice',. a7 H5 W4 Y4 R+ d% w0 R
'new_item' => 'New Site Wide Notice',
$ g5 i# b2 D/ L- l 'view_item' => 'View Site Wide Notice',1 \: f6 Z; w4 e$ g2 Q) p% r
'search_items' => 'Search Site Wide Notices',
% s5 O5 k! `' ^3 X4 z( n# ~ 'not_found' => 'No site-wide notices found',! K+ t! w/ [% J9 Y/ A k/ H6 v! O
'not_found_in_trash' => 'No site-wide notices found in trash'7 j* D; b; e4 o; _
);
1 F* z, c6 _9 j" C$ _7 g) g) T% Z4 c& m
$args = array(: }8 K7 S S7 j% k1 m6 X5 R
'labels' => $labels,
8 @* j( _- W; A% H: Z( K 'public' => true,1 B( f+ d( d3 R9 Q! J H
'has_archive' => true,# g I% M7 ]* e/ o( v1 ?* U
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions')," Z5 z* ]1 l' N( V' N# |4 n
'taxonomies' => array('category', 'post_tag'),
5 S, R: T. ~' J; O2 T8 X 'menu_icon' => 'dashicons-megaphone',
- l) g. w( f' p- k6 U: O1 G 'menu_position' => 5,
6 [0 @5 @) v4 m0 B- a; ]5 s8 F1 E' A3 K 'rewrite' => array('slug' => 'site-wide-notices')0 ], T* A W& T" K0 K
);9 X' Y, ] _4 X$ X# Z
) h$ k9 c; M- D p( o register_post_type('site-wide-notices', $args);9 m( Y2 A* [/ r% h+ Z
}
$ b) g4 s/ g1 j9 p ```0 P/ ^1 ?/ b$ W
- e% \4 ?8 m! X3 ?7 p$ O ^. g 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。% o1 S, h: K5 ^- g z* N0 }
8 ?3 O. {" j3 s, d
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
' r* y1 Z3 l0 e7 T* l# t k0 |# K% J2 o- P% a+ h% m
```& w* Y- V. Q4 v4 {3 T$ |! `- y
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');+ V0 K1 h2 w& b
function add_site_wide_notices_boxes() {
% v7 I4 I. o5 [+ ^+ C7 P add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
. Y8 L4 V4 p, B, p; c8 o: O% x }) s; u6 d8 z) X p3 f
/ f( ~" `: w3 G9 d
function notice_details_meta_box($post) {
9 I" ?; k" g7 ]2 q6 j ?0 y: N$ B wp_nonce_field(basename(__FILE__), 'notices_nonce');
5 W8 f6 `' ^* i* b3 J! V1 ] $notice_title = get_post_meta($post->ID, 'notice_title', true);* M# y, v. O9 m( Y
$notice_content = get_post_meta($post->ID, 'notice_content', true);
9 e U& q- n$ {7 G+ I0 i5 u ?>
3 K: _: ?- y h' e3 C+ j <p>% N# ?9 o/ B5 J8 {; F
<label for="notice-title">Notice Title</label><br>! y2 v+ _! J4 u$ ~3 g4 H
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
' A: X: s# R+ k4 s+ V </p>
3 C& N: T1 h" E4 @ <p>9 H% s- b. g& Y* s7 e
<label for="notice-content">Notice Content</label><br>5 p/ K' f. f3 M1 }5 B8 x" {
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
! G! v1 V) U- {! m4 j( B8 S </p>, Q4 Z, S* x! _- a+ @1 P4 f
<?php6 r v& h f5 ~$ L% l! c, e7 U7 ^
}% P& }% m# H) H$ z. w
* E3 [3 H/ r- n3 p f9 O2 L1 X9 F
add_action('save_post', 'save_site_wide_notice_meta_box');
/ T+ I5 a- Y, F: @4 ?+ T function save_site_wide_notice_meta_box($post_id) {
. s+ o6 l% ~6 ~$ t: t1 T) H3 O if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
6 `6 s5 q% H# z. p1 z" n return;+ g& v( W' k3 X& t7 S4 {8 U
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
! p7 j- j+ A& M8 K' c. D return;
( [# X$ |5 T3 v+ b# f2 A/ R
k+ s# p8 l% _) Z. G* V u$ A+ c0 ` if (isset($_POST['notice_title'])) {
L- |* L$ j4 N+ f4 w) s; @ update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
1 q' w: f5 V4 T" k: H* I }+ ?% ?2 j3 z& S& o' y6 o
if (isset($_POST['notice_content'])) {
/ W: H- C( A2 h) `) D, z4 W update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
* m; z Q2 D5 c9 _ o" t }
. b' t/ X, W' ?/ }2 e }) k9 t0 B. l, @
```
0 a& |4 q9 I' m" a) o# f3 B3 H( j3 W w4 E( @3 G& {
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
" O( c9 l/ [3 f' @" c
4 s+ |# x4 n) S: z$ S5 f/ m4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:/ W! D2 ?; s' d: E* m6 u
6 B0 `& A& n* y) f! n- A/ e, v) |
```2 F9 \- ?: m: v6 R
$args = array(
+ {1 V" k6 s1 [" E! i 'post_type' => 'site-wide-notices',
: V9 K! j6 ~# R 'posts_per_page' => 3,, s& b; ^+ |, @, K
'order' => 'DESC',
9 C$ s3 y' a- S* [6 x. B' b 'orderby' => 'date'2 n3 c: Z" o: A5 i8 I2 p
);2 M* M% U9 {" ]; N
$query = new WP_Query($args);
" a0 E: q4 u7 o$ y" ^+ s! z W: e' q if ($query->have_posts()) :, f: d! J" w+ u$ J* f0 O
while ($query->have_posts()) : $query->the_post(); ?>
( o; l& c: K1 w: `! W <div class="notice">: y+ ~5 |5 W* \5 J
<h3><?php the_title(); ?></h3>5 l6 p( P0 S5 b- F3 z4 ]& E
<div class="notice-content"><?php the_content(); ?></div>7 }/ O- ?" t7 n# _0 k
</div># C9 }8 v/ t4 H7 `3 O' j5 o
<?php endwhile;8 t' B# R [4 j. @# s6 O X/ R
wp_reset_postdata();
6 n2 i3 G/ v* U5 P/ d% r7 x endif;# b& X2 o. n( _6 e9 ~9 |' r: g3 K
```2 P0 p' H0 ^$ Y( j
3 }& k; @! E2 L$ ^# | 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|