|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
) G" y7 N! f0 r& n5 G4 f2 K& }4 O7 g. {" i! Y
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
+ ?" {% h! d) ~; m* t
- H g4 I9 o) M$ C+ W以下是创建自定义插件的步骤:4 c; P0 H. {* D/ Q
U/ U# R2 }5 |# \; g
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
' @" ~# Q% p( B
: X& Q+ }" M$ a, f( ]0 t, M9 u ```
2 k$ C! r# h; o* j <?php2 C* v* I/ s& y/ ]
/*
* f5 u) c5 G8 ]# x Plugin Name: Site Wide Notices Plugin4 p( V: x9 q- j' J
Description: Adds a new custom post type for site-wide notices.# k$ S1 j& E' s" n5 s
Version: 1.0! ]: w! C8 B" k
Author: Your Name
4 @, J" s1 n5 b9 l# N) P* L# g Author URI: http://example.com+ B! _; I; o t4 }! ~/ h" h0 f
*/' z. X( J6 }, A) ]! V, k' J7 `
& N: p, P/ g% | // Add plugin code here...$ b) j6 |" |" j. ^3 e- Z, Z
```
, b6 O+ r: ~* }$ C1 x6 V* `7 p5 u+ k4 [, u
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。; V" Y7 z! @& y' d- t7 }
! v/ H* x. M5 b* d+ O2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:8 y6 X9 f& t& Z2 o% l# O% u
. P: a# D6 g, k ```+ S' p* Y P# `: n9 W6 N3 J
add_action('init', 'create_custom_post_type');3 r+ _# I" V% r( a
function create_custom_post_type() {
( @4 F9 s0 l' W) X0 A: r- w7 e& z $labels = array(
8 x/ m, G' V. J+ V) Y 'name' => 'Site Wide Notices',
9 j) P. G2 ^( D: w7 e 'singular_name' => 'Site Wide Notice',) ?) x2 h/ @" Z4 O" `5 e$ Y' M
'add_new' => 'Add New',! ^ v. |% L0 p1 ~; b9 Q+ y
'add_new_item' => 'Add New Site Wide Notice',
# B5 z# [& X3 K' A5 \# @ 'edit_item' => 'Edit Site Wide Notice',* o! E4 N6 u' N* P$ Y/ P' R
'new_item' => 'New Site Wide Notice',& b. m0 S! d! [
'view_item' => 'View Site Wide Notice',4 w8 V2 Q+ l1 L( t' d
'search_items' => 'Search Site Wide Notices',
" ]8 Z* ?+ s7 P1 Q4 b 'not_found' => 'No site-wide notices found',
) ^+ N4 N! I& |) b3 G 'not_found_in_trash' => 'No site-wide notices found in trash'3 w3 X0 n9 \9 }) X. J3 C. k
);7 ~' s. p& I6 i$ _
% a, K' G- q! d1 S/ v; l
$args = array(" M6 o+ n P# s, v- x
'labels' => $labels,
+ L, |* [. T0 T" e6 Z4 `9 J7 Q 'public' => true,
8 [' U( X* ~8 L4 K7 Z. n7 Q. N 'has_archive' => true,
9 g+ M* u0 \6 O. z. Z' b 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),2 z" _4 O z! D. s
'taxonomies' => array('category', 'post_tag'),( `2 K6 w P1 F! W; ~+ m
'menu_icon' => 'dashicons-megaphone',, p$ D' b% f9 b! S' p# s
'menu_position' => 5,( V& Z0 s$ [$ D0 K% ~
'rewrite' => array('slug' => 'site-wide-notices')
! x/ ~: b$ }- R4 F5 v6 R% p5 J E );
5 g- n6 ]' y0 \* M9 Z' r* u6 @+ R: p
5 |( b& k: o9 G4 ? register_post_type('site-wide-notices', $args);
+ h2 ]+ f& i" J4 J" R0 J }# V" K* M) h0 ~4 ~! H% ~; ^
```
0 {1 T+ M5 a) H
9 W% o6 d6 n: X9 a 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
' F" [2 C; u2 \0 h
0 E( n( V8 u+ Q* v( H+ O3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
" F+ R5 `3 w' O( H8 i% ?% H; X: {" f Y* p3 L2 L$ H) N3 \' G$ k
```# t, K1 b0 ?) c( G- H9 M
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
! B V. g% y' C6 J+ ?' H( ]+ N function add_site_wide_notices_boxes() {! f& }4 W) L* `; l4 m n
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
9 z' V- j4 W7 x0 E6 }1 o }
) D, |, V8 z: I9 Q. l: K1 Q. g3 J8 Q* ]! @* B
function notice_details_meta_box($post) {
" @7 E- t I3 g/ p0 ~8 G7 q$ N/ x1 o wp_nonce_field(basename(__FILE__), 'notices_nonce');
1 g5 ^, n0 ^) E5 v- |1 v $notice_title = get_post_meta($post->ID, 'notice_title', true);
0 f: d4 i/ m( f $notice_content = get_post_meta($post->ID, 'notice_content', true);
! ]1 a! t4 f j0 X# o7 _ ?>- n. Q6 Y- h; x
<p>
: E3 W$ p7 x; F3 q# [7 } f6 u. ~4 ` <label for="notice-title">Notice Title</label><br>
! r# }" p( D7 M0 V) p( {0 x <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">8 M2 G8 @ Y& q7 T/ k
</p>
4 F2 n& R+ Y: z" a. a" a <p>) J9 }. }; \5 z1 P/ w
<label for="notice-content">Notice Content</label><br>
" a# M4 `) l2 x: r$ l <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
/ ^2 w% o) @: e& b4 g( H( o: m </p>
# }: Q/ F1 g; I) ^4 c/ w8 Y( A <?php1 }0 p' e) z! R* a
}# ~0 U b/ H* M$ m7 {; ^ M) X
9 m' u3 `0 k1 ]$ L
add_action('save_post', 'save_site_wide_notice_meta_box');0 o7 n0 o$ F- z5 @4 x( G5 E
function save_site_wide_notice_meta_box($post_id) {6 s4 H x3 U5 w) ]! F
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))/ _. @% j; s4 \. l6 y |) _
return;& R* `+ Y5 k2 s6 S. @- o) B( |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)* Y. l4 m6 K+ h& z
return;% b5 t8 o7 B0 d3 E5 ^3 N" z
4 d, g3 z! \4 }; h
if (isset($_POST['notice_title'])) {$ a- T$ ~" m% M! v$ u, S1 p
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));6 n) T" Q) K: H4 }4 Z) r
}* i0 l) e1 h6 s0 M) T/ R4 O% {
if (isset($_POST['notice_content'])) {
0 u& {! k3 w1 D( t update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));# b# ]1 W# g. M; O# u/ C
}
+ z- n X. b2 t2 e% I3 t9 l5 w }
2 r- C' J% E# U( L: G ```! h( r; ?8 x8 }3 j9 i
5 ?0 H- B3 R* J
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。9 Y; P1 z ?; V0 s
% D0 ^( [) g+ c" _- e% b
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:! H8 s& J; [ F; j4 Z3 I6 ?; u, V
5 p9 V$ Z8 ] ^% h; L9 W ```
( H$ I+ E& g( t1 ^; S# y% C1 E+ ? $args = array(. \1 J5 d$ [% X; u$ s M% x4 J+ E% W h6 E
'post_type' => 'site-wide-notices',
7 H% |* k$ L( Q- J 'posts_per_page' => 3,: c5 e2 n2 Q, `: c4 b' X/ w( g
'order' => 'DESC',
$ x0 `% e h; Y2 R& g% Y R1 H 'orderby' => 'date'. C7 G5 U! L- S8 a) K; o! X& a0 x9 P
);, t$ Q! G9 R( l- g) y
$query = new WP_Query($args);
) C2 [# U$ _7 V5 ]: H if ($query->have_posts()) :1 v; [) H( g0 A0 y" S) X
while ($query->have_posts()) : $query->the_post(); ?>
- o8 L+ [' w6 K5 c <div class="notice">( f4 G& m `$ N. Y
<h3><?php the_title(); ?></h3>
; o; c/ e/ Q! g5 r8 W <div class="notice-content"><?php the_content(); ?></div>
5 s: E- M q$ l </div>
4 g4 @% s/ a J" I! z! L4 t# K4 x <?php endwhile;" a% {3 W, M' |
wp_reset_postdata();
$ y, p ^5 \8 D- d# _ endif;
+ B2 P0 O$ G4 W; i/ ~! D ```
4 H% a) n9 W5 m$ e o& o
# l P G z# L( U! D/ S; }8 S 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|