|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?# m( v, |: y+ b6 l1 W2 ]
. X0 n# W( N; n" c7 O' W如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
5 ]$ L0 n4 b$ j+ j
/ i2 ^" j1 q, _! [* F/ X" m* c以下是创建自定义插件的步骤:' @7 r* A2 I7 ~$ c ]7 |
) j9 ^8 Q6 a2 r/ |3 d4 X1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
% Y9 `; _) ? l3 n5 g6 n
7 ^2 l s9 }) @3 u ```
$ Z9 Z d( r% _4 S5 I- j$ k <?php
9 j$ {% l# e7 a$ r& b9 } /*7 M+ X9 t- N" r5 b7 ?9 G8 u5 w
Plugin Name: Site Wide Notices Plugin+ H. G/ w* @" o& w! l# z6 e
Description: Adds a new custom post type for site-wide notices.
5 @8 F, P5 E8 u+ ^+ H: q Version: 1.0
! E5 @! ~9 h. a Author: Your Name" F; b5 r/ |" F
Author URI: http://example.com
$ T% z9 v. o: [$ x' z3 Q# a */
' [; f& s; n* D- ^ F1 |/ p" s/ q
# S7 G* L+ V4 T9 d5 y" y. {! { // Add plugin code here...
! F* w3 z. [) _( e, S, A ```( I' `2 a3 t& M4 L+ T6 A. |. }" Z
9 r; o" g3 E- [( @ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。6 t4 Z' G6 f9 G' J
. H' N3 m$ h4 w+ j& X. c; u7 I
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
5 E+ o8 S/ G1 w1 t: G) S4 w
, M. G/ \3 N( B3 }! s# o ```5 K) S* m7 x. ?) L k. a$ Z/ d
add_action('init', 'create_custom_post_type');& [: N) s1 k# [2 f4 Y2 d
function create_custom_post_type() {+ g a% P0 ^) x0 s, G0 |4 m A
$labels = array(
8 x( m6 s1 j* r- P$ h, h: _ 'name' => 'Site Wide Notices',0 y: Q& F7 W) h4 i1 G, j& [
'singular_name' => 'Site Wide Notice',
b; v$ d0 S; ?7 |5 c 'add_new' => 'Add New',6 S7 L) x4 D/ t2 u0 |
'add_new_item' => 'Add New Site Wide Notice',, O0 q# B( }1 g9 o
'edit_item' => 'Edit Site Wide Notice'," |/ D% x5 V1 u& f
'new_item' => 'New Site Wide Notice',
: n. G% ^" S( n. ` 'view_item' => 'View Site Wide Notice',
: B( s7 k# E9 t& O 'search_items' => 'Search Site Wide Notices',' j, T( @* K1 q( d! w. I
'not_found' => 'No site-wide notices found',$ H; N; c, U6 e$ |! {+ P" k& Y" M/ W
'not_found_in_trash' => 'No site-wide notices found in trash'0 X5 m! X+ U6 b( }& b
);9 b9 l F, v a3 n
- X( P, g4 F6 o2 O$ Q1 c# ^2 H2 G* S
$args = array(
3 L1 ~" g& P7 J" X- L* t# r 'labels' => $labels,5 d/ o" _+ \6 e: j$ j0 ]
'public' => true,4 s' w% x5 h. N
'has_archive' => true,
9 i2 }5 }3 J9 s 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),0 P. a4 w. A) a+ m6 o! _* o
'taxonomies' => array('category', 'post_tag'),1 x6 ]9 ?1 [2 m
'menu_icon' => 'dashicons-megaphone',
) [2 u2 o, m& L7 b+ u4 W& I8 } 'menu_position' => 5,$ k# m, j) s$ ^ P
'rewrite' => array('slug' => 'site-wide-notices'), d" w2 O& I) x: I1 C& |+ p
);6 q8 n# P' m+ X% o2 [. Y, W2 f
[1 X/ n' v' h& Y' r" ^$ k* b# D9 q register_post_type('site-wide-notices', $args);
# U1 n2 d# r3 K } Y# a8 k9 J4 Z1 S! b
```
7 D( h4 o9 M$ T, q, H/ c. |* Y
- L: A. A& A9 o5 V 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。8 ~8 t5 g" A& C
/ n }5 `# J1 Q3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
: B, |. Y/ I6 P# ~3 y" J
- o! o m& L! q) o* A ```0 [5 c3 ~# `$ q
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');: a+ X9 `' n4 D2 }7 S
function add_site_wide_notices_boxes() {5 R+ \" v+ q {; I O7 C
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
* B; P5 R$ U/ H! I s }0 o) ?: q# R8 r+ N2 n
- d+ p+ z3 y3 Z
function notice_details_meta_box($post) {. ~: a3 i6 z( I" i0 M6 [
wp_nonce_field(basename(__FILE__), 'notices_nonce');
9 U# `4 p0 P! `0 D $notice_title = get_post_meta($post->ID, 'notice_title', true);, t+ z5 Z, y$ M" I
$notice_content = get_post_meta($post->ID, 'notice_content', true);
, s1 R( }& {* L1 K3 n3 d: P ?>' T2 D; _& J, T* ~2 F( O1 s% C$ o4 G
<p>
/ N8 V. `8 a# p1 k8 ?9 s <label for="notice-title">Notice Title</label><br>
3 v7 ~" U) n8 C/ W/ M6 H: t) o2 K <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">+ X. G+ y% `1 B! r! R: Y3 ~- m
</p>5 m5 I5 X: ? n0 D
<p>
) ^5 {; v! ^7 A <label for="notice-content">Notice Content</label><br>
, G% E0 Q$ t. q6 c1 h+ Z <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>3 T0 m |4 p) |8 c
</p>' e, M8 e, N4 b# i2 l5 }
<?php: l3 X7 D+ g; ?, G2 U( S( O
}
, C4 H6 A/ F- X3 Q# b# t
9 p4 ~1 R/ |( d+ V add_action('save_post', 'save_site_wide_notice_meta_box');
0 U" v0 B }8 _) T function save_site_wide_notice_meta_box($post_id) {5 k* _* |. o; O& E
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))0 K. O7 S& {+ h0 u) y
return;& f3 b" S+ V/ @( `
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE), H; N* ?6 s$ x1 R! v8 ~
return;# \7 W6 ~ K2 p
; C* U: b! u4 V, l$ _4 i% |
if (isset($_POST['notice_title'])) {
' T, m2 E) U% h1 Z9 ~# \% [ update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));( A8 ~4 x; r( y( I1 Q
}, \3 r( b0 N# M, Y' G' q8 R$ L
if (isset($_POST['notice_content'])) {2 [# W2 g K. r1 h! m$ ~: }/ W7 S
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
' Y1 h! ^! I s0 \( s1 g- Z1 O }
$ q2 h! K0 q2 G }
- H, d1 e5 o9 R ```
* @3 [- \" i: d+ G `+ _. u7 `4 L6 B+ z7 o: ~& s
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
% v. s, ~9 ~! @
) W- l. b/ e/ T1 A$ ]" ]4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
: W" n; s9 x$ L; e) u% N. U$ Y1 I& x0 w, q) }+ P- v
```
1 s0 E: n$ |7 s7 c6 x& M $args = array(
7 g" {. U- z8 X! W/ u% [ 'post_type' => 'site-wide-notices',/ M* }. C& V3 F' M* b, C
'posts_per_page' => 3,# f: }$ y Y) ?6 Y( e) K0 [6 U
'order' => 'DESC',
5 ^1 l0 I, Z$ l" l 'orderby' => 'date'6 B5 E9 \2 w! D, w& \7 H. {
);
# s5 p4 u: d | ^; S $query = new WP_Query($args);. D+ b6 ?! J6 W' D3 a, H# Y) U. j. A1 S
if ($query->have_posts()) :" u: m) `+ _ g9 b" y! g" R
while ($query->have_posts()) : $query->the_post(); ?>
; q4 a6 ~0 b3 C# [' N+ \ <div class="notice">
1 @# U9 x' X" A1 V, Q <h3><?php the_title(); ?></h3>/ l$ W: ^4 S' ^, `. H' P$ {( ]
<div class="notice-content"><?php the_content(); ?></div>
4 J" T7 ~* a- d* s. c0 q, \ </div>5 t$ Z) d1 \; ^8 S
<?php endwhile;8 ^" ~8 C9 F3 v w' j$ V
wp_reset_postdata();
$ u" W9 u- U4 @- X: |8 r0 t: g7 p endif;
3 v& w3 B5 Z, E3 e1 k ```9 F/ ?9 v4 }( z+ a9 D( }
- D$ Q: p; X# n0 d# i+ t5 \2 q5 ? 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|