|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?; U) R% z/ a3 w' k/ \
+ [# Z" R+ c H+ R& T
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
( ~+ }% l! U0 o" U/ ]
4 P& _$ U1 `0 n- @7 @6 ~0 \' u以下是创建自定义插件的步骤:5 p. @6 `+ K+ i$ e# G0 y; Y% U
1 {8 N' s" M+ w6 O: N1 w1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如: H. U; b% G; Q& t8 |* b4 M g. C
! _0 O! C" M; k/ M$ ~ ```2 f( N, \" T8 ^: F, ^# `8 [
<?php* H0 ~& o+ W/ v; C
/*
. T) e% \- i3 Q/ M/ Q* V- N# X Plugin Name: Site Wide Notices Plugin
9 ]* m+ N; h9 S/ Y2 a( Z2 Y& | Description: Adds a new custom post type for site-wide notices., m, o6 r) g. n4 c- p( U+ I
Version: 1.0
% Y* c+ H/ d3 |; @' B u5 x5 q) j Author: Your Name, | V# o! j, g V
Author URI: http://example.com' j1 p9 D% m+ {0 y, p
*/
6 a1 L6 h4 J% r1 T' s9 ^/ O6 \
* K5 @ P; I! B" g6 i) E& t9 v# l // Add plugin code here...
2 @6 n/ J3 Y" K ```6 h3 D6 \3 M1 g9 ~# v# w
2 Z" E/ f% B- W5 `) w) { 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
5 F+ ?3 y& @% _- `5 V) ~! N0 o j) m$ o" F
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:) b. B* a) X" C1 j$ x3 c$ L/ n
+ R8 m" D2 j) ]9 k$ b ```
) t8 _# z+ o# b) K7 r add_action('init', 'create_custom_post_type');
: v6 |3 A& h" t- N1 j! }+ l function create_custom_post_type() { {0 \1 ^" v7 @! h {5 m2 a
$labels = array(5 f* N' _5 x& d# \9 T
'name' => 'Site Wide Notices',
+ b' P! A! \8 ?- R) w0 @ 'singular_name' => 'Site Wide Notice',
/ V7 ]5 A: F0 @0 Z, _5 u ^ 'add_new' => 'Add New',
" E7 P' y) o8 _6 F% @1 O 'add_new_item' => 'Add New Site Wide Notice',
* y& n9 q1 Z7 A% T 'edit_item' => 'Edit Site Wide Notice',
1 s$ w' a8 L& c/ g! z1 ` 'new_item' => 'New Site Wide Notice',
; X Q8 g( s" o# N; `( x 'view_item' => 'View Site Wide Notice',
$ I2 I; @5 V# v, R8 N& p# X6 {% n 'search_items' => 'Search Site Wide Notices',
" T5 y6 G; H5 F: y( v$ ] 'not_found' => 'No site-wide notices found',& J$ O2 D; q7 n5 ]) b& @
'not_found_in_trash' => 'No site-wide notices found in trash'
% o; ?9 g5 Y9 G# |; Y );( M( i% c. ]0 g3 C
% f ? T6 v: a z. b! D1 f" F
$args = array(' C2 \& g; z0 j6 b( r
'labels' => $labels,0 b' W, ~3 K0 j* d
'public' => true,
3 F, U0 N2 ?# q- r 'has_archive' => true,; X) `4 _1 J2 V4 s0 W( i. y) o
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),' O# v7 S: ^/ ^
'taxonomies' => array('category', 'post_tag'),6 W' L' J+ V" B
'menu_icon' => 'dashicons-megaphone',) i. S1 o8 q# X1 s8 L( p
'menu_position' => 5,/ o" d% P8 y* |& y
'rewrite' => array('slug' => 'site-wide-notices')
" T: |6 W# M$ e' t );' N& v. i, ~, F0 q3 w; J5 Q
/ f, d- v8 X( t! |4 A, I; r
register_post_type('site-wide-notices', $args);2 x: x0 L* x( e0 \& p$ Z" A
}
' S9 ~' A- b+ ~; d& n( U% b! A ```
5 O$ e$ n! ?2 j/ i4 u" N
2 }4 R1 R; P3 h2 G% ?# {' o) B 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
& g4 {" {/ E$ l8 Z% C
: x( }# Y( z1 R3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:/ f; [2 y' L* [( Z" X1 [
8 z+ o6 ^9 ^4 \/ J' f7 q ```
3 |" n0 q8 J; P. H# K$ w8 U. W. c add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
" D4 ` h O9 m- H( \' Z function add_site_wide_notices_boxes() {, s6 t7 k6 T# E
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
6 l: n6 I3 o$ u9 @4 U8 m! L% `& ~ }
) c9 R$ C n" A) p; F, f& A! V, Z. H5 U
function notice_details_meta_box($post) {& }, H6 H* h% O( M# J
wp_nonce_field(basename(__FILE__), 'notices_nonce');
! n" k/ C2 R( ` $notice_title = get_post_meta($post->ID, 'notice_title', true);
l% R+ ]; f3 i6 Q; b2 z7 ^' ? $notice_content = get_post_meta($post->ID, 'notice_content', true);+ v$ I5 ^' ]# M) G" c3 d* D
?>
6 A0 p" d( T/ }8 N8 Q/ Y( u <p>) x9 x* y3 w. R; H% X& o
<label for="notice-title">Notice Title</label><br>5 [' a& U/ a0 a7 A
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
2 p( O; X# G; y/ _' `0 ~& a9 @! t3 }% T </p>& P' p7 @1 X e! i' I3 }% d
<p>
* t/ [- l% F! c2 |+ m <label for="notice-content">Notice Content</label><br>/ [, j; Z" F" b2 l) a0 A) i: o* B
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>- S) G( t& D7 b6 y: `
</p>2 a) J# \* K: t; s6 ]7 y+ Q
<?php3 l6 P. L0 S. n* C G3 E) @2 Y
}1 j6 `0 s2 q. o2 I
4 o& K0 W3 y- i4 A1 m$ g8 x, ]- `
add_action('save_post', 'save_site_wide_notice_meta_box');* k$ F( K3 \# G7 P/ v9 C
function save_site_wide_notice_meta_box($post_id) {, s: I* i8 [! g( k+ |
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
5 p4 P9 A" z" I$ u% ?& H% Z9 [! ~ return;
9 a" ]* g M8 y2 E if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
" ~3 f, |( q7 Z9 Z5 C8 O& @ return;
' }& q. L* F: L
* w% u8 g6 t7 u4 D+ K- q, Z* \. ~ if (isset($_POST['notice_title'])) {
4 d/ M/ W7 Y) m update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));. R# y' K( j+ [
}
8 s% F( x. k4 O# d if (isset($_POST['notice_content'])) {8 J# [! E h2 {1 N2 L& ~/ y
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
/ b& z$ V4 V0 g- R }
0 z0 f# G; Y0 c2 D/ {5 x+ ~ }) F3 q) @% |3 z" h# U
```
+ l/ y0 p1 `/ ^1 J, w% T9 p: |
0 T% P5 x" w6 F/ {# N 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。+ I O. \9 P7 _8 C, d; N/ u0 |
8 n' g& h) D. N; I& V- N
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
0 \5 Z' T/ T5 k# z) K) L. G5 d
' T9 M! \3 \3 _ ```
, Q8 ]! C. x- R8 H! \5 d $args = array(
7 q" d, H/ V' P t 'post_type' => 'site-wide-notices',
J- q( T* E9 ?/ j4 P \ 'posts_per_page' => 3,+ M9 |* R6 H4 i$ t" n: |
'order' => 'DESC',
+ }& `: t( {' P* }+ {1 ? 'orderby' => 'date'0 c0 f2 ^3 h1 v# n
);5 r$ e: |$ A( s# w; f
$query = new WP_Query($args);% J4 k# e* B' Y# o) D
if ($query->have_posts()) :6 U) |9 P9 S- }( s
while ($query->have_posts()) : $query->the_post(); ?>5 B8 o& h( h" Z5 D5 p8 v
<div class="notice">
4 D+ e3 y/ t! J1 D2 V <h3><?php the_title(); ?></h3>
9 r! ]8 h8 t& U8 t L# Q) r" P <div class="notice-content"><?php the_content(); ?></div>
_4 p% T7 r2 \ </div>
( X5 Z, g# V- ~% |- J <?php endwhile;( t/ v6 k+ Y' u
wp_reset_postdata();% |) _) a2 j' h# d- Y7 y
endif;
3 ^ L2 n, ~/ Z' s, j# k ```+ [) c; y* ~. r. {3 v* d3 [6 z
8 ^5 U: {7 ~: B. N/ ]4 w) e) r
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|