|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?* \, X; T' a# A2 l
% ~5 V( I: E$ [3 n/ y# c1 { |
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
: `6 }/ I5 r9 Y v9 P9 ?- o- n' H
以下是创建自定义插件的步骤:
+ g( ^1 |' w T; ?* _: r' g& P8 {# ~9 h/ V w
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:/ `: v2 c- f+ q+ b- R* |
* L" C. D3 L. x. ?' K
```
- ^6 H' T; M3 o B5 r <?php5 B6 h8 H3 m, k9 @* l9 D0 }
/*: ~! ]0 }8 j0 L
Plugin Name: Site Wide Notices Plugin
! w# c6 @/ d" t6 N* g Description: Adds a new custom post type for site-wide notices.
6 w% x, i1 e3 r8 ~: H Version: 1.0
& T' T- A# _/ z0 R( R9 N Author: Your Name
+ ^% C2 ]" n0 Y8 L/ ^ Author URI: http://example.com2 c$ n3 {1 m N2 m4 w$ B) Y( \9 c
*/6 m# j8 K1 [4 x; G# y2 d
f# Z \1 e* |. a8 \ // Add plugin code here...
1 Z) i" Z" g2 Q. a! \. i: p ```
: g/ ^2 n" d2 A$ y' O: }
, T2 r/ S; C0 t 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。9 n. A5 t* u$ i7 x2 t4 s
4 Q$ B2 r$ {/ O# b
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
& G4 J6 V7 B; T2 |5 ?4 f- u Z' P( R* E
```: B% V1 i" w! J7 c' {
add_action('init', 'create_custom_post_type');# N4 I, w5 Z( ^* i9 B
function create_custom_post_type() {1 b. `: D; j0 G/ M7 S" |6 f
$labels = array(
& B' i5 E `2 x/ y! P0 A 'name' => 'Site Wide Notices',9 _1 @* E; r! v w1 H7 c4 ?
'singular_name' => 'Site Wide Notice',
' t# d E* _# k) L% |% ~7 o 'add_new' => 'Add New',5 }0 c% H4 {) |( ^ b4 Z2 ~
'add_new_item' => 'Add New Site Wide Notice',
6 z+ w8 D: `' W 'edit_item' => 'Edit Site Wide Notice',
: D3 m7 t+ |: h# a7 c1 q 'new_item' => 'New Site Wide Notice',: f/ ?9 h$ y3 {1 O: `
'view_item' => 'View Site Wide Notice',
6 N; G/ p) M% w; ?( z/ [ 'search_items' => 'Search Site Wide Notices',* h0 J8 y* ?2 q( ^
'not_found' => 'No site-wide notices found',
% h" T" B4 w# g" V1 o" {: f 'not_found_in_trash' => 'No site-wide notices found in trash'# s( I! ^/ v5 Q( F$ t; J& ?- P6 V
);
7 F8 s/ M/ I- q. w) G- Z8 @3 }1 {! j- [( A5 O- N' v
$args = array(6 S y5 w! Z- f U; q- Z9 m
'labels' => $labels,9 O* p) X7 Q `& A* B. [3 G7 [) b, m
'public' => true,
3 V+ O O/ Q8 X7 _8 f! L 'has_archive' => true,+ J( A7 k5 d7 f( }: m/ C
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),8 E f/ e" F1 {, s6 l, g2 v3 ~
'taxonomies' => array('category', 'post_tag'),
3 E/ T C/ |2 L. d 'menu_icon' => 'dashicons-megaphone',
% R% b2 f2 ^+ N 'menu_position' => 5,
0 i) Y0 l! e8 {$ f7 F* H 'rewrite' => array('slug' => 'site-wide-notices'), A. v4 Q) V& y' L
);
' q3 K0 a' s: B, O7 {( A
5 c2 d0 q( }2 n' w0 Z3 u, { register_post_type('site-wide-notices', $args);
' {1 }5 U) v7 s( d" Z }
: h' b# [ n8 E ```2 A5 _6 C3 m" f/ ?* e
: W9 A- ]3 l1 Q3 }: ]/ A0 g" u 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。& T4 _% g9 M- T8 j k- g e0 {, T
. N7 c( r# t" _! @! B- g' a7 {1 p+ k; D
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
( [! ^- t, O: f! y5 U6 R; _
# C7 |0 V* \/ D' U ```
& h& s" J p j' x. V+ S( {7 z add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
. g; o* _2 [9 m5 H( } function add_site_wide_notices_boxes() {
# D9 U; F: Q( ]9 a1 I add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');7 w0 b& }- L5 q$ }
}
( d* R$ j/ i7 [1 a% E8 f) P$ }. b' m. K( b' i# G# ^. ^
function notice_details_meta_box($post) {1 A: @ [8 C# F' i/ M5 L- Y |
wp_nonce_field(basename(__FILE__), 'notices_nonce');. M; u9 M. N/ a) N/ @
$notice_title = get_post_meta($post->ID, 'notice_title', true);. }1 `4 E) i4 C7 \1 g1 _- p! D. K
$notice_content = get_post_meta($post->ID, 'notice_content', true);
. Q0 u# c1 v+ y) U ?>
; Y, f/ K) S6 Y8 b" }5 r8 c <p>& t1 c5 i# @0 _/ ~6 L* w& L
<label for="notice-title">Notice Title</label><br>5 o D, e; U2 V" K% M
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">' |+ S% s( ]1 D! X6 s
</p> |3 N: H! x4 l. m! @- d
<p>
, h: h, g k. e# v! {& @; P) w <label for="notice-content">Notice Content</label><br>9 q: ?, _( q+ Y3 h) t
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>5 `' T q% r, `( `$ I1 x
</p>0 U/ W6 i% |4 |
<?php
) C2 I0 M: f* i6 y: x6 F+ D }3 c" L; B! h3 p
1 ~2 v" G! H+ q) p' d0 H
add_action('save_post', 'save_site_wide_notice_meta_box');0 f5 ?6 m5 d5 P1 y3 ]. E
function save_site_wide_notice_meta_box($post_id) {
! i/ U8 i+ P% a. v- [9 v if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))9 H; D& A! b1 U9 `1 V" I6 Y' [
return;
1 E$ T J. @+ F, p* a I! I if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
9 U0 x+ t2 K& z! Y return;
4 M$ a u) y3 _( ~% w, L; Q& @
$ ^; o. A/ F+ `" D* u' V1 P7 u2 h, i if (isset($_POST['notice_title'])) {
* F: [% i, n0 F" E update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
% \1 R+ A; q g- V1 M7 Q0 d }
; K7 }5 e$ a$ U+ K$ h9 z7 H( Y) | if (isset($_POST['notice_content'])) {
6 v9 y$ Z, u u, i2 f update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
& X' I+ O$ f0 a. Z }
0 E% R4 Z; x) t4 d7 S }# Q; j2 z* V& o! X% J
```# V" v* k: y3 _2 z( D
5 g y. q$ V; R, R
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
5 f/ t* [9 J; N, f7 X# O& l& z, k+ l5 i, @
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:/ L. b, j, K5 p+ B5 i
* g5 O9 Z3 M0 N; a7 K$ ~
```
. d* Q4 l' m, x( g $args = array(* U0 j: B2 Q0 x) ]$ g
'post_type' => 'site-wide-notices',
4 d( C* q& O8 s6 A5 n6 i 'posts_per_page' => 3,
$ I {; ?; A* s I 'order' => 'DESC', y( C& q: `* m6 Q: T/ [
'orderby' => 'date'
! r1 V i7 C% _( G& W# l );
( e! x7 l% Z1 u( K $query = new WP_Query($args);/ g0 `1 h1 ^+ B' o' N; }1 X
if ($query->have_posts()) :
+ c+ O! _: A, L/ E! a, \- \ while ($query->have_posts()) : $query->the_post(); ?>( y5 z, D8 g, |% ]4 Q3 h& e! }
<div class="notice">
, \$ C0 ^7 e3 W <h3><?php the_title(); ?></h3>' r9 a0 n7 `# E Q5 ~( w6 m
<div class="notice-content"><?php the_content(); ?></div>
) t& Y8 X0 s% ` g/ _ </div>+ H: I/ i# v+ q2 C' l
<?php endwhile;7 h3 O1 O( N+ X. C& E
wp_reset_postdata();* q) O: `+ P, @4 z" Q( Y
endif;
P1 i/ @+ R% Y5 l4 W1 E ``` G/ p- d. v! U" \" ~+ T, e$ k
7 G4 J, r6 o% t2 ~4 ^( ^4 A3 ~ 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|