|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?/ U y1 \3 f" c' _: U/ C" `; E; t
; J8 \0 ?9 M# {如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。; T: y. y/ m5 ?- R
' ]# }, }2 P+ `: w9 U8 q2 W/ \以下是创建自定义插件的步骤:
9 N6 N) l$ ^+ D9 C$ u; Z8 V/ A1 [8 }- s) ]7 ` B: ^/ k
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:* y! k& H. W }: x! g6 ?
* j5 l% X( W; C- n. R0 X ```
1 s7 M/ f4 V) E4 n <?php
0 ^7 [& T, X, ~" G& s /*( d( O; b2 b$ ]: `! c! P
Plugin Name: Site Wide Notices Plugin' L! d/ m! g( ^! X \" t$ p
Description: Adds a new custom post type for site-wide notices.
8 N2 G, V+ ~0 y4 ^9 K Version: 1.0/ ~' i% L( o! ^8 ]& Q/ }
Author: Your Name5 V$ T7 U1 ?7 O
Author URI: http://example.com4 z% w0 V1 q _, g' I% B: e4 S J0 ~
*/
5 X* f- v0 e6 c" b# v1 H$ N: m% J! e: X8 @: V* s7 S0 C/ A
// Add plugin code here...
$ x( F' V9 |1 l7 \. y/ Y, P7 _ ```
, y o! Q' e2 a( H& V3 s/ k" `. N2 ?5 A0 _7 P' |* R) n
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
: C, g r; I) N
% @8 w" L3 p2 O( j+ C& `# s0 @2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
; H+ n& u+ N- z4 p4 A4 a
( h4 A/ {. \# ?" ^3 t6 s H( N ```
8 u2 }) y# k0 J add_action('init', 'create_custom_post_type');' n. v9 l# }: e( a
function create_custom_post_type() {6 p6 y- x, d# _- z7 l- g: u$ [
$labels = array(
, h/ B3 i9 a* Y+ v$ d) \) @# w1 M 'name' => 'Site Wide Notices',5 U0 J. ^5 J6 r
'singular_name' => 'Site Wide Notice',
" D, [$ A! B) K |! N 'add_new' => 'Add New',$ [8 I3 R9 u; k, }6 W+ P; g
'add_new_item' => 'Add New Site Wide Notice',0 `$ q8 \* A- O8 ?2 h4 G; _
'edit_item' => 'Edit Site Wide Notice',. t4 M: z B. k# d5 z( w( a
'new_item' => 'New Site Wide Notice',- K. |/ b1 D9 g1 f! `
'view_item' => 'View Site Wide Notice',
: _6 Z; T# |. U) z6 M) u/ K 'search_items' => 'Search Site Wide Notices',
9 x1 b. n1 `8 C* g+ w1 a% I 'not_found' => 'No site-wide notices found',/ k. u, |2 b+ B `4 {. Y
'not_found_in_trash' => 'No site-wide notices found in trash'3 E6 p e1 E7 ~2 \7 k3 p( @. E
);5 r: v9 G# z6 p/ z; [( y$ m- I$ y
2 @# t$ Y. O h* H* Z $args = array(- \% n8 G% k4 e6 v; B
'labels' => $labels,
3 v+ b% ]1 S& p9 ^7 u 'public' => true,5 Q( t5 V. K* D- b7 m# N
'has_archive' => true,# H3 h+ l: f& Y) Z, U3 X5 o- Q: H
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),( }( k$ q5 M9 R6 @$ o
'taxonomies' => array('category', 'post_tag'),1 x: v* n% D+ [8 ?1 {
'menu_icon' => 'dashicons-megaphone',
4 H; n; V. m; q5 o- R) `- S. T0 Q, c; Y 'menu_position' => 5,! }1 P" J, K5 V4 Z' _7 g0 ]+ J' |. @
'rewrite' => array('slug' => 'site-wide-notices')
7 A+ B: O+ s! t8 ?5 D: ~ ); C% u9 G( ]5 j; d. w# o
/ q/ V0 L* z2 J$ ]& ?6 V register_post_type('site-wide-notices', $args);
) o5 m+ J) u6 A }
' T( l3 W: \4 F ```( [6 w- F3 c1 i4 l* y# z7 C
/ ~' I: B" p. `: a l
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。4 V. ? F$ B4 U5 Q0 w4 O: F9 d
$ B: c4 r, {; J' T9 r- U3 e3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如: z$ _" k( F, S5 O ~6 e" d6 N
' |7 I, P- ^ T" t' D$ X; h
```; @8 `, R7 J d0 o8 j! T! w
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
- a: |6 R: S3 E function add_site_wide_notices_boxes() {0 Y9 ~5 N0 q* z* D9 u' l
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
' u, K* `/ z8 t! `+ h+ e }
7 l5 y+ g! W6 v
7 o* b! a- S+ p' } o* Y function notice_details_meta_box($post) {, m- T. F+ s9 m( d
wp_nonce_field(basename(__FILE__), 'notices_nonce');
+ P t+ Q K q9 q- h1 L $notice_title = get_post_meta($post->ID, 'notice_title', true);: ? y" g5 q7 Z3 r3 J1 ~5 c
$notice_content = get_post_meta($post->ID, 'notice_content', true);5 ~2 W; ]0 A+ \
?>
% k' F* D) F, G' Q4 R <p>4 K5 ]# Q* [# [1 F! _2 t3 c+ ]
<label for="notice-title">Notice Title</label><br>
$ s: }$ h8 c$ G# M! H2 w% ` <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">6 n4 i- R4 T& R' [) a
</p>, o" G* L+ V1 ^
<p>
& n5 N) R" _% k" Q <label for="notice-content">Notice Content</label><br>5 T3 q5 X1 n- s5 z& r# e! e; E
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>; I2 j. b* r7 [, Y6 W
</p>2 S: |+ m, R, ~5 L5 ^6 u. s8 d
<?php
4 R! J' M/ n5 r- d' i. \# { }) I! @1 ?, P i4 t6 I7 ^2 h
4 z8 [- W, M& ?) S
add_action('save_post', 'save_site_wide_notice_meta_box');
4 @4 o2 ~9 ~) a' J function save_site_wide_notice_meta_box($post_id) {
/ W6 n+ M% V( L! i' q1 r6 K: z$ S if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))3 n. D; W! v3 i: t; V
return;
% n4 G2 G; W9 i1 ] if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)0 e/ R& ]* t8 x
return;' G9 O% q- g- ~* ?, Q& u
! J2 K4 |; s: V& |) |
if (isset($_POST['notice_title'])) {
9 G7 F( p5 O4 m, p. I6 B/ j update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
+ L! o) A2 `0 n! q }
. {+ s- R# L& J& F: D& B# m$ W; ] if (isset($_POST['notice_content'])) {( `( B4 U6 L. a
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));8 Z/ }# A6 f% L9 \6 U1 [
} Z+ A" Z5 p+ s) W
}
: a& i3 p1 t0 P ```
" s5 o- w6 ~: n1 d, J! h
/ a: [9 t* X- o' H 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。- t2 y; \% I3 x: a2 P
4 ?9 a4 [8 e7 D. a7 U) K7 W$ r
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
1 U$ Y } T3 [. ^1 {% H
2 {7 W% i) {! Z4 o- v ```. O: m/ {3 l8 v1 _5 F" l" E
$args = array(3 N4 ~" n/ Y& @% [ M
'post_type' => 'site-wide-notices',& l: h" S3 z" w: o( F7 E7 t; i
'posts_per_page' => 3,9 u: u9 v$ ]: L5 z; h: Y2 A! g
'order' => 'DESC'," z. R1 N: s# K- r$ z
'orderby' => 'date'$ V0 t0 i( L+ ?! I7 M( `; |
);2 A$ }( G7 ?, E" W8 q$ r
$query = new WP_Query($args);
. b& X& P- F' V if ($query->have_posts()) :
; I8 L8 U1 R$ |5 F$ H+ q while ($query->have_posts()) : $query->the_post(); ?>
% E$ l! T+ D. G$ T <div class="notice">
o0 k! j$ c6 J; P <h3><?php the_title(); ?></h3>
2 F! }) U; F! a) k" C( ] <div class="notice-content"><?php the_content(); ?></div>
$ U* O1 N; w2 H8 r% _% W' L </div>
6 }5 g1 z# g" B, k0 l: H5 }* N& H <?php endwhile;& O4 p M! t* O. K4 O2 l, @
wp_reset_postdata();8 m" L* U, `, K
endif;
! y1 B( u7 a$ T& P% c1 T J ```
, I6 a9 P) `7 u+ l( T) w2 s
* I/ N( Q- F. h$ P. C 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|