|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?" ^) x7 m( N% W0 A: j+ U( c
- a, A: ^9 c0 c6 r如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。: `9 H% O0 S( f- [) L) j7 Q$ O
* P* z. A. g5 P& K
以下是创建自定义插件的步骤:! U1 T$ r0 l( }' U B
$ o, F# c' q+ K- P6 S4 ?, G
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
! `8 Z, `7 ?7 b C' T7 \! P% R" }. A
```( W9 q! `/ S2 `
<?php
4 c5 m# Y, x2 |! B( P. a( S /*
/ X" H' K% |; d; u( S2 _! ^: i Plugin Name: Site Wide Notices Plugin
2 `- d" E* \7 I0 V+ K6 B% ~; R Description: Adds a new custom post type for site-wide notices.
+ f, d" ^6 J7 O1 p4 ] Version: 1.0& r8 n1 \& j2 w7 s1 D
Author: Your Name
: E, e+ ]: X8 n& Z# M Author URI: http://example.com
: [2 S! {8 k; J+ L */1 j, m a, u: a' d
+ V# [" M" b( |" I% v
// Add plugin code here...
# e. a0 ?9 A: N1 ?4 Z, ]$ d ```- C6 x( T0 y5 c* w& G1 s1 V1 m
( X" J8 d8 z& `! W4 n( P, z5 s5 n
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
' h0 g! w# d7 A2 J7 Q) H* C- t4 f
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型: E) I! J% r" V- Q% f$ ]3 N2 N% ^
, R8 V2 O! _; q$ m2 h0 z ```" F) _' ^# n X) Z' @( v7 t
add_action('init', 'create_custom_post_type');: R# M/ V# O* [
function create_custom_post_type() { ~9 |0 t" M u% |$ h
$labels = array(# K: y: H6 d4 r9 R) d$ q2 k
'name' => 'Site Wide Notices'," l- F5 o X, y H, u; c G
'singular_name' => 'Site Wide Notice',6 p e3 h- k3 x7 x; C/ O
'add_new' => 'Add New',1 N" g- J! e: J5 {) E# |- M
'add_new_item' => 'Add New Site Wide Notice',
. ]) ^; R) D% E. }% V2 r 'edit_item' => 'Edit Site Wide Notice',! P3 _! l+ g2 d* x( p
'new_item' => 'New Site Wide Notice',% W: _& I0 `- x
'view_item' => 'View Site Wide Notice',
" ^% J. r) q8 G* m2 z$ c 'search_items' => 'Search Site Wide Notices',% Q# L1 x7 w( h3 b" x. c
'not_found' => 'No site-wide notices found',/ p3 v/ ]5 J, |
'not_found_in_trash' => 'No site-wide notices found in trash'
, p( x! P- Z) H- j9 f );
6 s( }* S( C" y" d" H6 e. \$ v; d, d& B
$args = array(5 Y0 D' ]/ x( p& @& ^4 b; ]
'labels' => $labels," [! T. k& X; n' ^2 o/ N( z! D
'public' => true,
1 o) P& I* ?2 J0 j 'has_archive' => true,7 A5 s k8 C0 R/ d @
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),7 ?0 c1 I& @! p% h
'taxonomies' => array('category', 'post_tag')," G9 R9 j8 j, s0 b3 l5 ~
'menu_icon' => 'dashicons-megaphone',
& b. M9 w. X s( ]' q8 R& P 'menu_position' => 5, d% \" e! s2 a0 S: Y
'rewrite' => array('slug' => 'site-wide-notices')
, P, B% g2 c# h, D( c1 _9 F; c );
! k2 ~6 s; F0 T9 d- _' @
: a# f+ f7 S# V7 U6 R register_post_type('site-wide-notices', $args);$ v5 E, m6 o; n0 `( |5 g6 o
}" q6 l* T/ i) M3 l; L2 U2 I6 a7 Z' O) A
```
0 J+ _2 X, _4 }1 x0 ]7 u9 R
5 W, k6 T5 v4 ]8 C2 Y" d 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。! ?2 S% `9 R" u; T3 N7 \) D5 T
m; M; w2 a2 R! |$ N3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
4 l1 j' Q$ x1 @! ^5 N" X1 L5 H6 l) m5 W0 V2 g
```# \9 i/ ?: L% i% E
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');8 u+ ^; P) @- v+ D6 T/ L' Y1 j
function add_site_wide_notices_boxes() {2 v" _# H7 F! t0 U7 O
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
5 x' S' l& c: n6 y }5 S- L$ X8 b2 Q. J- b
U4 j# l3 H! C$ f3 J4 y
function notice_details_meta_box($post) {* }8 i- J( `; u1 f& l: x, C% J7 b1 @0 @
wp_nonce_field(basename(__FILE__), 'notices_nonce');
4 D3 k6 E/ ^. u1 l# n2 W $notice_title = get_post_meta($post->ID, 'notice_title', true);
# d3 i8 [+ M" m& I% n9 a $notice_content = get_post_meta($post->ID, 'notice_content', true);
* a% h7 U0 W9 H ?>
% k' t A" m6 [& U <p>2 t! v4 h- \* Z. d. N% l& s: t
<label for="notice-title">Notice Title</label><br>
7 S, O! }8 E9 T: @- v( y9 A <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
, ?; U( ]. Z0 Y" G7 @ </p>& @1 Z& }% Q6 D o
<p>
1 K6 e( N" ?# p, o <label for="notice-content">Notice Content</label><br># E! @: q) o4 S; F/ ?; A
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
! H; I& N0 j( T5 y2 |' M# ` </p> j5 {+ y( u2 f# s6 D# U3 J
<?php7 N5 \( p6 f/ C* ]
}
' ^" O* s& ~ J+ P
! u, [$ M) d0 h add_action('save_post', 'save_site_wide_notice_meta_box');5 V% A) G m. n& m
function save_site_wide_notice_meta_box($post_id) {( B; k) I- R {2 e
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))! m- l* C' |: X R+ }- w }
return;
% |6 \! j9 `; X4 y- ^ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)5 r) x2 E$ v9 p+ t$ I( d3 ~/ z
return;6 y. h/ \ W9 D$ o% u
8 `7 L. O8 w2 V% |: N if (isset($_POST['notice_title'])) {
2 ?5 B. C) s T# M update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));+ U: k( S5 Y% K! }$ l
}
' F2 s1 w2 v3 z; w/ u* p if (isset($_POST['notice_content'])) {
4 h& Z9 V: h3 R) {5 ? update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));0 v+ i3 J( r2 T9 H3 x- g- b+ J
}
# k0 X2 j. l9 C2 n }: R1 d6 j' ?" X; `: B
```
0 y* a p* G2 o& N* B; c0 d$ g/ q
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。3 q$ M2 J" J1 G. O) @
9 | e t: c% m# A; c4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
+ V! d( @% |; D* _# b2 a& |& {3 H) y% M/ q( ~
```
" K% M& H p" O$ Q/ ^: | $args = array(% m% Y+ F5 a8 D$ z {5 W
'post_type' => 'site-wide-notices',: G) P/ _9 h3 E* d! A) {! f+ M
'posts_per_page' => 3,5 Q& g' B# y; i
'order' => 'DESC',
" |2 }# @8 m' @. n 'orderby' => 'date'
/ |+ G9 O% f+ x9 P: f6 N );7 O0 N2 @; y: Y7 w9 @
$query = new WP_Query($args);
' o. b$ |. j: D- \. X% E if ($query->have_posts()) :7 A4 |: s( o1 S# f- a5 h
while ($query->have_posts()) : $query->the_post(); ?>0 x$ Z! c( S& x. [9 n! `+ T: |
<div class="notice">
" I3 \! [! L9 \+ E! Q$ K" l <h3><?php the_title(); ?></h3>- E3 l) z8 Z6 w* G/ H# D D' Z# h9 g
<div class="notice-content"><?php the_content(); ?></div>; A( R6 C# H) c- {9 Z
</div>
; @1 r4 s4 i; ~. | <?php endwhile;& i" q3 V) V( d
wp_reset_postdata();
; s# y8 u: I1 v1 Q- X5 K" M endif;
8 g/ R: K! X* v5 {1 k7 t2 y) @6 s ```
h1 h+ f! M9 L- Y: P9 \
# k9 w+ K9 O6 r& B l6 d2 V 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|