|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
9 }! }+ d& K' O: m4 T. Q* P1 a- d8 D1 f: h. E1 m& R
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。$ d6 {8 w; o' J, z& f
( M( L$ {) Z0 A T1 a6 L+ k4 Y以下是创建自定义插件的步骤:
4 ]6 k0 _9 P5 }8 k2 O# J
' l- H5 g/ K! N, f1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
7 w0 W6 n1 ~! m. |$ Q) ]7 J1 F5 m0 t9 s7 M$ n9 |* y# x
```7 v C& G8 s) C, A; W0 Y5 C
<?php
* }2 f* [8 o* S! X/ p5 | /*
8 r+ Z8 @4 G9 }2 l- z, O Plugin Name: Site Wide Notices Plugin
& J, t w9 q# t1 K Description: Adds a new custom post type for site-wide notices.
% y, @4 z3 r( l z; e' F$ J" z Version: 1.0
9 ]; \) C2 G. I& u Author: Your Name
s+ {* x- ^2 i, } Author URI: http://example.com
- d x- c1 G+ D; | */
) d% U9 o" o5 F- y2 v3 Y2 W
, w* Q: ?8 Y) ^* k1 h+ A, J // Add plugin code here...7 E, |. V% @/ c! K/ w1 u
```
0 R; ^; _4 {! h9 k2 }
) \* n4 O$ \) e7 @! J" D 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。' _9 h3 o2 a$ q+ d7 x; V ?
& W5 x8 e# E6 ^' r: A
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:5 `1 E1 ~. [# j4 z+ q
' h3 W" [6 ?- N5 n V3 J ```
. [* P2 c$ v1 l: o5 h add_action('init', 'create_custom_post_type');
' z2 Z' k; ]6 \2 V/ U! {" S0 J* ^ function create_custom_post_type() {
* b( u! v8 m5 }8 f $labels = array(
& ]4 B+ B9 J u 'name' => 'Site Wide Notices',
* o9 A% v" P% B# H9 F6 @0 q 'singular_name' => 'Site Wide Notice',9 F- E. R" i3 j: m1 ?% J
'add_new' => 'Add New',7 t- J C8 F9 y! p3 i% ~2 @6 x
'add_new_item' => 'Add New Site Wide Notice',7 K) L) ?* e3 G3 A& c
'edit_item' => 'Edit Site Wide Notice',: i9 q1 t0 x6 v/ n0 [$ o# m( G
'new_item' => 'New Site Wide Notice',
. j6 g; f. m! t0 D) Z0 d 'view_item' => 'View Site Wide Notice',
$ G: u4 j; }6 t: k; U 'search_items' => 'Search Site Wide Notices',3 s( H0 Y4 P- ]8 l/ T
'not_found' => 'No site-wide notices found',* I8 B0 q: x( y8 j `( F, ?' O
'not_found_in_trash' => 'No site-wide notices found in trash'2 z9 A( b5 e/ J$ z) E6 O
);- Q) S! \) n/ Q
9 k# A! z: y6 l9 A0 i $args = array(
4 b, f2 N+ L1 a5 \/ N 'labels' => $labels,5 _5 [# z2 d* b1 z7 N
'public' => true,
7 r& n! w" A+ S 'has_archive' => true,5 q# G; k c8 g
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
4 Y3 }9 `0 S; J5 f# r9 k: ? 'taxonomies' => array('category', 'post_tag'),- J9 r0 C" ?2 M8 c, Y8 \ @0 i
'menu_icon' => 'dashicons-megaphone',
# \( L+ k3 L4 v# I2 Z 'menu_position' => 5,
; }* U. T0 b$ ?+ ?7 K 'rewrite' => array('slug' => 'site-wide-notices')
! ?. {% {8 E3 v- @5 C3 |) i );! I+ a/ O. j I: u: i! P4 m% c
/ M/ g: a& k! @; |+ D+ g
register_post_type('site-wide-notices', $args);
! o4 e* q6 {9 N/ E1 }; t( Z* J( M }6 q0 \; C7 }% E' y }
```
2 F+ @4 q' ~: ~. b4 T2 A
$ {9 D4 ?/ ]; ]4 M5 P' v 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。3 j5 O* U# c- Z% N
3 ?9 ^' o+ k: v# T3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:& P9 \; _! U5 M; U3 l* H4 a
6 A5 V; P! f& t) ^% M0 l5 c2 D
```
! p5 o4 ?, U" B1 F; Z! i add_action('add_meta_boxes', 'add_site_wide_notices_boxes');: E% H l1 K R# p3 k
function add_site_wide_notices_boxes() {: B& }4 w; w3 |- v: s9 I0 G
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');0 V! Y3 I6 d( i( s& P& c4 ]. Y- l+ X
}
7 F5 y* w P" X) }% H/ V) T8 p" [
function notice_details_meta_box($post) {6 g% l3 x- U5 L" k' a3 s
wp_nonce_field(basename(__FILE__), 'notices_nonce');7 i/ ?% b3 \7 x, A( `5 R
$notice_title = get_post_meta($post->ID, 'notice_title', true);
* l+ [6 ]9 N/ A U6 [ $notice_content = get_post_meta($post->ID, 'notice_content', true);
8 z& w( f3 r/ n) B ?>
) U& w/ B3 X( m# k3 t' ~8 n <p># F1 i y% v1 X9 w
<label for="notice-title">Notice Title</label><br>
9 L" [& ^9 S# @- x9 ` <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">9 Z2 w8 n9 B Q9 B8 w- c4 }
</p>
; M) E0 I8 Y8 Z; k! M- S <p>
3 B$ m/ ]4 c+ ^3 E <label for="notice-content">Notice Content</label><br>8 ~" D0 N1 ^7 n* y) q6 m
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
/ Q" _$ a4 r2 y/ D </p>5 W1 C1 V4 L! P1 Y
<?php; T: O+ [3 Q4 D
}
3 @8 I. l+ e0 Y0 \# u$ G/ K7 Q
! X* E7 V7 m4 z! w5 R; L add_action('save_post', 'save_site_wide_notice_meta_box');
* P& C x- Y9 ^+ l$ d! ] function save_site_wide_notice_meta_box($post_id) {
8 _$ V3 H9 j! W- M7 h, u if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
" s v" ^" U' u1 { return;, O8 J& e$ F, p
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)) M8 V( D! x5 M" v% {. [9 C+ v( `8 }
return;% ~2 y% K2 n* z, i; h, S1 J- W" s, }
" K- r v- ^4 ~2 k% e
if (isset($_POST['notice_title'])) {
& C+ @6 j: T: K5 J+ A update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));9 d& r7 D! J1 C2 g& ^( |
}8 [! u' v2 W! o5 N0 _5 s
if (isset($_POST['notice_content'])) {
- T4 ]: `+ |7 H& |2 o update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
. I% Q& v. ?( R# p6 k$ w }
4 s0 ?1 F2 ?' n* e }
7 M4 c/ W) ?7 L: @ I ```
- K. V- M- ?; R7 @8 ]: E
. @. g; l" [" \ 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
1 b/ u% l' h5 J5 I. e+ O Z p& J
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
2 t) B( R0 Y, i4 P
0 S# }1 g: n4 T8 V& f6 w ```
1 u# n# O, h3 t" c- w $args = array(
6 c; Y" t: | L u% @8 b8 \ 'post_type' => 'site-wide-notices',
8 J- \6 V8 C. j) A+ s 'posts_per_page' => 3,
9 ?" f, W: a- U+ g7 F# u 'order' => 'DESC',/ {0 m, ^ w1 a( O3 U
'orderby' => 'date'- ?7 s. z/ p, N( H' j6 w& Z
);
/ x. w, \" m4 J! H0 F$ k( ^ $query = new WP_Query($args);
/ N' a' G( C, A if ($query->have_posts()) :
: X" W* v8 _' h2 [: _4 b6 R3 _ while ($query->have_posts()) : $query->the_post(); ?>1 |' v6 u l: a% Z
<div class="notice"># B- S# r5 u* Q
<h3><?php the_title(); ?></h3>
1 O+ |. F7 u/ C; B: S6 _ <div class="notice-content"><?php the_content(); ?></div>
9 d y9 M/ v* D: Y7 ~1 T$ t1 s </div>
3 y( W7 ` E% u2 H; S <?php endwhile;
; V6 i/ ~) x* ?' v' m! u wp_reset_postdata();1 I/ g1 D( [: D6 Y6 c& H* D
endif;
: a8 G% k! ~7 C4 h. Y4 q- p ```
5 H9 ~) l" R/ E2 D1 D8 a A+ T, H7 }4 O
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|