|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
4 k$ K8 W! g& ^- X2 e( }* h1 T; h l: t+ @
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。8 o: m2 A4 H0 H
3 i) E6 i' R) J2 A8 [- h3 F( k以下是创建自定义插件的步骤:/ e" {% ?+ K5 G* _! I6 J
% ~. {* ~& Y4 X+ h/ v
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:1 f7 o5 @4 J% H7 i
8 I- w+ `% A' {. q ```4 h4 ^7 G. o$ A& _6 C( Y
<?php, M1 Z8 B3 L, M. w" m+ j
/*9 i; m3 {' F- n2 p1 s) ?8 R
Plugin Name: Site Wide Notices Plugin' B/ _# T7 i9 |- g
Description: Adds a new custom post type for site-wide notices.
4 }* k" {! U3 R: Y Version: 1.05 I6 ]# L0 U# Z) I x
Author: Your Name- w, _' q7 J3 E& `
Author URI: http://example.com4 ?) o$ b1 M: c. K6 s; j' H# z
*/( c$ \. M" a; l$ p2 v' o
( J! G& H9 Q; ~! W // Add plugin code here...) [, z% @3 W/ ?- f
```) @8 J& l$ R/ T+ f1 e, {( Q2 T$ F
2 o, k7 c# A& @; \9 P
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
, y( {* m5 G/ y/ Z2 ~# C! R% [3 E6 s- |: l) s. S# q3 y O
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
$ @; k0 T8 s1 a; z$ M% k) U7 J8 F7 t, b6 ^ P# b
```
; h4 L9 j$ o. w- @3 T add_action('init', 'create_custom_post_type');( [7 i A3 A# s7 ^
function create_custom_post_type() {7 Q# L% E. f5 Z4 \8 }
$labels = array(- M- |& I J! m5 [8 k
'name' => 'Site Wide Notices',- i0 V5 }+ h6 _' }0 I
'singular_name' => 'Site Wide Notice',
h& z1 M' y- h 'add_new' => 'Add New',
5 q# Q3 o4 _" C* f 'add_new_item' => 'Add New Site Wide Notice',. M3 V9 S/ p0 F; s* e
'edit_item' => 'Edit Site Wide Notice',( s) f3 r( V" b0 Z7 d7 R
'new_item' => 'New Site Wide Notice',! e- K7 J2 s+ T
'view_item' => 'View Site Wide Notice',
# m- q# X# T0 H 'search_items' => 'Search Site Wide Notices',
1 V! j0 g" I' w2 w 'not_found' => 'No site-wide notices found',% D& c9 O& F5 m' w/ q/ [. c4 |
'not_found_in_trash' => 'No site-wide notices found in trash'
8 C6 n" R0 J1 p8 {( O' m+ b );
2 ^' v, }: I2 C h; t' j/ H, z% q' J7 s Y. t
$args = array(% n8 ^6 ]9 G% h* T
'labels' => $labels,
2 C2 d* h) Y! U$ N7 {( `* a 'public' => true,
# ?. |" v1 F0 I0 V: N 'has_archive' => true,
/ e; o& X7 s9 }' \, K% O 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
, r" r. X9 |4 ^6 \) b 'taxonomies' => array('category', 'post_tag'),
+ o+ f5 a. M( e7 \8 c 'menu_icon' => 'dashicons-megaphone',
6 p7 Z8 A6 i8 m' ]5 h- B% ~ a- R 'menu_position' => 5,+ @7 `' c4 w3 j' _
'rewrite' => array('slug' => 'site-wide-notices')
' \1 L" Q6 U) Q# k; W& G );
" m! {2 r# I* W+ ]( ~$ u5 `" @2 ^7 c+ h: {9 l6 P0 q; I
register_post_type('site-wide-notices', $args);) W8 f. N# P/ J
}
4 ]7 Q4 ]2 |. ]: w" W ```8 A" Y% W0 ~( l
( S8 o3 {7 U) c7 ] 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
8 v5 i* F- I" K) [, L
& z4 ^' X; m: M# F9 H) H3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
- x1 L' W! d2 s5 j: y% J- G. T. i: f3 }2 L
```
- t# {8 M* y' e0 e5 A8 R add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
" d7 B$ v0 A1 D* @. r( m function add_site_wide_notices_boxes() {' [5 a& r j7 F9 }1 q
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');$ ^ `, X4 O$ _5 [% Y$ v
}! x5 Q: _" u" Y% |3 w7 o8 @# E, v2 z
: b* A# M @5 z" a* D7 n7 b M
function notice_details_meta_box($post) {
5 J; g9 M1 r; U+ ? wp_nonce_field(basename(__FILE__), 'notices_nonce');; Z2 ?9 N- e- ]$ B! [5 i0 g$ Z/ v* [
$notice_title = get_post_meta($post->ID, 'notice_title', true);
6 e' y8 H9 x3 _ D, x# `2 z' U! F $notice_content = get_post_meta($post->ID, 'notice_content', true);
9 n F. E) ?! g# m" n ?>
2 z; m$ i; f/ d, Z: c <p>
, d8 r! q+ r' z# m) e1 p+ { <label for="notice-title">Notice Title</label><br>
: T" i0 U) ^1 T, q8 R) p <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
) x/ a5 D! ]1 {& `0 {# ~ </p>
# r% u# ^/ c! o" o9 k" m <p>
6 R. C; T! i7 ~! Z' {/ m+ ] <label for="notice-content">Notice Content</label><br>, Y, f4 y+ _* n* K' X! L
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
, F' w8 S; v0 Y </p>
' L! i( y4 r' E5 U" Z, W <?php
4 F3 Y- f4 |7 x/ R( E8 A6 H }
+ k; j+ o n0 d' `5 T* p& e4 O) s
add_action('save_post', 'save_site_wide_notice_meta_box');/ o& Y" H; @9 g4 N/ g4 Z
function save_site_wide_notice_meta_box($post_id) {, T$ j9 p) G! n( P8 k
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))): m# e! S6 r7 d2 }$ U
return;
- W9 V, O& r/ H( B% \8 ^ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
7 c3 B" F7 x$ L5 E( s2 X return;
+ z; u) Y& V9 R4 K, U# K; T; J
if (isset($_POST['notice_title'])) {
. N; J( n* h; P' m) B2 [& G5 D update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));5 Q, q4 r" M4 }1 O
}
; n0 l3 q1 T; d4 v if (isset($_POST['notice_content'])) {
4 b5 U: g3 P8 q update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
" z* A' M' @8 W, a4 R' v4 H) m }
; T! q6 Y6 ]4 u2 |4 Z/ | }
3 W6 V3 x8 V% t& { ```
, N$ S a: W2 _% v% N7 w; X2 g6 C4 b! J& n7 J8 h% N
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。- m0 g* X1 y. S" v) d4 f
8 T' t/ ~& T5 M/ N4 g& _$ Q. r4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
. u# O1 Q1 Y! P, a D. b! y, i
2 E9 E: @6 h4 ^8 x, H' I ```
. U$ D$ E9 i* V+ l $args = array(4 m; T9 t3 ?6 s+ o* B! M! a
'post_type' => 'site-wide-notices',
/ ?3 r( c& l9 K( ]1 h ]3 E4 z; N 'posts_per_page' => 3,, c, o8 p7 ~6 i1 a% c A; f
'order' => 'DESC',$ g8 K1 y3 L: m9 i
'orderby' => 'date'% `, ]8 q; |) n9 k* V# ^1 R
);
& Q- T6 I/ {3 w $query = new WP_Query($args);
, a- Z* d" h9 @ if ($query->have_posts()) :
: d* k% B7 j5 }% R6 I& D6 s while ($query->have_posts()) : $query->the_post(); ?>
" w( J2 @ f7 T% k! y2 | <div class="notice">
/ o8 I) y- P# `( f0 J <h3><?php the_title(); ?></h3> j t$ G4 q; G
<div class="notice-content"><?php the_content(); ?></div>* [+ }- u) { `0 d( Q1 w- ^
</div>
1 n' ^- V% A/ `; j a/ O <?php endwhile;
T+ A, P% I; \/ K5 H wp_reset_postdata();) o; R$ j6 V/ X
endif;
9 z; n" J" P( X9 n2 x$ T ```& h( k0 Q, `+ h
) I" N5 g6 g4 B7 q2 S- J1 w/ T# } 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|