|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
4 v6 j d% N6 q' p/ ^" w( v3 s- o9 z, ]) I+ c" E" D+ f
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
- @/ ]7 X+ Q* C& O/ t" r1 d( z
, p; b1 b2 p$ u9 ]$ O以下是创建自定义插件的步骤:; N7 _5 a4 z) b3 c' i) t1 K' ~
/ ^( f1 k+ s! y9 X
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
* _" Y) A' p. y w7 V
! G& ]7 _: G2 Y) G; I ```
2 Z: O4 S+ P6 I9 ?, J <?php
( g3 w3 L- o1 _/ Z" K1 ~ /*
& }7 L" P4 ~, v8 ~9 [ Plugin Name: Site Wide Notices Plugin0 u, g8 r& x4 [/ s. @
Description: Adds a new custom post type for site-wide notices.6 M% Q+ o1 }/ W9 ?$ V
Version: 1.0
4 z( p8 H. [0 y6 I% E# o Author: Your Name
) W* ?; C$ m4 K2 b5 x9 e# g Author URI: http://example.com i: i5 ?1 ~2 w* V3 n* W5 T
*/
1 F6 z- I+ C6 N+ b* d2 m/ _7 b7 x- s# O3 e7 Y0 a
// Add plugin code here...
0 u$ ]9 p5 ^! Z6 |1 W" W" @ ```0 r9 V' v8 @4 }0 i
$ \7 v* `, l* _3 t) u$ `1 P 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。2 a5 Z( l0 i# E" q8 I: O
. v2 ^! r, _, P; h9 L! ?
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:$ s$ \5 s8 ~+ H
+ u) q* f8 D+ _, P
```/ e: y$ g6 }4 Y! u: ^0 q" V! }
add_action('init', 'create_custom_post_type');
5 k E0 U" b0 v% F& D function create_custom_post_type() {5 f5 K7 ?1 ]- `2 l2 j. ]
$labels = array(
) x0 A( B3 Y- K- E: j# e) y 'name' => 'Site Wide Notices',* e K# \' r! s; [
'singular_name' => 'Site Wide Notice',. _ ?3 c2 W! Y1 L% b) y
'add_new' => 'Add New',
7 J8 O1 C, F: A7 l 'add_new_item' => 'Add New Site Wide Notice',
8 f: f. G! E# U4 C 'edit_item' => 'Edit Site Wide Notice',, B) j7 w# b7 O* T3 e
'new_item' => 'New Site Wide Notice',9 c e( R1 o" t6 _* M7 k' C. Q
'view_item' => 'View Site Wide Notice',
: i7 O/ R0 i+ }' O' S( F& e D- G 'search_items' => 'Search Site Wide Notices',: T5 K% r0 [" r
'not_found' => 'No site-wide notices found',5 U2 o1 O& |; I! S9 C! U# N0 o
'not_found_in_trash' => 'No site-wide notices found in trash'
4 b- e! f/ ~; E1 Z, F& t' g );
2 G4 k5 F# o# v! z# U. h+ A; D- J: ]4 t9 a9 J6 s
$args = array(
, W# c. T, Y2 p7 h! A5 v 'labels' => $labels,
( t9 u6 l* y, u" E 'public' => true,
( q3 [- X8 Q4 q$ N$ t 'has_archive' => true,
1 c: a) T" j$ c 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
& m& ~5 C- {* G* e; s 'taxonomies' => array('category', 'post_tag'),' ^) n- Y) y( o3 } P; b
'menu_icon' => 'dashicons-megaphone',
3 |( X9 W5 K! Z( t% u6 }* U 'menu_position' => 5,0 u* f: H; H6 v6 d
'rewrite' => array('slug' => 'site-wide-notices')
( a, u" S% E: } );& @& w$ r/ y% H. {& F3 c" r
' D* {3 x: z/ q! P
register_post_type('site-wide-notices', $args);
( n* s- N" o" ` }: ?+ C) l% W( ]3 b1 }
```
& i( B# ~! I) w6 r, k4 D
9 H+ E# Q, x" p ^ 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
& `) w/ G" G0 j: F* s, g* I# Q
% u& Y- u0 \2 G$ Z$ ?& @- ]2 @# ]2 \3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
b5 J: n$ U/ A4 Y) u2 ~1 d5 i' \8 d. o7 [5 u/ H
```6 W0 }2 ]* ^) S. ?' g
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');$ }, E: r0 t( a+ u7 `/ j, b
function add_site_wide_notices_boxes() {
) f9 Z# }6 x) m8 t; D T add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
$ H6 n* c! e c }- q1 e S' l1 y5 T3 [
5 I V/ W8 m3 N; P& o4 Z- F
function notice_details_meta_box($post) {+ ^ f9 p Q) O$ ^
wp_nonce_field(basename(__FILE__), 'notices_nonce');5 s, f; `8 Y# N$ D6 M4 a
$notice_title = get_post_meta($post->ID, 'notice_title', true);
( l9 k( ^, m: h9 a6 S) K' i% O8 C $notice_content = get_post_meta($post->ID, 'notice_content', true);: Q8 _2 |; G' p6 d8 h; b
?>
" C$ F+ m* i" M4 P& i6 H; @" V <p>' b4 q; F, l7 F# D
<label for="notice-title">Notice Title</label><br>
# s2 T$ H! ]% k; d9 J0 R <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">3 Y- c4 c+ U: ^- E" L
</p>
4 U+ W9 G9 z* z/ n6 }$ I <p>% Y7 Z) q- Y+ S6 m# g: C
<label for="notice-content">Notice Content</label><br>
5 X+ }1 \1 R7 L <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
0 G: P. q' h0 V' e# ?; D9 Q/ Y- O* Y" K </p>
4 |; e1 U8 B5 R6 Z <?php
; u' J' R, v) p/ x: F$ f, T6 z4 m }
' G( ]) N# v* u2 {1 }* h+ o
2 m s" K! V( n! Z' o6 e/ n5 v6 R$ I$ G add_action('save_post', 'save_site_wide_notice_meta_box');
* ^* ?1 l/ c% C9 P6 L3 f function save_site_wide_notice_meta_box($post_id) {
3 `% q& {) |, I% @0 g$ q4 w if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
0 U$ y j- m- v6 K return;, {. g& [8 v0 g% C) k9 D
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)3 E; n! h; j9 S
return;
3 K2 i6 t" _, T6 l3 D! c& a
: m" G/ R; h: [) E/ [ if (isset($_POST['notice_title'])) {* T* D5 M3 x" j
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));4 O+ h3 X5 K. i. S( T2 ?' ^
}
' V6 [4 r7 [; E if (isset($_POST['notice_content'])) {
0 t( @) Z" W) y W9 l; x* v update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));: I0 D# b7 {4 r
}: v! Z1 |# Q" X3 H2 ^) r" H
}
+ ^) o( \3 [5 G+ ]" W0 ` r ```
5 u" X9 j/ T n2 \8 S; ]+ C5 P7 M6 R& F$ r0 Q! R; ~! o
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
* L n, H: w5 a: @3 ]) X& ~7 B+ C$ ~4 p
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
! j4 _' N( u [" W7 R0 Z$ d& t
7 b! [0 Q9 L8 O# ?+ N( P ```$ z+ U I; L( }& ?
$args = array(+ J4 V0 x0 L* H8 W3 |
'post_type' => 'site-wide-notices',7 [2 R+ @! J+ z% s+ b3 N
'posts_per_page' => 3,& i6 o- `) x- z/ p* v9 A6 i
'order' => 'DESC',
( u7 a0 h% C7 h, o. L& c, k2 D& u 'orderby' => 'date') Z4 j" [! P- A3 b1 X: d# @
);& a' i! e/ V' \# T' @$ A
$query = new WP_Query($args);
f4 S1 |$ W. N- | if ($query->have_posts()) :
$ Y+ E r3 G0 `, N7 e6 I while ($query->have_posts()) : $query->the_post(); ?>, Q9 l9 y6 L3 ~0 ?/ @. t
<div class="notice">
3 @. N! P" h2 G: }/ j0 f <h3><?php the_title(); ?></h3>! L/ C' x7 q( V8 h
<div class="notice-content"><?php the_content(); ?></div>* [/ u, {, D+ Y: b* _% ?
</div>
' s7 B4 Q8 F3 C5 k, @! m <?php endwhile;
4 ?; Z# `& g! d' I8 Z0 D wp_reset_postdata();
# @( a5 f* o+ I9 w# t endif;
6 O" s+ ]; O, b ```
) i; i3 O, |" V1 `# @7 ^' t- ~- i- j8 W0 @4 u/ z
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|