|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
8 T. N8 i' N$ }$ r; M
; y# N q0 {) H如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。) [8 j$ Q; |1 M
3 n. M- G0 @% h. t
以下是创建自定义插件的步骤:
( f$ L! X& k( a- ^
- i; `- ?3 {; Z9 i1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:7 ~* J/ ^) \8 H* L- J
( G4 [' L: _; I ```
3 Q8 }: v% B- J8 a1 w- F, V <?php
X# c. N. F+ v9 C /* @) f4 H- `% P n, U
Plugin Name: Site Wide Notices Plugin
8 |8 j8 U* B6 u- c Description: Adds a new custom post type for site-wide notices.+ @, E! E' d$ g4 Z0 Y7 O
Version: 1.0
" m t( r5 W9 _+ g+ _6 e3 k7 a Author: Your Name
- ~$ I* G+ y. O Author URI: http://example.com
( f1 c% {. U7 E- z& U' b */
g6 i( i$ s7 S& P6 V% H5 K \2 u: b9 @$ x& q& N
// Add plugin code here...
/ ~$ C4 h6 X5 v) _0 |" o ```
. a$ A6 p- p, J: Q) U5 m
' d, U; s" p3 o+ E7 E 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
; J5 {) y0 s; w5 f4 [
/ i" \) w2 H d' m# @2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:. ?! O* i8 ?$ A
+ k& B9 J L6 K4 f
```1 W' `9 \0 }/ D0 @' i0 }. m4 e# Y
add_action('init', 'create_custom_post_type');& T( G2 E+ E" W9 _# k n! Y
function create_custom_post_type() {0 A7 f5 G' b: C% o" W
$labels = array(/ p0 G" I$ z. a% X: X
'name' => 'Site Wide Notices',
& m& B z: Q3 x 'singular_name' => 'Site Wide Notice', e! S3 ^$ x% n/ W; e. W* S4 G
'add_new' => 'Add New',; ]9 i7 @, j" n3 B3 K2 c
'add_new_item' => 'Add New Site Wide Notice',
: @& c, \1 T1 r. g 'edit_item' => 'Edit Site Wide Notice',6 H) |+ W5 D8 [' f( x* ?: o V
'new_item' => 'New Site Wide Notice',
. P0 E2 ^. R; V) @ l 'view_item' => 'View Site Wide Notice',
+ S( v* f0 \4 v. x; f 'search_items' => 'Search Site Wide Notices',
6 M6 B H& h7 _ f% p/ [1 G 'not_found' => 'No site-wide notices found',
. y5 Y* | x2 i$ \/ I 'not_found_in_trash' => 'No site-wide notices found in trash'& h; W' u* n. k& A
);% l; J4 p% _8 c( m; }0 _
+ H; ^2 L( i) [, \ $args = array(3 H# q# d: o% v5 E
'labels' => $labels,
' H+ v" i+ H; x& ^) a' S# ] 'public' => true,7 {) x+ ]& [! C3 t; r
'has_archive' => true,
# [/ E- e5 }+ w2 Z! R) h 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
5 i1 n4 J# K& s2 m* G* L 'taxonomies' => array('category', 'post_tag'),' J/ ~. q: n- x( M* X( [. |4 k
'menu_icon' => 'dashicons-megaphone',
2 i+ n9 n/ {" e4 _( N 'menu_position' => 5,
4 {/ X# L# J! b# W9 D, F+ Q 'rewrite' => array('slug' => 'site-wide-notices')" C% Y9 {: ^8 d2 t# s
);2 }( @4 H, c1 X* A7 ^! p! U
/ ^0 Z; ~$ A; @3 A% \0 Q% n' B& K
register_post_type('site-wide-notices', $args);& Z! R# r6 m& Q. T9 Z
}
8 o% @0 \2 {* H# I6 v8 \ ```
9 W3 C# {9 X3 J" l: ~- W5 o% J" J' O" o X- [
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
6 b2 U: i% D8 p N, P4 o
8 S s" e7 l+ M( B3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:2 s6 e5 k. u- ~2 J5 I% k X
+ b; ~& ^$ A" \4 A- f' b" `
```
& h. v- e- I2 q4 x8 e add_action('add_meta_boxes', 'add_site_wide_notices_boxes');5 g( D, h. e/ h, n
function add_site_wide_notices_boxes() {' n* g% k R9 o$ q5 f9 X; o$ b7 W( M
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');3 q5 ~: b' w- P- x+ h( \" q* _
}! ~ P% a! X3 c) q
3 w& o$ P2 ?& G# E" k function notice_details_meta_box($post) {2 K' S; b! `# L5 u# O. @& y( P. D
wp_nonce_field(basename(__FILE__), 'notices_nonce');! t i+ l: }4 m1 _) ?, u3 I" m c) C2 x
$notice_title = get_post_meta($post->ID, 'notice_title', true);+ p4 }# t5 e/ R0 s4 @
$notice_content = get_post_meta($post->ID, 'notice_content', true);
/ o L3 t# G0 s. F2 O/ u, c ?>4 s, h; N3 e: [
<p>
5 w/ C& q/ M( s# V <label for="notice-title">Notice Title</label><br>4 f; h }) a9 p; B' x& `4 W0 _0 v, e
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
. m; C6 d2 I1 S/ k1 _7 l </p>6 O: ^- T! g; G6 ~& ^
<p>
( I% o$ Q, a- z <label for="notice-content">Notice Content</label><br>% A/ y( A8 G- U1 [4 \( C; J! d
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
- K9 }! ]2 f3 G {' N* [4 B </p>+ E0 ^4 w0 O4 G% |
<?php. ~! I2 ~ E! ]; H1 M7 O
}2 ~; \7 {# ?$ d& t) g$ e1 j
; A; |6 \- _5 R3 {9 Z; |- P: O add_action('save_post', 'save_site_wide_notice_meta_box');' s0 E) v- B* h3 S9 s& H
function save_site_wide_notice_meta_box($post_id) {# l. n6 @8 u. M$ e
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))! k1 _7 p- z/ m* L, [
return;
# h+ S6 O4 N- v4 S2 I if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
% r: m) J* c1 v return;/ W D* L3 b, T4 p
1 ^2 m4 @: e3 ~1 d* l3 [4 s if (isset($_POST['notice_title'])) {4 o% m+ g2 L. f
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));( F4 ~2 c% y: M2 G8 x, [
}
' @: i- _% ~; T" j8 v& H if (isset($_POST['notice_content'])) {: d0 q9 z' e* P0 z+ L! {
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
. t/ l7 r) s& c' K3 j& |! |0 _ }1 e$ y6 l" A5 {- y8 p- d1 B. ]4 V
}3 d( w f8 [* l; w+ ?
```
# E# v6 `9 |1 J2 Y& O( v
% a4 ?! {) j; p# q U5 z: @- s$ G 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。& r9 Y5 ^$ b3 q" C$ Z
3 c* _8 _0 n- C2 H4 |
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:' {/ _0 {* U- N
% V& z+ ^' B# S0 J* G
``` c- C" A! b' D* k" k/ i3 ^$ n+ J
$args = array(
) b9 r! [+ |7 _8 b9 i8 L& n& ^- c 'post_type' => 'site-wide-notices',/ c, ~# P; M2 F
'posts_per_page' => 3,
* n* }7 [% N2 F" O) P 'order' => 'DESC',
9 G, e; t1 o4 q1 q5 ? 'orderby' => 'date'
1 z; C+ ?8 Q" \, A! H8 J! f: v );
! `2 A ~4 f9 p' g* s) e $query = new WP_Query($args);2 t# P; I( P2 g+ h
if ($query->have_posts()) :- ?5 k& `0 o2 ]+ H
while ($query->have_posts()) : $query->the_post(); ?>& Q: i5 a* @! f$ v
<div class="notice">: h; W. c8 n, b( \ g* E
<h3><?php the_title(); ?></h3>
4 n* } A) M) [ <div class="notice-content"><?php the_content(); ?></div>: B. @& t" B4 n6 w9 ]: u
</div>
2 v M c7 [) M1 l' O <?php endwhile;
% T8 H, v5 X4 K0 v2 M+ {, w S$ { wp_reset_postdata();7 c2 X9 l4 R' q7 h6 o
endif;# E' ]' ^- t2 J) Q; n
```+ ]! T- N. Z, I% }# k1 k9 ^
$ O. x2 Y2 v& K# y
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|