|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
O0 D8 h) A; C8 _0 b7 W; P# J2 _7 \9 i/ l- }
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
9 Q. `3 i# Q' _" T0 b5 \
; f. P( y" @1 M以下是创建自定义插件的步骤:0 Q) F/ ]: a" _1 y+ T. V9 M" q: Q$ P6 f2 W
" ` o3 [' U. h, B+ h" g# U1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
4 F9 h: F8 y8 p% A" `$ j0 _+ ~
, U( T$ M4 j9 e, ^6 r5 N. z ```
4 W0 D0 g" d, F4 D <?php& t( i+ u% I5 i! W# S3 s0 U$ r
/*2 w2 _: \3 L; {$ x" ~/ i4 L9 A
Plugin Name: Site Wide Notices Plugin0 X8 c3 y' x! p4 W5 {/ w7 Y
Description: Adds a new custom post type for site-wide notices.; g5 o: C ?6 J0 [
Version: 1.0
7 V h7 r* B& j9 U Author: Your Name
1 p0 j2 q7 D s) I1 h( I Author URI: http://example.com3 Q+ \- P/ Y8 Y7 S
*/0 G) x @7 E3 ^: f* T* o
1 ?" L( O* l. l: }7 X3 q' t% w: m
// Add plugin code here...3 g9 F' b' U1 y1 ^$ m' ]
```$ X* p1 J! [6 T6 A
* [: c U: Y( ~
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。0 f V7 I- m6 s, N3 Q/ i" J. j
& ?, A; A) @1 A6 a
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:2 o( }2 J s" \3 B% |
- ?4 u* t, g5 V" p& |
```
, H2 i. x7 [- i( u8 @/ g add_action('init', 'create_custom_post_type');7 R, S8 r1 P8 W1 b$ x" }$ {7 l
function create_custom_post_type() {
/ p! x8 } v8 U1 q1 c $labels = array(9 Y6 t8 f+ I4 u
'name' => 'Site Wide Notices',
, Y( S, W. W4 L8 ]3 a9 Y 'singular_name' => 'Site Wide Notice',+ o' S8 m9 b/ E. h+ H7 F
'add_new' => 'Add New',, V, t( v! G5 Q7 D: c' u
'add_new_item' => 'Add New Site Wide Notice',
. I2 L+ L3 c6 f+ i$ u0 z" Q; Z 'edit_item' => 'Edit Site Wide Notice',
* O( J, {+ `/ C' l 'new_item' => 'New Site Wide Notice',+ W( q$ |( B3 [$ X6 U! |
'view_item' => 'View Site Wide Notice',& U* z" r/ Z* r$ U% U3 y$ l
'search_items' => 'Search Site Wide Notices', [$ q, L i0 I0 Z, T
'not_found' => 'No site-wide notices found',
; `& E2 k5 J- ^' f 'not_found_in_trash' => 'No site-wide notices found in trash'
1 G" D& G& [8 i6 @2 p );
# G+ }$ [! {2 m2 y& q! `* j# A0 j% v" x4 W7 S. n6 k0 o* n
$args = array(
* u% l" Q! z$ n1 p, z+ ?2 i, R1 @ 'labels' => $labels,9 e. l1 p7 C* B) v3 K5 f
'public' => true,
; [. V! N8 v/ v* l4 E' m1 e* G3 f 'has_archive' => true,1 z; E6 }' f* w, [: J+ z7 D
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
+ Q* ^; j, y; M 'taxonomies' => array('category', 'post_tag'),. b" }! k' J4 U) h
'menu_icon' => 'dashicons-megaphone',6 ?5 X4 g" |+ g, n
'menu_position' => 5,( F* H n1 o |
'rewrite' => array('slug' => 'site-wide-notices')
& A. p. s% F4 i+ \ );; k* g0 c& O/ K( S8 x3 L% o$ G ]
0 |! m1 D e& f! p8 l' b4 M1 T register_post_type('site-wide-notices', $args);8 x: c3 y# [2 {- W( Q
}. r8 c c* N+ l3 M6 k4 L9 f6 U8 ]
```
; \! U9 M5 M: p0 J1 }/ _/ J1 X0 z/ P6 U) V7 B
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
' J( e# Q0 _( z$ P: H: `7 ?8 V! c) t( V; P$ U# P9 v! c9 n9 z
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如: M& V7 ?3 W, N2 |- ]
" R K2 R& j/ u# }! r) o6 u
```
4 o& u! O2 Z* F add_action('add_meta_boxes', 'add_site_wide_notices_boxes');* ]5 p, e7 q# R! S2 w5 k
function add_site_wide_notices_boxes() {
# c+ H/ b9 j: v add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
- e# k* [5 ~+ |" E }
8 d7 l) |" N' k& T$ f" A4 K# |" G0 u* D) {
function notice_details_meta_box($post) {3 j* U0 H v* W- a& @- a; o
wp_nonce_field(basename(__FILE__), 'notices_nonce');
' u$ q: n/ A# t' ^ $notice_title = get_post_meta($post->ID, 'notice_title', true);! x' l; \" P7 F& m% B2 ^7 _8 }
$notice_content = get_post_meta($post->ID, 'notice_content', true); u+ ]" S" X5 w% \4 l9 J
?>( b# X' q2 E* u( w2 p! t
<p>8 ]" i# \5 N E' Y( o ]9 g) R5 p) Q
<label for="notice-title">Notice Title</label><br>
! _1 r: }8 K& I0 d <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>"> v2 F- b. k3 w; A, E, H) i3 Y
</p>
& f2 i8 z$ B4 h6 q# u! W! A <p>
0 K$ z8 Z K4 o! o# { <label for="notice-content">Notice Content</label><br>
/ E4 C; [# v3 c- B+ U2 W- h <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>( v0 B- D4 {* \! M
</p>& H& k, e) ]( j3 i$ Z0 S2 q
<?php i, l6 V7 |$ q6 c
}) p2 Z8 A* @% o
' M& G5 r' K+ l t" m3 a, v
add_action('save_post', 'save_site_wide_notice_meta_box');
4 t7 B" z1 e0 Q$ Z3 n: }# H function save_site_wide_notice_meta_box($post_id) {
# Q7 h7 ?9 L( |- A) Z: R- a if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))6 k3 ^* o, \$ v4 j. Y) T9 X# N
return;" I& H3 `! j7 l# g0 y! e8 D
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
& c& J q6 t( Q8 H) t5 t; q2 t6 L return;. Y0 F6 S" b( f& M
% f* }- R# t K% j% P$ ?
if (isset($_POST['notice_title'])) {
: Z$ `; {- X+ b# X/ @0 e update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));$ t K3 @4 F" u. P L c/ x3 {
}* j! ]" ]" ?& y3 I! `
if (isset($_POST['notice_content'])) {
# z9 S( S: E. f; ~+ ~7 o update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
. {) x0 t5 }! i. o9 F3 | }) k+ j) b2 ]7 r/ Y
}8 Q( S+ ?: M9 w6 Q2 r
```/ j( L- ]% y" ^8 @
- g$ c7 I) H6 w
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。3 w: G- K+ U3 q6 O3 Z# R
) f4 G" r" T, E9 A* V4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:8 r$ f$ H/ g2 A' b
/ K; Q; ^/ o, ?$ R! q
```7 `4 K0 ?& H' Y' L7 w' f/ |
$args = array(3 B2 A+ `; {$ l$ d
'post_type' => 'site-wide-notices',$ @( I& E o6 J m3 `) n- A
'posts_per_page' => 3,1 Y; N! A. V/ ~7 ]4 Q; T
'order' => 'DESC',
6 c& m* l* d9 H- m: w 'orderby' => 'date' S! H' d0 J( p. l7 l/ i$ e
);( T4 n/ P% u" t/ D
$query = new WP_Query($args);# _$ P- P: `9 A1 s# d
if ($query->have_posts()) :0 W( ?1 K: E, N
while ($query->have_posts()) : $query->the_post(); ?>3 {* P/ d4 W7 i. K
<div class="notice">
' T( {$ f+ }' l <h3><?php the_title(); ?></h3># V& q# [+ N8 K2 G, q S
<div class="notice-content"><?php the_content(); ?></div>: B& C" A7 Y% m. }. U
</div>7 |1 q2 G. W7 B3 Z. @ M* R" o
<?php endwhile;
" s# y) C+ {4 [. l wp_reset_postdata();. T" a3 J' `* }7 j9 v: i* ^/ c
endif;
) b/ D+ o- ?; _# S1 ~ ```
3 C/ m9 g* t% t$ X t5 q% e1 z" j1 ]6 Y9 c" g
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|