|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
. x: b; N2 S% e; g; [' h; _$ e! C1 ]: j* z$ {& ~* F: c3 b
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。" J/ M8 {% x# ~& J' W9 h% L: H8 q H9 S
: C' v" G8 d2 [; _5 r+ K0 A: ~以下是创建自定义插件的步骤:" @. s. n& _# N+ r( Q1 W
4 f% g% W+ g+ z, f0 ?: n% U7 B1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:- ^0 }( |8 u+ I o/ i8 f
6 Z, O8 |; Q9 h4 p5 c& r8 x3 F
```" t8 O' x& O4 S
<?php
* {1 H, q2 o' l, f5 ? /*
: [+ }- ], P4 ?+ D6 M Plugin Name: Site Wide Notices Plugin
3 s5 b2 w) h# O/ V Description: Adds a new custom post type for site-wide notices.
# L& _' t7 ^6 z2 u$ E7 D1 u1 | Version: 1.0$ L2 F% Y/ J# E1 G0 \, g
Author: Your Name
" l3 U, r# J7 Y5 c' I Author URI: http://example.com; r, r8 d( M( x+ F% {7 ?$ o
*/( `( \5 l( Q( ~/ d9 {- H
. c) J7 A8 y+ G {- n. X // Add plugin code here...
' N% h, c4 Y4 x9 w+ P' o ```" S# G r+ l( b) Y9 h8 U" ?/ d
4 ~8 j; q5 }. o: K8 R4 v @ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
6 y5 D1 W9 ]/ \/ z. B2 ^# d9 |; g- l. F- K
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
}( G( ^6 ]5 H9 m) K
j& t$ |5 A+ M2 B; u1 J4 D ```
0 w; B, Y% H0 k. h6 H add_action('init', 'create_custom_post_type');- X' k6 H; Y) {( U4 C0 y
function create_custom_post_type() {
0 a8 M" L2 F* L, D) j $labels = array(2 [6 k) U6 ^) {$ T' O& L
'name' => 'Site Wide Notices',/ v" R- M1 `9 I# Z t0 v
'singular_name' => 'Site Wide Notice',
1 T/ `5 q/ k3 m/ ? 'add_new' => 'Add New',
6 }+ }5 ?3 B( ?! Z" A8 M3 [ 'add_new_item' => 'Add New Site Wide Notice',
( |4 D( _) d" m+ ~7 Q 'edit_item' => 'Edit Site Wide Notice',1 j b9 g8 |1 j: T s# T- w0 H+ U
'new_item' => 'New Site Wide Notice',
9 a5 [, Z, P! Y 'view_item' => 'View Site Wide Notice',
% e* S& p9 J8 h5 Z& e* v [4 s 'search_items' => 'Search Site Wide Notices',( i# E7 G9 R/ F, ]4 ~ m+ Z
'not_found' => 'No site-wide notices found',0 H. p6 G* P; r6 R4 w" V: R& [, o; a
'not_found_in_trash' => 'No site-wide notices found in trash'
# _; A% H% L! ]% c) ^( O7 @ );
4 r+ \; X# ?$ H9 h. W9 A. q) }3 j' N8 m! }! s) |$ n
$args = array(
1 g6 B+ z, I4 Z# i4 H 'labels' => $labels,* @1 @4 D" k, }" f9 H. ^
'public' => true,1 S, l* u2 I$ S( e4 [6 U) J
'has_archive' => true,
+ p* B; c# w' ^7 b3 d/ s# C9 Z 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
# ?9 Y" m' F7 D! z; { 'taxonomies' => array('category', 'post_tag'),
& \, Z2 Y( f1 S% ^. C ~ 'menu_icon' => 'dashicons-megaphone',
% [9 n1 M& s' H" B" a( R- b 'menu_position' => 5,9 f6 {* W- I- ~. B4 x
'rewrite' => array('slug' => 'site-wide-notices')* ~0 V5 w4 ]5 o t% L
);
@+ E1 @) O( }! B b7 M6 O$ L% ~! H2 S. U& M% }
register_post_type('site-wide-notices', $args);
7 h* S, _. E; y; g0 A, f }+ T# x' K3 g3 L# g" O y0 U k
```- j. g) D. F+ ?, } f/ F5 N
3 ^* p% i7 `; J* F
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。6 w% S( p) p& M5 Y3 R
' ?# ` C P2 q4 ~. N" G+ W
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:2 W4 b# y* u8 O. D8 k" u
, q2 m% b4 @2 y0 ^1 o- u7 ]
```
- i) x) ]3 S! H) O' p" G& w: R" V8 f add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
# m5 J% l' G: c8 q7 t0 V0 r function add_site_wide_notices_boxes() {
$ d* I4 v' [* `/ G& f add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');5 |1 p! }: r g* R
}, I/ f/ O" ^: r2 c5 _) {* c' S2 f
! v8 r8 A; g) d& w function notice_details_meta_box($post) {- M, S8 N9 E! e/ U
wp_nonce_field(basename(__FILE__), 'notices_nonce');
" \9 g7 k$ l: A2 ~- x. l $notice_title = get_post_meta($post->ID, 'notice_title', true);
# Q. A" I% E0 i" g N3 O/ E $notice_content = get_post_meta($post->ID, 'notice_content', true);
6 O* J5 s! _" G ?>% } Q% }7 d; i) _
<p>) n; _& M4 v' y @( b) K
<label for="notice-title">Notice Title</label><br>6 ~# k, j9 E- f' K
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
1 V3 f3 p4 `' e+ E9 \" c </p>, n$ p0 n/ L* e( ^" }% C9 Y2 B
<p>: I, {5 p# y; C% Q4 t2 _
<label for="notice-content">Notice Content</label><br>; Y6 k' M, R0 H/ q' E, b
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
0 Z; u/ L( Y6 R$ B* u </p>
3 m6 ~# U+ U: h7 W$ W <?php8 y/ E8 Y" y+ W+ \. a+ d
}
4 l7 g8 f3 I9 V3 Q4 C$ k! _/ Z
& |- A: b1 L7 A/ ^0 t add_action('save_post', 'save_site_wide_notice_meta_box');
& E: M' I; Z! o% G8 F6 J function save_site_wide_notice_meta_box($post_id) {
h) J" w" @0 T( W: R. e if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
8 a! S2 [; F4 k& Y& Z4 H7 l) |4 x6 Y return;1 k9 @' J/ C& v6 C3 y- o: J
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE), i# T' r9 s2 C ?
return;: \ {! v" c: |& F
2 m T, |- a, _4 U- {
if (isset($_POST['notice_title'])) {
; r7 V% f9 X+ Y update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
: c* y. J* J( f1 `7 m2 a# B$ Z( {5 ? }9 \5 U- H& j: b, c v, {* H
if (isset($_POST['notice_content'])) {
6 q, J7 } U$ A* T/ R: }7 p2 Q update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
$ _5 l( ]$ P- D& c; p; T$ K }
C: |. _8 p+ D4 R$ z! y }
5 j8 H H5 @7 `2 k9 h# } ```
8 q' K" I; ]3 I7 p- u- O1 J! I Z& ~; N) I% t, J
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
$ i- E+ V# z* D
. ~2 A. S& O7 X% H$ K1 C4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:1 E$ e. v# t! ^7 ]; }' k: x0 V
$ S3 h) u( v, Y- T ```
! o4 h# g1 g" n7 V/ l) C $args = array(
8 T, R% v; Q% A: L% e 'post_type' => 'site-wide-notices',
: s9 g1 l/ L! I" Z7 _ 'posts_per_page' => 3,
- w8 T; ~5 a8 L 'order' => 'DESC',. o+ T' r' y. ~% h
'orderby' => 'date'
9 ^5 p, }: `% y. i R+ }# b' e );0 @: ], e; k) k- r
$query = new WP_Query($args);
4 S# ^! K2 z$ Y* P3 p2 }& C) D% Z if ($query->have_posts()) :, p9 i1 J1 @. h/ I2 t
while ($query->have_posts()) : $query->the_post(); ?>2 d/ e1 B. Y5 l7 O% J; K9 R
<div class="notice">
& f" b8 c0 z* w* m5 j$ G <h3><?php the_title(); ?></h3>
4 a$ \- D* y1 T <div class="notice-content"><?php the_content(); ?></div>/ t5 r" [% c2 X) c; s0 E7 _; G: s- x
</div>
* f4 c' n$ y1 w <?php endwhile;
* f; R: o6 H9 E% w d& m wp_reset_postdata();
$ i) o) b' {! ~( E3 P/ A endif;: V) [8 X! }1 A& a- Z
```
9 L4 T$ w5 j! E5 F6 V) _* p& B% Q0 K1 O" u. C
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|