|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?9 |* P# m. S) |& ~# {/ N
' s6 k* g/ p; B4 ^如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。' Y! {" }- q4 f! l# a5 }
: }7 L# k8 Z1 D+ {' h
以下是创建自定义插件的步骤:/ j k0 I: C) s2 g' V
( u; q1 H7 ~( H9 {1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
# k& S+ c- U: b* l; ~
6 v+ j) B; C' j7 \* m" Y& ~ ```) i5 F9 z' X9 J+ d# \' Q3 h
<?php( L% a& _. B0 F% ]8 P* U6 k8 n
/*3 V3 t% y6 u- o5 X5 e5 W9 Q" U
Plugin Name: Site Wide Notices Plugin
: [) t7 q/ x+ o; q Description: Adds a new custom post type for site-wide notices.6 a2 U6 ]% w. E
Version: 1.0
: P8 t, h9 `/ `4 Q1 \8 R" f4 j; i Author: Your Name7 x; j1 e% S% ^! p) W* p0 H }( t
Author URI: http://example.com2 c8 ^3 H5 _0 J3 r- S; Q
*/
t0 Q/ v# t/ K0 O4 M9 c+ @$ {
: c7 p5 ?- N1 K& M: c // Add plugin code here...7 N$ y# V- X# z" H% g+ w
```& s( m& T9 H( k6 R
& o7 U6 Z! N4 O4 H7 x 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
$ Q3 `* p* j! N: L+ L9 P) V+ e5 P( N T) s( e( y- c
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
! e& x9 U0 M: j
% @' m+ P5 H: U9 D$ o. J ```4 ~( W* b4 G% |# S6 G8 _. }
add_action('init', 'create_custom_post_type');
* u0 Q$ g% ^$ q: A9 A; E function create_custom_post_type() {# C0 m, Z! V/ C" o* o8 ?" y0 D
$labels = array(0 n" N! }* y( Q G
'name' => 'Site Wide Notices'," O& k4 z& G! @6 X- t9 b
'singular_name' => 'Site Wide Notice',) Y* V1 C; C9 k% ~) h: W" v' v
'add_new' => 'Add New',
# q& q+ p7 H9 h! c" b$ ]4 a% Y 'add_new_item' => 'Add New Site Wide Notice',
( @- i5 Y2 |5 M# a) g. x 'edit_item' => 'Edit Site Wide Notice',
: S" c6 {2 q+ z3 T' q* u& W 'new_item' => 'New Site Wide Notice',
$ }/ R) f& j9 N+ f+ D9 m/ j& q 'view_item' => 'View Site Wide Notice',' {+ o- N; n" F) I9 ~
'search_items' => 'Search Site Wide Notices',
( U+ z. D0 \# ?0 ]# R& J 'not_found' => 'No site-wide notices found',6 l5 C. ^. L5 ?# j! X( j5 w& |
'not_found_in_trash' => 'No site-wide notices found in trash'$ t6 J- c) c: E2 s) h( S
);% u0 y# i$ y# c' I- ^% @: d
6 E" \" o. N. K- @- M
$args = array(/ z& F2 s2 \7 _! h% m7 r" w
'labels' => $labels,: N+ o8 @. ^* ?
'public' => true,
$ C$ V2 L: ^$ b: ? 'has_archive' => true,& ]: H9 f7 h, P; A2 Q
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
& `9 q2 J2 x- p' q 'taxonomies' => array('category', 'post_tag'),
: x, G; l F8 C% \# z% d! A' u 'menu_icon' => 'dashicons-megaphone',
& v `' ~5 ~4 i5 V7 c& Z3 t+ t 'menu_position' => 5,0 T6 D2 Y' V* u' m7 v; X# C' n! C
'rewrite' => array('slug' => 'site-wide-notices')' R& F4 [/ j8 a% ?# Y* q5 b/ _
);% G& }$ }- ~% r9 Q5 u
( R) \. [" E1 D/ T
register_post_type('site-wide-notices', $args);. K, u$ ^$ C ^* w, E* o
} F: x& _) _8 B0 ~! _
```* ^1 Z/ q' _6 \. L
0 w7 m3 V+ R) P: v R 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。; J$ M' S7 q7 t y5 W- _9 x
/ x6 M6 l% \; k* B( Z4 k. ?* Z3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:( v* B e# v7 M3 H
3 [/ H. m5 N; k g4 [, l ```
- x& m7 _ A+ p( w. v( m5 r add_action('add_meta_boxes', 'add_site_wide_notices_boxes');" `5 \6 }5 o& U7 s
function add_site_wide_notices_boxes() {
) J+ g# F; F. F; ^7 M add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
; |$ b/ _5 l) x4 L+ v7 O! F }
3 |6 c) ]5 G6 A$ t( k$ }1 ^" T5 e
2 ^+ y$ J% E' t' y6 d function notice_details_meta_box($post) {
3 C8 w. m4 f& T* o5 ? wp_nonce_field(basename(__FILE__), 'notices_nonce');
( a3 x3 w2 G$ P5 A9 u! _ $notice_title = get_post_meta($post->ID, 'notice_title', true);
% x- b- B/ z0 o# G; T $notice_content = get_post_meta($post->ID, 'notice_content', true);2 M% c5 Z' D& x" {
?>% u, D* H/ {3 j/ E% F
<p> W) W; i* J7 w5 O
<label for="notice-title">Notice Title</label><br>5 o0 h" ^- t/ m& o- s
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
* v7 _/ f; t1 J9 v( } </p>
) T! ?" G @4 c! H+ ` <p>, K# X$ n! J0 [- F: |) f2 Z
<label for="notice-content">Notice Content</label><br>
4 n- n- f/ e: E0 x; g <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
3 y# v1 l$ K' ?7 h/ T' _ </p>
! z0 [& a# l) q$ [ i <?php
/ R7 K3 v" {+ D+ p3 ~; N$ ]& f } k5 e& o# [7 F$ M2 [& B
* d' k- k% V! {& o add_action('save_post', 'save_site_wide_notice_meta_box');
. {- P4 P& y- H) [0 @ function save_site_wide_notice_meta_box($post_id) {# J2 ?2 [3 l0 F* ]
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
$ C: M1 |7 f7 B f! p- ~ return;
4 s/ P: e; f6 f0 v+ ? if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)' N7 _5 ? n; n+ b- ]
return;
2 F6 J! @8 y! z: I1 y1 R$ w: Y, w1 p/ U1 s- a6 h
if (isset($_POST['notice_title'])) {
4 V6 f3 X$ L# f# T- {! z, l, |$ Z update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));4 n! u; ~/ R( n% N2 G- f- a
}5 [# F7 z1 f& \5 B( R8 v
if (isset($_POST['notice_content'])) {
) m" b8 q* o0 |8 P update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
) i3 u' ?3 H6 G2 I% k$ P) k }/ Y/ e9 s4 v. g8 G+ X0 q8 g# `
}
6 u2 L9 ]; p# Z% G+ U2 s) u0 C ```
+ @6 Z4 Y5 E; ^9 b7 b9 p' y2 }3 ~& r/ q) P5 J+ p" f# c
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
5 i$ N4 Z7 r9 d; A) u) Q
& x( }5 F/ r4 w& B4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:+ l o" C' }. P! [ J) e$ |' u' |' a$ ~8 d
) f, s! b. X' R9 {# r; P) N, i- P ```
$ Y6 |; M# G( ~& T4 L" m $args = array(, ^* m" G8 J' ~, X# p
'post_type' => 'site-wide-notices',/ E" m" M4 |( w7 P8 {' A& ~
'posts_per_page' => 3,
5 H# X# p D2 {3 y 'order' => 'DESC',. u9 c6 s/ r0 x' ]4 n2 _
'orderby' => 'date'$ C& x1 R* V8 I3 M3 X1 o3 C
);
5 ^; F; u' k/ n $query = new WP_Query($args);
/ a& G: u* v8 w8 \$ p+ t3 T# F if ($query->have_posts()) :
: j; w6 C L! J- @6 R) t$ w' u2 ? while ($query->have_posts()) : $query->the_post(); ?>
7 C! v p6 ~5 y/ t# B <div class="notice">
C+ ~1 Q/ v z7 @; U <h3><?php the_title(); ?></h3>
! N' J( p S! }+ X <div class="notice-content"><?php the_content(); ?></div>
, v3 _) I9 D. o# g5 Q0 n% H7 u </div>
7 C8 G: I$ s5 Q) | <?php endwhile;( }' p. ]9 d1 I- b7 W) B
wp_reset_postdata();
( w0 P' X6 ~8 `9 m+ @3 {* a endif;" Z, k. N% a! B
```
* {1 ]8 x8 R; F( `
& F0 {0 w( W' S7 L3 H 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|