|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?, c( w6 Q# s6 | O7 W2 }3 T
' T5 G) W2 c+ }0 g如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。" n% F! Y" C& q: |% ?1 a$ Z1 v. B
/ R4 G# ]8 P# G. S8 e" P# r
以下是创建自定义插件的步骤:* @1 T$ p9 |+ G, t) c- Q* D- d, O
* U# y" ^# Z! @" f: d1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
( a3 G h% N4 g d4 a( h" J- Y8 u4 }$ o4 T! j: R6 T, R
```
$ B5 u% s3 i4 v# ?7 `% j <?php$ C7 g8 u! d; D
/*
4 D! f$ b: b! G& U |% i# W! o Plugin Name: Site Wide Notices Plugin
6 R. G& r8 k8 U: X$ |( Y8 o! n Description: Adds a new custom post type for site-wide notices." ]8 m$ k. b0 \
Version: 1.0
+ I! c; c7 Q' x, g Author: Your Name
: D p( C. J9 }' W& l/ ? Author URI: http://example.com
2 |, n( l0 w" p- u- J# a */
# S0 S' ?" L" n; g" F, F4 }0 Y' u1 l: E
// Add plugin code here...9 h% f0 z* A( U/ @
```/ K3 i8 s" z6 c0 I7 B4 j
0 s* j, P: s8 P
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。2 Z) p& K/ l: t5 K- E& w4 R
) a3 k$ A0 ~8 H3 ^( L9 C
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:: |: Z) b m; g# L9 E- u9 T
! W; O& q6 V- g n/ |9 f+ `. N% I2 W
```
. d" z" f1 r1 X1 w, ]3 E$ W, l% ] add_action('init', 'create_custom_post_type');
- y$ y/ l. @2 e& G- f function create_custom_post_type() {
/ \) W- @ {/ _2 c4 l/ X0 L( J $labels = array(
; _/ t5 H- \! }# w; k 'name' => 'Site Wide Notices',
: D2 F. F0 p% x6 Z 'singular_name' => 'Site Wide Notice',* ?+ L/ |. M1 c$ d
'add_new' => 'Add New',& w5 h7 A5 ?% i. ^
'add_new_item' => 'Add New Site Wide Notice',/ v5 `" C) Y3 ~/ n
'edit_item' => 'Edit Site Wide Notice',, ?; ?5 E+ g4 H8 R
'new_item' => 'New Site Wide Notice',
0 j: D8 i+ e) N5 K% F1 h: J 'view_item' => 'View Site Wide Notice',( |+ b: D/ W, a1 [
'search_items' => 'Search Site Wide Notices',6 C: t9 d- G7 t0 [5 ?0 g$ F
'not_found' => 'No site-wide notices found',, [/ c. h! |2 j$ `; E" b, d
'not_found_in_trash' => 'No site-wide notices found in trash'
1 x r, R* x8 p );8 _6 d" {3 V) S
" r2 `) X8 K0 f $args = array(
- Z! D N; }( S3 ?% T' Q( ^ 'labels' => $labels,! `$ D: @# m- P
'public' => true,7 F* l8 z f6 L2 {0 {
'has_archive' => true,# d" t6 }* d; `# S; c- r7 j: V
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),: q2 j0 s2 l2 J7 |( d3 v' o4 D9 m. u
'taxonomies' => array('category', 'post_tag'),& q+ a6 q% C3 A( z0 H8 R
'menu_icon' => 'dashicons-megaphone',7 r' l# A7 u7 F Q) U. a
'menu_position' => 5,
7 E4 ^. G5 Q2 m8 X% M8 ~; o- U 'rewrite' => array('slug' => 'site-wide-notices')
" n3 h0 f u' F% h: Z4 E/ a );
7 s5 C; R1 j2 I' _( | {7 v3 ^
7 V7 T. p+ |" F2 B! J) o7 ^ register_post_type('site-wide-notices', $args);
7 |3 U+ o+ |* Y7 j$ W }
; _" X M4 l: j" _0 } ```
. s0 L8 {% y- X; b8 W, H; l' ?4 e
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。+ P# ?! s8 Q2 _. E. G2 S1 ^! y. e9 P
& n% B8 D. n* o% y
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
. _1 J8 u4 J9 K3 N e: O6 Z" A9 u' ^# F8 G* A$ N
```1 k: }8 _0 q0 m; m: I
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
! r" k: J) [$ D0 v8 ?0 T+ j& L function add_site_wide_notices_boxes() {% u9 h& A- b( m
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
( F4 W( X4 p6 L3 w/ X; ]6 i }2 Q6 m+ }+ \0 l3 d" m
; u8 H# t* J5 G2 u! H; n3 X function notice_details_meta_box($post) {7 z- s8 E- ^! d* }& _
wp_nonce_field(basename(__FILE__), 'notices_nonce');, h0 K# z* ]1 S) c) Q
$notice_title = get_post_meta($post->ID, 'notice_title', true);: \" r* [4 G6 v# r# B
$notice_content = get_post_meta($post->ID, 'notice_content', true);# z* f, N6 t+ M$ b8 a$ x7 H$ u, g, p7 \
?>
% p* t G. v0 Y! t, J! [ <p>
7 S9 G# n' B1 l, U# C <label for="notice-title">Notice Title</label><br>
2 X+ }7 _, E8 m; I2 q <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
p4 J8 _# ?: E( h </p>
1 h |8 ~2 B( X <p>$ l q# A. }' T) v
<label for="notice-content">Notice Content</label><br>$ c2 |/ |, j' B5 W
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
) u/ l4 z, a; j2 \1 `. s; l4 X </p>3 Y' L7 y) y% s0 p1 g7 `) g, ?# F
<?php' L$ V! s! a" ? Z$ B, H
}( o& c/ h w0 V6 Z n, s N
2 F0 ~3 f/ U' |( C( X add_action('save_post', 'save_site_wide_notice_meta_box');
1 M' o h8 t/ B5 c' `2 F1 f function save_site_wide_notice_meta_box($post_id) {" z2 y* V/ S: a( r! m1 N
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))/ q( X0 H' M: n9 z8 Y1 i+ S$ ^0 L( x6 {
return;
0 u& ]; A+ S& q2 T. W f" p4 g if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)6 |' C4 @* A: L, v8 o7 D* a
return;
3 {3 p# v& i7 l" p: v+ y' k1 k& s9 A$ ]% ?) k- ~
if (isset($_POST['notice_title'])) {) p9 X( \- i1 m
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));& L; Q7 B8 n( d4 f. v: n4 Q
} ?" u Q9 N- ?2 H, F9 i1 n
if (isset($_POST['notice_content'])) {
7 t+ y. @8 Q7 y4 K update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));& {1 T! C- h' `- M/ e y1 U
}+ S0 U! F2 l$ r: c6 y- k2 |6 v
}
& I" c4 z2 S$ t. V" T) E& ] ``` @& h( d4 x0 A3 q* m/ d3 K
+ T0 r4 c8 C" i' a) g" S" E0 I5 T 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。5 T( j5 y5 [) u: w9 `! U& |
: h! B; a1 w9 ^! Z/ \$ U7 @/ ^, n4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
0 y" T, D: T% V, R% U0 |' R
; |2 R N8 z5 F* i: L ```8 L/ a0 e9 R7 A, Z d P& g% X
$args = array(0 C: K% ?: [% H. m
'post_type' => 'site-wide-notices',
) a3 _5 N9 r9 P, q7 w/ X _- O 'posts_per_page' => 3,' F5 F D+ q4 d3 q% x
'order' => 'DESC',
8 s+ ?4 h9 L& o# A/ A4 F 'orderby' => 'date'
+ P3 g! [( X5 E! ^5 `+ b+ e* k );5 X. |8 \2 M# j# [0 }, N
$query = new WP_Query($args);
8 H4 K" W: o& \ if ($query->have_posts()) :/ j" G4 A) `/ `5 W1 n* w- F( _
while ($query->have_posts()) : $query->the_post(); ?>8 W2 Q( }$ b& u0 ]- F4 E
<div class="notice">
6 W5 k& v4 {7 O& W1 x <h3><?php the_title(); ?></h3>5 t6 j- F+ K/ _2 ~
<div class="notice-content"><?php the_content(); ?></div>
* l/ I4 [% _8 Y* z </div> w& T4 \% N2 ~5 L+ P8 ~
<?php endwhile;- B2 o# O; W8 p$ l7 V0 |3 N
wp_reset_postdata();' V1 e( C) ~& @
endif;
3 X$ ~$ t/ x- L" _- O/ O& z4 r$ ^% n! W ```8 m! W# y# S! i, ^2 H
2 J$ ?4 {; h, V3 @' Z7 a5 u
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|