|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
5 I' K- k7 G% R7 U3 M" k- C) t% J
" g4 e5 Y7 J- H( j0 _& I0 w0 |7 ^如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
A% k: y! P8 N* F, [* S/ p
( l. E$ I; B7 |/ D. y以下是创建自定义插件的步骤:
7 w0 V8 e9 J9 @7 d3 h7 T/ F. [
1 G- q% P+ s1 h+ ?1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
& t0 q! A: B# J2 V4 g$ H8 a/ d* S t& V T; z
```1 R5 J2 O$ M+ L$ b; m
<?php
$ K; h% z- S, w: i7 F( P /*$ T* D$ X F* ^- ]
Plugin Name: Site Wide Notices Plugin
5 L- g4 Q! |& C, c+ | Description: Adds a new custom post type for site-wide notices.
* v' @/ m- _1 N' b( Z- O Version: 1.0
: x& L \9 J' Q1 @6 x* Y; V Author: Your Name
( G% B; w9 {7 x- ]6 J; T Author URI: http://example.com, e3 q& }5 t0 Y8 V+ j, G
*/
0 m) A4 c( @- }, c% t
3 Q$ V. o* n, e9 Z% t, u // Add plugin code here...; g5 O1 K4 x+ f# d+ s5 T
```* W, S M9 J! y7 \7 I1 p3 t( k
4 N7 V5 S! M: d" b, [
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。) J, v! \+ x6 O3 d( O- c
4 Z! A8 c, n0 y8 }2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
* b: Z) r3 ?6 w3 T3 ~6 b' b0 g! A, Z
```
" R% A0 }( X) a( V6 U% E( d add_action('init', 'create_custom_post_type');2 i) M* `; o$ K: l! n4 ?# q
function create_custom_post_type() {2 U, K v' ^* m/ f6 H" U9 M: X8 ^
$labels = array(
" y# w) \/ @1 J: L" [ 'name' => 'Site Wide Notices',
7 d1 n, O) x3 Q, N. G( W( m4 w6 A 'singular_name' => 'Site Wide Notice',
3 S: G* d( w6 p. N7 w" d 'add_new' => 'Add New',
4 K, s. D/ w6 N 'add_new_item' => 'Add New Site Wide Notice',9 R' V! t8 S. _, a0 \
'edit_item' => 'Edit Site Wide Notice',- [$ w: r' [$ |# @- k) ]
'new_item' => 'New Site Wide Notice',! U7 r( y: B- f2 g& ? K% g' i) R
'view_item' => 'View Site Wide Notice',9 q$ v: L, Q$ L3 a. A) e
'search_items' => 'Search Site Wide Notices',$ _, J; F' g3 |
'not_found' => 'No site-wide notices found',- r2 g, j9 Y/ ^) y3 K9 u [
'not_found_in_trash' => 'No site-wide notices found in trash'0 Z) i% e2 ?2 [3 Z- W. ^
);
) U, S2 \& `! N
4 ]9 M$ [- v/ E7 p $args = array(
! [/ m. Q3 D( e n, I/ B, E' | 'labels' => $labels,6 \( R/ l; d8 U" Z' K# a
'public' => true,
; ~& E1 b, A$ h1 g/ c# N, y 'has_archive' => true,: y- B, Y# H C( }* k% [8 f/ L8 r
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),* s& s0 B5 G3 W
'taxonomies' => array('category', 'post_tag'),/ b" n8 x1 b; M8 s) }( c: ^
'menu_icon' => 'dashicons-megaphone',
7 _( M* ?( ?7 d* n0 F. S# d, G* s7 p* B 'menu_position' => 5,
- ?+ i4 Y7 L7 p 'rewrite' => array('slug' => 'site-wide-notices')
7 R# v* P( [1 ]7 D7 _8 m );
4 O% L4 w7 Y( z0 r9 X2 S* Q# U- E
& _( l# a! T2 A register_post_type('site-wide-notices', $args); l5 S+ G/ B1 n( A( D6 w! u
}
; w, `! V5 R& F2 [) Z! b ```0 J8 U0 e; L, y9 W. O- m" I
8 a6 W( _1 i7 G9 v
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
# r+ k$ P, K, s, Q* Q, `0 T/ {7 _& H( d2 f8 F8 F/ X1 u
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:1 r5 ~5 b8 ^6 P
f E- @! z' ^4 Q P- e
```
0 j" F2 T2 [% u; g: Z add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
- a! U4 x& P$ o9 u, R6 K' b( l function add_site_wide_notices_boxes() {
, U/ N. J% {8 S* ^ add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
3 \6 l% F2 L/ h0 H }
. p1 Q0 m* O; B4 p6 [ d( D
- L/ j/ u3 p, T/ S$ _ function notice_details_meta_box($post) {' s, L5 r; ?, e) d( n2 t, d
wp_nonce_field(basename(__FILE__), 'notices_nonce');
$ I4 |; G$ r7 n, y- k! @" ^ $notice_title = get_post_meta($post->ID, 'notice_title', true);
- f1 u4 U& n3 Y) C x $notice_content = get_post_meta($post->ID, 'notice_content', true);
" }$ d# m- d5 a& q! n& `" h ?>) N( |2 a ^( B, W
<p>
' N" `3 P/ C6 d% R <label for="notice-title">Notice Title</label><br>
- V$ q9 c; d- V5 J. X% G! j! ` <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
# M! k9 v" _/ ~+ B* \ </p>6 O$ @/ g$ S; N% x' {& v% S0 J
<p>
" n8 o9 p: g+ p. l. n. Z. R9 n8 H% Y" F <label for="notice-content">Notice Content</label><br>
3 ~5 t3 |5 ?3 n# S <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>' q1 y6 Z m; d. F. U# A# {! [9 Q
</p>
$ S) ]( P! g( T& u; A t <?php; B4 |3 i& W9 i0 Z0 X# t$ t6 E* B
}+ T8 `4 A% ?( \3 S! y$ U( j
+ N- A4 F7 Q% P4 U1 @
add_action('save_post', 'save_site_wide_notice_meta_box');
, e( ^. N# g% E+ J/ P function save_site_wide_notice_meta_box($post_id) {( N, b7 ?! T5 q) {& R: L
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
1 ?3 {* G# A% D* t' \+ p return;# t- p: h3 L6 H; _8 T8 o. v5 u, K; ~
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)$ b. W- T" a* Y0 N. g) c2 h4 J; j
return;/ p! C4 N7 d. `) g, K
4 g2 h$ S- c S2 O3 L if (isset($_POST['notice_title'])) {
# }" o9 q# M- }* U" r4 a update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
0 @$ A! ?( C' o0 P6 K }4 b0 N& W% p: o6 Y, J
if (isset($_POST['notice_content'])) {
" c9 I" h+ G8 L6 B! E/ V update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
4 k- e' ^0 M( _# r0 t: _9 X }
, O9 ^$ P8 r7 n) C5 e8 e2 S2 ?' } }' ?: [! s- _, G1 y- X' M
``` f3 y# T: l+ T% Z$ p
2 i: ~9 a; T3 I/ R
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
1 i% Q: Z( i; t. V3 U
# Z0 {7 o- j) ~& ~4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:7 k& ?( h' L- ?' r3 z" F) |
; C2 x* y* m* M" p, l; ^
```6 m5 s' {7 e9 }1 D" N" r* L2 O) D
$args = array(
* u# R0 |3 J3 x* T 'post_type' => 'site-wide-notices',' ^& n' z0 U1 G) l/ c' r+ i& _% d
'posts_per_page' => 3," f2 D, J3 H0 p3 T0 k: G( f
'order' => 'DESC',
8 G0 B0 Q+ ^+ r4 c' I& J3 }% } 'orderby' => 'date'
# p( Q, ^" F6 R* w/ v* { );
1 ^" a& V' [1 k }* n" Q+ }) f $query = new WP_Query($args);0 b) G8 O$ o) D/ E' Q
if ($query->have_posts()) :
. j; H4 {4 @, W; v" i% X while ($query->have_posts()) : $query->the_post(); ?>' [9 D2 L) K4 D, O, H
<div class="notice">
2 C0 k& U9 p' d6 H q7 o <h3><?php the_title(); ?></h3>
. ]- B: P# o+ W( p% y: E" ] <div class="notice-content"><?php the_content(); ?></div>' x+ x" @( r, f% N2 z+ ~
</div>$ Q( Y, m3 l/ `' w4 W+ }) [- Z
<?php endwhile;
1 r" F- X4 {- w wp_reset_postdata();
; Y5 F. Y+ ^% H0 t endif;
, {) f+ {. f" s; c7 A2 X ```
+ s! l; L% q: Q0 ?* b$ q8 ~1 _
/ \+ v1 k$ r) X+ X# F" P9 G 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|