|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?) c% W1 A1 ?0 S. g; \# g! y( ~+ P
+ F7 ? [$ |5 ^2 N
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
" X2 [" I a5 `9 Q k9 m
p' ~5 d. I( m9 }* Y% V以下是创建自定义插件的步骤:! r- p" P# f, }! N. V K
% @+ j+ C- P# u4 N) P4 y2 k1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:# b3 c K# W# O d( j% H& B: [
^ R& b) ^9 l, L4 ?/ l ```
- c0 Y& A& `2 i z+ B. Z <?php6 g/ M2 a" ]- N# f: |9 d: l
/*
8 Z$ b4 t2 X/ n6 \3 T! _ Plugin Name: Site Wide Notices Plugin% u! J. b1 D q! s
Description: Adds a new custom post type for site-wide notices.
; z! R* @; \& K0 y6 o% Z Version: 1.07 |& V# M, A! j) L6 T4 Z7 }
Author: Your Name
5 b* |+ A8 T( }& _9 p Author URI: http://example.com1 `: X: j; O B r
*/) j/ A2 l2 n9 k! r, J! | O9 e
V# v- X3 z( }0 a& S6 V7 s; { // Add plugin code here...
+ f" V, U) K/ F, F; z. ^7 K. s ```& @: W$ e" Q0 u. _
8 l! I# U" {' A4 K 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。7 w+ |: v1 b% D9 L# u" A$ C
: K" |0 Q j4 ^8 j2 Y; P# `2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
8 d3 }+ S# `: X6 T
& p' r/ ?; F3 R ```
4 ]6 x4 k# A2 ?- m add_action('init', 'create_custom_post_type');7 b. V$ J S" d/ Q8 ]0 Y* F, V) ^
function create_custom_post_type() {1 [. Q4 }1 C. `
$labels = array(
! [- K; _6 b4 I) }) R; \& z 'name' => 'Site Wide Notices',
3 B6 A; t0 e. v' W9 x- i2 {- w2 q 'singular_name' => 'Site Wide Notice',1 ^0 j, \4 k" \4 t8 |/ X+ D3 b
'add_new' => 'Add New',
6 T% L& }( h. \- g 'add_new_item' => 'Add New Site Wide Notice',2 f: P7 n' }, i9 {1 n$ X
'edit_item' => 'Edit Site Wide Notice',
2 ?. |0 l! g7 N4 s 'new_item' => 'New Site Wide Notice',
& D& o3 N1 n( G$ b2 r0 _1 ^: ? 'view_item' => 'View Site Wide Notice',4 y# E( e2 z. I
'search_items' => 'Search Site Wide Notices',. {& F. K8 }4 Y q0 D6 \
'not_found' => 'No site-wide notices found',
2 h1 A+ X( l9 \ 'not_found_in_trash' => 'No site-wide notices found in trash'
* n2 M5 F; {' D9 N, V ); u) b5 |0 b% w1 Y) e# @1 F$ l
% w# @7 y; k$ [2 \
$args = array(7 |( B& E2 w1 ^9 h; K6 e% e
'labels' => $labels,4 V# F9 x9 k) U% _: x/ U
'public' => true,
; t. o- V9 g5 u1 ] 'has_archive' => true," Q; n4 J: F W/ P
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
1 E& r3 z3 u9 H' z; M, k 'taxonomies' => array('category', 'post_tag'),8 C( `* j2 p. l+ C& w% h
'menu_icon' => 'dashicons-megaphone',
& ^8 Z6 Y8 T( p5 C# _4 h5 Y+ j 'menu_position' => 5,
8 U% R$ v. y' }4 O/ k 'rewrite' => array('slug' => 'site-wide-notices')
8 u' }$ o9 C7 H: B/ H0 @ );9 X( \0 j0 B2 G
' z7 U' Y) h: F' E- t( ]5 E register_post_type('site-wide-notices', $args);' I1 ]% G" r6 y$ Y7 b% c2 c) n
}4 d% c) N3 q! N3 G/ D" e( c1 K! t
```) J i+ S; r2 }; w
" m0 v! A7 ?- a& w1 c+ q
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。, b" n0 R: z3 D& D# U! h
" v0 X7 |6 h! E) l3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:: ?/ F" {9 d' D
# K" c" n1 m8 v( p; F2 X" j8 Y
```: K$ V j" ]2 i, @
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');% C; u& S& Q% t% f
function add_site_wide_notices_boxes() {
: l. k& ]" C4 t" u add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');4 U( h0 T, F! c' x
}6 ~- L; d+ i: }" g
2 z# t; k9 G- `( m function notice_details_meta_box($post) {. o7 ^2 m! Y8 p" x
wp_nonce_field(basename(__FILE__), 'notices_nonce');1 ^ o! ^5 H: d6 r( i
$notice_title = get_post_meta($post->ID, 'notice_title', true);
3 x3 D; H% o. P) |) Z8 m $notice_content = get_post_meta($post->ID, 'notice_content', true);
9 I; x2 O6 |- i ?> }& ^( A4 j& W: F! l4 ?
<p>- O4 w H5 j/ I$ v! a( F
<label for="notice-title">Notice Title</label><br>; E% T, m. A* i8 ]
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">' K$ [* I+ g- L
</p>
* l1 ~8 B% j' V) t; q <p>
& {+ W4 ?1 M9 Q. @: r5 C2 p <label for="notice-content">Notice Content</label><br>
/ g# \" ]7 Z8 u, U# n. @ <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?># @& \) a; s6 F! b# ~5 l1 U
</p>
/ V3 p/ R8 L2 a# C <?php
8 ^% U6 m5 ~+ x# }1 n. }: z% K }
8 n. R6 L6 \* _/ f* P5 m$ o. y* k4 |0 D N3 J& s
add_action('save_post', 'save_site_wide_notice_meta_box');* R$ u: d3 X' l/ k
function save_site_wide_notice_meta_box($post_id) {+ j5 i1 q2 R' Y( R8 b# J" w2 [* b
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))( Q9 _+ h. m9 n( m: m& Q% G4 U
return;
1 m) k' o. m+ n3 y6 z if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
9 ]6 Q" i5 t- W5 J' y! [ return;+ O) S$ i/ A0 R1 {! s
; W# K+ W# y U3 u6 [& N
if (isset($_POST['notice_title'])) {
' _0 C8 @" L9 a" ?5 _+ D update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
$ }6 x- V* V) z' J( G9 m }
$ b8 U( i$ r# s" d2 }, g9 y, p if (isset($_POST['notice_content'])) {3 f* d& J* n* z7 T
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));- A& W0 b8 K; Q, }0 J0 D
}9 R7 k& K# {4 F7 \* s
}
3 L% z: D" m% `. C, m* ]" V ```
" w8 I2 C3 y! W- K5 t; N
$ M7 w! e* D' M+ a 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
# R3 m5 l: g3 k2 I7 E; @
2 G" C% [4 b0 ^$ W {4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:2 ^2 y1 H2 s/ S- E J7 F1 D. x; U
0 ]. }9 u; k, e9 I9 _, v& }, F
```( X; P; X, f0 q0 D
$args = array(: S. Y: Z! y, M9 s
'post_type' => 'site-wide-notices',( Y. M8 E: A T- _. O0 z) m- z
'posts_per_page' => 3,
+ U1 }) ?* @' g" D Z 'order' => 'DESC',7 L) m% _8 V- k# V" r4 Y
'orderby' => 'date'. x$ z: S1 S: L7 C
);9 _2 Q# e) ~. _) p6 g
$query = new WP_Query($args);
9 Q' d- D" A1 r- K9 r5 ?' Q if ($query->have_posts()) :" E2 U0 ~: _% Q! N! E$ t
while ($query->have_posts()) : $query->the_post(); ?>
, I$ n* e- ?0 Q) k9 W( t' P' b <div class="notice">' F$ K6 {3 V- I9 X4 ^3 G
<h3><?php the_title(); ?></h3>; j; q+ I. u" N
<div class="notice-content"><?php the_content(); ?></div>
# f3 a+ W9 x* \2 @& A$ e </div>
/ L" [4 a4 G5 T( R) Y5 @ <?php endwhile;
5 `" i G5 s& M0 l, K' E$ E wp_reset_postdata();1 e+ r5 N: l+ Q" T
endif;6 q9 t/ `5 d/ U, ?4 J$ V! k7 Q
```5 z- P( r* y: e# i$ @* _: q2 R
3 J& `7 g9 ^5 O! U4 j: U/ o 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|