|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
, f- `1 p+ t. A+ Y) B' Y/ R; f; ~/ { M4 {' ~6 d2 l
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
* \% ]7 x0 |8 k% P% ?
/ S0 ?/ g% o5 n9 V以下是创建自定义插件的步骤:
2 l0 O. _3 U' T: E+ Q4 `- q( c' N4 ]6 [: \9 }0 k- T
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
5 D% R. Q2 l1 B2 L4 t# A9 R$ ~: }: N- j5 P" h8 {/ Z S* v
```
2 O) E! T! c! t: w+ v <?php
9 |' l4 t W$ V) P! I; A# \' ] /*
! s+ \+ s4 z/ y* W8 A Plugin Name: Site Wide Notices Plugin
4 \! ]; a3 | j$ n9 g3 ^ Description: Adds a new custom post type for site-wide notices.: \+ q! ^; M6 ^; }
Version: 1.0" v; i/ f/ N4 ^% ?
Author: Your Name/ z% Y- j3 Z7 |" R
Author URI: http://example.com0 G8 B' S5 Q/ [ y2 s" b+ Q
*/
2 h- e/ r6 ~7 z) I# a* z1 G0 N( l0 e
+ o$ c8 g3 ?* }! a4 `1 L1 `( M4 u // Add plugin code here...
9 }) j! N; R/ ] ```$ S1 h. i$ K& N* ^! X- P1 I
! v: S% e" d+ H' r 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
' z- s! E6 D! X s! h
, {$ j( U3 Y! \9 y" Z4 { i2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:+ `. [2 g- s# o6 G1 x& o
+ S" m' j. v" s1 H( z Q ```
9 q9 B+ s8 {5 W( [2 v9 g add_action('init', 'create_custom_post_type');
2 d" A. n6 n s. z3 x7 l" \ function create_custom_post_type() {, N8 Q3 i1 m+ a$ Y
$labels = array(
/ \* Q) ?4 I) j* @& g8 b' S' T0 ? 'name' => 'Site Wide Notices',7 v. w! V3 \" I( P3 I$ V# u
'singular_name' => 'Site Wide Notice',) T+ T6 ]3 N( J1 ~; H
'add_new' => 'Add New',. l: o: H8 p- [2 b
'add_new_item' => 'Add New Site Wide Notice',
3 S9 g. W0 _5 t1 ^ 'edit_item' => 'Edit Site Wide Notice',; d+ h$ Z' H4 _) Y& G
'new_item' => 'New Site Wide Notice',$ l: e t1 G3 Q! [/ t& ~ ^
'view_item' => 'View Site Wide Notice',
* K c5 E1 C6 S4 C: B 'search_items' => 'Search Site Wide Notices',
0 k# D- [, A* T' L 'not_found' => 'No site-wide notices found',
/ ]) m4 B0 h6 W 'not_found_in_trash' => 'No site-wide notices found in trash'# T0 z! Q- X+ i/ d# R, @4 l w' J
);9 N, H9 j6 p$ G. B5 K
$ m d: w4 o( a $args = array(3 L6 Q: G& L! u7 D/ y
'labels' => $labels,
% J1 f4 E* P' N' Q 'public' => true,
6 Q2 x6 {" R. e2 k3 _2 ^' _8 ^& \ 'has_archive' => true,
+ ^! n, F% X6 ^6 u 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),& D4 {) |1 r) x+ u
'taxonomies' => array('category', 'post_tag'),7 O, w. W2 o+ G% ^5 h
'menu_icon' => 'dashicons-megaphone',
1 @6 \2 v: L) N+ v# M3 o 'menu_position' => 5,9 s0 E: z+ t; U6 O7 {- H5 e" F7 y- w
'rewrite' => array('slug' => 'site-wide-notices')
* V/ F( D/ X! c$ k2 E8 j );
3 z1 [( E: ^0 x* I2 t# b/ X
/ V2 g. ^! y, A+ p$ L1 j register_post_type('site-wide-notices', $args);( q) o7 { F; _. Z* \" H9 ^1 O3 t! U
}
( Y$ i; L1 ^$ Q$ |+ n ```
$ |6 {7 z+ D$ _- S( E* s& k* ~% G" G$ e) X$ O- G2 C$ x( n+ I6 b
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
" \; `+ r2 I- b% \ Q+ o5 o
' q3 G- w- D s7 Y! T2 W3 Z; \5 Q3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
9 c% P6 [: K" s1 y$ w% r9 N g) n# G' t
```
/ x$ i; @% T% m2 a1 O4 ^! G$ p) t add_action('add_meta_boxes', 'add_site_wide_notices_boxes');, \& K e4 w5 f/ ~! U# N* O
function add_site_wide_notices_boxes() {1 j( ^2 w2 N( A
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
2 i& ~0 D3 Z' {7 w0 Q& k5 T }3 r. P- \$ a6 a1 I+ Y, X
. A0 r) J7 u& X7 _+ s- y
function notice_details_meta_box($post) {
9 v- E% P3 G4 N8 V, a3 b! F1 ^1 k0 K wp_nonce_field(basename(__FILE__), 'notices_nonce');
( K1 Q6 s: w" c% Z $notice_title = get_post_meta($post->ID, 'notice_title', true);
$ y8 V/ d% f& |8 E $notice_content = get_post_meta($post->ID, 'notice_content', true);
+ x; O- u3 U2 h* q7 t3 z$ C ?>
2 T0 d- I. P( D <p>
- U& i: v$ f5 r9 p <label for="notice-title">Notice Title</label><br>' {7 j3 ?5 R# U3 L4 p
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
3 N/ K% e$ X7 L4 A+ `: g* Q </p>
$ h8 z. y6 w# X) E& [& c3 b3 }" b' K <p>
; i+ p! t: ^7 M. W0 G2 ^ <label for="notice-content">Notice Content</label><br>
8 T- x/ S! C8 E" g& {' S9 f/ | <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
Q# U: a r( S2 b u2 L </p>
0 u( _: V3 C4 v) s <?php3 Q: _/ q+ `& S& u( I9 ~6 n) a! H
}
+ M5 j$ K9 B) G7 k& z
5 Y2 d7 X; z# d# A add_action('save_post', 'save_site_wide_notice_meta_box');0 k8 o0 Y# R5 T0 a! B+ o
function save_site_wide_notice_meta_box($post_id) {4 W& c3 X- N7 x/ Y3 {
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
# w6 T4 h R5 P. W return;
7 n) ~; g' K& a! k' v if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)( t7 O3 \2 h/ b3 U6 A
return;
2 { ?* i/ e% J8 v4 k7 ^8 [. L2 J5 F, Y' C- e* p
if (isset($_POST['notice_title'])) {% }) \; P: D( e# Z- G; ~
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
x+ H5 e4 K8 g, R. O6 { }
0 h; a& t. T7 M) @, O. w h* M4 J2 Y if (isset($_POST['notice_content'])) {
/ ~. [( ~& f7 j update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));! e. G2 ^* ~! v
}
" @7 D% }% Z, z- L" ` }
8 {6 w$ j# A7 Z2 @' |# i- G ```1 @4 R* D: N: T. C
% U7 t, Y6 B7 T
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
' ~# `* p1 v% p! L
' b/ \6 F( |% F/ G- a/ \3 s( c4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:: E' z8 y# V- m2 H& @" u% ]
& J# {0 X9 {& t9 k5 l1 @ ```
% H! G3 c1 l2 G6 _ $args = array(; M4 F8 J, I8 M" `9 h3 }) T: j
'post_type' => 'site-wide-notices',4 E6 ]4 ]# v3 f! ~ X7 |
'posts_per_page' => 3,6 e& q$ [" o+ o* T1 D, k& m
'order' => 'DESC',4 W& U1 j+ {7 ^' Z( X9 L
'orderby' => 'date'
* ]2 H* w. k# N7 C/ T+ a5 [ );
/ x0 Z& [/ v& g' p- i, [ $query = new WP_Query($args);
3 ]# l4 r1 A0 L* H) { if ($query->have_posts()) :
2 o7 t! h4 z' S# } while ($query->have_posts()) : $query->the_post(); ?>+ f' g q, n- S! w
<div class="notice">, Q! _# S& ?& ^9 L
<h3><?php the_title(); ?></h3>
' ^2 y9 N x, ^5 ~ <div class="notice-content"><?php the_content(); ?></div>" n: c/ G: ^9 \6 K
</div>
7 f& E, ?& T* ]! t Y ?1 O4 { <?php endwhile;9 o/ G4 b: ^5 ?; K( E4 X
wp_reset_postdata();
% Z5 }( w: X* Q( A endif;
! i( I( z# m' x6 d ```
6 ~/ i* _ T+ a/ i5 u
# L7 ^, L* t& l K$ T- u! T 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|