|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?" i" b& u+ ]1 s+ [3 |2 l
+ i) H8 ~/ X* U" P/ K1 Y如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。9 A! v r* \* P1 [4 ?
( O4 v% t) c: o' c6 Y7 _' i8 D, L
以下是创建自定义插件的步骤:
% C9 `5 l1 p n+ ~8 X
% q$ c+ x3 D3 k: ~3 E1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
% K5 X0 a, f% Q+ e3 V N9 m o0 u# d; `6 x
```
$ q" P) {$ ?; V/ j <?php
* `1 A* T: o5 l O9 P% j- t2 e: e /*, D* @- _$ a4 Z+ f. B7 |
Plugin Name: Site Wide Notices Plugin3 A5 ]- Y, {& G: ~' M4 |! k
Description: Adds a new custom post type for site-wide notices.3 e# E& C; Q c% A3 |8 @
Version: 1.04 \) _1 k2 ?9 E) ^
Author: Your Name
Q# ?5 p2 q8 \5 K* ?% I Author URI: http://example.com9 O- N' C; ~% t4 q# x' U/ T" u
*/) L0 B3 n; T8 q) M! q& m
! _6 [: j& N& j" |2 b // Add plugin code here... w0 g8 ^7 D$ Y2 S1 o/ Y; Q
```4 [+ ^7 A' [! [2 ?5 p
& R5 x. A: s! S! M8 s3 `
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。, o, O1 Z) i1 P$ j8 n# o! p8 ]( \2 U
4 u/ `" O8 d4 W4 I0 r$ d
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:1 z- o0 C1 J' H, p) x
* m5 c# }, J* z9 r# K( ~+ A" i ```, \# n. E# L7 u# v b+ h- h7 e6 P
add_action('init', 'create_custom_post_type');; e1 l. T( V" |
function create_custom_post_type() { m$ C4 e" o+ C( t" S/ O
$labels = array(- S8 Y' q5 @8 t7 {9 C
'name' => 'Site Wide Notices',
$ [+ I% \& P8 C4 |, w; \0 Y/ r 'singular_name' => 'Site Wide Notice',
6 R* e/ ]! V' H2 u3 x c 'add_new' => 'Add New',1 C. ~( P, s; O8 c
'add_new_item' => 'Add New Site Wide Notice',$ W" q- a1 U5 t! p" t" k! W
'edit_item' => 'Edit Site Wide Notice',
, T" U0 C3 e6 m8 \; d- k 'new_item' => 'New Site Wide Notice',& i1 |6 E9 S5 W( H
'view_item' => 'View Site Wide Notice',
( \. ^$ @7 B" `! W/ E) A 'search_items' => 'Search Site Wide Notices',
6 j, }& Q0 Z* T0 I5 j- j0 U 'not_found' => 'No site-wide notices found',; x7 `/ p" `2 }0 T7 v5 w
'not_found_in_trash' => 'No site-wide notices found in trash'
- g$ d2 S8 c9 }6 Y# v3 h# k );
$ ?- f7 f( T a7 Q2 e+ B# d$ X- F7 F4 Y3 ^, Z3 u0 i$ ~1 z
$args = array(8 l* f7 u& L% Z; P
'labels' => $labels,
; s) W* j% J7 V# w; q4 }3 _ 'public' => true,5 }8 A/ b, G( l) Q1 {4 z
'has_archive' => true,$ Q& i5 b2 C( Y4 b; v+ M) a7 K
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),' }! b- j' t" ~6 B2 O
'taxonomies' => array('category', 'post_tag'),
4 [& n2 O+ d# @2 i i 'menu_icon' => 'dashicons-megaphone',
7 q: W/ b2 o$ H2 N W) O9 d9 a1 g/ ]1 ~6 y 'menu_position' => 5,- ^( O5 f6 |# n/ o! Z- Y- s( b
'rewrite' => array('slug' => 'site-wide-notices')
) q1 [1 b2 j$ V );2 Y# c3 A* e, A$ x, T7 J8 I
3 R% F' ]/ A5 V0 P1 h7 ` register_post_type('site-wide-notices', $args);
$ h3 c6 S7 ~2 L) b: ]: x; E }" o( _+ L6 n0 V$ [
```; J6 d, u( [7 n; G6 F& k, n+ F: G
8 f3 U9 a U) l 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。* l: Y. J9 \4 B( o8 f$ Q. ^
) ]' _- @0 c8 x9 s0 d$ u+ j3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:0 v% E/ n1 |- J( t! w& L* ] N4 m
$ y5 @& m( m/ p
```
- k' n5 Z7 x$ }7 t' J' P add_action('add_meta_boxes', 'add_site_wide_notices_boxes');% Z* ^) `! K5 y8 X
function add_site_wide_notices_boxes() {* t7 X0 W: T4 e! A' N0 u
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
2 n4 B2 X7 F* n; D- ^$ y# `8 p }
0 A% m' N1 I& P4 I6 ~- w3 v! |, t% I- m; N
function notice_details_meta_box($post) { I: g' A( [ r" {
wp_nonce_field(basename(__FILE__), 'notices_nonce');7 K7 S8 s) X# M, O, m& v/ D/ N
$notice_title = get_post_meta($post->ID, 'notice_title', true);" F) r! K) S" p( M
$notice_content = get_post_meta($post->ID, 'notice_content', true);
, K! S' i% R$ G. H( f }' _ ?>
' K1 ^9 C- f$ m <p>7 N5 T3 a" j# B4 r- y
<label for="notice-title">Notice Title</label><br>. B g0 N6 Q: _. o6 l
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">: Z) r4 m5 K! e, n5 W2 ]4 v
</p>/ x: G- d+ C" Z+ i$ Q5 ~
<p>
" X3 K3 h3 _2 y+ s, N" _ <label for="notice-content">Notice Content</label><br>
6 k, f! \/ n8 _ }* ], s% Z <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>/ A! C M7 ?5 P- t4 M" |8 X) I+ x7 O
</p>
' V0 z. V% D' R7 N. j/ b <?php
6 K% I6 d) W0 ~; n' D9 m1 p+ w( t' f }
4 d+ h% X8 O& e* G- d, m8 Z# [9 U! s& Q
add_action('save_post', 'save_site_wide_notice_meta_box');
3 t' u# i) I+ W6 W function save_site_wide_notice_meta_box($post_id) {
* f& W/ E- E- P/ M" ?' B if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))1 @$ F* [' q" t! J
return;6 f9 U1 b, q% Y% F5 z R
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)+ U- C5 ]2 J; e! f( P9 Q6 ~# I! \
return; e8 k4 ~: X, |8 S% p" x& [! J: ^( C
$ K, C! J) ` [' |$ R( h2 G if (isset($_POST['notice_title'])) {
0 w8 A0 O: ^7 M* w7 r2 C update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));0 M$ ~3 N E! |3 ?: w& B
}
s. m8 `2 T% {3 k0 t6 m/ A" M+ k if (isset($_POST['notice_content'])) {
9 `3 V; ]3 A& W7 i& o update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
3 x3 [' |4 Y; M( ^, ]3 G# F }
$ I, N3 ~/ ^$ M9 N% P0 P$ \: ^( O }
( U7 Z5 h0 v7 M! x. [5 Z- ]5 @ ```% F1 S0 e2 Z* y; `, U
6 ^# p' n$ J @8 Y 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
; F; ]: F9 T9 T" w2 c5 Y! Y7 Q
* T9 Y9 \. U! |$ w" }8 V- `4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:4 z& {; X7 u5 V W) r1 A/ D
! i% G. _/ Z- m# v- |- B) }% ?
```
5 Q" d/ |8 Z0 f; a: Z4 ]- W $args = array(
" @+ l( ]6 R9 A; J' ^) \+ y8 t 'post_type' => 'site-wide-notices',, ^; J2 g r7 D# C! O4 C# O
'posts_per_page' => 3,
9 i2 V' {% W. g' V1 p 'order' => 'DESC',
& T& j$ z2 z" ?0 Y+ D+ H2 z" e; Q 'orderby' => 'date'5 c' F9 Z: `" I
);
4 z- @: R. k; E% ~3 A& W $query = new WP_Query($args);* R+ H, }; N# f1 _
if ($query->have_posts()) :/ n6 U9 T/ f: K- j5 c
while ($query->have_posts()) : $query->the_post(); ?>: v5 _9 [' s5 o! y" I
<div class="notice">! r3 t3 q/ }8 q) I* V9 }* u, ^
<h3><?php the_title(); ?></h3>) K& r2 H! `, e b1 y
<div class="notice-content"><?php the_content(); ?></div>: Z. `! N$ F' l2 w( ^( n
</div> {* u! G* [: B3 t5 H9 _
<?php endwhile;4 o- D% }) {0 ^' ~# N. P
wp_reset_postdata();
" I" {* Q+ d6 B7 g1 v4 y endif; r( G4 A' l- x5 p
```
- Y7 {- O. E6 p
9 L' V2 R3 n; F' X& P4 ~ 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|