|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
: \2 F( K" a$ {
' S+ j. L6 P# t0 B4 b如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
5 _1 k! n) F k6 O/ V1 t! F9 _' w
; U; q" g% p& Q' D( j2 s% d以下是创建自定义插件的步骤:6 G: R# V$ V" S, ~! |
! _( R& G/ G4 q4 h0 h: ?
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
& _$ X6 K- I. y+ f
" n* Y- T% N5 T& E2 ]/ t' \ ```
# ?3 v9 z- d- d K <?php
1 M# V8 O- A! ~( d9 N( a7 R7 ` /*
! b6 z# N; H$ a( h, K1 ^7 s Plugin Name: Site Wide Notices Plugin% V. H4 E* R i9 m8 l2 m
Description: Adds a new custom post type for site-wide notices.1 b% |, z) B2 S( z# }& X: q
Version: 1.0
% Z3 _: w1 e( E% \$ m F Author: Your Name F% [* v: s7 t* H$ h$ m
Author URI: http://example.com+ b% U9 \+ H# Y' ?8 K
*/' t% ]0 C/ L1 K- a; K/ q
4 y, Z2 f9 [9 A
// Add plugin code here...
( c' r- k c) m ?# y+ B* C$ w7 d ```+ s4 Y I r1 u4 j' Q$ d
6 S% e% }+ A6 Z, ~& B+ Y: v 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
" o" j0 m5 U+ `9 |2 Y" w' w2 |
* R! B; J8 f9 g9 [ z+ R2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:6 o# v2 @( J7 k8 H
1 Y6 D* ^, ~0 N8 Z3 l; d$ w
```% o8 \: q; Z; \( d& m
add_action('init', 'create_custom_post_type');; t V) H) [5 O' T- E) a6 d$ n
function create_custom_post_type() {. W# _! \3 q3 z3 E0 z
$labels = array(
6 h" }1 W' q& x4 H' d$ v0 V 'name' => 'Site Wide Notices',$ X# R0 H4 F4 z" q6 r
'singular_name' => 'Site Wide Notice',7 I" T9 b0 }6 H: H7 P
'add_new' => 'Add New',
) [0 M' Q# H4 d) K- k% q( ] 'add_new_item' => 'Add New Site Wide Notice',. L2 C0 Y! P9 E& R4 L9 L
'edit_item' => 'Edit Site Wide Notice',
% |; P) L& A V2 X, b 'new_item' => 'New Site Wide Notice',
1 |/ {4 [+ d! h8 r% k 'view_item' => 'View Site Wide Notice',
! ]( X- q& ~4 B+ z; |6 Z 'search_items' => 'Search Site Wide Notices',
6 [% p" k H- s' ^/ X" i- d 'not_found' => 'No site-wide notices found',
. h9 r+ j$ V& a 'not_found_in_trash' => 'No site-wide notices found in trash'
" r- d/ {( m- q; L3 x );# m- ^, ~: C" g' P: [
6 R6 @6 B: c4 L& w4 A
$args = array(7 _( U2 A0 R/ Y, V! U
'labels' => $labels,1 h% c' d5 r8 e
'public' => true,
0 r9 x( B) D( @& B 'has_archive' => true,
4 i1 f, T/ {* b5 }9 j3 @+ v 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
6 o$ A; J7 a& r* ] 'taxonomies' => array('category', 'post_tag'),
) E# ]. f' H& [3 a 'menu_icon' => 'dashicons-megaphone',
! z1 Y9 l& V5 _9 u9 l 'menu_position' => 5,( A7 L) @' x& y# s
'rewrite' => array('slug' => 'site-wide-notices')* J" H) H* k3 X+ ~
);! U1 r, \, b& ^2 C/ V8 y2 h
- J) i% X; M4 x/ t( d* ]( z$ R
register_post_type('site-wide-notices', $args);
2 ^; S# ~5 P6 M) N! S }8 e7 N! X. D' r! S* H
```4 U+ J6 c8 b" q' m% I0 Y
( v3 n* k& L1 H5 C7 v& p: f( e
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
9 }7 d, W& I# q; Q8 \* I& p2 S- P6 [4 V2 c
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
% b( r( v; k/ a/ b. \: Q$ E0 {! |( s! |7 Y
```: W! B8 N, S8 t& t8 h
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
2 J4 Y. f1 w Q' }6 C. q' s function add_site_wide_notices_boxes() {
, d; F- O: V2 T' r1 E add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');$ f! n& w& B5 z0 n" Y- {3 o
}$ j- D) y4 } O5 v+ D* Z
7 v# ^; P) A4 R) } s, ? function notice_details_meta_box($post) {6 o1 @' E' B' Z, G" O) q X+ f
wp_nonce_field(basename(__FILE__), 'notices_nonce');
/ V& F F' ~' l! f $notice_title = get_post_meta($post->ID, 'notice_title', true);8 W9 T3 X& r) a) ?( \* s/ Q! D
$notice_content = get_post_meta($post->ID, 'notice_content', true);# D/ `6 e9 `9 _8 a
?>
5 ^/ }5 z E. R. g8 U% e2 P <p>" O, ?2 l4 o% C% `& }
<label for="notice-title">Notice Title</label><br>
+ t' w2 ?% O7 v! {) @. { <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">& c: ^0 s" \, x. o% |
</p>6 h# a5 H0 q* o; \' x
<p>& f9 `2 c4 l8 n/ d0 Z4 n
<label for="notice-content">Notice Content</label><br>0 h" U6 z* O7 |2 v/ e1 H
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
* U+ ?4 q1 `4 B; s5 T </p>9 G% E" a1 p* S0 l; T
<?php6 M5 Y. d$ i; W- z& g( h7 _
}6 d* q" B1 {& N4 u( c# \3 d# D( s
! i$ ?8 A& ^1 g0 H5 ?& c9 d
add_action('save_post', 'save_site_wide_notice_meta_box');
k: ~+ J- _- w. N9 N$ E3 c' s$ l function save_site_wide_notice_meta_box($post_id) {7 ~5 F, ~% x" _; v3 @
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))% r2 C: F r6 ]% R3 G/ k, P0 z+ ^
return;
; B1 G4 k3 T0 i1 `) ]1 y2 ^ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
; V2 k; o$ _. w" m2 Q# ` return;
: M: _2 j, P1 i' C# C. x) p$ q& B/ F$ ^6 R1 z# T$ [! b
if (isset($_POST['notice_title'])) {5 _, f. b& `+ K! x
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
) M9 ^7 ]8 b! d }0 |; l) v2 j* A; O/ R( c; Q! L% W' ~
if (isset($_POST['notice_content'])) {
6 p; _6 |. X/ _( c, w update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
- \, T7 N1 I' w1 k4 X; \2 g2 v }# u, `/ Y: W- I. e, n
}* y+ e7 d9 }" i% V
```$ W7 B \ K% n2 A v6 }/ z. j
- i# N5 i( C Y: h3 u* K
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。/ u& B0 `% A2 M3 d# V5 F
1 @' V1 Y$ T$ _2 R+ J% u# z4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:3 s& ]- `" J0 h- p$ B. U
[3 p- B" A3 ], b4 |, u3 S5 X& v, L
```+ ]+ @& s5 \2 x1 B$ U
$args = array(
9 C4 Z# S9 P; Y! q V0 U; S" }$ h 'post_type' => 'site-wide-notices',
0 P" u. H$ i( l& [. q( D 'posts_per_page' => 3,
: O; p( v, H1 y- {; P8 q 'order' => 'DESC',% C2 a' @7 l/ [& K# x: h" L
'orderby' => 'date'
9 ]2 X6 u1 }' T" m( }8 c9 ?7 ? );
' e* y5 t4 M2 s0 W$ k& O $query = new WP_Query($args);
# y; s! a5 d2 n+ f if ($query->have_posts()) :
6 K, _3 @' a3 D0 S5 G' I while ($query->have_posts()) : $query->the_post(); ?>
3 J2 f) Y+ {. e( z' s9 F3 j' I0 o <div class="notice">+ A7 G) T2 u; G; i/ o
<h3><?php the_title(); ?></h3>/ y+ ~ _) ^! W* K" O0 N* w
<div class="notice-content"><?php the_content(); ?></div>
1 m B8 U, t( w0 z* M' Z& S </div>
: }. [$ L! C& {$ O* G <?php endwhile;9 i d; K- g' n' K: i* e
wp_reset_postdata();, F$ x* _" M1 c
endif;
: X, K7 l' e' f- E ```" {3 U9 O( E" ~, W
% B& q* ?$ H. @) d: u' i 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|