|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
, d( X* ]' o! S S3 q: i( ?3 z* o8 L u+ B! W0 b
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。3 l q) p( O) p2 C, q4 q
# H/ I% u" M! f2 `& J1 Q4 ~; `6 I
以下是创建自定义插件的步骤:
( o% I7 R. d J/ [3 ?( Z2 B
) K! Z s# ]& B1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
9 B h4 t" s8 r- m- b" b4 @+ i2 x$ ], p" _9 D) `0 U- k0 i3 `" Y( l
```5 [5 C/ ]3 b; s y
<?php; Y2 T8 m# P+ T7 d3 q
/*1 }% s7 [% [3 N2 |% u
Plugin Name: Site Wide Notices Plugin
: x! V" l) _0 b+ d- g Description: Adds a new custom post type for site-wide notices." b( V+ n) x' H2 u+ X
Version: 1.0" |% F" Y, g d0 n/ h
Author: Your Name
, K% K/ \) d; p6 Y. T Author URI: http://example.com
+ @* ~3 \0 T7 M */# \: N* v5 z9 k! S
+ b# J6 E4 ]# i* f+ m3 G! o$ j3 e" }( y // Add plugin code here.... s j# a: Z5 C' k, i
```2 \' R( x+ m4 ]' \) M5 k' Z
( G+ y+ J+ }6 g8 L9 O8 M! `, b" {- p 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。( y8 M) F) E( t- Y; j# F! x
& ^$ u5 R9 f" W6 r9 W( N2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
4 z- m7 u# X# u. x: d0 a
( g* `/ E" K L* m8 B# B* i ```3 y: s4 S; ?1 V; g
add_action('init', 'create_custom_post_type');8 Z. I Y+ X- y
function create_custom_post_type() {
" H, m7 | w( S6 m' I4 Y $labels = array(
1 F) f" o. T4 g- Z" _' J" y2 F6 u 'name' => 'Site Wide Notices',
5 H7 n* m9 [% m, p: _' \ 'singular_name' => 'Site Wide Notice',0 y/ v# W. i5 ?: I, t. M q
'add_new' => 'Add New',$ t- ~' u6 _1 N7 t' A4 h
'add_new_item' => 'Add New Site Wide Notice',
" |9 z0 U+ s( _9 u0 S! R2 [ 'edit_item' => 'Edit Site Wide Notice',6 R, {$ ^! P' j$ r( g1 X5 {6 |! g1 X
'new_item' => 'New Site Wide Notice',4 Y w# [7 H5 f, X* }" n
'view_item' => 'View Site Wide Notice',2 ?: ^( \; J; ~( I
'search_items' => 'Search Site Wide Notices',
K/ W* W1 o# o# i9 K3 ~. z 'not_found' => 'No site-wide notices found',, ?3 P* O4 Y! Q1 Z4 ^
'not_found_in_trash' => 'No site-wide notices found in trash'# Y1 q+ Q2 k" v* d
);
, P+ V( y! |8 u' E& |: r+ S8 e3 q, o
$args = array(
8 W+ e# e( \+ [- w+ `9 {: {0 d 'labels' => $labels,
. {4 R* Q! h2 r p4 i" O 'public' => true,
$ M, e" b/ `7 A: I; L! e 'has_archive' => true," w: ?6 v2 g: f4 M5 E
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
$ s8 X, d, ]& H% Q6 J 'taxonomies' => array('category', 'post_tag'),
: U) C! J9 n' k* e 'menu_icon' => 'dashicons-megaphone',1 N" p5 K/ d8 d3 q2 z% U5 g
'menu_position' => 5,% h9 ?$ l( o, p% H4 y% y4 s
'rewrite' => array('slug' => 'site-wide-notices')
$ N; c! K6 f' ^. L! ^ );
# ? N; D: x. ?% G9 s8 o% f$ W7 _ b1 k2 Y
register_post_type('site-wide-notices', $args);
. H* u! ?1 U a$ S7 N }
9 n1 @ A8 D7 `& ?* [7 E: ? ```$ N$ u. f( z$ }4 ?) y0 H
$ U2 [4 s( t& U2 a8 {+ Y9 z% H 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。1 z& {) S& [ b
* W# r) o6 X. G7 e6 |7 c
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
: P$ Y, `( W- k: ?7 u& I# H& s3 s& y) ?$ R
```) h; d% E4 w" Z# `- K
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
( x! d2 |4 ]4 ?8 U4 l function add_site_wide_notices_boxes() {0 i; w9 Y' W9 S) s+ h+ v. P1 d. x8 C
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
u0 O- L2 R3 e }
/ t1 e* X, j6 n) _! y# @: r4 N. D T$ E* I: a* f
function notice_details_meta_box($post) {/ N# Z: S5 w1 |. u4 D" s
wp_nonce_field(basename(__FILE__), 'notices_nonce');% ~' J' C, _) Z4 y5 I3 d
$notice_title = get_post_meta($post->ID, 'notice_title', true);
# `' j3 ]6 d; P- Q2 r9 b2 H# @2 { $notice_content = get_post_meta($post->ID, 'notice_content', true);; b$ `9 p* b% x6 O0 l
?>$ v" I' K# Z/ P! O6 R. d6 U# B
<p>
; ~/ x9 ?4 l! w/ p <label for="notice-title">Notice Title</label><br>
, S; {- i H6 D8 o1 @0 ?8 l+ Q <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">2 H% C4 c q3 T' l7 x) f
</p>
" e2 O& }0 X5 t6 I1 w! h2 e* v <p>
3 j, v1 Z. f# g; ]2 T% _ <label for="notice-content">Notice Content</label><br>6 Q! @$ E6 g5 i" c
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>$ |5 [. p0 `1 d, p; Z
</p>
, D5 D' d$ A3 d3 E' @0 ^ <?php
8 [0 o" y8 N O* S5 u }7 ]4 o t9 s" Q3 |+ C5 s* X
% W$ Q: S) A6 w: @; r) ~ j add_action('save_post', 'save_site_wide_notice_meta_box');
. q: a2 b( }+ ^4 o8 e) S: ^ function save_site_wide_notice_meta_box($post_id) {6 t: N% S, \7 J; y
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
- \# n) R. n* O2 S5 x! N return;
$ h7 @% O( h# `% R. r0 D if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
" w+ w" B, B$ ^' o/ w9 t return;
# M% H4 J& Z! S8 U9 ~6 U1 D! z* c8 @6 R9 I7 S+ t# a9 r3 M6 c# U! y7 h
if (isset($_POST['notice_title'])) {: f9 i- L$ W5 o1 F" T' A
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));4 ~$ S. ^: J5 F8 ?, O
}
- L# [/ o/ |: ?3 p! l# v if (isset($_POST['notice_content'])) {
. r9 v; C$ X6 u5 K- I* Z update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));3 v3 B0 I7 `+ z8 ^0 y9 t
} x1 o2 g3 k$ o4 g# W
}
- [# w, P* D2 _$ A* f ```
9 C3 M: W" ]) @* u4 e4 h; W3 e. j- k) f5 O7 C) _/ y
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。! D; @; O6 k4 e8 i1 K. \$ R
5 M# c& F% n0 o/ A
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
* G* S$ t1 a/ n6 B4 J' M. m, b# I
```2 z; v! j5 u" t7 W' h
$args = array(1 C) w/ S; V- E" O: Z) m9 j5 |
'post_type' => 'site-wide-notices',5 v. C7 r" c) q2 m; s" O9 u9 f' f2 P
'posts_per_page' => 3,
$ P2 f3 W+ Z! M* a( { 'order' => 'DESC',$ p! O& t4 r9 ]$ Y t5 l; h) G( j
'orderby' => 'date'
9 f8 u, |" Q; ]8 T& w );
8 J5 D- R& y& c: \. W1 K $query = new WP_Query($args);
, z5 f- ]9 e# | if ($query->have_posts()) :$ `9 j) e8 q( d `6 `, [& ?3 M
while ($query->have_posts()) : $query->the_post(); ?>- q5 A" t9 [. _. F+ [9 a, f) v
<div class="notice">6 c" L0 b# z4 ]$ Z
<h3><?php the_title(); ?></h3>
3 H: Y) j5 x0 l: u6 O <div class="notice-content"><?php the_content(); ?></div>" V" T2 w+ v6 b; |% K0 G# s( {
</div>
3 X0 m) M" K, n& w/ {/ M. Q <?php endwhile;
2 _6 s! R9 y+ H7 H, |5 L% I2 _( ? wp_reset_postdata();
' t- P# W! X4 `; k: z endif;3 _& t1 Z3 N4 \0 u8 W |" Y1 P5 S$ [
```
9 Y/ V7 b' N% J$ U* a4 ]4 [
6 M" ^# D- c& R 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|