|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?% X( p1 K2 ?$ _3 j/ q! L$ r8 l
& Y* a4 N( y+ ]8 E; _; z4 V
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
8 i1 ] B7 t( U9 R
' j$ d" f) R2 r以下是创建自定义插件的步骤:& b) s/ ]! {" ?3 ^6 L" M
9 r( m! }" R! r9 C
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
. A! g7 e( n2 ^* E8 o& w+ N1 O3 w4 ?1 S" C
```
# Y+ m. v' H: r( v% X$ t: W <?php
6 f2 C4 N8 G8 x; p8 E+ a /*
' m4 h3 a' X- n" p9 Q! b) x Plugin Name: Site Wide Notices Plugin
* C4 V. f- k* ^) E; V- m! j+ U* r Description: Adds a new custom post type for site-wide notices.
4 d" `* F9 y, Z9 P* _0 a# k Version: 1.0
% V9 K! y9 O5 `8 c3 t Author: Your Name" H# m+ p% I' A- V
Author URI: http://example.com
' I7 G1 J' {3 u) g) K" Z */7 O, M- l; s" q; O! i9 ?
2 K8 M0 {7 V8 I8 O" d4 [+ h
// Add plugin code here...
2 ]# S0 [6 @7 ]( t, a ```
0 ^% A B$ T+ U- f2 \) N0 g
2 q+ [2 p& o1 l ?2 u5 j 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。$ E! S) M+ ~( ^; ~
$ o4 G7 M( ~/ B5 P! g2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:. O, \, T. v2 k! j* B: b% m. z2 a
3 S7 f; n t' Q+ l! s ```
; k' a5 Y5 w& J0 x/ e' ` add_action('init', 'create_custom_post_type');$ g- D R' Q1 B4 n9 @% _/ |
function create_custom_post_type() {, O" y( B7 d2 x x T2 i' q1 T
$labels = array(
) G; g' g* u4 l( v0 U 'name' => 'Site Wide Notices',
6 L: O7 ~# u% H" A 'singular_name' => 'Site Wide Notice',' T& \& x% B5 U' _# Q- y3 r
'add_new' => 'Add New',5 N. ~) D! }; S6 O1 P
'add_new_item' => 'Add New Site Wide Notice',
$ C+ e+ U: l6 H4 Q 'edit_item' => 'Edit Site Wide Notice',
7 X+ a' e" C2 d1 r 'new_item' => 'New Site Wide Notice',
. A% a2 l0 i( g5 J" G 'view_item' => 'View Site Wide Notice',' e8 ^: b- t+ o: V1 ]
'search_items' => 'Search Site Wide Notices',' Z) Y+ ?( b# {4 V# u: {
'not_found' => 'No site-wide notices found',9 O, c: C7 a0 s6 N8 ?
'not_found_in_trash' => 'No site-wide notices found in trash'
( B5 J0 r0 e5 x! p! @% Y. b );
& R7 _- `- ?3 ?: t$ ~
# k4 e+ L0 ], [$ O: I- W# e8 v: ? $args = array(9 W: q% `: O& ^4 Q# m( a
'labels' => $labels,
6 q) a# m/ i5 f& K6 X 'public' => true," d) C5 t' X2 q, p, L7 Z1 I( h
'has_archive' => true,
6 L& D! A0 \0 L( D0 |$ t! J 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
) t0 m) y; q1 G 'taxonomies' => array('category', 'post_tag'),# m e8 g8 m& [$ G$ w
'menu_icon' => 'dashicons-megaphone',
8 g2 F0 c4 Q/ S& C/ H3 v# O2 Q 'menu_position' => 5,
3 m7 b6 Z8 \6 b/ } 'rewrite' => array('slug' => 'site-wide-notices')) e7 W: \0 G& Q! X0 n, \- ]+ p
);5 w/ E- E/ P' F. n: J3 |: C
; ~2 b& ?; N8 Q& l4 v7 ]2 @ register_post_type('site-wide-notices', $args);
. g: y5 ^( m$ } n4 ?+ z/ D }
/ l1 f L2 T Z1 @9 [; S ```) y- C _0 T F+ t2 ^/ C- o' C
/ g. O- J X) {
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
/ m) G3 W( z+ G3 a$ M$ d1 I, Z: v5 [) w* E, [9 B
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:3 c5 d' p* v. B# d4 E9 q
/ F8 M+ L9 M, a
```) f1 j8 E$ {+ b: N! N
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');: I; U" d5 ^( K }: F
function add_site_wide_notices_boxes() {1 I6 u8 J) k8 G( i( k" P
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
% r- b B* W" Q, D. t }
% h1 b7 z2 N0 E& ]9 Q5 P, k7 Q& N- {" Z1 W2 |$ T
function notice_details_meta_box($post) {# ^- h" @# `6 u. v9 y/ @( E2 R
wp_nonce_field(basename(__FILE__), 'notices_nonce');; S: P0 l0 ?" W
$notice_title = get_post_meta($post->ID, 'notice_title', true);( i! N2 Q1 A S3 A4 i9 t* ]+ y. n
$notice_content = get_post_meta($post->ID, 'notice_content', true);
) C* ^. u J8 Y ?>7 K4 X2 `0 I% Z, J2 e0 q
<p>. a+ A' r, h+ \. N4 j+ T u
<label for="notice-title">Notice Title</label><br>- H: _1 x+ |6 x( f3 |
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">6 q% T+ R( X2 k' y0 {
</p> J/ n! J1 d0 o
<p>4 l, A. {5 Z& A2 v" ^
<label for="notice-content">Notice Content</label><br>
: L" x7 S4 A. T; L- F5 _6 v <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>/ Y* j0 _$ Z; D7 X5 z
</p>
4 W0 t T! p" E <?php2 w/ P* S7 m+ I4 W3 _& k
}
5 z0 W+ T+ ^' G; ~) v4 b1 S2 f/ j( t% `2 b0 }
add_action('save_post', 'save_site_wide_notice_meta_box');
: K+ e8 ^- g! Y& t. ^: A% C; N R function save_site_wide_notice_meta_box($post_id) {
' s- S: b! ?; |# x/ v/ n& ?( G# I if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
0 n! L4 @ N; e8 N" V return;4 }/ F$ Z! C* v1 i/ I6 I
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)) N, d! K5 u* E2 ~% O+ i3 t
return;5 U1 M8 J2 ~2 i$ c4 m
! m8 T! t2 o! n if (isset($_POST['notice_title'])) {
3 x5 p3 y \$ ^& R update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));1 `: g% c* x; r8 p' k
} B9 D3 V/ _9 o ~) o
if (isset($_POST['notice_content'])) {. w" O4 I8 h1 j- j& w' y! U8 C. c3 E
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));6 j' g# w5 l1 L: x
}' `2 t* {9 [1 C' x) k8 L$ Q" y
}5 A7 H% S+ J) B; f
```. }1 u) N2 V9 h8 n4 Z; I
" G9 k, K2 |4 e
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
, o) R2 q0 ?5 C7 D; L3 V7 N2 M) R$ P! W6 j5 s3 ~( K' G
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:( W+ Y9 o* t4 l5 `2 ~
0 x! p( W, \4 z0 f( ]
```
& R4 Z& _% H7 p9 \; ?% k8 k $args = array(5 z7 V" D; _& }7 |% x4 w0 p
'post_type' => 'site-wide-notices',
4 q* Z4 c# P4 f: }0 G 'posts_per_page' => 3,9 Z8 n$ [ z" X( o/ K6 X$ K
'order' => 'DESC',
* B3 h( ?; G' L# ~4 D" O3 | 'orderby' => 'date'
1 n+ @& B- t- N6 r );
. Z4 {! M% v3 F' u* t) P; f $query = new WP_Query($args);
3 H- \$ n K/ v6 [( k if ($query->have_posts()) :
e& m% @4 x' {# S1 `6 ~1 q while ($query->have_posts()) : $query->the_post(); ?>
! x8 h; P9 h) C7 W6 p <div class="notice">
( }& U1 y; Z" t- {% y <h3><?php the_title(); ?></h3>. ?3 m0 a9 P) h: D
<div class="notice-content"><?php the_content(); ?></div>
% A: c! V* ~# W) g; w! M1 j </div>
2 h( K7 b- ^: X6 D% F# V <?php endwhile;
+ L6 F7 @8 _' [. m wp_reset_postdata();! D% @% \) i+ |$ U1 ?8 P
endif;
+ n3 g( A' C' u) E% T9 G ```
7 g7 p* J7 U" k" d; g
$ e* j( v1 v; Z- V$ Z! @ 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|