|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?: W& k6 D }' T* W; ~$ |5 o0 |/ l
( u6 l% n. X2 \$ I0 P0 c
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。: Q8 \* x @) M% U+ @
2 a0 A @ e5 v& g# @" x
以下是创建自定义插件的步骤:
9 _9 P) `7 x0 d$ P$ X0 X- n2 O& \# m: r3 x
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
4 P" h3 p+ I, @% K8 U. H2 [% o8 i
* c8 a" k7 E) l' r ```
1 @4 q( J1 E, O/ {! i8 ?8 [ <?php
) B- b1 W5 J. i8 O$ m /*8 R+ D: M- ]2 I- t' d
Plugin Name: Site Wide Notices Plugin/ k# v6 c: v# |
Description: Adds a new custom post type for site-wide notices.
) U8 b/ [) k/ ~" P6 I; Y( T Version: 1.0
% [8 F: C% Y& ~ L7 Q2 H* V3 q Author: Your Name
* x2 z! f. x7 P* C+ q+ O# |1 T Author URI: http://example.com; F( O6 {6 E3 s- _6 h7 Q9 n
*/
6 U; p) Z9 F* q1 g7 L& b- ?
( o& s) F: k# S- C5 ? // Add plugin code here..., X/ ^$ h2 Q7 v5 k5 S
```2 @+ C- D I; _
% x* f/ h, {) ~
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
1 G+ @/ {6 A+ w a) k8 z
4 P1 E' G/ e# I7 h% |1 l# E2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:% Z: y. P' U2 y" l: l" l- R
8 V6 J5 @# j) ] Z- E- s" `/ h ```
3 A2 }% C4 A' X; O8 @ add_action('init', 'create_custom_post_type');
2 U* ?! H0 z+ R1 n o! `) b& d function create_custom_post_type() {- T ^/ @- R; W! ]" {
$labels = array(
; h5 Y" g. D. E& t/ c' } 'name' => 'Site Wide Notices',! p1 h4 Y$ j, V" X3 F8 F1 N% p& `
'singular_name' => 'Site Wide Notice',0 T3 K% u7 B1 Y6 X0 q$ B" j
'add_new' => 'Add New',
* C2 O7 i( Y* x* K5 ]" W8 e 'add_new_item' => 'Add New Site Wide Notice',
5 j' |4 C' k7 @( l4 X V 'edit_item' => 'Edit Site Wide Notice',+ I3 g" C9 M5 l% w
'new_item' => 'New Site Wide Notice',
' X6 E# E3 e; F" I 'view_item' => 'View Site Wide Notice',3 M* S6 z/ L* M( ?% s
'search_items' => 'Search Site Wide Notices',
. H6 z, y4 G0 j0 M p$ y# g 'not_found' => 'No site-wide notices found',# ]# d( {0 L' I8 m0 I9 M! i
'not_found_in_trash' => 'No site-wide notices found in trash'# l) y) [: x/ `3 E% s0 U3 y
);+ E0 c% }8 f# J j- m
3 V% V, R" `, _5 b6 m" }) N9 ` $args = array(- Q. C! z- f- @) i K9 q+ X
'labels' => $labels,; {, k+ R7 c% H
'public' => true,
3 I4 j0 Y4 Y- v# g( H 'has_archive' => true,- d: V) H5 |! T5 y& K9 x) T8 ~0 Q
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
/ L9 e; W8 h5 _9 U! @6 A7 _ 'taxonomies' => array('category', 'post_tag'),% Y1 p$ F6 B& o% l I% E- s
'menu_icon' => 'dashicons-megaphone',
: A$ B: M& p" p$ }& Z( }$ y9 a# J 'menu_position' => 5,
" S. K9 `9 Q6 d+ M) Z 'rewrite' => array('slug' => 'site-wide-notices'), e( |( [8 a. z4 j
);
]3 X" L% C+ r% {7 [% m# L
8 D7 g) a7 w) e5 \ register_post_type('site-wide-notices', $args);. p% \! r% }1 Q5 I
}9 m: E% w" |: o
```8 b, I' }6 H. ^. Y4 {
& V; G6 ~6 h. D" _ 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。1 _' @/ _/ f0 i' y# f2 I
$ c+ S) c1 P* D& y
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:7 E0 O) S/ C) c! i0 l% l
! t# X9 q- a, a7 w; ^! p! ?7 V
```
3 V; c' A# c Y add_action('add_meta_boxes', 'add_site_wide_notices_boxes');" J" ~- f1 u/ j9 w
function add_site_wide_notices_boxes() {8 M9 E: J, S" e% Q; M( S: x& D4 F4 _! C
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
& n, g1 g1 { I% n2 s+ A( Y3 @; N }
# ~* m% Y- }+ G
1 W: ~; |6 F& g+ j4 T4 t function notice_details_meta_box($post) {
, t; ]3 i) k h; H wp_nonce_field(basename(__FILE__), 'notices_nonce');
# h* n& T% ?- I4 X4 @* ?8 y, ? $notice_title = get_post_meta($post->ID, 'notice_title', true);5 t% G! V1 ~2 d C
$notice_content = get_post_meta($post->ID, 'notice_content', true);5 D* ^& v# s4 s2 c( V
?>
" y3 ^+ A7 H9 o2 h5 N1 U, I, [ <p>
9 Y; Q$ C# E7 t; J <label for="notice-title">Notice Title</label><br>
& [/ {- q7 y; p/ h <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
* R' t: t6 _5 f3 x& H5 C E/ J </p>" Q: E3 m) _ ]+ x9 r/ ?9 v
<p># e" V; P5 U- p& U9 m
<label for="notice-content">Notice Content</label><br>
7 E' ^6 _# Z! |5 X0 g <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>; v) D& U/ V5 j
</p>
6 g) k( S% c$ O' B4 x <?php
/ {) V0 U/ f: \8 U4 Q' E1 f X5 s }
# t6 |' Z3 u5 b: x1 V8 V% S2 u0 P: Q" `, S) t
add_action('save_post', 'save_site_wide_notice_meta_box');
2 ^ F1 w+ p& v3 ~/ {0 b function save_site_wide_notice_meta_box($post_id) {
/ `8 w& [7 F' ?7 Z* C7 O/ A8 Y" | if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))) _, e2 D4 f- k7 j) ^
return;6 U& s5 L" t' E
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)( j5 V' G# c) W" `6 x z4 ^
return;
2 ]6 Q* b9 {# ~) o4 d. R. u+ ?) s
if (isset($_POST['notice_title'])) {
- Y5 ]& y1 s6 _1 L b update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
+ \5 x/ U, }, s, Z$ j! @ }
0 s3 U- D7 p9 O' t% v8 Y3 L2 H+ G if (isset($_POST['notice_content'])) {2 _6 w8 W4 i5 `+ U4 V& I2 I
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
# \/ c9 ~* w* U1 {, K9 V; y3 W0 o }
: c4 p$ s6 L# _# c# U }5 P. a8 {5 p2 e7 k
```/ C, a& A+ F. w. j& Y7 Y- C2 {
; ~$ ~" t3 _) e" V) L) t$ T7 T
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
) |, o* m- y' @4 U/ }$ t8 h; W) C/ V1 {" X
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:" v: [8 B. V% f% d; l }
. I; v' x4 G8 I9 X) Y- Q ```
! k& ?9 i- H& Q- v $args = array(
+ i- g9 a/ ^9 ~ 'post_type' => 'site-wide-notices',
/ A- s k2 V* v* w7 y( s V9 p 'posts_per_page' => 3,+ J5 R' g6 h2 H: E
'order' => 'DESC',, p9 o4 I; B& W+ a# c6 X
'orderby' => 'date'
9 K) ] B4 R: v! ?2 c );
- X7 l% w; Z7 M: H: Y; G $query = new WP_Query($args);+ N$ h/ V( Y! a. e6 A1 R0 y
if ($query->have_posts()) :5 Y( F: ^+ ~7 K- t3 E4 F; r
while ($query->have_posts()) : $query->the_post(); ?>9 N* H4 l8 y' d5 T6 k Q
<div class="notice">, J( K c* }& q9 N6 U: T) ~6 e9 Z
<h3><?php the_title(); ?></h3>
' b. G4 A3 X5 r <div class="notice-content"><?php the_content(); ?></div>
" ~4 i) f5 \; d8 [ </div>
, c7 |; l) z5 }7 H6 { <?php endwhile;9 Z0 ^! z3 q: b, @" ]
wp_reset_postdata();
: B* }$ T/ x- i' J% V. j endif;
" j+ y5 U/ A4 d ```
7 V0 N( j- Y- e% F8 O* s& m% l4 m
' T6 U# p4 @$ T0 s- D 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|