|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?1 K( w9 P" Y: v" g O9 i. u/ f
) z1 K( b5 S' J" o如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
+ V& w1 W* @+ _" [0 Q9 _
; b* R4 g1 _7 J5 }+ D以下是创建自定义插件的步骤:; H2 J3 I3 p$ y0 ~/ K1 V7 B) \, c% P$ J
! c, s" ]4 m* h. N8 t. C1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
: r: S! z$ o8 X; ^
8 u: n6 }4 v* G y9 k6 o" Z2 ] ```
7 I& v! |2 N- V0 A5 H5 a9 t <?php' i5 V5 y0 ~' u u
/*
3 F9 Z: a! O# Y& k; U6 J) B Plugin Name: Site Wide Notices Plugin
2 {6 ~* x2 }2 {3 i Description: Adds a new custom post type for site-wide notices.
( i' G' G* O: d" g1 { Version: 1.0 P7 E1 B( @9 y" E" n6 h8 |
Author: Your Name, t" B, c1 W: b
Author URI: http://example.com2 ]$ r; c8 Q! H& B8 R) O
*/
( t, \* D$ g/ k" Y3 r4 q2 t v2 l7 l
$ U4 Q- @% z8 V+ s( ~ // Add plugin code here...
1 X: \ _0 \1 r$ Z) ^ ```
# Q6 P% g" P! U4 g6 I4 D+ o* f2 U/ z6 m2 @+ i% y1 _ z' x' f
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
' ^6 z; q- s z) y3 x/ }. @: b
9 Q2 Q9 R& e3 }8 D+ M3 v5 ]1 D2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:' R* K4 s1 b/ m, g) y
I& z! q) K X% t7 q ```+ r3 ~! v+ I, |1 Z1 `
add_action('init', 'create_custom_post_type');, Y( n4 k8 {' S5 {& l# X
function create_custom_post_type() {
# C5 G; c5 g: Y8 M% S $labels = array(
& E- \5 f" Z! o# M, ^ 'name' => 'Site Wide Notices',
( W7 D% u: \' F6 ~ Q/ `9 y- q 'singular_name' => 'Site Wide Notice', ]; A( `0 K. b% [# e: N( F' b& e% J+ p
'add_new' => 'Add New', D9 y- d, o& b' N! r k; `
'add_new_item' => 'Add New Site Wide Notice',- ~) b2 w: H/ V+ \' G% B7 Z' F! I5 F
'edit_item' => 'Edit Site Wide Notice',
. b/ Z- y4 J/ f 'new_item' => 'New Site Wide Notice',
- w* V% O1 l- j O( @ 'view_item' => 'View Site Wide Notice',8 l; E. H7 Q1 ^% E) Y/ U! p! C- x
'search_items' => 'Search Site Wide Notices',
# Q& r e6 L1 m. i, r: a4 |3 ] 'not_found' => 'No site-wide notices found',
; a8 N0 _2 d& Q z7 C& l- a4 P 'not_found_in_trash' => 'No site-wide notices found in trash'
- i8 q' v2 p5 L );) Z: }, n; q2 X
8 C( x1 Z. [% a' ` $args = array(
2 E) E9 |1 ]# w 'labels' => $labels,
. V8 r1 P3 R) y3 Y* F- p( L 'public' => true,4 c- I2 Z. F2 q8 I7 \& E3 i
'has_archive' => true,
4 g3 G, e' p5 l3 e2 E+ x, B 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
* L! D! K' o) A3 u7 P2 }: G 'taxonomies' => array('category', 'post_tag'),* N, _1 h0 [6 O. g$ A- o# k0 O
'menu_icon' => 'dashicons-megaphone',
% G8 d2 e" I9 b2 w6 g 'menu_position' => 5,
7 R9 i. L, w! g o" F 'rewrite' => array('slug' => 'site-wide-notices')
: n9 b6 H$ O+ l) v8 r );. y1 e* `/ [+ m4 _1 _
" K$ T% S( k6 R
register_post_type('site-wide-notices', $args);$ l1 `7 G2 u7 a% q* n$ K8 D
}
$ L- A$ ~8 w' Z4 W ```
, h7 \ j7 C$ N* u# @6 \0 C2 i5 {0 J: f- B
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。' `. H3 k1 j2 z4 h" V. W
" F4 m* l& W# q1 H2 S3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
' w t! q' O; A i$ Z
3 {( Y( H0 b/ y, Y5 | ```% {7 y( {+ Z9 L5 c% O/ _7 n
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');8 A+ ^% |( l! b& s& e# M
function add_site_wide_notices_boxes() {
( r' E/ d4 l! F: r" v0 U+ `) P add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
) [) W2 u8 I: q) _& H! m } p. ~. i$ i' Q ^5 F
7 j) Q2 s" x' B9 t function notice_details_meta_box($post) {
" g1 r9 r1 p% @! @1 x wp_nonce_field(basename(__FILE__), 'notices_nonce');0 }8 L$ u. i+ o; M% z' a
$notice_title = get_post_meta($post->ID, 'notice_title', true);, Y& Q1 l) ^$ q5 p; {. U
$notice_content = get_post_meta($post->ID, 'notice_content', true);
! \+ B$ U1 ?! }- `9 Y" y* c ?>/ u# R4 N# ?# S, _0 o
<p>
! {. Z8 q' @% d+ U, n <label for="notice-title">Notice Title</label><br>
+ X% L6 O- J+ W5 r3 [( Y4 r. z <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">: E( l: G9 o' M! c
</p>
9 } s6 T# P/ R2 i) e3 p <p>
! l2 J& B/ p. f# i2 s& U3 k+ s } <label for="notice-content">Notice Content</label><br>! Q8 x6 k/ A9 Z, @& m
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>1 V, Y. K( V6 v" d# m4 p
</p>8 x. q, P; J: h* T1 ^" s) ~
<?php: H7 D. d, o8 w
}
4 ?6 Y5 d3 C6 x# t" \5 L2 O* x, D" [6 `8 l$ V
add_action('save_post', 'save_site_wide_notice_meta_box');' ?! q; {4 I y
function save_site_wide_notice_meta_box($post_id) {/ Z: y& X% b+ H- R1 X5 L! K
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))" p @8 @9 J/ [2 w) g
return;
& G( r! h) n& ^ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
7 {+ ]5 J: C7 s1 r1 T" z3 ?) n return;
. H1 {: u8 Z$ X* q' Q a
* h, f0 [2 s3 d! R; U) T if (isset($_POST['notice_title'])) {
/ r5 E# I1 S/ c3 { update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
* W! s% j1 k7 m! m v }" h; N0 z4 J( t/ U
if (isset($_POST['notice_content'])) {
$ A4 y& I# J( y- J, o$ C c update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));) z3 e7 N- j' N1 W
}
+ d o0 r. ^! S) ^ }
6 f: M# q9 z( s t5 o# g. B$ g ```
9 V* ]# Y3 i$ `$ e, s' ?) {0 J, c K9 u. _
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
4 @. }" s! _: k' G2 v
E3 y# ?# t- c' M* X/ J9 w4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
+ |0 T; p: K0 r: t; r$ n" k( @1 h+ k6 g/ p3 k# p( j* Y
```1 Z% I+ U0 O9 _8 b; b0 W
$args = array( w! |1 ]8 p0 C, { L: w0 A
'post_type' => 'site-wide-notices',4 [3 I6 n4 }( k0 x1 W1 S
'posts_per_page' => 3,
( n4 b8 W" R! Z2 O" ` 'order' => 'DESC',
; i; O+ u# ]9 [$ u& Z 'orderby' => 'date'
- t+ h( m2 A/ z- @ );
' p: n3 g: K( ]* I $query = new WP_Query($args);* B/ R" Q6 C7 j# [( G
if ($query->have_posts()) :
1 ?9 g- C6 Q8 k% n- |! m while ($query->have_posts()) : $query->the_post(); ?>: D0 u. k- B9 R9 Q( c
<div class="notice">" f, b/ N" T8 ?' f& X9 e
<h3><?php the_title(); ?></h3>
/ q) G q6 ^; B0 ]" {, S <div class="notice-content"><?php the_content(); ?></div>" J/ O) G$ E% ^: k
</div>8 S) d: A- s/ F% x! D
<?php endwhile;
* s7 G9 A4 `7 m1 A' W% U wp_reset_postdata();+ Q" e6 A; x# o( e8 a1 {
endif;5 N2 w; v8 B! @( C5 [ k
```
' ]1 ?( W6 V* B5 K6 q, ~4 Y) q z; X' B; [
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|