|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?1 w/ @6 J" m3 o @4 V& x
7 L Q7 M6 R5 M% \
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。, a7 c9 z2 X M4 W
; i7 F0 K5 A1 y( o7 O以下是创建自定义插件的步骤:
D& }% [ D( _( g# C1 _6 V9 N
' o" t& ~; U2 P; z8 t) |* w1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:, _9 ~. w: R. K5 |8 E
+ |& {8 r0 |9 y- f G! y ```
9 W, r! O8 X7 H; V3 |) V <?php9 K3 A/ [8 `0 \. m
/*, j' h8 t# l. I$ I @ g
Plugin Name: Site Wide Notices Plugin: j5 H S4 B# m; F) Q
Description: Adds a new custom post type for site-wide notices.7 F# C+ ?) |8 e
Version: 1.0# ]: ~! p5 r% @ u% p
Author: Your Name
' c. @" d. d3 G, x) w! h Author URI: http://example.com" i( K+ a+ l% X; a" C! j* b
*/
1 f. t9 i4 m, h2 \
: P; ^, E' m8 h' F // Add plugin code here...
6 w" W: }% n; K& w) J R2 w! p- E ```; o0 y8 d2 v7 T! H' q& t+ g2 ~
2 T8 ~( L9 u- I
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。$ \1 X4 \5 w2 n) q n& P6 |- j
: E: A: `' a' u0 ^, H' [( v
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:8 n' H, l( h6 O5 C8 m7 s, l
/ E) K+ r0 G2 `* h1 [# C
```
1 T, E4 Y& ^+ U$ C add_action('init', 'create_custom_post_type');" h5 u1 g! ^, }9 ]. \
function create_custom_post_type() {+ r6 X% A1 o; F! v4 y% i) H
$labels = array(" [6 h" s6 P- l6 ]! E" m( ]2 H
'name' => 'Site Wide Notices',
* J2 i0 {$ k4 S2 D8 p5 ` 'singular_name' => 'Site Wide Notice',
- a5 T; {9 `4 a1 c 'add_new' => 'Add New',# N5 S4 x( [) g! ~& _4 D( F; z7 N
'add_new_item' => 'Add New Site Wide Notice',
" o( T% v. T9 U# `% {$ S) e3 {* R 'edit_item' => 'Edit Site Wide Notice',2 I1 |2 g& ^3 h* ?! `, |/ v
'new_item' => 'New Site Wide Notice',
- I1 d$ F2 Y" O# a* C- p 'view_item' => 'View Site Wide Notice',
' p' U4 F$ Q$ Y8 ^/ ^. ~! Z 'search_items' => 'Search Site Wide Notices',$ @0 F( j; k$ M" z
'not_found' => 'No site-wide notices found',+ s0 V4 l( K8 f
'not_found_in_trash' => 'No site-wide notices found in trash') E! F" Z7 D! {( E z
);
8 i3 H9 X& h) t$ H% E3 h# C7 f2 r' e
$args = array(
% }1 ^! }) A4 S0 f! J# R, M, e 'labels' => $labels,' u- e% r- I- y9 n2 y1 q( A' d
'public' => true,* B) U( E% z3 E/ n. x1 W' X- Z3 ?
'has_archive' => true,* }- F2 }6 ]7 O: M
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),- ~- P( _- Q4 y% f6 \1 R
'taxonomies' => array('category', 'post_tag'),. j/ U- y% w, X3 i: K( h
'menu_icon' => 'dashicons-megaphone',
_" |. J4 `! j& c& @ 'menu_position' => 5,
! d; z5 M. G9 h" S8 m 'rewrite' => array('slug' => 'site-wide-notices')
- K* D# c! c( g5 @ );$ I$ }3 @" f/ X# j2 S
! ]( ]) Q# }+ w
register_post_type('site-wide-notices', $args);+ u3 x, m% f% C) n$ B' p
}. H% S& h3 t- ~, L: o* X* a, ]
```
, B: c6 { S q8 j
X) m/ c: P' z; Q. c 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。* A( p0 [- y/ f, T6 l
* J: Y. F, B6 d \3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:6 M) @& J# X# s: _! G# o
: y- n3 F# k9 E! p/ ]* e, y3 y
```! r' Y, ^8 Y# A. B Y" W
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
( a0 L+ P2 O7 j$ `2 i: Y( C function add_site_wide_notices_boxes() {
* ^" y7 q) L! ~1 v/ @! F add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');, ]$ Q6 n* Y- ?6 w: p
}* S! m, |# l7 X" Y8 [( R4 l2 Z, u
# L7 T6 g9 f2 ]& ^! x) f
function notice_details_meta_box($post) {! M5 J5 E9 v6 `" u7 t/ K |/ Y
wp_nonce_field(basename(__FILE__), 'notices_nonce');
& Y5 x+ P3 G* I- W! P# s $notice_title = get_post_meta($post->ID, 'notice_title', true);- Q$ T2 Y5 V' Q4 d- [
$notice_content = get_post_meta($post->ID, 'notice_content', true);0 [4 i# M$ ^) K/ C+ a$ l
?>
- w" b- `( {+ m* x <p>
" _: ~# w% v% Z& K% j <label for="notice-title">Notice Title</label><br> Q4 d3 R. N* z2 L* d1 v) ~3 Z. Y! w
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
' F9 `4 h" i9 Q: r; @) K </p>
& w* A/ A* D. g% T3 [ n2 b <p>6 z6 Z+ Q" M/ H" T1 E5 t
<label for="notice-content">Notice Content</label><br>4 T6 o! Q5 \7 A* i" M) |6 m: G: D
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
% v+ [0 M' r0 T' f% G+ q& b5 h </p>" D+ M$ B! Y- @9 [
<?php% A4 b1 P% z$ A% N. n# d
}9 {3 _: t- t: H
% x; {- |) Y" t+ ?. ~ add_action('save_post', 'save_site_wide_notice_meta_box');) G* [- i5 ~2 |/ a+ B1 r
function save_site_wide_notice_meta_box($post_id) {
; ?: j: L' }! y7 p% ^+ ]+ _4 Q if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
' L( H! t1 j2 P' |' ~ return;
7 t: }3 S( ?( @+ y+ O/ a if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
8 n. K* d0 r0 u | return;) M7 I1 C1 W0 a& t8 d7 u
- r# C5 L( F% K* l2 P if (isset($_POST['notice_title'])) {5 w! E8 h5 }! e: y0 I" o
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));* ?$ I; d# _: j+ @; X% i6 B
}
* W6 ^9 E; o0 r if (isset($_POST['notice_content'])) {
j2 _' c# V" C3 Z( I: X update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
6 M2 D# p! s5 K9 ^0 Q- @, ] }
! K' f6 w4 D' A" c- K& }$ Q# g }
/ X Z. |4 m. L5 v6 e& J ```
. m7 J- L0 b0 S8 P# \( w# k* S6 a7 Y0 V$ q+ S; n! S! H2 ?. I3 B7 ~2 w
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
7 ^7 Q5 m! i H2 B3 f* ^7 N
! y! F& E+ o3 ]! A7 s' m' g4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:/ r' c1 e4 O% E# m! d9 j
' W% N) u' J0 e
```
! X4 w7 }; D' V $args = array(: Y6 T( M0 f2 Q, T# v
'post_type' => 'site-wide-notices',9 ]) {8 S# V# p$ I4 M% O
'posts_per_page' => 3,
, M% ?8 y8 ]( r# [. |7 r 'order' => 'DESC',& {" v% D: J9 Q1 K$ `
'orderby' => 'date'
' S2 l9 x# @8 S );9 W' @. o2 g7 ?$ u. z$ f/ m$ _
$query = new WP_Query($args);
& X+ c, I9 ^0 F, q' B if ($query->have_posts()) :$ `, O; p' v1 g |5 N* y
while ($query->have_posts()) : $query->the_post(); ?>/ v* Y- v3 t7 s4 p; H) l! C
<div class="notice">
: Q( N) m" V7 n( s* Y& y+ h <h3><?php the_title(); ?></h3>
; {, m7 ]* I" J9 a3 @9 k <div class="notice-content"><?php the_content(); ?></div>
8 k% j$ w& x& d" U% n; s. }/ W% r' g </div>1 x6 F1 {, C- m5 ] H! _/ [( X
<?php endwhile;7 e' R. {) J( x( x
wp_reset_postdata();- G) x) o& J' U! I
endif;
" T' h1 I. ] m, K: V ```& _. u! {. k4 F! u
+ h/ U% p! V8 ~9 Q3 X 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|