|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?0 i$ O1 x) q9 R1 Z9 p) Z
: K: {3 h2 {7 s/ g5 a9 _1 h
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。" e0 `+ A: u1 `2 g
, F$ ?# D; z* q( Z' g
以下是创建自定义插件的步骤:
1 l' u# O% R" i3 y1 L# H, F/ _. x' R+ N4 S9 H: h/ r( I" Y* n7 V% z9 j
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:9 i0 ]* Z6 \* r! k G
3 z3 N( f2 q" F; ]; Y* {
```8 @% I6 E2 i2 t3 Q) X% N9 }: C
<?php
- `; o# K. k4 F" l5 \. c; U3 r /*1 J$ ~4 n9 e8 ]% L+ @
Plugin Name: Site Wide Notices Plugin
4 j8 e0 t# a. j' }* W i2 ]# n ? Description: Adds a new custom post type for site-wide notices.
5 Z$ {3 }1 c5 I Version: 1.09 g9 g( g& b. U# ]% s5 a3 {% u H
Author: Your Name
, V7 n5 a) k* h/ b9 S( E Author URI: http://example.com
" k# @0 ^8 n% M */& T6 y' A y4 `7 r" M0 S
1 G. ], B# }3 k0 A+ z
// Add plugin code here...
' p8 N" q( h# I& q( R5 [/ n2 R9 G( q ```
& R: F2 Z) r4 s
0 m# b6 S- f+ J1 s5 `% \5 C 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
. }6 ]' P* W _
( [0 u9 U9 ?2 I. Z4 V) D. M2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
9 B/ I- x. z5 n" ]. v2 G. R$ t: g" L+ m' k- G/ k* C$ P h# I
```
+ P; A, t1 N4 P* c" c4 [* d: ? add_action('init', 'create_custom_post_type');
& _) ]- M8 Z5 H# X* b, U function create_custom_post_type() {
4 w3 U; |( | W3 p# _1 a $labels = array(6 T: j7 |/ D. s& `- j) z
'name' => 'Site Wide Notices',3 g; E; Q) o" | s
'singular_name' => 'Site Wide Notice',
" t. | s& { ^% L. R7 a$ A2 F 'add_new' => 'Add New',
g1 \, M- X; { 'add_new_item' => 'Add New Site Wide Notice',
, q/ {" n/ N; H6 V: Z8 B3 G 'edit_item' => 'Edit Site Wide Notice',
4 c" }$ M2 w& {- Q 'new_item' => 'New Site Wide Notice',
7 [+ J) F; c7 Q9 Z! i( R 'view_item' => 'View Site Wide Notice',
$ _' @, V; g6 y- x/ E7 X 'search_items' => 'Search Site Wide Notices',9 W: a" |4 K, C* d
'not_found' => 'No site-wide notices found',
6 t5 a; U. z4 H! R3 W5 X, D, C- T& ~ 'not_found_in_trash' => 'No site-wide notices found in trash'
) _: s8 u7 t4 u& E );4 N$ ]& G b) l: Q) i
/ x# k* z. D* I/ P
$args = array(0 v) }; W; h2 ?. g: M3 H
'labels' => $labels,
: \2 j: l4 m5 A 'public' => true,$ }$ D! j; d1 M- o
'has_archive' => true,% X1 @$ c' w9 ^9 T
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),0 V: k m0 k/ |: y) q) k
'taxonomies' => array('category', 'post_tag'),
8 J1 G! B- s. D0 f2 \0 [% v2 Y 'menu_icon' => 'dashicons-megaphone',
% t* T8 I5 ~ [; v( V4 q) }$ H 'menu_position' => 5,
9 B' F1 y) i: ?( s 'rewrite' => array('slug' => 'site-wide-notices')
7 H4 D% U/ B) J( `& f );5 O0 Q9 M. t6 }6 o% n W7 C' y5 j' r
' I, b# g& v4 ] D; f
register_post_type('site-wide-notices', $args);( v! Z: @& M( h
}& V% B3 f1 E8 u) { I% l0 y
```
4 K }2 O4 }% F) l; Y! l7 F! X+ q* }' s$ e5 p
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。2 I! d) e2 p' Z% a5 N- Q5 p0 e( P
+ I. J4 a1 X3 D0 O7 u1 p3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
2 {, z/ X. D3 A7 u J, x6 _. v+ q; M* w1 \" D
```
( h' q' r* z, E add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
# p, B* c' O c& k+ i, h( N function add_site_wide_notices_boxes() {4 f0 [. B4 }( a6 }* a8 A& u
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
. T; l6 Y' Z# n7 Z! q }
, ]# U# a' Q$ L
1 w S, S) @ E function notice_details_meta_box($post) {
/ y9 b; S+ w! \ wp_nonce_field(basename(__FILE__), 'notices_nonce');
5 ^% U4 @2 y1 X k! W2 X" N# U $notice_title = get_post_meta($post->ID, 'notice_title', true);, L) i' i+ l: L; S) q
$notice_content = get_post_meta($post->ID, 'notice_content', true);
& K8 H; e- V- c3 S6 l+ o3 i' W ?>3 q* s( \$ B2 ^. S1 c
<p>
4 E1 q1 {# H3 L) G <label for="notice-title">Notice Title</label><br>
: D3 F/ \7 `1 N! r" X8 X3 `. ^ <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">1 Y. A$ N8 Z i3 t: |3 J) r- J
</p>% S/ F# g d; |* v
<p>
/ f: ~0 f- r5 L' ^( |3 M. @4 ^ <label for="notice-content">Notice Content</label><br>: q8 a+ e, h: T* M
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>5 `# s, g" Z0 E& Q+ r4 T9 L' g5 r
</p>8 a$ R/ i9 E5 Q, B/ @0 l
<?php0 q- k" W& V/ ?+ e
}
4 ]$ j. e; N0 t% H' f& {9 F
4 k3 l- ~; u) ?6 Z add_action('save_post', 'save_site_wide_notice_meta_box');
9 G/ ?9 W1 `8 q# W9 S# H3 ^ function save_site_wide_notice_meta_box($post_id) {
! N1 L4 X; X; [8 L! k& K; }2 B if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
: K8 j7 y4 r& Q1 n: A6 Y* G return;
& }7 {/ w8 J- |0 s" t' k, w3 a, n7 Q if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
) C1 A" @2 T' n return;
% G: m6 i: k9 ~+ I; h0 v1 G7 k5 V$ Q/ w
if (isset($_POST['notice_title'])) {! R7 @! l- W- i& x0 ?" k
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));* G$ `; t) G; W. N/ Y6 u [
}
: E' N6 J, U% m5 d! K2 i+ M$ Z if (isset($_POST['notice_content'])) {
8 Y. u9 ~$ \. [$ r) u+ O& j1 ?/ \ update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));- Y1 U' `; C; g, V3 D
}) e+ W. Q! _6 I% f X& w8 [0 ]6 k
}2 X) J, L, S8 I
```
& A* t$ B5 E. ^$ M8 h
9 I) `" u+ i1 s/ u 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。! i. u0 [6 U+ M- x
4 z. z: Y& p' D% g* Y
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:& ]. u' A a4 A4 ?0 `# n; n, o
( x7 R8 l a# j* V
```
8 a9 r$ K# `9 g/ m $args = array(- d8 ^% i! y' m4 P& T4 ~
'post_type' => 'site-wide-notices',
2 V% ~8 d& U8 S) f 'posts_per_page' => 3,
8 w5 i6 t& y7 S p 'order' => 'DESC',4 q8 }$ W0 ~3 f% V" M
'orderby' => 'date'
" p" V7 y$ D- V; H );
; K3 b( h7 j* N' l9 E $query = new WP_Query($args);
6 M6 H7 n2 {, ~0 F if ($query->have_posts()) :
8 J1 x6 p, A' \' _/ |3 w: C while ($query->have_posts()) : $query->the_post(); ?>$ U I1 V; U2 h) _- \
<div class="notice">; F% J# t4 ?# o6 P
<h3><?php the_title(); ?></h3>" I6 S) A4 X- m L6 d; T
<div class="notice-content"><?php the_content(); ?></div>: o) _3 \% W2 T" X* V
</div>1 F, i5 ?9 j1 Q' F1 y7 H
<?php endwhile;9 f6 Q' R" W& _2 Z: f
wp_reset_postdata();
: C& i/ W6 Y% q" { endif;! L6 i7 i {3 {: l
```
Z, U# m+ P2 g% f- L5 H1 ?: k3 z& j( G$ p! x* D% a7 [
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|