|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
, s' i( P s7 |9 b1 f7 m/ L. u+ _5 v4 f3 M) Q9 N
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。2 T. c5 R- U( e0 {
$ p% S+ y& L" o+ [5 N9 f以下是创建自定义插件的步骤:
7 {; L, Y' I9 z, `% s3 @ n- c% K9 z5 ]) Q& ]
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:" V: Z6 C& z0 T5 s6 g9 V
" Q$ E1 K5 z' X7 D ```2 R' h( J0 `9 d
<?php% r9 Y. k9 |) _ o: j9 d
/*
9 M$ D3 |( {- h Plugin Name: Site Wide Notices Plugin- ~) T4 c8 \+ }
Description: Adds a new custom post type for site-wide notices.
! Z4 i' p! y" N6 k- k Version: 1.0* z# |, b. U# g# U" j
Author: Your Name- ~8 @, P1 l( F R
Author URI: http://example.com
+ X* Y$ B! t3 C */
" z. c- @. D1 |; o. l: h
' s: [+ d# M& v4 Y9 Q k // Add plugin code here...3 \4 R" K6 g4 G( i3 d- V5 q6 A8 R
```# b% u% }( A0 d+ l* [6 ~
2 B3 N5 g: b9 ^. G
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
Q& `: b" w* n, W/ z0 z) ]; F3 {' |$ H* J0 H, d
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型: Z5 J& z* u* q& y3 G
* N% ]5 T# D* b2 M% K" _; T
```
7 @# A" a1 N6 L- J! b# q add_action('init', 'create_custom_post_type');
; [5 B7 i0 m3 _$ U; `' ` function create_custom_post_type() {6 `6 G# r7 F8 I& g" E) J
$labels = array(
4 T6 W" c. f$ u K) s 'name' => 'Site Wide Notices',
" j' e- J. G9 ] 'singular_name' => 'Site Wide Notice',
+ v% A9 L2 O: G% e 'add_new' => 'Add New',
8 }, N; x: S. ^# g) S* M/ B 'add_new_item' => 'Add New Site Wide Notice',2 K' b) G+ K1 B1 }" L
'edit_item' => 'Edit Site Wide Notice',
: V2 U7 m, Q+ x) b' _ 'new_item' => 'New Site Wide Notice',
( ^/ ^+ g A: U" O% {7 K# x 'view_item' => 'View Site Wide Notice',
# ?: Z( h; `; ~: @0 L7 H 'search_items' => 'Search Site Wide Notices',% h. L4 ]& x; Y# t5 g/ R
'not_found' => 'No site-wide notices found',
2 P: V7 x t9 P0 ] 'not_found_in_trash' => 'No site-wide notices found in trash'0 f( e- V8 U* R2 r
);
# |1 Z( G+ [- }. ^5 U# L1 V( K8 N! @0 @
: l: F# B- i9 M $args = array(* a+ R) ?8 {& H' k8 c3 V0 i
'labels' => $labels,
) O0 i8 J q0 V" h6 ^# h 'public' => true,: m. W _2 q; ~. I2 I8 Q
'has_archive' => true,
0 G* H" H2 V# C! t% f+ m$ X8 r& d 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),1 Z3 [0 F8 m9 X% M6 X2 c0 a5 a
'taxonomies' => array('category', 'post_tag'),$ x5 N) [9 F% l6 Z' a7 i% V! f2 ~; f
'menu_icon' => 'dashicons-megaphone',
. Z# v1 P9 r- M' S 'menu_position' => 5,
0 p: e' O7 ^8 u7 ~/ d9 ?6 M2 q) @ 'rewrite' => array('slug' => 'site-wide-notices')( P8 p0 ~& O6 T
);3 N$ ^! e( S: @- p) j7 M2 y
- h0 F: P# O0 H
register_post_type('site-wide-notices', $args);6 e; L/ }. C, ?
}! R* C; N; T5 ]
```" h/ s) U& m; c2 ]! l. g9 x
I6 ^) j6 [' k M1 I: C# Y
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。8 o& i6 ^" b) k3 h
. ^7 B6 Y8 L6 U0 g8 Z( M$ W, d3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
" B& z9 S+ C* ~6 f
) |$ Y) d1 t+ z3 U) F7 _' e ```
' l4 c! f3 J: B add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
" v# `! X# v9 G function add_site_wide_notices_boxes() {
2 K% Z6 w' D) n* J add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');6 C: i" w/ p7 p& c' q
}* U$ V. a& h; R
0 T, A' }/ k1 ^3 y$ @! x" t function notice_details_meta_box($post) {
9 U- }( D, ]0 v wp_nonce_field(basename(__FILE__), 'notices_nonce');
. s1 V4 j! [( ^) |5 f: N $notice_title = get_post_meta($post->ID, 'notice_title', true);
' Y+ ?, g6 ?) j, }5 ?$ l+ L $notice_content = get_post_meta($post->ID, 'notice_content', true);0 ]8 a+ g& x( B3 z1 L
?>. `: X! e1 M1 W# c8 O; {. R
<p>- i L& O$ I: x) u9 d* F
<label for="notice-title">Notice Title</label><br># }4 M# ^" t! I$ O5 w$ K
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">4 E u% B: m0 Z' ?# M7 M, K
</p>- v' Y! A& q% R: |) {
<p>
+ |5 F$ u$ D* _( @! i4 {! J, e/ ? <label for="notice-content">Notice Content</label><br>/ o, x# B/ R0 n# I+ K
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>- z( D- k4 x0 T
</p>; O/ u: N& Y: M
<?php
% }$ M8 o3 l5 k. ?6 g0 G3 ? ~ }% n- O% |9 o6 B: `& r
7 R f9 G8 x, C- @/ O7 n add_action('save_post', 'save_site_wide_notice_meta_box');
w2 |: Z0 Q8 x s function save_site_wide_notice_meta_box($post_id) {$ t8 v) T( O+ W# P
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))): F U; g- |4 E- E" y
return;
U/ R. G @; x" ^' u- v if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
0 c0 _/ T+ M. W3 M return;5 ~; g1 p* W" D! `
* M& T' y# i' Z0 m
if (isset($_POST['notice_title'])) {
+ ?: R& B& @1 S3 l3 w8 H; u update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));) V4 F/ ~8 `* p9 t8 \2 {) N. W. _
} b( V" h0 c; k* [0 S, ~) u
if (isset($_POST['notice_content'])) {
* k! `+ O3 a9 G1 W update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
+ A7 y% E; p& D' d& c }
/ O, I/ W+ S3 a }. y2 h+ p0 G0 o7 A# u+ X. G, e6 F$ x
```
2 j! D: J+ k; U2 d3 y' u0 ^* m! f1 C1 ?
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
: t# G) d9 @, b+ l
& t) Q7 w# y; e- p" d4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:8 f2 U. O G9 c; n- u
, m" [: j3 d* B- c ~/ M ```7 n2 q* p: \5 n& s" H7 r* ~
$args = array() l9 y, d% T+ ]- H, ]; G
'post_type' => 'site-wide-notices',
# n- [8 s) v5 g 'posts_per_page' => 3,
4 L+ `3 ^. \) d/ [( p 'order' => 'DESC',
( x8 X1 K! S. Y0 J+ w$ P 'orderby' => 'date', I* C4 j1 ^* S
);
9 P0 k, R% g& x' y- t* D $query = new WP_Query($args);" {9 x# n A: @& m1 U
if ($query->have_posts()) :+ w* o- U! s: q; ^( y( i. b/ g
while ($query->have_posts()) : $query->the_post(); ?>
3 d- q4 K! s4 t ^- X" J <div class="notice">; ~9 a! y4 x* u7 A8 y
<h3><?php the_title(); ?></h3>
, e4 I( P, m3 y! S/ U% T1 ` <div class="notice-content"><?php the_content(); ?></div>( t) I6 `/ F2 U! g; X: Y
</div>
% c& o2 {4 G& O <?php endwhile;
7 N% I! U6 r: z. I+ A5 m wp_reset_postdata();
) U2 q) j2 B% \5 `. T9 i0 {, H* | endif;
w$ V$ h3 h$ D2 E1 |& p ```
; }& j3 n$ F# }+ [, f4 O% H; \8 u5 z: q1 D+ E+ b
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|