|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?$ `7 C- v9 C" m
& Q4 W# T" x8 S& m" }. s; A: W( `如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。$ c7 D. z4 W4 [9 h9 `
7 I. ~# J6 E t% \. Z1 S以下是创建自定义插件的步骤:
9 \' B3 e3 H7 Z( h8 K& W! ?# w3 D& ^
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
1 J, k5 {# J5 E: x6 U4 Q
) w5 v2 C! |$ D5 v6 T ```
. v- x0 m) X" L4 V, f) y- C <?php% \$ j) l& o' D! |3 \+ \& A
/*" j/ Y+ A. W3 y: Q) ^5 I& R0 Z
Plugin Name: Site Wide Notices Plugin" H/ S% t: J6 o, [( B
Description: Adds a new custom post type for site-wide notices.
, e# d# [, I8 S6 h, @; j Version: 1.0; s* I. C' p" ]: l4 o' @
Author: Your Name
% g7 O$ r3 J; ?' H% a Author URI: http://example.com
% v# g4 G7 k5 {5 r$ t' O, h; ^ */# {1 v# d/ C6 R: W5 H1 K( o
, g' ?: I. r) |- L0 a; |
// Add plugin code here...
R# D C3 O% h ```
" D% p. M6 J. X/ ~% }' O3 n& d+ H5 S7 B
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。+ g# R$ i& r- X K& D
. h, m0 R) c( H9 O/ M0 V1 d& M' t2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:% Q- D4 B' y) |* h D& X# J
7 U- o. q1 K+ u. `0 z$ N7 d. s
```4 a3 ~& j9 v8 e6 ?4 f' m! F$ d
add_action('init', 'create_custom_post_type');' |$ J+ u. E5 O2 A% r7 }$ L
function create_custom_post_type() {
( G) x) |6 L0 d' ]. j- Z $labels = array(
! d/ h/ l* V9 T 'name' => 'Site Wide Notices',( x W' ~/ X0 c- b
'singular_name' => 'Site Wide Notice',) P, F9 L3 `$ |% t: f7 r
'add_new' => 'Add New',
( c# O1 e: u3 f% @0 a8 w; b 'add_new_item' => 'Add New Site Wide Notice',( \/ V8 k+ A4 _2 V8 }: a
'edit_item' => 'Edit Site Wide Notice',
2 Y; f, P" D$ n 'new_item' => 'New Site Wide Notice',
0 ~& `6 R1 _, b9 R1 @4 M6 L 'view_item' => 'View Site Wide Notice',& a$ N6 n) H- u( I
'search_items' => 'Search Site Wide Notices',
7 I% Y$ M+ v+ K( J7 m( @ 'not_found' => 'No site-wide notices found',0 V1 Q8 k5 e( f6 }( O
'not_found_in_trash' => 'No site-wide notices found in trash'' W4 z' Y) v' d0 L! e& Z
);
$ X, y5 [8 l' @4 P" L% p
. s: \- o$ K. H* a! @8 C$ H $args = array(
! E$ ?6 f8 |) W 'labels' => $labels,& q* r- J" F7 B) z# m, G; U& g, b
'public' => true,
F; \$ @* m$ g0 Z 'has_archive' => true,) R) i) k# b0 r D$ l
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),2 J5 i# G7 z. h. d# d
'taxonomies' => array('category', 'post_tag'), Q* @4 @7 v+ U( J p) _) L ]
'menu_icon' => 'dashicons-megaphone',! n" V2 h. b% W( J1 u, f
'menu_position' => 5,2 I1 C8 ~0 z4 r$ f0 E. U
'rewrite' => array('slug' => 'site-wide-notices')* F. I+ |$ b6 D( @' i
);
* x# d* J8 e2 f" `3 W- A! J2 X7 j5 ^! }$ O
register_post_type('site-wide-notices', $args);+ p. V4 D# j, ~$ Z5 l, j3 h: `5 ~
}8 g! ~) |- `& k
```
( l1 ?& ^4 T @, z
( \/ j( @2 U/ M: M, e2 r6 i 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
% O# O |% a( Z) D% |7 w
: H0 ]. p7 M9 ^# A3 S. g/ D3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:% J; ?% s7 K& {" w( P* v) j/ I
5 D, M: k# e8 n! K/ w3 i/ _ ```$ ` w9 c# t8 q* d
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');# c$ G* y& P5 M# {' |
function add_site_wide_notices_boxes() {: [% E9 g# M) f& Y( s. ] [
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');2 }2 T& c. P( u& D# m
}$ e) z1 J" ~5 b; W# E
; g; f7 F+ d2 U1 o$ S$ g
function notice_details_meta_box($post) {7 l* v6 [0 t. Y* _
wp_nonce_field(basename(__FILE__), 'notices_nonce');2 e1 R' t0 \9 H q
$notice_title = get_post_meta($post->ID, 'notice_title', true);
1 t6 \5 A! l4 V! ?) N8 }: W1 t $notice_content = get_post_meta($post->ID, 'notice_content', true);0 e$ l" V2 u- U% @$ g
?>/ d4 M2 V0 X9 e1 w
<p>
+ R3 W1 \ x3 `7 {, A8 s <label for="notice-title">Notice Title</label><br>
7 g/ f$ P! `, s" `1 L+ P <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
B$ K V+ m" |' z( V3 C </p>
2 W4 p0 F6 b: M4 H <p>* l# O. [8 i) f8 g, ?+ H9 H
<label for="notice-content">Notice Content</label><br>
" \( X6 D( F+ h <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>1 z! k8 Q6 P- ]! H/ p
</p>
5 ]. z: H! A4 c% {$ A0 n7 T- y1 B <?php
- M# k4 {6 O( G& y) k }
" p9 k: Y& i$ ^4 B" y
1 W; ]) r" y8 n, z, P" |( e9 {% @8 v add_action('save_post', 'save_site_wide_notice_meta_box');9 _" _% T3 [, C% c/ W& e- Z
function save_site_wide_notice_meta_box($post_id) {
& n% M8 [" h8 v" A. T9 Y0 B8 y if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))8 H; v! Q& n5 O5 R( `
return;1 ~8 _4 t z, l+ U! {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
|; @3 x8 V! ?% O2 k. L B return;
0 t7 X! J. Y& R* T( ?1 ^6 |& k
0 h: J# b/ F4 p- X3 _ if (isset($_POST['notice_title'])) {
0 i l$ \( D, j( ~, r update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));9 E% S8 z! O. @! C. V5 ~
}, n; M% d0 o* B1 ^
if (isset($_POST['notice_content'])) {( `; P/ A3 |$ X; q+ E2 T
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));$ _ J; y7 e. V1 L1 v
}
7 j' C6 v+ G% s: m: ]: J- h }$ J) P7 I: Q) [9 u
```, u: z+ W5 i G, R+ l5 d3 `/ B
+ n4 {0 |' g2 ] 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。3 O6 m: ], C" P" Y# H Z! M$ a
' K( E% [) ]3 _! P/ ^) X% ~* M
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
/ n. e( e! p, `! g6 f+ G9 Z3 @9 t# \: l8 L1 I% I8 d/ F T
```/ B, A/ K t. ]/ D- T1 j1 B
$args = array(
& g6 N$ ^/ T @9 ^ 'post_type' => 'site-wide-notices',2 u2 b, `/ U3 n) ]+ R' z( m
'posts_per_page' => 3,
8 u9 ~9 E; M) E# M" q5 i6 k 'order' => 'DESC',% ^# @" J0 V S( c5 Q
'orderby' => 'date'1 U4 k& n: k" x) G# J
);
7 ]* a7 G# o+ ?8 G" x7 q $query = new WP_Query($args);1 p+ j4 K- \7 E2 K4 u
if ($query->have_posts()) : A: D$ T+ ?: ?2 z# H/ F
while ($query->have_posts()) : $query->the_post(); ?>) ~! H- K$ z# j' M `, d
<div class="notice">) W3 v! Y2 x6 B# T, N
<h3><?php the_title(); ?></h3>0 _* f0 {( L. c5 d7 x2 R2 Y
<div class="notice-content"><?php the_content(); ?></div>
1 Q' J- n+ q5 `( ?# w& A </div>7 C$ J" E5 N; b
<?php endwhile;* S, V2 U4 `3 H* |3 _8 m! b/ R
wp_reset_postdata();
|( f2 {0 S( z2 U% @ endif;
9 p' ]# H$ `' D, _' N ```: f3 J3 v B$ V: M" S/ D0 F% ~
3 c0 Z8 u& {# ?8 g9 o) p 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|