|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
+ l& ]0 [+ Y% @) H- Z6 X
$ v e r. u/ P# H如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。! M. k. R G7 A6 v% l1 T
3 {/ z8 Y4 S6 S: k% Y, I- c( l以下是创建自定义插件的步骤:( V* t6 s4 g$ X8 H4 }6 u" T# {
) x. Z" Z! w7 G0 @2 ?/ K
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:6 m T, a3 S7 F' D
& I4 @. J( ]9 F ```
, {+ a- k6 |- L/ e2 K! l+ N <?php8 C( |, D0 T$ z
/*
# r1 ]# h( h7 `0 q, d' F Plugin Name: Site Wide Notices Plugin
) w P5 P6 Y u8 y5 a Description: Adds a new custom post type for site-wide notices.
" O0 l6 m+ P7 p7 s, H x+ i& } Version: 1.0
. k" k$ X$ X, }4 \0 [! v Author: Your Name
% X; l. B, I g( T$ ~5 S# w5 ` Author URI: http://example.com. w: ~; ^$ r( f+ _1 y' @
*/# c6 p, b5 {" U& _9 n; U
/ C' ?3 p9 B5 V; s% u f( _+ I
// Add plugin code here...' u B9 g* P0 [8 m! P9 ^
```$ E# D6 L# x( o% K* G
9 d# T+ y' a% H* | E
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。) L+ _ }3 T0 u
( O2 ]( I# u; n) A2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
8 `( }) V4 x6 {' d# x
; x/ ~! |$ E$ \0 D% b, L/ F ```$ Z" Z) M8 m5 V
add_action('init', 'create_custom_post_type');
. \; p6 C/ J5 I0 p function create_custom_post_type() {4 r, U5 U2 E) l- V$ n
$labels = array(
" P4 j& n! Z+ P. S& ^" g 'name' => 'Site Wide Notices',
! e E* I& P$ B; N6 a! o 'singular_name' => 'Site Wide Notice',- V: a1 y8 [ A2 q6 w2 M, C% }
'add_new' => 'Add New',8 N) E* k0 b7 n( o2 c! o
'add_new_item' => 'Add New Site Wide Notice',
: H- ]& `1 ?# B/ z9 @4 y8 i5 n 'edit_item' => 'Edit Site Wide Notice',
- ~) E1 K, c/ w0 V 'new_item' => 'New Site Wide Notice',
! g2 ?$ I; t3 Q( ?" K5 u 'view_item' => 'View Site Wide Notice',
/ D/ W. r0 ^+ G1 F 'search_items' => 'Search Site Wide Notices',! O0 e! }4 Y4 l/ ~5 \$ w4 l
'not_found' => 'No site-wide notices found',
/ @; s$ [6 s" |, W 'not_found_in_trash' => 'No site-wide notices found in trash'
5 k7 n: ~( p( O, J );
# J5 ?; f' N3 Y* q5 Y0 T
! H% S4 x/ }. O: n $args = array(
7 T6 ^4 Q0 r3 g; r/ i 'labels' => $labels,; O+ A, k/ ~ w
'public' => true,3 Q1 f6 ~8 G" l$ i' r: p8 ]+ b
'has_archive' => true,
" }( f1 H8 G5 f! g8 b( \ 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),, F8 U( d A! m: u' X- ]
'taxonomies' => array('category', 'post_tag'),/ S6 w4 C1 n* Q0 P
'menu_icon' => 'dashicons-megaphone',+ V# J( A" [& r, V8 R& m
'menu_position' => 5,: T# s4 U0 v* o2 |- |/ ]
'rewrite' => array('slug' => 'site-wide-notices'). z$ C, O- \0 N4 j
);
4 B& Q$ g3 `( Y* T+ d* ^+ j
6 I7 S7 R; Q0 q" n: L, f0 r5 G7 L register_post_type('site-wide-notices', $args);
+ B* v' L( K- V; Z* q" [1 ? }
6 q# o8 u" P4 E4 a' p9 c ```
' u' d% P7 X" [: l0 x' S8 i$ Q; v% f, E! `; E& r
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。9 u) L1 I- W Q7 I' B
& _: q7 h9 g' A" @2 z$ j5 G) {: X
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
8 o2 p9 y* S8 v2 m! `/ Q. r7 ]% L' ]# j0 k
```
% E2 b* U/ H8 d) G3 T% G+ F add_action('add_meta_boxes', 'add_site_wide_notices_boxes');% k9 m! z) f9 f3 t
function add_site_wide_notices_boxes() {& s% E$ [6 h6 R! f; P1 f" r
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');* a- n, S" p1 j/ Z" u" v* `
}8 j7 i! P2 Y7 s. S; d
" b) x& M) L& `
function notice_details_meta_box($post) {
6 i: c" E+ U3 n k) U wp_nonce_field(basename(__FILE__), 'notices_nonce');
. V, X, ]# p# _0 C( H/ E* K $notice_title = get_post_meta($post->ID, 'notice_title', true);- r1 r( S% y# f" j4 y5 S3 F( ?2 c
$notice_content = get_post_meta($post->ID, 'notice_content', true);3 ?' O0 ^3 d/ @' b
?>- g! U: R# i! U
<p>
0 ^0 a% F' B+ z: G, A <label for="notice-title">Notice Title</label><br> K3 Y) D2 ~$ I# H$ m- g& Z
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
! L1 t N) g+ P7 ~ </p>
* @) \; t; c+ j: `1 L% Y <p>
5 i5 i h& l/ A+ H! c3 o; ~ <label for="notice-content">Notice Content</label><br>
0 H5 A, b" f* J, b/ A <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
6 T q- [9 Y* L6 t, g2 q </p>
, O2 ]3 @9 o4 U& T+ b% i: j <?php
1 n; w% i7 r8 f* x& f" \9 y }
, _ `( g) u3 P% O# ]( M! A( I+ J, O7 h( t! L* r
add_action('save_post', 'save_site_wide_notice_meta_box');
8 J8 H- b* ?8 V' l* P function save_site_wide_notice_meta_box($post_id) {5 v; m3 n' K1 s+ W8 e' ?. S
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
# k8 b, C: J' F9 a( G% u return;, ~ p- l7 ]0 C) m
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)) e! F: G5 g, X% ^
return;7 @* I' q# l# V$ @) W- T$ d
& _; |1 S3 E! t" p if (isset($_POST['notice_title'])) {
7 H. n& j4 E! ~! e2 T; A update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));! Z3 r. ]' h+ `; p
}9 a! t6 h n! A7 q% R
if (isset($_POST['notice_content'])) {
4 H& K8 O1 R) S) x7 E2 G update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));" D2 m( X6 b+ x* v n" C. R6 F7 P
}1 }: ]0 h; m9 u8 K
}, q- z* x( R% e5 F7 B: k7 f
```# A& ?2 B0 x+ `3 h8 i
/ F3 d4 ?, y7 w, {5 C6 @5 I 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
1 W" G6 M1 T- c% `8 B+ k6 z( z! v
! C! |' O) A G& @. K2 R; s& Y- F$ @4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:( j0 m# T6 e4 G+ ^0 o6 [
8 H) f) P9 H: f0 ]
```
1 U' V, f6 m* |/ t4 G' M $args = array(4 A0 A% B+ [2 k
'post_type' => 'site-wide-notices',
1 ] { I$ t9 M& D 'posts_per_page' => 3,& @9 \/ p. ~- p- G# {' i5 F) g
'order' => 'DESC',; Z- E4 X4 y @$ U" }8 e
'orderby' => 'date'1 T! f( |+ n' ?( ], W
);; X" D; p( j3 h/ t/ U, J
$query = new WP_Query($args);- j) d; W- S& M- y' t1 E
if ($query->have_posts()) :
* G: F4 o0 I; c while ($query->have_posts()) : $query->the_post(); ?>0 d' _: T- b1 d8 U9 y6 `3 Y) O& A5 I
<div class="notice">5 O% T5 Z$ w( I5 ]
<h3><?php the_title(); ?></h3>; R' ~+ s2 C) \. |0 C/ i: `
<div class="notice-content"><?php the_content(); ?></div>/ w9 ~! I4 {+ _% ]. H# ~
</div>
: s3 J+ G, V2 ?6 b/ \2 E+ Z5 k1 ` <?php endwhile;
# x. A2 b# x; }( W7 V7 O6 I wp_reset_postdata();$ X, G: g3 m1 H. k& W
endif;( G; o4 U$ j' i. @% L
```, f; I3 I% ?, j% X
6 G3 Y& W5 o6 z' R0 Y 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|