|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?+ o b' y5 r5 `6 z3 u9 n, ~
5 p3 s8 v" ?( j, Y* p# ?如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。( o1 T( ^ t; v8 o+ d y; m
J" G2 e% b0 \& a( M* H1 ~! K) s
以下是创建自定义插件的步骤:
( w! g& J' n6 ]! n6 }- A4 a* [6 R9 r* D- s+ k8 } T
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:* `: t+ R+ l7 G1 D& e7 ^+ P
' ~4 ?. i c1 v0 ` ```& ~$ x: l( y- ?8 D
<?php
0 B3 k5 |$ _4 J' H1 @! K /*
, D6 K O& o8 P8 z# G: w# d k Plugin Name: Site Wide Notices Plugin
% [ V8 h# W& m2 t Description: Adds a new custom post type for site-wide notices.+ v1 |$ d( }2 F+ \/ f; L- U
Version: 1.0
$ x1 Y' d, R& B( w) I% b8 J( A Author: Your Name
5 b: f8 k$ p' ]. R Author URI: http://example.com
$ a2 {$ x5 d6 q8 H4 F( y& V: }, ] */7 C7 v) Z5 ~+ G9 ?/ W
* [# j) A" x- i i // Add plugin code here...
! A2 p, n1 J9 z) ^( k8 w' X! ?5 ` ```
* C. }' Z/ ~: |7 P P' }5 G0 x3 H1 p w+ _# h
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
7 m% k0 ~4 C# ^: B0 p6 p' M
' U* R% x, g6 y) r% ]6 |2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
; x+ Y, o9 `, b2 f. d
( a5 t4 B- v; b7 c. A) ~ ```
8 o6 }! k7 n+ J add_action('init', 'create_custom_post_type');
, j: k1 n; g4 r2 A9 M3 E0 A! e function create_custom_post_type() {, X9 O$ v: Y4 N' a$ Q& D
$labels = array(
$ z P( j: A5 ^+ l4 ^ 'name' => 'Site Wide Notices',
6 f3 E0 W" K! L K7 w2 ]1 k% K 'singular_name' => 'Site Wide Notice', r* f$ i6 c v
'add_new' => 'Add New',
1 z; ^, N9 r5 R3 `( ~: B4 b 'add_new_item' => 'Add New Site Wide Notice',
+ T8 V j- k! G- W 'edit_item' => 'Edit Site Wide Notice',
/ r& y: F; R' {( J7 N" F+ Q 'new_item' => 'New Site Wide Notice',
1 {4 w6 h7 Z6 R7 P7 z: n 'view_item' => 'View Site Wide Notice',# C. [' t# \% a9 Y
'search_items' => 'Search Site Wide Notices',
8 m8 `5 p/ n, t6 H 'not_found' => 'No site-wide notices found',
4 J1 k: E* g# ^) X- A& e: j 'not_found_in_trash' => 'No site-wide notices found in trash'9 l* o9 [& A/ v! B5 \
);. E! T7 \. n: J3 U
( O/ k- B* c1 b $args = array(5 x% Z! S' D* N6 N0 m3 A+ j
'labels' => $labels,
: {8 Q6 H; X% K4 k4 m! G% f 'public' => true,1 H5 z* k! y0 z" e5 g7 l* k
'has_archive' => true,
/ J) {' s0 o/ q' z @ 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),6 m: Q" o1 `4 }; E: v/ z* k
'taxonomies' => array('category', 'post_tag')," G0 Z' m# `- Y
'menu_icon' => 'dashicons-megaphone',
8 a9 d' Q" j0 g# ? 'menu_position' => 5,% y: J" y/ E2 x
'rewrite' => array('slug' => 'site-wide-notices')# c; X7 r* h; ?* S( s4 m
);" |8 s& o3 y2 W2 p2 H7 K
+ [& Y3 p& ` }% f: k2 x register_post_type('site-wide-notices', $args);! ]5 i( E) i) N/ S' c
}7 ?% i: s. k" ]' {, [& g1 r& A
```
' ]# R9 _9 u4 X
2 O% ]; c" Y) m2 D 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。$ ]8 V4 p% x0 y; \9 K
B" [, S* f- \: P0 P3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
- E" B$ e8 p! Q. p% B
V7 S6 k2 C+ F9 M, Y2 U/ L! e ```0 L, T% ]2 M4 g, b) t; Q# U
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');/ T9 q2 l J) Y5 i; V
function add_site_wide_notices_boxes() {" N; K9 X2 Y @; h1 Q3 e+ E% _8 z
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
& n9 a7 T9 F1 H- B; z' G/ s }2 s# A b' H0 {7 Z
# ~8 d) R8 A* t: `: A$ j1 ^/ w function notice_details_meta_box($post) {3 p* f/ ^* \. u C( o" A
wp_nonce_field(basename(__FILE__), 'notices_nonce');5 w; m3 ?7 f+ F5 Y3 c; n2 P
$notice_title = get_post_meta($post->ID, 'notice_title', true);- i" k, k. e* _# p* @* o
$notice_content = get_post_meta($post->ID, 'notice_content', true);; w" _3 r4 x- b4 M
?>% ~' M2 @# Z8 E/ U9 p; I, g( _% K
<p>/ f: ?$ R. r5 F M; W2 ~
<label for="notice-title">Notice Title</label><br>
5 Q9 @7 }! F' }7 @+ m <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
$ g$ b! ^+ V3 ~' N+ W" Y </p>5 |" }. n8 y3 H& p9 U
<p>
0 C, e c, S$ ^ <label for="notice-content">Notice Content</label><br>7 Q$ h) H5 M5 r( O/ h ? Y
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
( F% }$ q, k4 m/ @ </p>
* @" V6 e7 g" k! k4 A <?php
1 g+ O" M( _" | |% z, b: n }$ r6 }8 Z2 d$ d, n. b
0 Y$ `4 ^2 s# B; n$ G
add_action('save_post', 'save_site_wide_notice_meta_box');4 T+ ~; j0 m# w$ Y
function save_site_wide_notice_meta_box($post_id) {
3 L3 K( `0 P# A if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
+ P6 \& S; `$ G& p& a return;7 n- u! O% s/ { v
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE). s) Y" G- b5 s: D9 A8 t9 r6 X7 Y/ g- q5 n
return;7 i# O- ^" Z c; |7 r$ K# d% s% S
+ T l/ N2 u4 K# q q' F
if (isset($_POST['notice_title'])) {
* {/ K# s! v5 [3 G5 y/ K8 a update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));/ F5 t- |9 ^' |6 N
}
' l5 l D7 g' d1 J* K if (isset($_POST['notice_content'])) {
7 v8 J7 S' Z# s3 t1 D update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));1 F9 Y1 a$ F/ N6 l+ m
}; X# T/ T/ ?( ^2 y L5 H1 e
}
9 g; _ m8 _( C8 h ```
4 j- V* }5 K8 i! V! P- L+ L7 W( ^, v- ^$ x0 S- Q
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。4 b. L% t4 t) B+ T$ c; |% V
X, c+ _* s# e# ^ v' v) o; }4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
5 V/ N; u( Z" ?$ y
/ [5 K& h) R2 D& a% Q6 [ ```
) h+ t4 U8 B0 u2 v) V $args = array(
% ~3 x% f( |- o 'post_type' => 'site-wide-notices',
/ v/ X7 D8 R6 ^1 \4 k( c$ [ 'posts_per_page' => 3,
% z8 n5 P5 h9 Q; K$ b) { 'order' => 'DESC',2 l& j/ e( i* D
'orderby' => 'date'3 }. w a% d( H) M; @+ K8 S
);6 ^) B: F6 [* i; O
$query = new WP_Query($args);" N( d- H" d5 G1 I
if ($query->have_posts()) :) u& {1 d2 Y2 S
while ($query->have_posts()) : $query->the_post(); ?>
, z& g# i! ]* Q% o) g# T& [ <div class="notice"> s8 N. x, f$ z/ |
<h3><?php the_title(); ?></h3>
+ c# e: x* i6 p <div class="notice-content"><?php the_content(); ?></div>
1 u6 p0 H% T1 t, r% ?1 Y </div>1 N! u4 X& [" C# H
<?php endwhile;
% S6 r+ z$ G1 a) ] wp_reset_postdata();
8 W- d% P6 H/ g3 A) s y endif;3 @# G% A7 Y( Q( F8 Y4 l5 S: v V
```- {) t. W5 y' o
( V& X. O9 U; a( x6 C/ I/ C" l( c 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|