|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
( G4 M$ X& L% G) @* H! J" o: ?% b
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。. Q, V3 d) n6 |# S
* Z2 `/ s% X+ l# x, u以下是创建自定义插件的步骤:. T6 f4 g8 C) G& G
% ^3 F. W8 T& v2 \* W
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:0 r, o; E- x( V/ U
4 N0 n' }) {/ Y% a @ ```
0 b* K$ H% K, i6 F: q! w <?php( n; u4 F- m; h0 X! _1 u P
/*+ Z! k6 s% y# G/ K+ z
Plugin Name: Site Wide Notices Plugin
3 R8 y: N% O, b+ i0 z% c3 G- U2 \! u6 R6 q Description: Adds a new custom post type for site-wide notices.& ~6 p" C. m2 G: m5 I$ q" f
Version: 1.0$ W, S. g0 r. j& A' q
Author: Your Name
/ u/ n3 Y$ D) [- _/ m# E8 c# j/ O Author URI: http://example.com' l. _ [" j$ Q% Y" }: y& a
*/$ s6 e0 \" l' E4 o
; \/ O3 Z$ p8 D# n2 M, j1 m
// Add plugin code here...2 a/ h6 c! H6 H ]! p
```
3 a9 u. h* z g9 D' y/ z$ L/ L3 ^. J6 h: z9 z
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。* B: z- e7 T& U0 F" b: B$ ~
/ D8 c& t } T+ _! C+ n9 G
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:8 O# t) H1 ]) J; d9 T& w1 Q. J
5 P# m- T D, G7 n$ v- s2 d) { ```
/ T% }: k ] T* D3 t6 v add_action('init', 'create_custom_post_type'); o$ @2 C& u8 w& B9 J% j0 ^, F
function create_custom_post_type() { d, r" O- W5 E y) j! Z
$labels = array(
( @" A6 \; A$ x. [ 'name' => 'Site Wide Notices',
3 M6 Q/ R$ Y" C( d5 @! g% K 'singular_name' => 'Site Wide Notice',
4 I4 Y2 z. n+ L( g9 W 'add_new' => 'Add New',4 J c8 F$ {$ Q& q# r
'add_new_item' => 'Add New Site Wide Notice',& `" {8 ?- {% ~5 ]0 u m
'edit_item' => 'Edit Site Wide Notice',! L9 i. p- n1 z# y% Y+ z' q* B
'new_item' => 'New Site Wide Notice',
% C) l: N" F4 M9 R9 Q2 a; B w: _4 X 'view_item' => 'View Site Wide Notice',
7 G, z& t6 k$ r0 ]7 X 'search_items' => 'Search Site Wide Notices',
' ?4 y; E% R( l+ _( O# i 'not_found' => 'No site-wide notices found',
F2 R2 ?& t% ]: s 'not_found_in_trash' => 'No site-wide notices found in trash'/ c' @$ e# R( }( u& c
);' C3 s* D0 |5 j7 ~ L* W
$ H2 i: m7 Q6 V; b
$args = array(( @/ N6 t/ n" s' s6 U
'labels' => $labels,
7 ?8 T4 v5 r ^/ G* l 'public' => true,1 ?% E' E! v# M7 l4 ~% d
'has_archive' => true,. G# e8 S' |9 q/ {2 g9 b
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
# `2 K# T: M& p) l 'taxonomies' => array('category', 'post_tag'),3 _$ g; ]6 f! W9 k, A
'menu_icon' => 'dashicons-megaphone',
/ \& N' X2 P7 Z5 t6 y8 ^, L; b 'menu_position' => 5,' x7 \" }5 Y; C
'rewrite' => array('slug' => 'site-wide-notices'); m0 }9 M( u3 a9 Q& B5 q) \( H; @
);
( q' s5 J# R. c" g1 P' }; L
* m- K6 a- F4 Q: V register_post_type('site-wide-notices', $args);
" b5 U3 u' j, C1 q7 e" d# { }
( m9 }' j* n. t2 ]( r. j ```
}( F/ w8 i: F1 {- A+ _2 x
2 Z/ H! M- ?6 c/ o4 o1 A 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
1 M+ P3 n" B& j- n \8 J- f
7 d# j8 B! c; A4 S9 O5 N% ~+ {3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:' ^5 i' E! m4 ]) e2 V% \$ o1 T( O
6 ^5 w: |5 ?6 @; o2 Y& H4 z
```
& U" [# W! c8 t* }; ~1 c add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
: p/ h- t8 p F function add_site_wide_notices_boxes() {
8 `. x3 a: Y! \ add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');, A1 x4 _; U6 D) x6 |" X
}- ?) O; [( o5 ?6 W
6 g7 y8 O" x5 i- r" [0 L- r1 _
function notice_details_meta_box($post) {
, c! ~% e2 J" R: b0 S1 H+ _$ @# f; C wp_nonce_field(basename(__FILE__), 'notices_nonce');1 y2 r+ A- F9 z1 q
$notice_title = get_post_meta($post->ID, 'notice_title', true);
& I5 I# R4 ^+ O# U1 b $notice_content = get_post_meta($post->ID, 'notice_content', true);2 Q8 G7 p' v2 w. a( ^3 t
?>) q9 `1 D! Z# h* M- B9 Q9 ^
<p>4 F0 y, F3 N# A4 u& p) D$ G5 u; M
<label for="notice-title">Notice Title</label><br>& ?" I C3 Z$ o. m
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
$ h+ l& e8 G1 K. t8 I </p>
! n& \. X3 C$ K! o! h; D$ ^ <p>9 a; _; P4 `) p+ T5 e% }
<label for="notice-content">Notice Content</label><br>- E1 F" E4 N9 {$ H/ t! U4 m
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>8 E: m5 K* a' C9 K" @
</p>
' o2 }# S0 x: p, E9 d0 S, }4 ~+ S <?php
8 h9 O" h) X" F3 V. E }( y8 g5 K. N' h- M: ]7 M
' H, x- D) d# d( c; t add_action('save_post', 'save_site_wide_notice_meta_box');8 T6 Y$ n) P& p& C& F. m! L9 f0 D1 Q
function save_site_wide_notice_meta_box($post_id) {0 B* t) X- L% b9 O$ S
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
4 h2 a J% U% M6 ] ]! u return;& I. o- V# J* k" G7 a0 U$ ^6 F3 C. U8 Y
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)1 q/ ~ z$ i( Y- C2 A% g- X
return;
. r/ a$ i6 F* J, V+ V( a' M
: d+ _0 r0 ]" G+ Y) o G9 M; w* } if (isset($_POST['notice_title'])) {! t9 a/ ~6 t2 W( N$ ?$ U
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));4 w( q$ |9 G; _+ i4 Y$ e& D
}9 A7 R; M: B7 _ K8 \
if (isset($_POST['notice_content'])) {
8 j3 T2 u- B2 O# h9 w6 ?, P update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
, w0 ~2 V! l* _! F: `1 ]) W2 m n }. b2 V8 [6 z: x- P; X6 I1 q- t
}, w- L+ R- {" x+ r8 J ]/ {
```6 Z' Y4 W) e$ d# Z* y \
1 Y& j! H* [7 E Z' z! u; H 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。1 f8 m0 m& ^/ U& J
4 D, z' E& @- }" Q$ S
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
; c, Y: F0 z+ M. H- e; F6 V2 j" A- S0 p1 V* s
```
5 x+ k* M: ?$ l! C/ C $args = array(, Q- r ]# y# l& {1 d
'post_type' => 'site-wide-notices',' v% t+ v. Q- Q1 \% D
'posts_per_page' => 3,0 l& q* S, {7 b, E
'order' => 'DESC',
2 x# G0 J2 s& P* v2 |# I+ v 'orderby' => 'date'4 { J! B) K1 m, n2 c2 U: `
);& E$ T6 Z2 z' s- c- f' h7 O+ Y
$query = new WP_Query($args);
; U$ Y2 d/ s7 X: Z if ($query->have_posts()) :. E; T" Y" G) h& A: }
while ($query->have_posts()) : $query->the_post(); ?>
/ z1 W, G7 ]8 a- Z s <div class="notice">
- k! B( B8 }6 F+ n <h3><?php the_title(); ?></h3>
: T" G3 H) R! j- Y, W <div class="notice-content"><?php the_content(); ?></div>
( \# n. c* b! s# @( ?% g# U% H' t: ? </div>: u5 a R# O; h$ T
<?php endwhile;
5 M( D; C, P' [ wp_reset_postdata();
2 e7 ^; ^. \5 y F7 o" g# e endif;
8 Z2 i, w" a' H7 i4 G. ] ]! C ```
2 K4 R2 |' L( Q- ]; I+ h6 W' a
- `, h( t0 f+ h8 n& A' P% f( x, W 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|