|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
; \/ Q2 H& e) |9 M7 S2 F$ [& E5 B) Y$ | N1 h2 u
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
& V+ Y, [6 k1 O2 e5 L$ o0 [! \) z& u: V3 I5 |, l# F' `
以下是创建自定义插件的步骤:
9 E! ~3 A3 S" K; u# q" v% ]5 l: y8 k5 A: f
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
2 B- Q: v2 q/ W! g. o5 ~( S% U$ p8 f* m9 T$ A
```0 j7 P t( o0 P3 o+ p
<?php
4 |# S' @/ _+ T' m: I /*; y) u. x0 l! l( [, b
Plugin Name: Site Wide Notices Plugin; z5 G( ~0 k6 e8 M5 ?$ g- U
Description: Adds a new custom post type for site-wide notices.* @8 y# r5 T# |' X: z
Version: 1.0
: W7 j; j: A% X6 l Author: Your Name
L- H4 a6 b# E. E; y \: `) X Author URI: http://example.com" E! ^" H0 c6 |/ L A6 v7 p- n t
*/: K3 c$ W9 L7 B
- A* A% u/ m4 b4 W // Add plugin code here...5 i6 I* @( V7 s& m5 ]: W
```
+ R6 ~; R% B, L' [5 p) n: r6 ]+ h7 k" }$ h5 m4 x$ G& D, K
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
0 V) Z# G' [. r6 M- O# Z: p
+ M x3 W3 {+ X$ t; @2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
4 f* k+ |0 j' u( d; i5 L1 k2 _) |0 k1 [' Z& @
```" ~3 j, @' H4 y2 W1 V" i
add_action('init', 'create_custom_post_type');
5 z" l5 B6 ^- N function create_custom_post_type() {
7 V2 _, J1 j# p( {* \ $labels = array(7 k5 z/ _; r0 J4 x( V& ?5 s& R
'name' => 'Site Wide Notices',
+ W* N! ]. Y8 R+ k9 a 'singular_name' => 'Site Wide Notice',; C2 f ^1 r; G0 m: J
'add_new' => 'Add New',
2 f+ A9 E: ^ Y 'add_new_item' => 'Add New Site Wide Notice',' k4 c+ k/ R) n: K2 E& m) @/ g
'edit_item' => 'Edit Site Wide Notice',
5 f$ C7 U6 w! ?+ ` ~1 j0 ~ ` C 'new_item' => 'New Site Wide Notice',
! H1 e: K% w% w! Q) Q c) ] 'view_item' => 'View Site Wide Notice', V; q+ N8 s: l& b j/ K f/ P
'search_items' => 'Search Site Wide Notices',1 l7 Q7 E! H! R) Z% `" R0 T
'not_found' => 'No site-wide notices found',
$ Z# D3 Q9 Y5 R* R 'not_found_in_trash' => 'No site-wide notices found in trash'
+ p, d! k0 Y" U; a5 t& \ );
8 ^; ]" y1 r% K5 Z; k* C9 @$ g' f& W3 n& \9 g. J3 ]) ]
$args = array(
3 n5 g' Z* m4 @" L& r b 'labels' => $labels,0 a* {5 k' L- j9 ?: ~% I: b3 Z
'public' => true,$ ~7 g$ H$ b& |, H% ^; M3 J
'has_archive' => true,+ d; p9 g6 w! d
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),# W9 ]. U# _9 |8 E& I" l
'taxonomies' => array('category', 'post_tag'),
5 c& z% S7 J& h O6 v9 U% C, o# f 'menu_icon' => 'dashicons-megaphone',; |+ p u/ u9 j/ i7 X: e
'menu_position' => 5,5 S7 f+ |8 g E0 F0 a
'rewrite' => array('slug' => 'site-wide-notices'); B4 f# u) L% ~. m# F6 [ P, b: F
);
+ K; E3 Y) l: I* F8 t2 {
9 G4 V9 ^6 e( u4 {! ~! A$ F register_post_type('site-wide-notices', $args);/ e$ G5 p: k7 Z3 m/ Z" l
}
. \/ p) y3 m3 g* }7 ?; U ```
( I9 Y" a7 Q+ i( K9 z" `$ D1 L7 _2 ~9 j1 `' C1 w
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。8 J/ Q( P4 c$ c# f" N0 G3 K' x
- E$ V2 v2 z6 }# k" }
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
# V2 D9 n" i/ v! y; y
, @" K6 Z5 I$ y3 W }- G ```
$ u3 p) @- R* W% r) E1 Z; _ add_action('add_meta_boxes', 'add_site_wide_notices_boxes');+ P( _$ e- D; V8 `
function add_site_wide_notices_boxes() {. J7 L8 |' n, u" t! s5 f, @
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');$ S X3 a9 I" P. b
}
) ]" M% K" l* X
+ ]6 S, Q2 q* ^0 n. Q. w6 d! s function notice_details_meta_box($post) {
3 O" G( L& C# X wp_nonce_field(basename(__FILE__), 'notices_nonce');
/ g) f8 w/ E. E. Y# c5 ^. f. V $notice_title = get_post_meta($post->ID, 'notice_title', true);$ w# v2 C7 ]9 ]! l T
$notice_content = get_post_meta($post->ID, 'notice_content', true);
, {; H7 Z, [: [1 f: u. X ?>
! R: C; _# H: J3 }* s1 q <p>8 r$ @: w) v. g
<label for="notice-title">Notice Title</label><br>
& c5 b, D! O) W7 O% J! V& c/ q6 b <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">$ `% F% ?) T! m! [
</p>* Z+ V/ k6 l# b! ] _ W% a
<p>( ^5 d6 F) C1 m! Y! f; N
<label for="notice-content">Notice Content</label><br>
' S5 ~# n* s: I8 X7 n1 {! A8 R <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
( z; S7 x7 e C( P p: u </p>% G$ n0 I: v+ ~ }3 n3 T
<?php
+ e8 I7 ^+ V+ b- t }( ?+ |! o8 ]+ Q" x/ x" T% N2 w) p
' U! Z. J& [" x- ]% v3 o+ c/ p/ f
add_action('save_post', 'save_site_wide_notice_meta_box');
8 `# Z) _& j! ~' Q$ k1 \$ r function save_site_wide_notice_meta_box($post_id) {
4 O& S+ E# T% X& ~! p$ B0 Z! ~ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
! y# X$ ?9 a* @0 `- ?0 c% V return;7 z. }7 {+ U R1 P- q$ S- k
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
! J* {7 D1 K E/ W6 L return;6 t/ J9 C5 p8 ~1 F
0 w( f, ~+ A9 @! I# f if (isset($_POST['notice_title'])) {
: f6 _) W3 D/ `4 }( s& y( Q update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));& w% B/ D, A) I: F- j
}
/ v. s: I, c4 c5 b+ N- _: W4 F- x: F if (isset($_POST['notice_content'])) {
. I, K8 }, f! C( V& v3 X update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));0 q4 T+ L9 m0 X, v0 z3 q) p
}
5 `, p, e5 ?3 L% h3 @ }) {7 K' g* t$ X, L0 l, x ~6 ~
```
/ Y* b/ S& ~1 ?& j$ m5 f" ?4 k
& h0 I7 R, J9 z2 |: A4 |* o- L 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
- Q3 C* C8 ?" v+ E' O0 f' X. C/ u
2 D9 W7 B/ F1 J( _1 g8 C4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
" C! w q V5 J1 u
9 B/ L# s4 s2 ?4 n! x! E6 h ```8 U8 ?6 X% n$ f Q5 g
$args = array(, X/ a, D$ N# _7 l& b
'post_type' => 'site-wide-notices',
% C7 V: h% F: |. }3 ~' | 'posts_per_page' => 3,, c4 k$ _: ~1 C. G- v
'order' => 'DESC',2 c/ B1 O/ x& g
'orderby' => 'date'; ^8 S* m3 V8 L
);( e" w1 e/ b$ B+ X3 v3 y
$query = new WP_Query($args);3 [# c' A6 P$ u Z" K
if ($query->have_posts()) :
+ L6 J8 A! G' h; b while ($query->have_posts()) : $query->the_post(); ?>
; I4 _& A$ [- O; G% J# x <div class="notice">
# v' a# Z, ~+ v9 y: ` <h3><?php the_title(); ?></h3>7 F, x" ^0 A ?4 O& f
<div class="notice-content"><?php the_content(); ?></div>
4 u3 i0 N2 a" A/ b9 u7 m/ g </div> [& ]6 h( y: O1 Z3 X% k
<?php endwhile;
% \% v( j" o8 T4 K! T wp_reset_postdata();
7 f# P4 S+ z+ M% ?6 H' [ endif;
% N" T, e3 N0 t, { ```
$ q% ?( J( m; @6 g T. U% y) H6 a9 J8 @9 K% \5 `
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|