|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
" h# g; O5 a, k0 j! c4 a# ?0 j# ~- Q8 x& Z, T
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
8 F9 L* k4 c% K9 P6 x, j! c& n
% I' s4 z0 E: M( D" v' g以下是创建自定义插件的步骤:
( f5 M0 U* U9 l/ h
6 d/ ?0 R, `$ n. S. { `, ?1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
. q4 y: S3 Z5 ]7 o% e3 c) {6 M7 D) B0 y ]. g& f6 ?. D
```
: V6 A0 u% w) b, E- V- y, M: Z <?php
/ B/ z) g7 k/ F4 p& k /*/ {3 B# R2 E3 c8 j0 |+ x
Plugin Name: Site Wide Notices Plugin
( `$ ]9 j% m. h2 g9 }& v% h Description: Adds a new custom post type for site-wide notices.
3 R, `, M) q5 w Version: 1.0
, ]/ i [ ~# f7 m# h, v Author: Your Name
5 W7 X. h1 \+ Y* k4 s; G V Author URI: http://example.com
4 V8 B4 A4 a: a( I' G8 P */
/ q7 P( s8 X& x+ u6 t5 r. s( G" _" e5 G. p
// Add plugin code here...
' @# e b" R1 Z. @0 e ```
; {6 k/ O4 j# O( d' r0 r
$ L/ k& G9 F, l' X D8 l 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
- S7 S4 s5 z* G+ ?8 _: T4 b( i/ I' {6 K
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
. {0 z ^4 n2 }6 }
; _- m( i: v2 C3 D9 s( ] ```
* D6 Q0 E( b5 a U add_action('init', 'create_custom_post_type');
7 t W2 G& F3 G. U function create_custom_post_type() {
c! M. _ o- w* P $labels = array(
4 z" N X$ @: u4 v1 i 'name' => 'Site Wide Notices',
: Y8 f$ v9 i) O$ O. a: c 'singular_name' => 'Site Wide Notice',0 a& V1 X0 t6 {" z5 o
'add_new' => 'Add New',6 M1 P2 d* q1 u# H, c) r* T
'add_new_item' => 'Add New Site Wide Notice'," r8 a4 n5 B& u7 k' d
'edit_item' => 'Edit Site Wide Notice',
& T! A# P( q/ }) f0 @ 'new_item' => 'New Site Wide Notice',5 ]" u& }& ~. e( h0 U
'view_item' => 'View Site Wide Notice',
! e/ e& \6 e" c. z 'search_items' => 'Search Site Wide Notices',- P2 K O* W! Y$ X2 v; N- _; x+ I- \
'not_found' => 'No site-wide notices found',* t. v6 P; B! _
'not_found_in_trash' => 'No site-wide notices found in trash'4 w+ ]+ n& p) E5 o7 ~) f% l
);
5 o& i% W7 l; r* o# j, l5 A
$ |. M. j2 S& @# [2 d $args = array(! H. V/ i+ C6 k/ P& A N/ G
'labels' => $labels,5 |4 P" t: E/ H; F
'public' => true,
' ?2 o, p- T0 `% g 'has_archive' => true,. S6 N: Q5 K" h0 \( m
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
7 s8 I0 K2 v) H" L& \5 L# d( R 'taxonomies' => array('category', 'post_tag'),
Q! h m* O+ y 'menu_icon' => 'dashicons-megaphone',3 }9 O! I7 I* E! n7 v! [
'menu_position' => 5,
- N% i; }; q; }9 w( B0 ^$ F( i7 H 'rewrite' => array('slug' => 'site-wide-notices')& q- v q9 r* y
);
' \4 u6 m u- W+ _6 `. ^7 y. I9 H1 @6 L7 W) A; M( }
register_post_type('site-wide-notices', $args);+ H I( O6 V" X" v0 U8 R
}+ u; S1 L. u/ F! F& T8 N! R' `
```- D A; x- M8 X% ]) B( a
( U5 e+ T. T" k& m
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
6 Y N1 U/ ]$ x2 {: l4 [* B$ ^! q- P1 D8 v9 X
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
2 m' p& q) |" U% V" h0 O" l0 W
, e/ T5 D* K. s ```( z A% Z; b, z* s! X D
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
3 B% X( z. W. I) w* H function add_site_wide_notices_boxes() {
& F' ^+ m/ K+ A: h2 \6 ` add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
, y d! D. P. q }
, j: B) _/ C& }* J2 M" h( @5 W+ a s2 k# m
function notice_details_meta_box($post) {/ T, ^# {4 o. e3 }
wp_nonce_field(basename(__FILE__), 'notices_nonce');
+ g: X. f1 ~8 P" ?9 ^1 L. k1 o( L $notice_title = get_post_meta($post->ID, 'notice_title', true);
v8 o. H+ s1 {- l; }. ^ z $notice_content = get_post_meta($post->ID, 'notice_content', true);; k$ H: y* m! h: p
?>7 |7 ~7 L2 A; H" y3 `& M
<p>
, @1 X8 r W. V6 R% e- [ <label for="notice-title">Notice Title</label><br>
+ Z, ]: b# G. f( z+ j* g <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">& y ~9 w! B4 ]: T# Q$ w. j# z
</p>
: C) T& ^% J3 A% Z. o <p># L$ n6 e; \; z9 |7 B
<label for="notice-content">Notice Content</label><br>
, p$ H2 o+ C- [. b8 f7 Z5 i <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>0 ]2 A& o, w8 a. X( R
</p>
& \& Z9 A: U) N D4 W$ i <?php1 o& J. c5 B N i; p
}
4 S7 q( ] }& ?1 ]- K0 f6 K+ `: J, i' P' N
add_action('save_post', 'save_site_wide_notice_meta_box'); V& F" D. o7 b" a. h/ z
function save_site_wide_notice_meta_box($post_id) {
& K! U7 [8 ?# ^$ C+ z4 S if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))0 N9 [' P2 t& Y9 ?# e- Q% a& ^
return;' E; C% R# b8 ]1 P
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)- q C- X, ]+ q! j
return;
! w0 C( h# ]) O: ~$ h/ ?5 y8 Y1 n8 J' F6 @; m2 t H# }4 x8 s) X
if (isset($_POST['notice_title'])) {3 Q" F7 G7 T+ W0 k U7 e3 ?9 |
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
% e: p( q7 n1 z$ g, i }
" y- H7 a) Q* a% c e0 s if (isset($_POST['notice_content'])) {! ^- Z: T: L- o! @) {- i
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
4 Q: G9 X( J& C% U' I* S }/ Z' a" s, C7 w4 s
}% m R% S: K; x; d2 y, P
```
+ D/ A K4 h% z, f
* N; @* U; h4 C( J' L- G% l 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。 u7 v3 j1 T+ p) I! ?8 j+ n
1 \) c# y0 d5 K- u1 g7 W# b2 e! c4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
% J4 S: V( m# L8 p4 ~/ R; [- O6 I
, g1 X# O# T$ L; o c" t- L ```9 ]4 C" D. b Z. z
$args = array(" P/ O5 {& Q8 e e7 W9 s7 v! T
'post_type' => 'site-wide-notices',
* g3 ~% G+ N; V$ ~* D8 M& w' ? 'posts_per_page' => 3,
3 W3 y/ _; D6 Z0 k 'order' => 'DESC',% g' Z) p: i8 O3 s. X$ `
'orderby' => 'date'2 f; ?3 d$ l; D
);9 a$ t8 u3 N4 M0 F3 x- \
$query = new WP_Query($args);" Z$ {. \; x6 w; A" C! H
if ($query->have_posts()) :
8 h/ X6 e+ O( D4 f while ($query->have_posts()) : $query->the_post(); ?>
. h" k3 ~, k4 u% C/ Z <div class="notice">
) s* n- H6 t' y- ~4 @ <h3><?php the_title(); ?></h3>2 q6 }/ j b- E3 [0 L& H
<div class="notice-content"><?php the_content(); ?></div>$ [* ? s# d$ E( u. n- ?6 n
</div>
; r1 U5 I! L3 h# |- C9 O <?php endwhile;3 r$ `, m6 W) @7 c6 W y
wp_reset_postdata();
% ?% Z4 n( f/ u$ C, o) K endif;+ c0 F s# K! _: ]' l
```
. F% W9 X. a+ y3 {4 [0 \7 }2 L$ A& Q% z$ y
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|