|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?1 b! m* c8 r# y) x
# a% R7 |; m. k' h: ^8 G如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
, r$ I0 h! q! G" `" G. K- s
" P+ P9 L3 C' @8 u* w S, L以下是创建自定义插件的步骤:5 w& i/ {/ y- ~3 ?) L0 a4 L+ f
# E2 ]0 U: _+ b1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
$ i8 ?0 {: j5 V x8 K: O0 Z0 ~" Y% S. r' }- R7 ^8 W
```
5 K n$ Y$ s+ ]+ V( u <?php' S; `2 |8 {; j9 ~( }! F8 J& p
/*
$ J' I- M& O. A8 h) V. ~- t6 P Plugin Name: Site Wide Notices Plugin
7 W7 E0 j' s G Description: Adds a new custom post type for site-wide notices./ C2 \6 H; K# |4 J, n: j
Version: 1.0
! u6 J! w. t/ S Author: Your Name0 m4 k5 v) A6 x' x
Author URI: http://example.com
$ M7 C! ~9 N* v! M! D */8 w3 I7 R; y4 M# _1 A. i: N
$ s, b5 W1 B J% J E3 j B
// Add plugin code here..., D8 x( G8 a# k$ a
```
' R. G5 T; l3 m( N# r2 V7 k& J
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。2 y/ J& y( a4 V' `( t
) \" F* ~# e* P( s. ^2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:8 o" \) ^& {; r& N
8 C- f6 t( n( p5 q' Z ```
5 `' G8 T# y" t2 \ add_action('init', 'create_custom_post_type');
+ O+ f l3 P1 L, l1 ?7 h6 u! W- q function create_custom_post_type() {
5 C R+ [1 R, F $labels = array(0 M: \, q6 p7 X* f
'name' => 'Site Wide Notices',) W& T( s) O7 ^. J
'singular_name' => 'Site Wide Notice',' K. F% I: Y7 Z& C2 L$ e* b
'add_new' => 'Add New',
2 s3 D. S) U5 F: `1 Z3 _6 w9 Q 'add_new_item' => 'Add New Site Wide Notice',
% s+ G2 E' O+ i 'edit_item' => 'Edit Site Wide Notice',* h M( H5 B' [2 W( U, v
'new_item' => 'New Site Wide Notice',
/ k# D/ D& U9 j- V/ d 'view_item' => 'View Site Wide Notice',
2 h& M& [. f, c0 w c+ E; O 'search_items' => 'Search Site Wide Notices',/ s: R. P4 P5 R4 x0 b
'not_found' => 'No site-wide notices found',$ I# Q% _ \' n& `
'not_found_in_trash' => 'No site-wide notices found in trash'
1 e7 ]! K9 j! F& } );
' x# o7 |. Z( |3 O
! m) D0 v% @5 q2 D $args = array(
9 [; B3 K- g5 t5 v 'labels' => $labels,
; @( L: F! O6 j; p' [ 'public' => true,/ C% \* [1 L7 Q7 f
'has_archive' => true,) p& B; f9 `( Z$ Q' o/ O: ]
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),9 d# y; m6 P0 J. x, W6 n7 T
'taxonomies' => array('category', 'post_tag'),
, w. H+ |) Z6 b g6 r2 L 'menu_icon' => 'dashicons-megaphone',
* w2 Z% D& G6 I 'menu_position' => 5,
% c( z K, s2 k) w4 Y 'rewrite' => array('slug' => 'site-wide-notices')
& H; w8 L4 Q' R$ e4 y) t/ O );7 M' Q" n$ u9 p" c
. w4 t/ b. D8 }) O& H8 ] register_post_type('site-wide-notices', $args);+ K5 a2 Q/ W. h3 }9 q5 E" T4 V1 w0 J7 H
}
' C: ?; D1 K1 q. m ```
; c* x$ C; ?7 k4 L/ R# B% W- l7 K+ o2 N8 d9 R! ?- P7 Y
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。: w' L7 i5 I( t: B
$ E. n/ j, c2 m* c* ^! X( e; h
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
" {7 M& b+ Z# a E1 y, k8 H- a% q1 f( u9 b( P( W0 c
```
2 y3 ~- S% H5 M. j add_action('add_meta_boxes', 'add_site_wide_notices_boxes');& @2 Y( `5 Z0 y3 |
function add_site_wide_notices_boxes() {
$ B/ h, F0 T' C9 N1 L6 m& m add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
. l$ [$ G \- b; E2 o( k }
- ^1 L5 H- Y- I Q' F p8 S/ X
function notice_details_meta_box($post) {7 J2 V$ u& D5 K' ^$ L# F! t
wp_nonce_field(basename(__FILE__), 'notices_nonce');
, h6 y) _% T3 N, d $notice_title = get_post_meta($post->ID, 'notice_title', true);
. H \: {+ a5 X $notice_content = get_post_meta($post->ID, 'notice_content', true);( _# R. h9 K6 `6 \4 z/ n
?>
( G1 ^% ^& p9 x4 m# i <p>; L. M& j2 _ S+ d; S- N3 ]5 \
<label for="notice-title">Notice Title</label><br>
: H8 M8 @' u0 }. ? <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">/ J$ Q9 L( o9 A8 t
</p>
3 r7 Q1 J( N! e1 u+ S& D <p>
# t9 j- N3 V6 ?( Z <label for="notice-content">Notice Content</label><br>
! m9 m0 q: p$ i, D <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>4 h% |5 o* U" g- O
</p>& c' x7 p7 ]) W# ?2 z
<?php
) u- q! F- ~) h5 E }
. p: Z+ e! Z% T/ x
: Q' V0 a' K6 Q' j3 h add_action('save_post', 'save_site_wide_notice_meta_box');
5 U- @) V3 C: e! n" E. Z function save_site_wide_notice_meta_box($post_id) {, t* Y0 t( I7 _/ F7 l A
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))9 x6 w* v7 I7 m" V3 W: S
return;! B1 k5 B" `1 x: U+ U7 C( P$ ~) }
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
5 }# e$ j: t! q return;9 q+ G8 g( o f
! M( U0 m# T! ]" e( p& \ Y: p( C
if (isset($_POST['notice_title'])) {! p3 m% L( i3 o' }; D i
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));, [ F, O* l) C' {
}, x! i* r% }2 @/ S! e& X E& g
if (isset($_POST['notice_content'])) {6 z4 |- P0 Q& _, `( `
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));/ \3 n( ]6 J; i* `; m" K
}2 }$ Q" u, X& E9 A5 @% ?
}( Z* F2 Z& u7 q8 H+ J7 ^
```% o5 f6 y- f8 m5 f7 ]! M& U
5 S9 H) d8 l9 ~$ N 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
. i8 [3 T4 a5 C2 z. P' h* g/ a( a. f6 X1 H5 p3 e5 I
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
" V& U' i( K1 `4 j) C( {+ l) m7 w* h1 \; h3 a( R
```* C8 m, E* _& K$ P
$args = array(
, [" _( U% _4 K( i$ S3 B 'post_type' => 'site-wide-notices',
* X' w$ W5 s( M8 f 'posts_per_page' => 3,
8 I6 L$ q, k8 p& |/ |) X. ^ 'order' => 'DESC',, }* N+ R. J) p4 ?7 R: j$ F4 y
'orderby' => 'date'0 M0 L( B8 A% T( G6 w# n
);
: R+ B& Y; Y$ ` $query = new WP_Query($args);' x+ ], Q6 Y5 U3 w+ L, G
if ($query->have_posts()) :& I. o/ w* t \1 ^8 [* P: v
while ($query->have_posts()) : $query->the_post(); ?>
; g1 W8 t. m" r: N! m! E) d <div class="notice">
+ ]- N5 [1 o5 u' k9 \( q ` <h3><?php the_title(); ?></h3>
: J% X" p. U5 @' F O <div class="notice-content"><?php the_content(); ?></div>
# `$ f6 P7 K# F `& {2 \ </div>0 j1 ?8 u1 z$ {4 g
<?php endwhile;0 ^+ [* w7 P) B- {' A9 G
wp_reset_postdata();* N1 w* G: p& w
endif;
, A- X, T; d6 a. C: }* P ```
1 \& V) ~- o5 K' T; o3 t
, n @0 e2 x( ~. q6 e* W+ p 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|