|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?/ Q) M- M0 D2 n' H
, R4 O7 H- C; S' _5 X# A" k如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
/ _0 H$ G/ Z4 q4 L# q. K7 y0 W4 ~0 v4 {/ `& h: J- h
以下是创建自定义插件的步骤:1 Q& ?/ p. J& @
& ^ T3 }# c( ~' G6 [5 E1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:: i; l% a0 A4 j
* h @) ^$ u; x7 w
```
1 c6 ]3 A1 m+ R <?php
2 D* j4 b2 {. F$ s, B, I0 z /*, X: l* S6 U% D9 C, J6 Z3 ^
Plugin Name: Site Wide Notices Plugin
. k+ L) k6 y( A" }; V/ w0 S- i4 O Description: Adds a new custom post type for site-wide notices. W7 B7 v' M7 i' ]
Version: 1.0: _) Q- d8 ^& x0 o( R* e
Author: Your Name
, D( a6 H1 Z8 \& O Author URI: http://example.com2 T# K6 ~! N6 h. O/ a" I0 x
*/, j7 f+ C% Z# h
4 N' d/ S6 S8 r9 c; j2 f6 \8 f& h // Add plugin code here...
5 n# d* [ I6 D0 ~8 }% R ```
9 t* Q2 d* X; ~* m4 X: j) D3 ~$ [% u" {$ A! k
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
- M$ E, Z6 i2 e) M/ q0 K, }# u1 j. V& B1 G
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:+ Z$ R1 u7 w$ |. ^$ [
' O+ r, v$ K5 s `0 ?
```5 Z: Y# ?) i( n0 l0 h+ M
add_action('init', 'create_custom_post_type');
& T7 G+ c2 D0 i9 B" D5 ^0 h function create_custom_post_type() {
1 z+ W6 c' u; L $labels = array(
, n0 m% s* T! M 'name' => 'Site Wide Notices',- W# T: t& D; u
'singular_name' => 'Site Wide Notice',' \8 N, _! y/ w1 [3 m- Y7 M
'add_new' => 'Add New',8 e. w$ K Q8 p+ R8 q
'add_new_item' => 'Add New Site Wide Notice',1 R8 h6 ]. b! }2 l+ p4 e) |
'edit_item' => 'Edit Site Wide Notice',5 [$ v2 j- E9 I
'new_item' => 'New Site Wide Notice',0 O1 a$ B) ] b5 S3 y) l: H
'view_item' => 'View Site Wide Notice',; h# n+ _0 P: _7 M
'search_items' => 'Search Site Wide Notices',1 |; D' |1 W/ Y3 q( `$ R+ v
'not_found' => 'No site-wide notices found',
2 V2 m2 M( ?7 D6 W. P 'not_found_in_trash' => 'No site-wide notices found in trash'
% J' P$ ~9 K9 s2 a, ]/ X. W5 h, T- X );. E2 Q/ a8 p t" G5 c s- ~
2 }5 q2 e3 A, _0 R3 c+ ? $args = array(
0 T( l' f! x% ]/ O! s: v3 |- I 'labels' => $labels,2 h+ m/ y- s @( J
'public' => true,
+ X/ M# l. Q+ ]. R5 c 'has_archive' => true,- S7 d: S+ Z! Y% S1 R
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
1 |& [6 l4 I2 ]( s, C 'taxonomies' => array('category', 'post_tag'),
/ |: b$ ?0 r, Z9 {5 O 'menu_icon' => 'dashicons-megaphone',
$ z9 e2 c0 c& ^+ A9 z% C3 ? 'menu_position' => 5,
a) k D2 z4 p7 D- t 'rewrite' => array('slug' => 'site-wide-notices')
* k9 ]* U6 ~9 X( z4 ~: ] );
3 }0 V5 q/ H* C! D8 n' N) k2 r, O
register_post_type('site-wide-notices', $args);4 K; W: g0 D- Y1 B
}
3 u& L& r3 _2 B s7 v ```
' a; i+ h, Y# j# T' a& o6 k, x2 `, o8 H7 B" G: |( [* M2 `& R
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。 h6 B' v' D1 W8 p
& g8 C$ c3 k" E. X/ J7 D
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:, z2 [9 A/ J ]. f
# z2 C3 p" f9 Y1 Q ```
0 z4 n3 \- l% _: Q add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
3 H+ j5 j; @4 R function add_site_wide_notices_boxes() {
5 S4 j, W6 u9 Q( q add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');1 I# O# t8 g- A7 H: g! v
}- r) ?) i) Y5 A- i
" Z/ A' p: {/ K# Y. Z& l$ d i function notice_details_meta_box($post) {
7 U: X; E7 y( I; m! u( S; p wp_nonce_field(basename(__FILE__), 'notices_nonce');# [* Y4 s3 [+ @$ i3 M
$notice_title = get_post_meta($post->ID, 'notice_title', true);
v& q& L9 G. m$ W5 O $notice_content = get_post_meta($post->ID, 'notice_content', true);
) f0 |3 {7 w% r1 D ?>1 }; f7 e a0 w/ h& F% M
<p>
, \! I2 o3 C( t u% C3 P2 a5 S <label for="notice-title">Notice Title</label><br>
) E" ]' B( B. s& V0 \! a <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">' I' ]6 L! e" W( g9 l
</p>
5 P* O+ G H9 P I, [ <p>
, I$ S0 M; ]1 S6 @$ ` <label for="notice-content">Notice Content</label><br>: x6 _$ ^9 F+ V0 E7 g3 p
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
( R0 H/ j7 E7 S6 V5 w </p>
; F: A1 @" z- t# T5 X <?php2 f$ \# X1 ]! @) n
}7 P4 l* r5 V# O8 B% C
/ ^0 z, G& _2 D) m+ W! D
add_action('save_post', 'save_site_wide_notice_meta_box');
/ I4 z, [) D5 I) p. J function save_site_wide_notice_meta_box($post_id) {8 \& w2 W# ]# {0 H
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))), `* z9 V: V( ]! E
return;" Y: r) D" E8 `0 [) Z- Y
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
4 ?8 E a7 h: h& ^6 h ~8 t5 s return;
9 |1 ` {, ?* t5 k8 z) Q5 h0 l+ E- i
if (isset($_POST['notice_title'])) {% M4 T# T- ~' H4 ?$ g) F
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));) P7 x4 K7 _# A
}
" ]9 ]2 J3 ?$ y0 s5 M8 X1 \8 C if (isset($_POST['notice_content'])) {
) |. x& u) ` x( u$ ~5 \! I update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
O* N8 q: z) C8 p+ } }! K! f, ^) x' F/ R
}
7 B# O2 Z1 B# J W1 \& } ```
6 D- t& J8 W9 R/ Y+ L9 \1 w
e& p. t+ ~( T4 B9 ?) C( @9 `$ H 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
2 v0 }7 m- G9 m9 x0 r2 o
7 d" x# { z' I: v2 J4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
, _! h ^( X, [8 z
% ]) z, b s& `2 P9 J- s% ~3 E ```+ V4 ~; g% n- D% ~; D) ]* |
$args = array(# h' i- E" N0 \5 @/ n
'post_type' => 'site-wide-notices',
' g8 h5 Y) O6 V9 F+ }; @& C 'posts_per_page' => 3,$ |; f+ a$ U9 k% R
'order' => 'DESC',: V+ D% y( D+ r3 ]/ n8 t
'orderby' => 'date'
- d! \+ _& ?6 P );
% q# I; w& q) Q; q4 J $query = new WP_Query($args);
8 x0 P: ]) v2 { t3 |' L if ($query->have_posts()) :& V& X. ?, H0 o5 G$ e* G: _( D/ [% [
while ($query->have_posts()) : $query->the_post(); ?>
+ E6 x7 A$ r% r( B# T& `! o3 g <div class="notice">
0 M( W2 a% g% Z7 i8 }. O <h3><?php the_title(); ?></h3>( x5 \. r4 R7 X/ m7 s
<div class="notice-content"><?php the_content(); ?></div>$ A, l6 |- v- j7 |1 W0 @
</div>" z+ Y; a& `) P) c, I( @
<?php endwhile;
/ |- j6 x, O# r5 Y wp_reset_postdata();; y1 a1 j1 |4 g3 v9 G3 P, ~) i
endif;
8 P! [5 W* `5 P/ f( c ```# H+ l' P v1 W1 R
* L" ]/ A7 z# Z3 j0 ~& u: z
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|