|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
& E2 H3 _% G$ F
2 @6 u' X# N/ a+ a如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。8 R2 _5 V0 g, {, h! X
& O9 Z" V/ R9 t- Q5 C
以下是创建自定义插件的步骤:7 ?3 _; D* n! M1 s) ?5 L0 g& W
# W. i4 U7 S$ X3 I# t) O3 e
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
/ L- P8 s1 n# C' B. p3 B
0 Y# V* r7 X/ d3 p7 V' C3 I6 | ```6 ?) t6 S! u& u2 g, T
<?php/ Z$ |! M$ U5 j. I5 q; Q$ G* L
/* _; K8 J! v; m
Plugin Name: Site Wide Notices Plugin' e" D, {4 B0 h I. f- g1 O
Description: Adds a new custom post type for site-wide notices./ P! O6 K* @6 c. @: h' b1 k6 y
Version: 1.0; [! t* [% [5 Z0 L5 k: k& d1 Z
Author: Your Name& T# \9 h9 a+ K2 q% I$ h- ]
Author URI: http://example.com
) b# a5 }& ]3 V0 S4 t */
% F& d; b2 x9 H; M3 v% [% ?( B* O, @( C2 U
// Add plugin code here...
- L& W' Q; m5 k2 i+ {) @. J ```
8 j9 x* c+ q" C. `: n# o& a% f4 i, y( d3 s
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。. z' s( L% N. O/ F9 D8 L+ D E; ?
6 n7 [) B+ g' @8 I+ j3 G
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
) {. Y7 L% i8 Y) [+ I3 V5 L8 q+ m |
& s3 k, x, V: L1 H3 l ```
1 I/ u% v9 r' `& ]0 n0 G& p" m add_action('init', 'create_custom_post_type');; j. [; k6 u- K* {( v" [$ }( `
function create_custom_post_type() {4 v) z4 h6 Q9 o' o0 Y: Z9 Z
$labels = array( x0 [8 g! P# ^5 G8 Y. Y: ]9 g
'name' => 'Site Wide Notices',
$ E" C9 R, H& j b$ f, I/ S( q 'singular_name' => 'Site Wide Notice',; V3 Z. M6 t* f2 M4 Z3 G3 _6 R
'add_new' => 'Add New',# L9 H- i" k' b) a+ D6 Y) f
'add_new_item' => 'Add New Site Wide Notice',
3 a9 V+ P) R7 v. G& W 'edit_item' => 'Edit Site Wide Notice',$ D* H) I5 ~7 v6 t1 g
'new_item' => 'New Site Wide Notice',
( U6 o9 t1 K4 q1 i) n( {1 o 'view_item' => 'View Site Wide Notice',- g: G# ?; V- i, U8 L
'search_items' => 'Search Site Wide Notices',! @# u" b8 w) B# [* h2 ?
'not_found' => 'No site-wide notices found',8 o$ K+ P0 |) ]' E7 Y- |
'not_found_in_trash' => 'No site-wide notices found in trash'1 h: A0 D1 g3 S1 Q, N# E
);
7 ]- r/ M( Z# a/ k {8 o2 J# Y4 A: M
$args = array(
/ v* n( M( u% z! E% c 'labels' => $labels,/ _$ N" {9 J' D- J, D
'public' => true,+ x b4 Z5 \" S d" z t
'has_archive' => true,
: l$ z( R7 u& O. H 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
3 \$ E: q4 u0 P" e 'taxonomies' => array('category', 'post_tag'),
' r1 h5 {' [" \1 G; t. ]$ }# J n 'menu_icon' => 'dashicons-megaphone',
( H' L/ R% s# ~9 g& e 'menu_position' => 5,
1 I4 E9 F. U) B: q" e 'rewrite' => array('slug' => 'site-wide-notices')
9 d: W! s' f/ T V+ \0 e4 J );& f7 ?9 R8 U7 ^/ b
+ n, m" |0 {2 l/ y4 w register_post_type('site-wide-notices', $args);
! ^0 n( c: B3 K. J, k }- h; J, ^0 h! Q3 ^- l8 c
```# X8 K" o% t8 t# n* c8 i
/ c6 G3 F0 @3 u. `. q l+ v% u; F- Z 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。2 x" j: q" h3 s% ?4 [1 }8 c; L q4 r
2 H; Q6 Q9 C* [$ |% L
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
" h h; U+ {6 Y3 m0 `6 Q
! M" E1 m' B- b* d9 H5 ` ```) z/ A+ s" N" a1 ~
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
5 i% i/ c. k) {# c( i# j) T2 K function add_site_wide_notices_boxes() {
% I) V& X# k( r' f add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');- [( T+ @- J% ~6 U' v5 y. e# ^: n! d
}" ?0 _0 X7 \1 q' z5 p! ?
4 g/ n, O$ }9 T function notice_details_meta_box($post) {! u) y+ B3 I/ [$ O2 K
wp_nonce_field(basename(__FILE__), 'notices_nonce');
5 k4 x$ F- H0 p+ j8 R+ q6 ? $notice_title = get_post_meta($post->ID, 'notice_title', true);
9 N3 T' g7 s" w $notice_content = get_post_meta($post->ID, 'notice_content', true);
# s2 ]1 G; p/ ]- { ?>
4 \7 m6 E+ ~9 X( l0 e <p>' T; m0 D6 E- y& r
<label for="notice-title">Notice Title</label><br>; ~ Y; J+ N9 L6 B3 N
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">. \8 f9 r/ E" M
</p>' D5 s+ {! {( w
<p>/ W4 J# m* [: ]( x* I& j
<label for="notice-content">Notice Content</label><br>
8 _$ G8 v9 k8 Q2 z0 W <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>+ a, u% s0 O, E9 V7 W j
</p>6 h0 t$ m3 v% N. K% B. N5 ~4 o
<?php2 p* }" C! ?/ E
}( {0 C0 }; y$ N% @3 p) G: P
; [" [) e0 M! I+ n9 _/ u9 \
add_action('save_post', 'save_site_wide_notice_meta_box');- l' R0 c2 `6 Q7 u
function save_site_wide_notice_meta_box($post_id) {6 e& h/ q" r4 h" q5 k l" e
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))3 D2 [3 q' U4 {
return;5 g7 f3 R2 [6 _: J
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)! [5 m- q2 R) A$ {
return;0 }4 |; _5 R8 K* m; h
" T" ~2 N: O/ ~; l* t" b) ?
if (isset($_POST['notice_title'])) {3 h$ C1 g, D# ]8 p) j# {% S, K' ]
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));' K8 y8 O5 ~9 B; ^: l
}
! G1 p6 C* n8 X5 h2 H if (isset($_POST['notice_content'])) {( e& `! C: H$ |5 C* H
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
5 k. @$ f3 l) G }/ Q) w) L7 B8 A8 A5 Q+ _+ ^
}9 d" g8 \: b. ]( D1 F% D/ \
```
# g3 A4 U4 g3 }4 U8 V- }& ]* f9 D8 ]6 P6 J3 i2 w
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。, [, o; v% @# z2 P& K( l: B, |- _9 i; ?
K6 R* ~3 T* m
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:7 x# K0 B+ L5 X; ~( f3 o) j+ S
4 _/ J7 K2 b/ `0 _# V& |% U
```. }' b% v# t- V3 u/ Y
$args = array(
( V5 c! |8 ~. y7 B9 j4 j5 p 'post_type' => 'site-wide-notices',
- i. P" n* r# A7 l1 i 'posts_per_page' => 3,
6 ?: u$ k2 u7 Z5 _ 'order' => 'DESC',
2 ^4 x. o* J: U5 n% V, O! y# Z0 f 'orderby' => 'date'* D& _7 F" G/ M. X% Q
);
$ j5 l! ~ v8 }5 K q: b$ {$ ~ $query = new WP_Query($args);
- q+ T+ U$ ]" c' L if ($query->have_posts()) :3 R0 W$ R' m' i8 k: g& e! o
while ($query->have_posts()) : $query->the_post(); ?>1 m! {, J7 K- c9 R, b5 c4 q2 u( x
<div class="notice">
* r8 j1 Q( b) S+ |9 g0 ?6 K/ }0 ` <h3><?php the_title(); ?></h3>
# ?' }& K1 H, i' C! k2 a% x% p7 s4 y <div class="notice-content"><?php the_content(); ?></div>& W7 B9 H3 _* Y6 C/ `; s. N; f+ r% F
</div>0 o5 n; w8 S* f0 S( Y+ e' Z4 H: @
<?php endwhile;- y' `; x- j. A& o
wp_reset_postdata();
; p$ T+ j5 c- z3 _* O$ P, ^ endif;$ ~- B6 z* h# S% `" ^- y X6 d
```
' ]+ @1 t' D& N. Z% ?$ v/ D
: A$ L) e0 h7 y/ Q/ J. @* y 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|