|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?4 |2 w- F o3 ~! y
% X4 d: p) T0 E3 g如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。1 B% }+ B n+ U3 F8 _
- o9 d7 F' w' J, T: U
以下是创建自定义插件的步骤:% V) t2 { f; H; R1 x
# i ]9 U0 |; Q& G7 B% _& Y2 M/ K
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
8 j6 ]( o( ~# p& @
& n" N, I$ P- ` ```; c5 H1 V) S0 l& p* B- @
<?php' Q9 P. X. {( A3 s2 k- B
/*1 {7 ]2 |( d. G: _ S
Plugin Name: Site Wide Notices Plugin/ O! r; i2 S; ^4 {
Description: Adds a new custom post type for site-wide notices./ m7 |% J2 [2 k: ], H
Version: 1.02 G' W' G3 t/ R+ K1 F: \! ]
Author: Your Name
: @% X% C. M z* s: `- F, r# o Author URI: http://example.com
/ d5 Z! w- H' z! y+ y7 l */" U% u2 D& y, b
" R$ o0 k3 u3 D% |
// Add plugin code here...
6 h6 \. a9 s% P' f ```
8 ~/ @" m# v: }; R, H( n: a
' V. O% b4 c1 t$ ]7 V3 C6 b0 B0 l 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
% `( V' T4 L! ]! C+ ~8 P- `: U) K7 e7 ^6 m7 ?
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
# c4 e3 _/ p$ K( N/ |# d" s6 _: A- M$ D" u- g1 o
```+ Z0 H4 Y7 P V$ n3 T; \
add_action('init', 'create_custom_post_type');
0 B \2 I0 x3 h) |5 _7 c function create_custom_post_type() {; @/ C* A( x# n) C$ D& O5 Y7 Y
$labels = array(
7 q. h: R9 t5 h 'name' => 'Site Wide Notices',
2 c) Q( A8 u) M; i6 N n3 j1 ~ 'singular_name' => 'Site Wide Notice',) L" h R! Q- n0 O" F
'add_new' => 'Add New',5 M, Y+ S- ?& Y
'add_new_item' => 'Add New Site Wide Notice',
% H( ^: ?& `! ^7 _& d# l/ `( E5 q) | 'edit_item' => 'Edit Site Wide Notice',
, f+ j/ D q( c8 v 'new_item' => 'New Site Wide Notice',' j. s4 O+ B" t! C0 m
'view_item' => 'View Site Wide Notice',
; u0 k w9 X# ] 'search_items' => 'Search Site Wide Notices'," t% c* W3 |+ m4 W- I) d
'not_found' => 'No site-wide notices found',
+ F9 l) z8 a" t$ ?- h! I 'not_found_in_trash' => 'No site-wide notices found in trash'
; u, K/ d, Z% i+ m" p' f. I );0 z! ~) {& Q& S- L
+ k# b, T* E- ]& B. Z$ m# b $args = array(
/ N2 e. n5 F# q 'labels' => $labels,5 J) |; T( y; c2 |) H
'public' => true,7 D! W5 P9 Z. |# R
'has_archive' => true,
u: \9 B: C. W 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),# e7 G2 e j" R5 m
'taxonomies' => array('category', 'post_tag'),' A# t9 p( _/ ]# D
'menu_icon' => 'dashicons-megaphone',
* m, m. Y3 j3 ]1 d) R/ v 'menu_position' => 5,9 v% b K Z9 h' B4 W2 O# ^
'rewrite' => array('slug' => 'site-wide-notices')
& T; S7 t8 v, z );
' k- ]( ]& J! ]4 r% Q$ }& ]5 S- z$ X: @
register_post_type('site-wide-notices', $args);# w7 {& s( a2 v4 C: Z$ Z
}7 L7 B, ?$ u' Y9 ?2 |0 }
```
+ H6 X' e6 k/ }- |* A# N/ b6 p; s% i- A
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。) y" S9 [8 y1 e/ F& Z0 M
4 F- E5 Z+ q6 x9 ~: N0 E3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如: {# [# B; B8 ^0 g+ p: `
; T8 z3 ^) G) E$ b3 Y: a
```( `4 \5 F6 }, O) x: ?$ L3 w0 R
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
& [* N" u* f, S9 G2 ~$ H function add_site_wide_notices_boxes() {; ^+ X" c) W3 d1 g0 ?
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
, e. Z/ n9 N: z }" y- h5 [2 p3 ^& s/ {
1 [2 q9 k8 z- v function notice_details_meta_box($post) {0 H9 g* g" ]- G( F. v
wp_nonce_field(basename(__FILE__), 'notices_nonce');5 E8 l0 q$ b* ?7 H
$notice_title = get_post_meta($post->ID, 'notice_title', true);
+ z7 i9 a5 h' F $notice_content = get_post_meta($post->ID, 'notice_content', true);6 H% G0 i1 a. A9 b
?>
& I8 X. S1 e3 X$ j9 B0 J <p>) D& m; [) k: |' P4 y
<label for="notice-title">Notice Title</label><br>8 H0 l c; y8 ^& Y5 H8 C) C
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
9 Z. a" h# x) g4 ^# O </p>
9 G7 u7 T' W1 w7 K <p>
0 q2 b7 w, U4 w <label for="notice-content">Notice Content</label><br>$ ?# p0 h2 {; ~% p V* g2 r, P# ?
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
6 Q% e' B+ u8 o( y$ _: j1 {/ E </p>
6 w2 b3 y0 n8 E: e <?php
* I0 L' C2 d8 ^5 f }8 E* L& r7 }( |
2 | |9 ]8 v; i- v+ a add_action('save_post', 'save_site_wide_notice_meta_box');
- w1 d+ x& M+ U8 F [. q- P function save_site_wide_notice_meta_box($post_id) {; i: {1 L4 A3 g% x( r2 v- H
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
/ b; v# Q; q( G3 ^! f return;* B( z: z& j% @9 C
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE): p6 T- B/ r0 d
return;6 M7 N& U I" C B, T
% ?( _% S0 P$ U if (isset($_POST['notice_title'])) {6 H0 V* E; q/ }- R! w
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
# Y& R _" u& H% c3 P; U }
2 Z/ W) s. Z- t) V5 L" u if (isset($_POST['notice_content'])) { f( Y. \) @; a5 J* D( z
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
{, R6 n! G6 Y6 n }$ Q1 }4 t0 ]. f; o" S; M
}
; f/ P7 q4 N# l ```
8 k: G2 c5 H& k3 B0 G' e" }3 N0 V K. c
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
) f. j( K+ _. f: \6 e% j, q! t( B% `! r% }7 T( b" s a7 W
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:% _8 D3 ], w! @" G- ~( U S" L% E
5 t& S, O+ ]' ]# K ```
% Q' j; r5 }& d% ^* A: @: [8 i $args = array(
, L, W3 U! C) v( |( I4 q' t' T+ y/ H 'post_type' => 'site-wide-notices',) H- S+ E8 Q' z3 D/ R
'posts_per_page' => 3,# H, ~5 o$ T' y& c+ H
'order' => 'DESC',$ d& n+ Z# J7 b* S- l$ C
'orderby' => 'date'
$ k6 m- j) O6 o% b8 @% { );
' S( D/ h5 E4 m1 Q $query = new WP_Query($args);1 Y- s& v# [1 d, m. {0 s2 k
if ($query->have_posts()) :* u/ r3 e: S+ J6 W- s/ b! q
while ($query->have_posts()) : $query->the_post(); ?>+ B. Q0 F+ Q1 o$ h
<div class="notice">0 M, g# h; ^; ^# C! s+ c
<h3><?php the_title(); ?></h3>
- f) k$ U3 X# I2 g% e! d <div class="notice-content"><?php the_content(); ?></div>' B2 L9 E E6 _+ }2 w# a9 L
</div>3 [5 ]" C/ H( G' O
<?php endwhile;
0 D8 y6 {3 M! {0 i/ _( H wp_reset_postdata();8 F9 a- [ S" O
endif;- L) S' \1 l0 \+ p
```. X* F9 i! y' h0 {/ ?( C$ I) Q
/ e- _5 L3 y. o: f+ K" t" m" [ 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|