|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
3 P! {, c q2 l8 D& ?* `* v% {$ F$ k
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
( E8 m' G& u# [. e, j, z5 T0 X/ T1 K/ m# J1 b/ V
以下是创建自定义插件的步骤:$ R5 y8 s0 B9 X+ u, Q
5 s% h/ Z/ M9 a$ k/ o; _$ J1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:( j* u/ v1 u* v4 D
8 `2 m. [ e3 E2 K
```
, k, r N. J8 M7 i3 Y8 U i* D <?php
4 R8 k$ g6 {/ N& T6 b1 u /*
: U( l( z* v# u Plugin Name: Site Wide Notices Plugin
2 h, f w1 B1 `9 K! u" }9 C Description: Adds a new custom post type for site-wide notices.
. P1 m7 {$ B$ c# R: P Version: 1.0
@) E8 F% _+ ]& V Author: Your Name/ v* A, r& {; ]# A
Author URI: http://example.com
+ j5 C; ~- K( o) C */
7 \: G: I2 H$ ^# n M. K
, l& A% k2 f( Y. g: k- Q5 H4 v // Add plugin code here...
# F9 a& S4 T" Z( I2 e! R ```
$ F! V" e8 Z0 b% v! r% Y/ F+ k5 _2 q' ~; g
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。6 ]+ E3 p5 c0 Q6 A8 j$ D* x9 U
7 l4 \ b1 I- ~- Y$ T+ O, d
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:* e. X8 B+ }8 a) q0 w. e! O3 U
5 @/ s1 p1 E' H+ w) {% l) t ```
7 L' } Q- r' V% i* ]7 \& L3 e add_action('init', 'create_custom_post_type');. G) f+ Z6 Z. ^% M( `" i
function create_custom_post_type() {
2 @7 m0 w: [2 x9 a2 l2 ~& v' M9 @) j $labels = array(
& d, Y5 n0 S, N 'name' => 'Site Wide Notices',# k5 _/ W* c0 Z
'singular_name' => 'Site Wide Notice',
, ~. e- _: T' [7 a1 k8 B5 e1 D$ G 'add_new' => 'Add New',! H( w( }% G8 H; z, q# r @
'add_new_item' => 'Add New Site Wide Notice', y) E' o" \7 M
'edit_item' => 'Edit Site Wide Notice',$ G; f0 |2 F5 u" a( b
'new_item' => 'New Site Wide Notice',; H/ H, I: g. w) C. g) r
'view_item' => 'View Site Wide Notice',
9 e P! S# x0 t0 p: p+ p 'search_items' => 'Search Site Wide Notices',% _9 {! S# ]% m) b
'not_found' => 'No site-wide notices found',8 p/ y& |1 ^; |0 i8 e+ {
'not_found_in_trash' => 'No site-wide notices found in trash'3 }4 S- }+ g8 }4 K5 F- v
);' J6 y! E$ O9 ~9 ?
) E9 P2 T4 f, y) Y- Z, z
$args = array(
( }" T* M' y6 Z, d- K 'labels' => $labels,
4 K$ _. U7 \2 Y/ h 'public' => true,+ \; i# q' k+ K6 J3 r
'has_archive' => true,
$ _; ^' s% x4 t/ w" d8 c; ` 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions')," r5 M/ z- ^: M5 p6 {
'taxonomies' => array('category', 'post_tag'),; ]" ^) R6 W0 |" L% a1 d
'menu_icon' => 'dashicons-megaphone',
# r- {" @2 N- x' ] 'menu_position' => 5,- S' S4 K a3 e }
'rewrite' => array('slug' => 'site-wide-notices')3 t! `7 ]/ W; W. v( A
);5 L) v6 O) b# T
( p: }# s' g* A7 ]1 z& y
register_post_type('site-wide-notices', $args);" Y, e" x+ i5 a- \( Y
}
* ~ N. V3 y: y( B: q+ T. r- W8 | ```7 [ `; r _+ V r8 G% z* x
0 j1 i" A9 d- N4 y; j4 G
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
( T! V: f) `$ U% ?
6 c( ?$ B$ R" K+ L% N+ K3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:1 P$ s% [0 g: S$ W& h
; y2 {/ J1 P0 Q ~4 r
```1 c0 ^- @4 v; _+ ]' e
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
/ I) T2 V6 @( m* I7 q function add_site_wide_notices_boxes() {
0 ~+ s+ v, ^( e U* C6 h' t add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');$ }( _5 i$ ~) U7 m2 D
}
7 q2 Z" [9 w0 O. k0 w; L- Y5 W- o1 `7 V; E+ l2 N# ?8 e3 `) s& V
function notice_details_meta_box($post) {
% `% P& g3 L$ F. p' D wp_nonce_field(basename(__FILE__), 'notices_nonce'); @# C; C- S, p% y
$notice_title = get_post_meta($post->ID, 'notice_title', true);4 t" Y. @* q7 h* G3 V, e) O) _
$notice_content = get_post_meta($post->ID, 'notice_content', true);
# g d- m; j% X6 l4 Y ?>
+ b0 |* l; H2 ^& F) w <p>' w. x u. V( Z3 m0 [
<label for="notice-title">Notice Title</label><br>
) \0 W- B, R" `5 G <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">1 c7 n7 u& v3 V0 v1 s+ _& E
</p>
4 q* F* L* A# z T: G7 D4 U <p>
5 C6 q0 ?3 ~; @6 ?& ?/ P- P% d7 D9 ` <label for="notice-content">Notice Content</label><br>" ~* T6 q) [ g- Q
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
3 M6 X- g+ y9 F/ _ </p>
: x/ D7 n; i! M7 E. h/ H. W <?php, K" f1 |$ n. |
}
) L' s) |( A; M0 G' V& u# q* R$ F6 O s/ f
add_action('save_post', 'save_site_wide_notice_meta_box');
1 @( {7 H# @; _$ _ function save_site_wide_notice_meta_box($post_id) {
. A" @8 G( N" J6 S( F( h if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
* T; T( @/ N, ^ f- k return;. }# c# L9 l4 U! {% i/ p
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
* S. T9 X* J. m& g! n1 n return;( f. N) J+ y' N0 t, [% j
9 y/ W9 N3 _- w( J if (isset($_POST['notice_title'])) {
7 j* a- e' ~( h5 W( s2 ] update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));" ]1 l% g% L* u2 q4 E7 m( {3 Y
}
/ ]2 |/ V* q$ [& R if (isset($_POST['notice_content'])) {% S, a/ h) j, ~4 ^
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
9 m* i0 m: p* W% ~, t n2 F: h }
2 c& Z4 v. G$ M4 N& l }
* p3 @& _, u3 P4 h7 z ```
0 ~6 r# y6 R" O- g! p, M6 C ?
9 O% s' \. }: Y/ b 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。. W; A+ K$ y0 X$ d% P% k$ R8 D3 G
$ V/ D1 U/ y' Z( E. ~4 _" `
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
j/ c* K- f, O! Z5 D( ^3 Y# ?9 ]) H# t* P; @9 W1 ?! h# A2 A0 N( B
```% \7 W+ |& G4 Y7 X; b6 y- s
$args = array(
' O8 i/ j$ V" ?) F# Z6 @: }7 ` 'post_type' => 'site-wide-notices',9 o, h' g, {2 K7 Q6 L/ T& N U
'posts_per_page' => 3,
- @; A! B8 m4 G! G+ T9 w" Y7 n 'order' => 'DESC',
- j8 t5 @& l% |% E 'orderby' => 'date'4 x6 v( A) t8 X3 o7 s* U
);% m' A/ X) I$ L
$query = new WP_Query($args);
4 n' A8 V: ?; R( o: f3 ?5 K if ($query->have_posts()) :
/ |+ H4 E9 x0 g7 ?! P f while ($query->have_posts()) : $query->the_post(); ?>% z, B/ ^( y( {/ l! W' C
<div class="notice">( y0 i6 P% l/ C9 Y7 s' L3 f: L' D
<h3><?php the_title(); ?></h3>
8 |, A0 V# `5 e- e8 U1 L <div class="notice-content"><?php the_content(); ?></div>: n& z# C6 n8 F% B( g' \9 s
</div>
: Y6 x* n/ @- {8 j) I <?php endwhile;( H! x K3 ?5 B7 }
wp_reset_postdata();# G2 j/ G: \- G& ?# l; e. A6 I
endif;/ S5 S% u5 f. L8 M% `' S
```- k; `7 G3 o3 }; m* B: P" `' r
! E# S1 p5 _; x
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|