|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?- ^3 k& s/ S+ T i, [8 p- L
9 l1 U! I; z% V
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。: ?9 G% a" N( U7 r6 T# c Y H
: ?% K' Y. w# W& ^) L以下是创建自定义插件的步骤:
2 l# F, n; N! |/ W
/ G' P" \* d2 a6 @% Y# Z: K |1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:, [% U1 f, @6 Z. X; ] N; i5 C8 M
( o& d) `. d: B8 _6 L ```
' c! N) R% u4 T+ C <?php& a) I+ s( C% h( m, x3 u/ {! H' [
/*
" t- ?% n$ x) ^3 n7 { Plugin Name: Site Wide Notices Plugin
5 J$ `0 ^8 R" ^5 E9 Z Description: Adds a new custom post type for site-wide notices.
# u2 _4 _. }8 O' O( i& g) W Version: 1.0+ }) M* r9 B! g
Author: Your Name P3 G3 f; ~1 `: P. w
Author URI: http://example.com
4 X1 C7 T4 _0 ^& `+ _ N */' S9 O- C" |7 }- ~9 K4 B
% J/ [ L6 ^+ ?1 E& @" ~, G9 l; x
// Add plugin code here...
' I+ o2 i5 @- M' q2 h2 q B0 r ```
+ o2 J( O. R, i. U
& I2 q( G- E. P: H+ M& | 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
2 i4 n. h: U% f# m& b$ d# ^# U3 t$ g: l1 l' n
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
7 s! X2 Q1 a- [$ [8 ^* D3 t% M+ y. {
```
4 {% u' m! F' T6 V5 K8 B' q add_action('init', 'create_custom_post_type');
+ u( Z# b6 ]/ G8 R9 O# r function create_custom_post_type() {. \0 H+ ?/ m) N& ~: b2 h3 M$ g, i+ y
$labels = array(" _ C" ]* ]5 w" l1 ^
'name' => 'Site Wide Notices',
2 P0 a" i- X* l) d: @ 'singular_name' => 'Site Wide Notice',
6 S4 y& S6 v0 j# K( k& H# `4 z! s 'add_new' => 'Add New',
9 N! x; a4 W9 }% j1 v9 e 'add_new_item' => 'Add New Site Wide Notice',1 _* L( U3 x w( U; f
'edit_item' => 'Edit Site Wide Notice',6 m& U4 d: M u# P9 \+ @2 X
'new_item' => 'New Site Wide Notice',
* `8 d! D( |) F: S- l 'view_item' => 'View Site Wide Notice',; q0 b1 T T9 q+ R$ b+ l7 _9 R
'search_items' => 'Search Site Wide Notices',
2 L3 m$ O' [5 I! i4 S 'not_found' => 'No site-wide notices found',
% ?) V* x4 `2 g& ?7 s. I% w 'not_found_in_trash' => 'No site-wide notices found in trash'
4 t" V B+ w7 W K );
- s2 J6 K) P' |2 @. U7 N2 ^; |
# d. n9 X# Y- } $args = array(; `7 p; V* t8 N9 d
'labels' => $labels,3 r% l! t9 q' z O6 }
'public' => true,
8 I. E! ], E3 |6 w 'has_archive' => true,6 e0 y6 z5 l' p' T# X5 Z
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
) ]4 S0 ?9 s8 Q6 r 'taxonomies' => array('category', 'post_tag'),
/ v- ^3 ]- T; M3 k 'menu_icon' => 'dashicons-megaphone'," T' s8 m& H% f% \# V9 e6 y' r
'menu_position' => 5,
* o: ]0 e4 U0 c 'rewrite' => array('slug' => 'site-wide-notices')
2 C/ L. h: |; L8 p" K" ^# n );# D' Z) D" O. C1 ` @
" p K0 X' N1 |+ A% Y register_post_type('site-wide-notices', $args);
0 u/ m6 [! F. x [" o5 o; Y }# x* J* G% M6 }7 v0 q' h% l* C
```
+ Z4 G0 i& I% M2 a$ T# P( f# y+ I# J: Z- @9 k1 ], K
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。9 t* l1 i: f- O
( m9 q3 [: x$ {: ?7 c9 j9 s
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:0 D1 ?% t2 ^% r8 N9 j: y' I! _
$ B: w9 e3 q3 ~. F0 I3 N
```& e9 s; n1 r* w9 H& n1 \8 \" v
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');( H2 r8 b- B. O1 }# C) o2 l
function add_site_wide_notices_boxes() {) t9 {6 \# M5 n* i6 t* h- k& K3 G
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
: d1 m) J. ]5 r* L# u }/ J. b* u3 o4 X: l q
# |5 d9 @! K0 E/ x3 j
function notice_details_meta_box($post) {$ O/ `4 }6 r5 Z7 n* q
wp_nonce_field(basename(__FILE__), 'notices_nonce');
8 @; c2 T3 b, d; b- o1 I $notice_title = get_post_meta($post->ID, 'notice_title', true);7 f( X: e2 {7 P
$notice_content = get_post_meta($post->ID, 'notice_content', true);0 l9 Q. R+ h: W$ M
?>
8 N0 y( A6 x2 ~1 z <p>
2 j+ u% p+ ^2 X5 u" \- [6 t <label for="notice-title">Notice Title</label><br>
2 A9 W( g! _! S# J. a <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
: m3 x0 h# A& Y; ~6 y+ [ </p>% {) u7 d- u8 _2 `9 W" \9 |7 W
<p>9 ?: H4 C# m. Y/ _4 {; j& [- _! g
<label for="notice-content">Notice Content</label><br>5 T( v, f, a0 r0 N1 {
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
9 R* `+ P4 j& l+ \9 @1 G </p>5 g. z6 L4 j7 |/ F! z) n/ n
<?php( ?* E/ @: \+ e, }; B
} ?% m# x6 ]3 C# b: a
- \7 C+ l& y" }7 Q( X4 k add_action('save_post', 'save_site_wide_notice_meta_box');& h) X/ N" B9 J
function save_site_wide_notice_meta_box($post_id) {1 Q3 |6 j; b& C+ G
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))- a( }! S# Z. @# _5 V' w7 \, u
return;* H/ V. @: m5 G! [: l& R! v
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
6 C( U$ A2 X- F return;1 K( W: \* R8 g0 k/ x o4 }
1 w6 Y; O* x* M0 B2 p if (isset($_POST['notice_title'])) {
& _ a/ N2 F9 s8 h) C& X% E$ A update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));( p7 ~# A3 `! B$ X7 G. }" p$ s a
}
6 _( u1 M8 }& k T! L if (isset($_POST['notice_content'])) {+ i1 U4 q% _6 u: j/ V
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));, O- Z8 ]: Q) x$ i
}1 H( l5 o" U% B q: G$ w, W
}
+ v4 k" G8 Z$ }; k) K' {, \) L ```. _# q% E z" h
8 r7 p2 R5 Q r( f9 R9 `
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。2 ~6 I; A6 z" v
J1 O# q' n1 R1 N4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:+ p9 w- {- t0 d, W% O, }! N
+ h! }8 U( a! S' j: C7 r* ?
```
7 P9 p0 N; Q) u" \4 r2 d) [3 ] w; i $args = array(
5 u7 O! e6 e; C5 x 'post_type' => 'site-wide-notices',9 F* V& M. w& x$ Z
'posts_per_page' => 3,
/ @. i9 f- T' \% Y9 U& D7 H6 Q 'order' => 'DESC',6 F! V9 ^: `. ~5 V% A
'orderby' => 'date'
- A! F1 R" H ^4 ~ ?$ Q& |% J );
& e2 R. q: Q6 V: I! A+ V4 r: L: _ Z $query = new WP_Query($args);0 p: [' n4 R7 y" I- Y* ]0 B6 A7 u
if ($query->have_posts()) :
3 V2 D! L) T0 e- @ while ($query->have_posts()) : $query->the_post(); ?>3 O1 B4 a3 G) G+ `3 N8 Q K
<div class="notice">
; }, U6 _) g( s' O <h3><?php the_title(); ?></h3>
/ \: R, V, e; d7 V1 c% [ <div class="notice-content"><?php the_content(); ?></div>( i" Q- Y% F& [5 p/ N5 t* I) o
</div>
3 ~! B4 i3 o" C h <?php endwhile;: G% s( A1 E& s# j J
wp_reset_postdata();
$ M8 C1 |, |" _ endif;
6 @% V5 H% c8 _5 p. { ```
3 A+ {; C: M7 K- n& A3 a) z' R1 n- w7 [! ]* h; r( _6 ?7 ~# E* d
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|