|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?; K8 S( z8 x, ^ k8 O) C1 V V5 J
5 @. M( } b* z5 D
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。; R1 F8 T. T9 J, R% N$ O% z
# q& q5 z0 `! L! c5 g1 y5 |以下是创建自定义插件的步骤:
3 i! f) D+ N/ R8 \4 T; o5 ~$ b% ]# G5 z; e1 m
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
0 O& |0 x1 c& [( ?, n1 H( n
+ ?2 o" Y! y$ A. @7 F" a6 M& f ```
4 z1 C- @7 f9 | ? <?php
+ M$ v0 i" i3 M6 E8 _8 d /*
; {0 M* @; y; T, a* { Plugin Name: Site Wide Notices Plugin3 E) H7 }8 v7 u0 l: n
Description: Adds a new custom post type for site-wide notices." k8 `$ v) F' y( }& R
Version: 1.0/ k t }# A6 G% p; j, n
Author: Your Name
2 r8 e4 V# x" o' W4 v' J Author URI: http://example.com/ B& X: `2 s( T9 Y8 m
*/
2 E$ d i" L {7 P, J
3 Z8 _9 q3 t" B& I // Add plugin code here...
- I3 `6 h+ u0 `1 i5 P$ Y ```) |. O5 S' S, a; u* p! J' G
. ^, r9 s1 v4 Z
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。2 X! J3 U3 {0 I
# ~/ }0 E/ B% e7 Y
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:" o* {& F4 i% ]5 x6 ~3 y+ y2 n
/ C9 C# F9 ?; ?# H/ q
```6 k' C+ I1 P" _1 W2 I% r( t+ j" k4 P
add_action('init', 'create_custom_post_type');1 j G t0 ]8 h. D) K* {0 t
function create_custom_post_type() {4 P, w6 X( y: F2 k3 W5 ]$ `
$labels = array(
4 |; S; q8 W2 @- d 'name' => 'Site Wide Notices',
" P% w& ^6 P0 J; G8 Y/ R 'singular_name' => 'Site Wide Notice',9 x6 y1 V8 A: b! Q
'add_new' => 'Add New',. g. ^- s6 j! h9 d
'add_new_item' => 'Add New Site Wide Notice',
/ {8 ^" A) W8 U! a+ z% `6 R 'edit_item' => 'Edit Site Wide Notice',
4 n# Q7 }) f# B' o 'new_item' => 'New Site Wide Notice', s! _$ o) @8 o/ u8 j7 _ x2 T
'view_item' => 'View Site Wide Notice',# t6 S2 Y, Z E
'search_items' => 'Search Site Wide Notices',5 f9 U- o7 |: G$ o1 i+ X
'not_found' => 'No site-wide notices found',
2 d* ]+ Z: Q g 'not_found_in_trash' => 'No site-wide notices found in trash'9 \3 }6 w# \4 U& E" M& u K
);: _$ A2 o& K9 l- c1 C
; X, _* B. R$ l _( }
$args = array(
2 ^& U4 p% M+ B: I8 C2 w5 Q& A+ \) R 'labels' => $labels,
2 _- w1 D7 ~$ q 'public' => true,! K& }! y7 V1 q# K6 o
'has_archive' => true,
0 b1 F0 V" I% i& M, y0 [8 V 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
$ R* l% X4 ]9 P- P+ { 'taxonomies' => array('category', 'post_tag'),
t$ E8 ?' c) t' ~! ^ 'menu_icon' => 'dashicons-megaphone',! ~2 j9 }: c& Q- i
'menu_position' => 5,
7 k" V* a* B. r: o' l 'rewrite' => array('slug' => 'site-wide-notices')
. M" r; L1 M; m" h9 c8 M% G7 v0 G );9 I i6 h7 n& {+ O: f5 r; w
* |' t4 t% R [7 @# p M3 n. n% K register_post_type('site-wide-notices', $args);
9 i' L7 c8 o/ L% q. @ }
2 |+ g& l4 p- Z& {5 g ```6 W* v9 u- p) r* j0 Q
4 T6 y! h/ e' t% w# r 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。% X1 Z1 z) q7 g- b6 }) r9 S
" Z( \" A) f3 f# N+ e! D
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:: Z5 ^7 E' A% y% J N
$ t$ ]! J7 E& S: I2 }' V
``` u& [- a0 ]) \7 \8 g
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
6 a3 ?. R. G; E! G3 v: R8 o B+ K function add_site_wide_notices_boxes() {
! b8 Y8 Q! T* H3 V& a4 M add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');+ I" [/ y$ l8 U1 J( P5 _- b" J
} ^2 ~- L) F1 a- u$ a8 \# P
5 n1 b* I9 I4 ]% a' s" G$ a
function notice_details_meta_box($post) {
2 N9 J# f3 l- R wp_nonce_field(basename(__FILE__), 'notices_nonce');* \7 g& c& K$ g. K' b3 j, L0 o6 X) l
$notice_title = get_post_meta($post->ID, 'notice_title', true);
/ i! W- ?7 z/ ]4 a- ] $notice_content = get_post_meta($post->ID, 'notice_content', true);
# a- _, g' @' R1 s# K ?>: B( {* i# q O: Z( E- r
<p>
$ v3 J. y5 _0 r <label for="notice-title">Notice Title</label><br>
) R7 ~2 N8 m; e; j# ~9 q <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">* F7 `; \$ w+ ?7 S5 G6 q
</p>5 k0 ?" p$ b( A7 F, t& E) Y1 v$ |* k
<p># B* i% h1 }7 j( K
<label for="notice-content">Notice Content</label><br>( m: f& w) }: Q6 l, M+ }9 s
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>! A" J* t6 o- D+ Y* G" D$ y
</p>5 A. L" B" C$ X8 q" ?2 F+ L
<?php
/ T) v8 P' F: ?1 L }* n8 w# z8 ^* s' _% C4 D6 a- G
" D! ]0 ?4 k: ^5 X add_action('save_post', 'save_site_wide_notice_meta_box');8 A% f" ~# t8 m7 [# u
function save_site_wide_notice_meta_box($post_id) {
" z. k7 Q; f5 `3 c: E' F' F! ]7 i- ` if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
+ F, c% k( J: D$ V/ }; G9 m return;
0 \ ?+ v7 X0 ]+ h+ T! S0 i if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
D9 F) k- T4 k9 Q. { return;
4 ~, _# n6 E3 F: t6 h8 V
# K0 ?7 P. ]/ |' [ if (isset($_POST['notice_title'])) {
% z/ }2 d6 }1 _! L3 O update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
7 c( H0 ]& r; K! h4 n6 n1 { }# s# P5 Z7 B: _" x; G R
if (isset($_POST['notice_content'])) {( [+ U) @! f4 N s
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
2 b+ @2 d B* [ }
. i2 e* K( D$ Z0 H6 e }
; W0 S, e" g+ u/ I; M+ C ```
2 v, V) @0 i$ |6 }4 M. U9 b9 ^- @, K4 r B! p% E
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。, H* Z4 V0 P& m" k
+ ]' ^) b/ D7 I6 j7 r( I& i
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
1 ^8 `- i) ^6 B9 s8 O; A: ^/ o( \) r! G( x4 r
```( D/ L2 @0 j6 J2 E" }
$args = array(' U: `1 w" n& n# f9 [$ r
'post_type' => 'site-wide-notices',
- C# s' T+ s6 K 'posts_per_page' => 3,
; f+ F: g+ r7 Z& P9 a 'order' => 'DESC',
% }. o5 k/ z. j A 'orderby' => 'date'
2 e+ [9 r8 n! g );
' Z- N& ^$ v$ @5 T1 ? $query = new WP_Query($args);
" v0 ?7 {. Y( H9 |' M" y' f if ($query->have_posts()) :
3 u8 i; o3 A- i$ n$ Z' C& u while ($query->have_posts()) : $query->the_post(); ?>' Z# t- h1 W& @
<div class="notice">
s7 Z# h1 V3 B0 z; z* u! c* l+ E <h3><?php the_title(); ?></h3>
0 o0 ?# w0 f' _6 Y <div class="notice-content"><?php the_content(); ?></div>5 R8 k' y w; U- M
</div>3 u0 }0 B, k+ m1 S
<?php endwhile;
; s0 K; b( u$ }9 i wp_reset_postdata();7 K# z5 @" G( w" B6 P c7 a
endif;
3 n& j5 E, N& w3 y ```
3 U% m* ?+ _2 z: t$ |9 ~: J) x! B6 a7 \* V$ ~' e1 W) ]7 Y9 p/ m
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|