|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?$ U2 r: k8 N( \7 _. Q, W
. |9 z" n" f: B8 M- @2 J如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。; i/ l5 J ~5 x, l6 e/ W& i$ T5 L
; E2 W, B o9 z
以下是创建自定义插件的步骤:
5 ~0 h; N, y0 F: q3 q" ~1 J6 D: z8 Y. p0 ]
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
$ w3 j* a' }7 |4 t' e
$ ?3 P# g* x# ?+ o% t% I ```
5 i3 P: ?- { y; Z$ Z# l/ j( ]) W; B <?php
( T4 D; d0 X5 E6 @ /*4 ?, s2 W. g( X1 \' H5 F
Plugin Name: Site Wide Notices Plugin/ f8 H; n4 r' `' N0 @
Description: Adds a new custom post type for site-wide notices.
! i' V$ T& k1 M" T9 _ Version: 1.0; z, f! X; b9 [/ y$ A* ^
Author: Your Name
5 D H2 _2 S) ?, K Author URI: http://example.com
* C s" [( R4 d1 G! l; V* C1 E */; H7 Z$ h8 N; D2 q+ b U
$ O6 y4 ~; \3 E2 [
// Add plugin code here...
; V, m/ R' W, G4 l, t ```# u/ r6 @ b8 L
* `5 H8 B; m, S7 g. [
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。- h4 k F" X" P7 C
( X! q* T- E& e
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
5 y, t: [6 _4 A$ A/ x
( _& t6 E w4 s/ `% ^; r2 F ```
) I/ v! w& x# r: t0 _. K- t2 r add_action('init', 'create_custom_post_type');$ L+ R+ D* T+ ]% q% K- O9 }; O8 T
function create_custom_post_type() {# q' |3 ^2 ~% \2 g6 a* C( B4 ~
$labels = array(
5 ?- e u. C2 Z6 u5 @" X 'name' => 'Site Wide Notices',
* W9 F- w3 O6 q: h7 l& X 'singular_name' => 'Site Wide Notice',* ^4 u5 |7 ], G8 s/ B
'add_new' => 'Add New',: V/ v" F0 {1 h! z3 d, F# I6 d5 g
'add_new_item' => 'Add New Site Wide Notice',
! \% a3 _* u$ f# }2 V! {3 M2 S3 E 'edit_item' => 'Edit Site Wide Notice',
8 Q9 u( z- P- X. Q8 _% z# n 'new_item' => 'New Site Wide Notice',
% b. [6 N9 j, I0 y8 @4 f8 F5 i 'view_item' => 'View Site Wide Notice',
! p* ^0 G& D6 q- o% h 'search_items' => 'Search Site Wide Notices',( c3 c3 {: s0 D2 {) A3 p1 a$ D! m
'not_found' => 'No site-wide notices found',' \8 m* R) v5 L; Y4 y
'not_found_in_trash' => 'No site-wide notices found in trash'% k+ j: p: \- d
);6 c$ \8 K B) K+ ~, o
, J& n u2 Y1 g) y; _
$args = array(* _4 z% Y d7 S$ X4 \! u
'labels' => $labels,
) {/ s8 _1 G J; g) p! @, d- X 'public' => true,
) M# ~- h( o5 i8 }1 k0 C 'has_archive' => true,& Z" j/ @- N% L
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),# s7 E0 X' l S. O7 o+ H
'taxonomies' => array('category', 'post_tag'),9 w. ?2 c5 Q$ [: h" G% Q& [
'menu_icon' => 'dashicons-megaphone',
\- f2 e3 V, x 'menu_position' => 5,
& t! m# c# \4 p# ?1 k, y4 K X) z 'rewrite' => array('slug' => 'site-wide-notices')3 V7 Z% H K( E: E" @( a
);* O5 M. k6 U4 R& R
+ x& S, K) U' ]0 ] register_post_type('site-wide-notices', $args);
! A7 k0 N: D* M8 n9 } }
* n; m1 H6 P6 l' u; ^ ```, h- l% k# N1 D) W7 @4 A% o3 t
5 a, T/ M% Y, ^+ M 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。: \( ]! P* t8 V* W& U0 F4 w( Q
0 O6 e4 d/ o8 P0 ?7 M6 S3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
5 c& z3 a( V; r9 H( q7 L) E1 F- L/ H4 g* |% F" M. [/ `
```, ~$ A' Y3 P. m. s8 C
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');8 k. ?. O# ?& \3 G
function add_site_wide_notices_boxes() {; {1 u$ U- a, q; A
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');/ q' o! G3 J% s9 y5 ^8 x
}
4 Q7 x6 T% n% t' n8 [: S7 Q. k, {% f6 U& ~, e
function notice_details_meta_box($post) {
2 w# e( { R$ q Y. u" I* B7 ] wp_nonce_field(basename(__FILE__), 'notices_nonce');
9 s, T. x5 \ k3 A4 x, [$ f $notice_title = get_post_meta($post->ID, 'notice_title', true);6 u j% p4 B2 _& J
$notice_content = get_post_meta($post->ID, 'notice_content', true); I/ m. t: ~" ^* }8 }$ R1 T
?>5 z+ w9 u% M* S; N
<p>
5 x6 z! a& P7 ]9 v2 ~) { <label for="notice-title">Notice Title</label><br>& ^$ k. z8 Q/ Q; @! H
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">0 q5 q. U* A0 `) A# b3 x
</p>$ N! y: K2 p0 U0 W: q! ^$ g
<p>
5 d4 G* ], F8 {+ S& z6 q <label for="notice-content">Notice Content</label><br>3 _' Y) H! T; q( j7 x
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>) ~& _, g( S3 A' E
</p>$ ]' d5 T3 s+ m6 a
<?php+ o4 M" i, T h& B" o
}, V3 M. W9 f( g- @+ a' p- }1 j4 W
2 F U- ?: m1 z# \2 \: L add_action('save_post', 'save_site_wide_notice_meta_box');" N- P8 N! ]0 X: w6 R* [
function save_site_wide_notice_meta_box($post_id) {
# ^! ?! L, X* |1 E: }" l if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
: ?" l7 n9 }8 |; l" I return;
% s4 Y9 R5 |5 \4 k$ U6 @2 a if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
\+ b# R1 G# `- { return;. {: o# E- C: M% F3 ^0 A1 P
6 @+ @6 K4 g' @$ s5 Y$ l: R! G9 K
if (isset($_POST['notice_title'])) {
' U6 ^& F! @ Y5 g% E update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
9 d" J3 g# e( Y. b& b/ o. z+ Y* Y0 B }2 ]) R! [: j; q! r) K
if (isset($_POST['notice_content'])) {' C, Q; ?, p7 B) B
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));' Z: ]6 s: ~0 o( Q
}2 Q: A, O" _1 s
}. M0 {; Z+ S/ u. V3 r: f0 I* T
```( s4 W0 A9 ]3 Z! ~
) L' Q h0 c' b3 Q6 `
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。8 B z4 O8 t4 O3 N K8 C' H. m
6 S2 d4 V! B" M! S4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
, y# u( c0 J$ O+ d N/ ?2 H" l- t& v* f! }
```! M. N) T9 L7 ]5 n4 e6 w9 j1 W
$args = array(
0 M& L6 ] V, r6 ~! m 'post_type' => 'site-wide-notices',; H* [: J" ^( S( G+ g6 \% D
'posts_per_page' => 3,
" i& V# s* K+ [. r# `5 a# u 'order' => 'DESC',
9 T# G$ C7 W, P- `2 J/ ~ 'orderby' => 'date'
' D$ {: N1 [$ v );
! g+ L7 q; R% z8 Y8 B8 y $query = new WP_Query($args);% T2 Q( u5 s9 T, M
if ($query->have_posts()) :
9 h1 J# d2 t- H( C0 y while ($query->have_posts()) : $query->the_post(); ?>
; x1 C; }2 D! Q% W <div class="notice">6 s1 D# O. O5 u) ?
<h3><?php the_title(); ?></h3>
/ R( e! K. p+ C& B0 l <div class="notice-content"><?php the_content(); ?></div>4 q# ]% Z/ g& ~4 l4 ?! H. B0 h
</div>
7 o% \7 n" |1 }8 l7 S <?php endwhile;
" c& V/ B. i9 h' A wp_reset_postdata();
) H+ S4 \2 N1 o$ ^6 }3 W endif;
, m9 c4 O1 p8 p8 c0 e" m ```
$ f2 f4 p, u6 l3 Y
. O! I ~" A. F$ C6 O4 X% [! u, t 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|