|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?: t, S# M! _- F0 M9 y9 I
: s7 X$ k. k7 ]8 g$ J% @如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
}' i% w5 o2 F, x
0 b% \0 W: F: P `" b- z$ ]以下是创建自定义插件的步骤:
& W. m- S8 o3 |2 b2 {' B$ K* k: K# G+ a/ N( K3 [
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
5 q$ d6 j3 p8 u& m6 q0 O# A8 _' j6 G1 I0 v W
```
+ x8 o( c/ g( k2 L { <?php
/ N8 T' D/ }) B1 Q! F$ t9 e2 x /*
. e" Q# {4 `) n0 D. r# D. J; x. p Plugin Name: Site Wide Notices Plugin
q) E, y' M+ p$ ?; {, L3 g! ~4 R Description: Adds a new custom post type for site-wide notices. {: P# { ^+ k; J8 C6 ^
Version: 1.0! t$ G& e& V K* w4 O- \
Author: Your Name( i& p/ V+ z2 O% B' S$ E
Author URI: http://example.com
; r' t$ W# u |' L */
. f# n* l5 \2 y" o, n' a. i% R4 J) M; X. f* ~0 @
// Add plugin code here...
. \ ^( _! O6 K5 _ ```
1 o/ n5 p- E! J* j; F5 {
* ^$ Q) n. j$ ?( W: L' z0 V 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。) c; T& ]9 _; m4 X1 k
% K! q8 {# D g& m2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:, D+ f3 k @/ W% C g; \% `& M
" \, R3 I) B) I) x4 Y% b& | {5 h ```
1 q2 H, D: b9 I9 e+ d add_action('init', 'create_custom_post_type');$ I# f% Q* q& R; p) a5 W
function create_custom_post_type() {1 f8 ?- t, b* f
$labels = array(
( }: b: K# N4 S% B 'name' => 'Site Wide Notices',
. b* l, G- @/ A 'singular_name' => 'Site Wide Notice',
; G" e, z# E( s" I3 o. U! Z 'add_new' => 'Add New',
& C, o3 k# s7 d. e* M( B: M 'add_new_item' => 'Add New Site Wide Notice',- z" u+ ~( w: }; T
'edit_item' => 'Edit Site Wide Notice',
! L. \3 R# X: T5 q/ M 'new_item' => 'New Site Wide Notice',
; W5 z# k- }9 H" A0 K 'view_item' => 'View Site Wide Notice'," t; v; S1 w. M2 x4 J
'search_items' => 'Search Site Wide Notices',
$ s! Z3 T1 y: n! c) E! I 'not_found' => 'No site-wide notices found',( U; ]1 a$ ]3 q, B& ?! T7 s: m Q
'not_found_in_trash' => 'No site-wide notices found in trash'
: V) _/ x% k) x; V9 g# f% R+ ~6 u- C );- Q8 p2 s- ]1 ~& \! H t) S1 f
" P+ }# T' k7 ~0 \# P" U: ]
$args = array(
+ i- ?# _, ^7 X" Y2 Y4 ] 'labels' => $labels,
* P* X5 V: q' X! |3 N 'public' => true,+ N) l% V. H- w3 n
'has_archive' => true,( C3 A% R3 Z* l+ S* h6 E
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),; m- y. V0 I% y3 ?2 ~! l" |
'taxonomies' => array('category', 'post_tag'),5 z: A6 x4 E( d( d4 O1 g
'menu_icon' => 'dashicons-megaphone',0 m( o" X' ?7 V+ d# c, i7 K
'menu_position' => 5," ?# v! A$ U' A t, {3 l
'rewrite' => array('slug' => 'site-wide-notices')
6 \1 L$ Y# O) g9 T: n0 C( B1 X ); f2 H8 v$ p4 D- h0 K( q
2 V# E* ]" A U9 k1 w register_post_type('site-wide-notices', $args);
) i0 J3 Z8 x, h. a- L+ Q% f }) P- v" t9 _4 ]' X+ d J/ m' T0 I# s% v
```
$ k! R4 s: Q+ }- Z) @8 l" M# w! |
7 f& D- P( A! n/ l% D( q 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
v, O. [/ L C; {( f* U! g. w
8 X6 S6 ~5 s, W3 c3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:" K1 x6 t, N1 y8 ?' ^- w( t
7 M. ~( @/ }( T7 v# ^: z: m
```7 a8 N$ w% J( k$ S6 I0 Q
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
: e9 ~$ O) c9 `1 U7 G function add_site_wide_notices_boxes() {
6 W* {& C+ I3 k3 U5 A add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');" y: C& J; B& W: f
}4 W/ r6 C/ E1 C- P b' H
8 X6 d$ \$ X. S4 H i9 k$ B4 [* E function notice_details_meta_box($post) {- ~9 B' S/ r5 M! P9 j2 e
wp_nonce_field(basename(__FILE__), 'notices_nonce');6 P! N- o' F2 A3 a: k5 w
$notice_title = get_post_meta($post->ID, 'notice_title', true);
3 ~$ ~ \0 h) D/ L $notice_content = get_post_meta($post->ID, 'notice_content', true);
- E: O' K" W" s0 ^ ?>; u h% D; q0 ]2 t
<p>
3 q7 t9 g5 P4 p8 K/ \- N1 T <label for="notice-title">Notice Title</label><br>) Z8 T% B# A$ o; N& t+ }. q" z
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
" `3 t" @1 y! \, E </p>
5 B1 S: y4 z L2 X1 t: Q/ W <p>
9 b+ T' C+ x. ` <label for="notice-content">Notice Content</label><br>- w2 t; O' ]& v5 H+ j/ I
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>! V4 s4 W/ K; ~5 ~. f9 }5 s$ C3 c
</p># _- I( |: l( s( k$ R$ n
<?php
& v3 o9 R* F5 i1 ?0 s3 k; _ }* f( ]7 F# J; L; P9 E% ?
4 [2 C. I! l# P% w6 P1 S) ]
add_action('save_post', 'save_site_wide_notice_meta_box');; B2 T7 u |* ~* @: z8 x' S2 {% C+ d
function save_site_wide_notice_meta_box($post_id) {
6 Z x4 j& @$ V9 W1 _ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))4 r1 ]! w* [. W& F9 y
return;
9 G4 H: m" Z* q0 F if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE), H3 W5 t2 q& \5 M
return; I9 ?& ]3 z! i6 R6 y0 y
4 K- I/ M0 ?% w; @4 S9 b2 W if (isset($_POST['notice_title'])) {+ k' B( R+ s1 K4 A; o( J8 o
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));- V/ ~9 t; b+ h ?" ]" l# h
}$ P8 w/ ]: r1 x
if (isset($_POST['notice_content'])) {
9 Z X# i6 y T, E/ r8 ~+ C update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));% h4 z, D. {6 h- @! Y
}
; O7 c" }& J) F$ e) U6 Q }
! g' f) q7 r, N7 } ```
4 p+ u. B3 I; c) M# b* L8 T7 Y& l, @2 ^
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
* @& D1 X! \) d+ u4 C
# a5 i! u$ f+ K: f% V; b4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
3 t7 G& W! E$ \% U4 c0 ~
8 ~( G( k& @; t& H& T4 U5 d ```3 _1 h7 Y0 {% A# d% J
$args = array(8 Q( K4 @3 Z8 J; e2 \& u0 @
'post_type' => 'site-wide-notices',
- }) C" E! Z; C" B- q! N 'posts_per_page' => 3,. B7 q1 a: ~+ h" c
'order' => 'DESC',1 b0 Q8 \; o1 g0 B6 X( ]
'orderby' => 'date'3 x: k5 |, ^" y f, ^ ?! z
);' p: N) {. \3 H3 H n
$query = new WP_Query($args);
% P/ @4 X" t! S9 \2 | if ($query->have_posts()) :: |( ?" ~ G" H2 K7 \) G
while ($query->have_posts()) : $query->the_post(); ?>2 [: a% x. v; ]+ @
<div class="notice">- O$ x# I9 L2 H7 e0 F( C! h+ k
<h3><?php the_title(); ?></h3>
& w6 B$ S3 q/ _ <div class="notice-content"><?php the_content(); ?></div>4 F6 S3 T8 Z5 C& V. K9 D8 v, m( ?
</div>& J2 z: H6 s# _% w% ]
<?php endwhile;
( D" m4 Y4 v/ k& Z wp_reset_postdata();
o' u8 s3 Q t9 _, v endif;! R) B/ J. a+ l6 E
```2 x* g% j- E! s( x. x: N6 _ u+ b) T
6 u! e+ ^1 M; V; n
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|