|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
" a* O3 q# \8 _" p* d+ I! T) T6 o9 i7 m. Q* }) v5 v8 P
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。9 L$ m' M1 F5 p% o
. R3 s' j% M$ u以下是创建自定义插件的步骤:
) X6 {; K- v2 }! p' L" @
( X. {8 o, _3 {" a1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:! R" n' \( N5 y* T, ~7 f: Q
0 ~& _4 C" J" `! s
```; L; b! u1 r$ I+ X$ M; l
<?php, E# o' Y7 ]0 I
/*
9 L4 K, _, |( j0 p! v5 F! | Plugin Name: Site Wide Notices Plugin. z- k+ s) A9 f E# k, {* w
Description: Adds a new custom post type for site-wide notices.
/ Y; s1 e, I& m d4 W' p( X) S Version: 1.0* ]3 a1 M6 Z) @) W( L9 t$ n! k
Author: Your Name
2 N2 p/ ~/ I3 Y0 `3 k5 X6 b Author URI: http://example.com3 \. h2 f E) R/ g
*/
% {7 v3 f8 t x, w A Q5 ?$ b% L! O2 @. ]
// Add plugin code here...7 A* C5 M1 Q. o4 v
```0 r% V, m) ~( B( m* X7 ?
; E6 a0 W" E8 D7 `+ N* Y' O
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。5 S& U# K7 [3 Q% l/ P H
/ h8 q# T. ^' s1 N9 d2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
3 {$ ^9 T- k) n- s4 L+ I/ i/ F8 W) g& S7 y H
```
* s M2 \' i+ R( p; b add_action('init', 'create_custom_post_type');( T/ M8 k8 H0 {& g% F" T5 ]
function create_custom_post_type() {
: g# b. g2 }& @ $labels = array(6 B m1 F s, Y, H% C
'name' => 'Site Wide Notices',& W) G3 E ~- {7 n4 |3 ?
'singular_name' => 'Site Wide Notice',1 C' ?& m( w* H$ L. ?* h
'add_new' => 'Add New',
: ~8 K( x! T% `! i( O 'add_new_item' => 'Add New Site Wide Notice',
2 a7 ^8 P/ U( G. ]8 p0 M5 C3 c 'edit_item' => 'Edit Site Wide Notice',, k0 j/ L6 |& }% p8 L1 h/ A' ^
'new_item' => 'New Site Wide Notice',+ ]% R: t- d1 x3 M4 ~- A
'view_item' => 'View Site Wide Notice',) |5 w1 D3 U u
'search_items' => 'Search Site Wide Notices',
! r; |3 L( S+ C' \' Q" n+ v4 M 'not_found' => 'No site-wide notices found',
2 W; z# {' ~4 F. | 'not_found_in_trash' => 'No site-wide notices found in trash': g/ {$ ^0 o! }3 R
);
# w2 Q [& G9 l# m' o( W
2 z9 J. ], T2 J9 A+ M( z" l j6 \ $args = array(
5 H8 ?- b3 ]4 X9 y! Y 'labels' => $labels,* T8 l% _/ \- h, ?' ^( V
'public' => true,0 p/ G- l) ~0 ^( H' ~# B
'has_archive' => true,0 N. z1 \+ `: O% v; F6 `# A8 y
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
% |1 B9 J1 ~6 k5 t8 { b) o 'taxonomies' => array('category', 'post_tag'),! ^. U S A& _$ X; h, g
'menu_icon' => 'dashicons-megaphone',5 O- ~8 B! L* [. ], Q
'menu_position' => 5,
|- ]! A" T; A- `4 r 'rewrite' => array('slug' => 'site-wide-notices')' E+ f5 F; S9 b7 q* D2 {
);% s, M: q4 q: `. C
. j$ v$ S7 q. ~- [; d- s
register_post_type('site-wide-notices', $args);% p+ D, y9 f1 a+ k: F
}
1 G( d1 d" V+ T% b4 v1 N ```
' G/ V. x5 B# z, j; v4 q
4 T& t+ k8 M0 b4 s* ] 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。( H5 P9 {- f6 s2 t/ v/ ?; ^ f
4 j* P( D4 F- k9 Y9 {; Q# D$ |3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:4 K0 N' N) H H* n4 n+ h3 S
6 Y8 m V: B4 }2 A( w7 ?+ f/ J ```
5 M% r. l8 Y7 _4 `" v& L add_action('add_meta_boxes', 'add_site_wide_notices_boxes');+ L9 u! Z; G+ Y0 S, m
function add_site_wide_notices_boxes() {1 S: f/ ~8 I6 j) I3 ]' z7 K* ^
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
% b, `4 b* D" P0 i# V" S }
; I' X% t, k0 f3 u. @, m0 ]5 H- G1 V# o/ z& `* ^$ [
function notice_details_meta_box($post) {
6 g, G6 I; e! h% \) l wp_nonce_field(basename(__FILE__), 'notices_nonce');4 d% ?. p5 a/ @: `0 `: M0 [
$notice_title = get_post_meta($post->ID, 'notice_title', true);
" ^- r. N2 I) T5 ^! ~+ k $notice_content = get_post_meta($post->ID, 'notice_content', true);
0 B$ S' ]6 ^( R/ m ?>
5 a" z1 u4 X) }2 ? <p>3 Y1 D( F2 ?7 Q8 n, @* N J
<label for="notice-title">Notice Title</label><br>8 L B" g: ]: m( i3 V& w
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
& D6 O3 z8 d( J x9 k </p>3 x! S1 @/ j4 w) M. r+ x
<p>; [' A8 |" u# m4 _, o6 C- C
<label for="notice-content">Notice Content</label><br>
n" l, h& R" r: Q4 u9 } <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
?; c& e& Y2 @- t3 }! K </p>
) j/ A. V$ _4 ^$ V9 q5 X8 W <?php, F4 N% b" [3 S* v4 \ l
}7 w; M6 `, R3 t2 h
3 D7 J: V9 p! \- F, D
add_action('save_post', 'save_site_wide_notice_meta_box');
7 h9 f2 q. z) ?+ ]8 W4 x; R function save_site_wide_notice_meta_box($post_id) {
) ?: `: z* o7 F( b* _- I) _9 E if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
0 n, F* |9 S) {) d/ O6 z- O return;
6 F3 @. y! j; C9 j) k; ?! F if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
5 r, ^& h6 R% Z* z- k# \. `) i return;6 z+ l5 V! u5 V6 ]
) f( R" E- m7 M. D
if (isset($_POST['notice_title'])) {
6 Y4 X* h, P4 y0 \6 L. O update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
5 V' t: z( y: A% X9 Q }
8 M; Q$ ^7 }: I J) o S" S if (isset($_POST['notice_content'])) {
# M- i) C# k' N update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
4 V' E8 B5 |/ G }
, T: P9 l1 L- w! k% s. p) Q* Q2 @; \ }) ^' R9 ?& w+ C3 C) w, s+ I+ i6 r
```
% `8 T4 N# o6 j- H0 N# `
: g2 h; _; [: Z" L, P 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。- M1 `2 T- H3 m2 c* w& _) ?
' }# D# O; I: I+ u4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:3 a& ] ?& N5 V* F8 X
5 ^% X! F' ?( K
```
7 m9 ]4 E' D* T; }9 e1 Y7 ~, m $args = array(2 I. s4 c4 `) v8 i$ H1 [
'post_type' => 'site-wide-notices',, u- I! E$ N. b4 X0 \. [/ V! G" A
'posts_per_page' => 3,' B, s+ w+ ]$ t2 x1 R
'order' => 'DESC',
- E0 o3 ?: n. ~# a3 T- ~ 'orderby' => 'date'
% u7 J o9 h7 ^$ e) p );
0 V( P& M$ W3 q N) T $query = new WP_Query($args);
3 B7 z# {% i. b; X if ($query->have_posts()) :8 |6 o4 c$ k( A! R0 |
while ($query->have_posts()) : $query->the_post(); ?>! N4 J* s3 C. x" b. C2 v- Q
<div class="notice">- r$ P+ t4 n2 z& ?8 j$ H2 N
<h3><?php the_title(); ?></h3>
) c2 u; ~# F" W. O1 o( b <div class="notice-content"><?php the_content(); ?></div>0 P; g1 z! p5 C( |2 b, c
</div>2 N2 C( W0 W7 b9 z
<?php endwhile;
) j' o8 Y9 S n4 T+ a, f: R, R4 | wp_reset_postdata(); \( {/ G8 S/ w& |
endif;
0 S5 K6 {% t+ O8 h1 o- L2 t ```
" Q& k% M: s( N Q W* W' n
" z, ~/ |' @; t+ M1 R& g 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|