|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
5 P; Y; o( s T. N4 P; X* ~) d3 ^9 `" `9 E) C9 i
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。: C6 ]' q0 V {% t- z. a j
2 q0 p, ?3 j0 K3 b6 F# _
以下是创建自定义插件的步骤:) I3 @* Z9 {1 q5 O1 F
8 x0 T9 V' g6 Y, d% ]8 M; y1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
' W4 d: y9 L% {$ @
% J2 e4 T+ y; O. ^- f" j ```
. Q0 P' Z) N+ N. V' S <?php7 D' F- ~5 k1 w" Q5 w8 Q1 z8 {
/*$ |! X9 w N) O: r3 t/ m7 }/ V- F
Plugin Name: Site Wide Notices Plugin" ^' x9 l+ s: {( L# ^
Description: Adds a new custom post type for site-wide notices.
% _1 |+ ?$ I# p Version: 1.06 r, q9 t& I/ R0 X
Author: Your Name% o. K7 t% |3 c- \" z1 O- V c
Author URI: http://example.com
' m5 P, {6 Y4 y( D V */
# D# s( E6 e, ?# M8 t& z
# W- i9 e; K+ g& z8 ^/ X; b // Add plugin code here...7 b2 ?9 F) v" E" r S. X+ G
```
8 m+ H- W+ A: Z# j6 F+ [# Y( \6 R! S5 e
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
. O& _& c" e) P7 \" R( O
5 z* I! {# `' o2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:' G' L; j6 k" K
$ C9 m) W' Z9 d& L9 `6 Q
```
2 R9 g' p! h$ D+ g) O r add_action('init', 'create_custom_post_type');' T3 f3 x M8 H& l$ X
function create_custom_post_type() {* r" ^, \5 m9 h) D) q* s4 _
$labels = array(
2 L& h( v. D$ S; }1 C5 i& o 'name' => 'Site Wide Notices',
5 I. m; N8 Y6 J3 A4 e Z% }6 d+ M 'singular_name' => 'Site Wide Notice',9 a0 J8 }6 Z9 l+ Y, m; g4 ~
'add_new' => 'Add New',
7 G- F* F5 Q$ g$ I 'add_new_item' => 'Add New Site Wide Notice',
K8 Q0 l) ?2 v6 q 'edit_item' => 'Edit Site Wide Notice',; T! Q$ ~$ \, t9 \/ i. f
'new_item' => 'New Site Wide Notice',8 X+ B; z2 |0 z
'view_item' => 'View Site Wide Notice',
2 x/ @/ z2 t% |& z& ^ 'search_items' => 'Search Site Wide Notices',) a( _8 O5 U" }1 P# K
'not_found' => 'No site-wide notices found',* K) U- j5 Q+ k& F7 [# _
'not_found_in_trash' => 'No site-wide notices found in trash') w4 Z3 `" ]/ A
);/ {& h, I% N6 p, }5 S, a
- W; {) [0 @: |; U) o0 L6 ~
$args = array(2 f8 ~2 V7 C0 e, G' ]" g6 m/ I
'labels' => $labels,; F& _2 k4 ]3 }5 e# r
'public' => true,
3 u( A' ?' s9 J6 t+ `; F 'has_archive' => true,
( \% q; `3 Q5 F& Y: F( H( l 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),! ^2 P0 ^0 Z) D& l7 X' [% _
'taxonomies' => array('category', 'post_tag'),
7 M; M- e2 H6 _3 B; @7 K 'menu_icon' => 'dashicons-megaphone',
$ X: {) x4 Q# p 'menu_position' => 5,3 r4 W% l' m! x, }9 r0 J4 F% T+ ^* d) R
'rewrite' => array('slug' => 'site-wide-notices')5 P& j+ e3 V' e7 u# _% `% t
);: [( ~" Q2 x/ h
4 X2 J. q1 D. j
register_post_type('site-wide-notices', $args);
% N1 l4 R6 m. y/ G# L& Z }/ }, o w# p$ n9 `. [+ Z" f! |
```# f5 y7 b+ @. S K
# U. Q$ @9 C; U' F; g4 i/ T5 G' K 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。( A8 ]4 E5 D" J4 n# S
2 B' N3 }; @) Y
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:6 q/ w' i2 R3 w
# c4 w% o" v1 t: s1 b0 V7 F ```
2 {" I" e1 ?2 y% j$ p add_action('add_meta_boxes', 'add_site_wide_notices_boxes');/ K- `6 `5 j: E) ]8 R
function add_site_wide_notices_boxes() {; b3 I! Q: U" w; t
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');/ \. e6 ^- d: C+ b! Q! W
}
9 ] v% p! X' Z: h5 a4 p/ S$ X& n& C' f9 ^# z( c; p
function notice_details_meta_box($post) {
" Z! w& C" M' ^0 c, f. k9 d wp_nonce_field(basename(__FILE__), 'notices_nonce');
/ b- I9 x( O! j+ k( }( d3 ` $notice_title = get_post_meta($post->ID, 'notice_title', true);% ~9 H9 u! e: O" k% {. \8 p
$notice_content = get_post_meta($post->ID, 'notice_content', true);; q8 Z2 `# S" d y: r) @
?>
' K4 | j) w- a$ _ <p>
* P. C0 i8 }; z+ w3 \; ? <label for="notice-title">Notice Title</label><br>) C1 Z! B Z3 k
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">/ u7 v. U$ W/ S- a" P
</p>, }; c D6 @; L; J
<p>
@4 X8 W; J5 H! D! p0 J <label for="notice-content">Notice Content</label><br>; w% N9 h2 }2 S' m7 M2 }' O
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>" A: a+ ]* h; n+ a" f6 h3 T
</p>
6 W v! ?7 Y1 q <?php
8 X7 j- G, f7 A+ l- W6 @ }
( a5 d9 J' ]7 }" j+ b& o7 ]6 c0 N4 E+ ~; e
add_action('save_post', 'save_site_wide_notice_meta_box');, d8 w) ?; `1 m: p0 U, A" L$ ?) O
function save_site_wide_notice_meta_box($post_id) {
8 |9 c5 ] Y5 p" H5 u% D0 B0 ] if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))/ O6 b/ g' l4 m
return;
5 n. L- ]9 D* C. v, m if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
; ?1 q# s, q; x3 V. o return;4 {; D* p! V$ K X8 m- o% ~$ i
' W- S: Z/ Q" ]% l if (isset($_POST['notice_title'])) {7 L& N4 j5 A) b$ W
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));) l% \( b1 L3 |4 x/ p' w! W
}; x N; X6 C$ S7 u, ?+ O
if (isset($_POST['notice_content'])) {
7 y, O' m+ D: U update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
3 j6 i* a7 x0 b$ J1 h& ? }, l% b0 W, Y: _1 V2 L8 s) {
}
4 @$ ^: v- p- z2 c' y' o) x ```3 D9 i2 W% B: p+ Q+ W
+ U( v& {8 q* k \, z. ]6 t2 V
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。) h' b, E& x0 [. L% R: B
" w* e, Z% R$ E' P% p: G+ h0 v
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
2 ^2 k, \+ S. i% H1 a; U9 n% O# b% u" ?, w3 S+ T1 u
```% }2 ^: q3 d) y; N% F
$args = array(0 o" d, ]: x# Z& a
'post_type' => 'site-wide-notices',
6 U1 a9 v+ W! D* D k( i 'posts_per_page' => 3,+ \2 G/ E- L4 g1 y) L+ `# p. D
'order' => 'DESC',
/ M' d. V3 G+ U: u 'orderby' => 'date'' \+ u0 g0 v9 A& V1 Q$ z' k
);
" a/ N0 @4 W6 U C1 o $query = new WP_Query($args);, K, m. A4 M( ?# r
if ($query->have_posts()) :0 k- Q) G. L8 M9 V
while ($query->have_posts()) : $query->the_post(); ?>
- R% V) h2 y: t& ], { <div class="notice">
* X& i$ B0 [0 u- k <h3><?php the_title(); ?></h3># B0 g3 p/ v7 G2 k/ @# _1 ?; K) \
<div class="notice-content"><?php the_content(); ?></div>
; d' D- d4 b8 B9 v; N' m$ l </div> U2 j. q, ^7 @
<?php endwhile;
/ C! ~# Z" A* O% j' T% ]- E wp_reset_postdata();8 j" }- Q2 E+ D, Y
endif;( A1 J- i, e) U
```
9 h) ]+ e8 F1 `2 S7 l& {0 p# F ]
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|