|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
" B$ M* `) m! q1 ]/ {7 a5 G' Q) Y. \
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
- v$ l [) ~ i6 g8 A4 p3 ]+ {8 h$ V( d
以下是创建自定义插件的步骤:
8 v+ p$ o9 E! ]
) |% F3 j9 t& N, s) K. z7 o1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:& d: r# r# I( n, h
% H) T/ _) ^. E9 h6 }) D ```
3 e8 I- q3 z/ R7 y0 F <?php0 F' P" k x/ F6 B+ R
/*
! Y1 y& f5 N& w7 a$ ~: I5 o Plugin Name: Site Wide Notices Plugin" p- M5 p; l' \: K
Description: Adds a new custom post type for site-wide notices.: o( z$ W2 J7 x, z" V
Version: 1.06 B+ @8 l4 D; ~
Author: Your Name
, |! ?8 q1 X" K Author URI: http://example.com2 K' t* x6 [2 j# u8 t: z
*/
6 p @. ] U. j+ h; L- T t$ e+ }7 Z2 \6 J8 Y
// Add plugin code here...
+ x8 W" L' _* q& @4 W ```
' M* k. @) b6 W; r i8 v" z% a) q
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
: N# k- b6 z, |5 [" R2 n: r! }
/ u% y8 ~) g% ]8 |3 X2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
' b0 ?" U8 c: B, V1 n" _$ m# p3 ? y- Y9 v c. R
```& s5 S6 o' V* O' x
add_action('init', 'create_custom_post_type');
% N5 n* ?& H% d3 C7 Q function create_custom_post_type() {; o2 |) K) {3 ]; Z) X0 W
$labels = array(
: s' D+ ^' ], E' { 'name' => 'Site Wide Notices',) D: ^/ w$ N) g3 N7 F, m
'singular_name' => 'Site Wide Notice',
) F1 m- M8 s8 U0 z1 L" J 'add_new' => 'Add New',
6 V2 R; S+ K/ v, i0 K9 _ 'add_new_item' => 'Add New Site Wide Notice',
: w# M/ `% V1 E2 U* u' h 'edit_item' => 'Edit Site Wide Notice',* R8 Y# j& q3 W7 C" C5 z9 D9 N5 @
'new_item' => 'New Site Wide Notice',
' g, i1 I) N8 U5 O) p% e6 m 'view_item' => 'View Site Wide Notice',) ]$ w0 V- P5 h; A% |( P
'search_items' => 'Search Site Wide Notices',
f. z* Y4 H: S; r: ` 'not_found' => 'No site-wide notices found'," t* a+ r/ _$ Y% l- p
'not_found_in_trash' => 'No site-wide notices found in trash'
$ E* i0 v: v) U8 h );
5 [: j% t; W+ A. }3 I, O7 L$ l7 f
$args = array(3 i* E; R% ~' D7 |
'labels' => $labels,( H! U4 ~* J( r/ v4 ^
'public' => true,
1 `8 z! u# r5 Z8 m3 j2 Y# v 'has_archive' => true,; S+ u6 S# h% r( D5 }
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
# X5 N O2 _+ x7 _# F8 _. L- x, X) o7 ? 'taxonomies' => array('category', 'post_tag'),; H9 l( t9 K. z
'menu_icon' => 'dashicons-megaphone',( p' Z+ ~$ o* ? D6 I6 v
'menu_position' => 5,1 c- Q' K/ _! [5 g* l( V: @
'rewrite' => array('slug' => 'site-wide-notices')' x) F0 l4 N, s" S
);' J% s/ s% L; J$ d3 U5 h' ?
. `) g. O% o4 S7 [; p
register_post_type('site-wide-notices', $args);
, n( c3 ?+ w$ p }8 ~7 l0 j8 f, c
```" N$ A+ m: O% u
" u8 ~0 y% n; z. @$ w& O
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
! O; l2 w- |0 R) f
3 F; h3 K/ e( H* q+ `3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
: U! g7 `1 @. Z4 ~
1 ?- b* J% P) @+ i2 E, q ```# U: Z/ G, A2 }6 R: I. p# p8 B
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');7 J7 C3 ?7 G# I% u. q
function add_site_wide_notices_boxes() {3 H& L7 }& T @4 t4 i
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
! G- e/ F6 d% H! c0 r7 H }
2 [$ y/ u% X0 s) f/ M4 I8 z' G1 k' L% m3 x
function notice_details_meta_box($post) {
) ~. j: z4 t$ j wp_nonce_field(basename(__FILE__), 'notices_nonce');
. O1 ]5 K* A* {& p" |4 M2 [' c3 M8 Z $notice_title = get_post_meta($post->ID, 'notice_title', true);) u1 Q, n- d% `5 q
$notice_content = get_post_meta($post->ID, 'notice_content', true);
- w3 s* X; e% \+ ?; i ?>" g2 {5 T3 G4 b' a/ W, o- l
<p>
! r0 f1 K! m$ k- C/ H7 Z. _4 @ <label for="notice-title">Notice Title</label><br>( c% R& C# `; k! _" t
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
2 Z, k* ~7 o0 D. X% s+ E* T% b </p>
3 B; g3 R0 X' J% } <p>
" C& l9 | Y! h <label for="notice-content">Notice Content</label><br>& n T9 e* C+ b2 m& o! X* {
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
4 w. S, n& Y2 a9 {0 }3 a </p>5 q' Z1 {% t* ?$ g/ n
<?php. F6 z% j. f j5 V: c
}
" j% m" H2 \" Z0 x
) w' L) J. e6 F7 v* U- r! S add_action('save_post', 'save_site_wide_notice_meta_box');
, [. Q7 ?2 t4 w2 S1 \3 f function save_site_wide_notice_meta_box($post_id) {
) u1 p' _9 w% M4 W2 o. v. s if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))$ M5 g- s8 N# k7 a! f* N5 r5 p D
return;
& {0 c% A) t9 K6 b* v) k if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE); O ~1 F, z- w9 R! |. q
return;
) H$ Y) p) |& o# }% ?/ W
% h# A0 f2 E' f6 | if (isset($_POST['notice_title'])) {
8 i' N s9 V1 V# Q* |) y7 r' ]! | update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));8 i8 @6 o6 f! [, v- r6 J* Z
}
) X+ c) S) H, i7 B3 d; _" s if (isset($_POST['notice_content'])) {
, g- e$ }# u+ N& K update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));0 O; x( \. P1 y+ {* I' n7 f; W
}
2 @5 Y, i" z K+ b }
4 O4 p! }- t ^+ T4 z4 ^9 A ```
- Y& v( F, K2 p+ a1 Y/ I2 `6 k# @ {: L8 o, b- S s6 ]3 e
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
' k+ @4 e9 U' S% k# E2 r+ @1 d
& v2 ~5 p9 |! O7 |' e j% g: M" T2 K2 |4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:# O; ^& ^4 d7 E
7 ^& h/ o& Y% U# s- { ```
0 P0 Z1 ~7 r) R3 c; h $args = array(
7 u7 {( F p4 i2 ^0 W 'post_type' => 'site-wide-notices',
0 M$ _8 h2 g2 r! ? 'posts_per_page' => 3,
7 F; R( J: r$ P% p7 `6 q5 D 'order' => 'DESC',
2 V& \/ X7 k" V3 e/ N 'orderby' => 'date'
1 t6 V* M2 D( Z );
, l$ L! N: S/ O1 R& I/ I $query = new WP_Query($args);% h: ~& `- [$ h3 X4 A
if ($query->have_posts()) :7 V9 d* a, Q2 v9 E. o. u- i
while ($query->have_posts()) : $query->the_post(); ?>2 K |9 x$ t5 k( ]( R3 ^8 ?, n( n
<div class="notice">; W# O7 X/ Z* p- S. K( Q$ S
<h3><?php the_title(); ?></h3>
" v5 {/ Z! Y d7 y& E* k" p <div class="notice-content"><?php the_content(); ?></div>
8 T. |; x* G. O& w: b7 ` </div>0 w# }6 i- }5 E8 ~) m
<?php endwhile;
9 K. @4 v$ ?# C! R wp_reset_postdata();3 g/ g4 p" g6 z& d* |
endif;4 i, p* f8 \+ B w7 i7 l! g
```
1 R( x _& Z- J5 a+ m% l+ \2 z0 ~! P2 O6 S" ~. E/ W' V" \& M" W
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|