|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
/ y1 B3 E* t& o' ~% R( R8 m+ o& t, Q
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
4 y a( g: B p, h7 ] ?! M( U' F8 {8 L1 n3 i( K! O. D* n
以下是创建自定义插件的步骤:2 ?+ Q# a4 ^0 P! C
+ r/ v# q9 {& @% i+ B1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:2 T8 }4 k$ A2 m, r7 B8 G
; t% z* b+ [1 F1 R8 w* b ```8 e; C _8 k' v
<?php5 ~7 b# X! k; F4 {
/*
7 N3 w5 O$ t4 r8 @4 h" v Plugin Name: Site Wide Notices Plugin* O l: C8 ?0 ]- ]5 f( k0 J6 G
Description: Adds a new custom post type for site-wide notices.
1 H8 X3 A' l1 @) Z( {8 v Version: 1.0% H( a4 O3 {8 i1 V. C
Author: Your Name
$ {5 [8 G3 R/ J. B% J& H5 M Author URI: http://example.com9 `* m; g& F% J4 n
*/
! g" M( C" H8 K, u
J6 i, G' B& M // Add plugin code here...
# B; f# o4 u! C1 c ```
, T/ ^! d2 ^( J& e" ^. D4 S+ R, z( V" p2 `3 |# U
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
- t/ B3 X/ L: G# f$ c* R" [. y% R& G" Z h* B5 Q5 f8 {% \
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:' | f9 H, O" C) f/ g) [0 F) J/ v
7 s1 m+ p! g' V
```
3 p( B$ A# J, D% t add_action('init', 'create_custom_post_type');: [! }0 t& f- d* }; ]4 [
function create_custom_post_type() {
: E6 h8 T5 I/ W! |7 a* S* z- g $labels = array(; a8 B1 T$ R* ]! ]4 d
'name' => 'Site Wide Notices',1 Q0 O6 d2 ]* H8 X' c' T
'singular_name' => 'Site Wide Notice',
2 n+ w1 l, m! n 'add_new' => 'Add New', W$ ]7 u' v& T4 g( J" N1 d
'add_new_item' => 'Add New Site Wide Notice',
6 z% q+ V$ G: Y" I2 p$ D 'edit_item' => 'Edit Site Wide Notice',- c8 e7 f# G' X0 h+ m" ?
'new_item' => 'New Site Wide Notice',( ~5 i( y0 [ S& j& f. ]& ?
'view_item' => 'View Site Wide Notice',
8 p( H+ f9 G( l& u 'search_items' => 'Search Site Wide Notices',
" z3 t2 z( k1 d5 v, j 'not_found' => 'No site-wide notices found',
! y; f2 y( F+ j 'not_found_in_trash' => 'No site-wide notices found in trash'3 ]+ U6 @9 H) B
);
2 o- Y8 p. B& A5 `8 `$ L* k% A Q7 W
$args = array(7 { d* |2 B" `$ s8 }! [" S9 K/ T5 l% I
'labels' => $labels,+ B7 C3 ~8 L' N9 Q
'public' => true,8 }5 V; b: t9 f+ G
'has_archive' => true,2 N. O9 I; i9 K" O1 q6 I
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
# P. ]( O1 F. t& Q9 ]9 W0 R- L 'taxonomies' => array('category', 'post_tag'),2 ]7 D8 F) {% M6 v, L2 d8 }4 T
'menu_icon' => 'dashicons-megaphone',0 U3 G. T) o5 R1 P- m1 T
'menu_position' => 5,
0 K1 b, X6 P5 j& o0 @+ C 'rewrite' => array('slug' => 'site-wide-notices')
& p4 U- w# Z& y. ?2 E );
, w* a6 J2 @. S& `! F- h3 k
$ D- V: Z, t3 e2 `. [. @7 Z a* A register_post_type('site-wide-notices', $args);
0 k: A' V U. P+ s9 p }8 `0 @* U' C" h; `! r
```
; P; d, W$ F' E& \0 P& [4 w3 b0 V; E( d- E! v' o
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
) ~* Z O1 _; ~/ }% Y: y7 y( O% D# @, h9 u8 ?6 y }7 J" W
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
% U- `+ h1 h+ R! d+ y+ H
1 G/ v4 K! L- @* T( H g% i/ b ```. b1 W# m8 V' k% A3 n/ y* D/ s7 b
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
& ?) H' F7 w# j* E a: s+ ^- v function add_site_wide_notices_boxes() {2 r, z) r6 T( O
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
. C1 j0 {; v$ P3 [, `( `6 \# \ }
6 U0 y+ v! C- G0 q( }) \$ M2 ?& M7 L# X+ T( X* L
function notice_details_meta_box($post) {
7 h6 \. k2 G3 g5 F3 G& Z wp_nonce_field(basename(__FILE__), 'notices_nonce');9 K* a: U. O7 P
$notice_title = get_post_meta($post->ID, 'notice_title', true);
4 ~7 d" L3 E: y# ] $notice_content = get_post_meta($post->ID, 'notice_content', true);
' i* F5 U, _0 U( i# {8 F/ G! Q9 M ?>+ Z& p* i& }) L( P, E0 V( H
<p>; O7 @$ c b+ g7 Y* p d8 g) g1 B
<label for="notice-title">Notice Title</label><br>
7 w- p1 H1 y# S2 W: o& V4 w6 { <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
; G5 W# B! ~" s </p>4 `8 q; o2 W* U/ p9 X$ H
<p>
5 A! R8 `( @& R, r3 Y6 s <label for="notice-content">Notice Content</label><br>
' `9 F( m5 i/ s6 c6 p* S2 K& M$ N <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
0 a& Q; E! L2 `, }% T5 _, G8 z </p>& y: K' e% Y: f: [4 ?7 ^
<?php
- v! H- L+ m5 `) I+ v I }! {9 i2 T1 t8 y5 @6 q
4 s- A7 t! C4 o4 k
add_action('save_post', 'save_site_wide_notice_meta_box');
" i4 m; f' k- m% q5 V; _+ h function save_site_wide_notice_meta_box($post_id) {
* j" n: v7 P- L O4 u+ K5 Y if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
# g. g4 g! D) k' B' b" d return;
* h* A! {: C1 s$ | if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
1 A; P+ k- F7 } D, H' E$ z return;) Z) }- t/ W" q2 u' P
+ D+ |4 ~, f4 Y0 O1 {$ s) R
if (isset($_POST['notice_title'])) {
+ x2 q% R/ V% A/ L" t update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));" |& a; ]* L U% p6 n7 z
}' M0 D% s1 ]0 t8 }
if (isset($_POST['notice_content'])) {/ e. U* X7 q; g& F
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
! |7 m' W0 Z' z2 O }- I5 ^0 [! d3 q; q2 {# M* _
}
4 B; F5 Q8 V" M7 f2 M7 p ```
/ d+ t& @8 t' @# h% h5 N; Q1 g
7 a6 Y8 W( f r$ ?( ` 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。3 L/ F8 _) D3 X. q& t. w% g0 ^3 J" z
1 M& P6 v( b, T) ]" R
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
: Q, V- h- e2 z$ s N& K# B$ B( w- v( {! c. S0 c
```" d( P8 y! y5 g: Y& F$ K$ r
$args = array(* s( k* n7 E4 X& H6 ?: u
'post_type' => 'site-wide-notices',
( P! O6 p, h* z/ M# s5 F4 M3 C 'posts_per_page' => 3, f G& U+ J8 [) W% W1 q* i2 m
'order' => 'DESC',5 K) b1 w) t- c
'orderby' => 'date'% t) y8 x9 {$ |5 u# I& _
);" Z- `/ N" n9 x- E
$query = new WP_Query($args); G8 d7 e5 u4 b* V E6 Y) Z; o: U
if ($query->have_posts()) :
; s. z+ ?$ M& \. M while ($query->have_posts()) : $query->the_post(); ?>
* h& |. J8 U' i( t <div class="notice">
% X5 x) G% k* X9 O4 l1 D: u4 f% r <h3><?php the_title(); ?></h3>6 Q* n3 K, u2 Q. q1 f
<div class="notice-content"><?php the_content(); ?></div>
8 M' i4 p& x! w3 Q- b </div>/ g6 T. u! B* R9 C6 g2 V8 Y( S
<?php endwhile;
; D) D; l( f' | Q k; A wp_reset_postdata();
' V6 W- C+ v7 h# V f# T5 s endif;. t* s% l W0 e6 v/ l( R
```# u# O; F0 L7 @4 c+ z. ^. h
3 J' z) |0 u! |. B6 U/ s 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|