|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?1 g/ Z% j9 h# K1 F3 A0 v
9 s1 ]* k) y/ @1 d如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
5 m* X4 c ~. D# l( U2 h0 B
( w" x3 m: o% b& q# L( Z1 E以下是创建自定义插件的步骤:
9 d) \* W' ^( `% e! {# M4 Y6 J" [8 q( x, L7 W0 M/ S5 B" L8 F
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:4 m4 c2 w6 f. j" ^2 j Q$ q
4 ?6 ^. `. L/ C/ g$ P5 \
```
9 U6 I0 L7 a6 m6 Q4 k7 N <?php
. V- k$ `9 V3 p- s) w7 O /*
9 T, A9 {. w; {( P l5 u Plugin Name: Site Wide Notices Plugin
$ b7 d; O& G9 @ Description: Adds a new custom post type for site-wide notices.
8 n/ K* p0 r/ r# m+ B5 B Version: 1.0
# n* w% l! |3 h+ V* b6 s5 V Author: Your Name
& v' V" g2 ^8 i6 P9 w Author URI: http://example.com' D8 Q1 p( Q) { w
*/
' m ] Z% B" ]8 D
9 Y$ R: C+ L. x3 P6 F // Add plugin code here...
k0 o) @8 Y5 K( Q* D ```5 J8 P, ?7 b; H& c6 F
0 d3 V1 s' [ h2 c. {# ]
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。7 j( u/ Z+ A% O9 o1 Z y) m* Y, E3 C
" |' E9 ?7 W4 X2 K" I2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
# ]( D+ B* \( K- ?+ G
/ |. g7 X A6 l8 h5 E$ S! N0 Q3 { ```1 t7 C# i/ U/ [# ]- o
add_action('init', 'create_custom_post_type');. L+ ^. X6 y" C/ j- Z- k
function create_custom_post_type() {
% b f# M! C6 _6 f/ p M2 {$ ? A $labels = array(
: v8 N/ K$ I/ x5 o# J 'name' => 'Site Wide Notices',
$ v; c/ i* e- Z/ Z5 P: r 'singular_name' => 'Site Wide Notice', K9 r2 k! L5 j/ F/ [7 u4 b
'add_new' => 'Add New',
3 h" n8 o Z+ _; ~. a) c 'add_new_item' => 'Add New Site Wide Notice',
9 Y% m ~3 w1 N5 M' @( `5 F2 x 'edit_item' => 'Edit Site Wide Notice',' p) v8 I8 {' `
'new_item' => 'New Site Wide Notice', G1 e3 O; d" f0 ?6 u
'view_item' => 'View Site Wide Notice',0 d a5 |. g+ O2 Z. j
'search_items' => 'Search Site Wide Notices',: e- \! N# s* e- c1 `
'not_found' => 'No site-wide notices found',0 d; Y5 M7 @9 V ]: x( f3 e
'not_found_in_trash' => 'No site-wide notices found in trash'- A! g8 ^3 _) }& V2 k g
);6 N6 {. J) s8 L2 n4 |$ u
0 b3 w5 \3 U. b6 Y
$args = array($ J) g1 ~8 v' H% t. V8 _) b0 H7 {% O
'labels' => $labels,
1 P% h" t' z. g0 F, T3 X 'public' => true,, m7 D9 {% |0 f; {* q3 K
'has_archive' => true,
* q" X! ^) Y9 H* w' \2 g0 |0 a 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),1 I1 ]/ c" g0 h
'taxonomies' => array('category', 'post_tag'),
5 u( K; W4 Y6 o6 q: r0 {) ?# K/ ` 'menu_icon' => 'dashicons-megaphone',
( k3 X* }9 t% n v 'menu_position' => 5,7 `% i- z- U+ F+ a
'rewrite' => array('slug' => 'site-wide-notices')5 S# y: f" K. ]! @; v: |
);
/ W/ R/ g% {0 ^/ u1 n$ B4 \1 P' q% g
register_post_type('site-wide-notices', $args);$ z) W p/ B K& L* o: d
}
: S7 |# f& Q2 T, v ```
0 a2 e8 e9 ~. a$ V4 z# L- Z3 L5 a9 U6 r: o
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
8 u% z4 h9 j6 q( q5 p
+ G- {, n4 G3 q& N9 Q l; A; O3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
4 h) h2 ~3 e) C, R. A z9 {' [+ P: ?
```2 c9 u4 P* u+ Q, y! ^
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');) @) M5 p' q; j8 b+ b
function add_site_wide_notices_boxes() {$ }$ j% T) N( O6 ~1 U$ e
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');4 u) ?# i4 s s+ ]2 {; M) n( b
}
- t$ u3 ]! v0 K5 v z0 m+ O" t5 ]% w0 k. c3 P; O9 K* B+ G8 f- p
function notice_details_meta_box($post) {
+ m4 W% ~1 ^( J& @ e# y wp_nonce_field(basename(__FILE__), 'notices_nonce');2 x: h! ]' D) P: d
$notice_title = get_post_meta($post->ID, 'notice_title', true);! h9 i* ]3 l2 a- Y
$notice_content = get_post_meta($post->ID, 'notice_content', true);
. o7 N+ M4 f3 f- ^& Z4 }& | ?>
9 Y* A) |, n- W0 Z <p>6 R% o; P4 ^! ^* j0 q* g! T# r5 N
<label for="notice-title">Notice Title</label><br># v' f( i1 [: o* E* K/ ~3 ?
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
) G" Z; Y: N% c% Q5 r& e' w8 q4 p/ ` </p>
5 w( d" A, F L( |9 D7 N6 p# q& H; I <p>2 X4 g7 r" I5 Q9 q
<label for="notice-content">Notice Content</label><br>5 P; q( C: d; r
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
7 B3 c. t/ B5 @+ c; q </p>
# f7 w) _" [4 P* b$ m/ m <?php6 r% ^ V% m/ w1 e: W1 J8 Y" J! w
}
# |8 y+ d+ W# N8 S/ d$ E5 e0 m9 d0 e1 o& H5 @- i
add_action('save_post', 'save_site_wide_notice_meta_box');/ ] ^; a; Q/ N
function save_site_wide_notice_meta_box($post_id) {9 \( v- T9 V A j5 M" @, h/ f# a
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
' g1 G: y0 p4 G return; R5 R- f$ u! U1 x
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
$ y; {5 S( M7 `1 B; d( x! R- C return;+ e$ {+ C1 Z$ L
$ P- U, q4 M% U, b7 O; G7 ?% x if (isset($_POST['notice_title'])) {% U; ^/ ^8 u7 c. G1 x3 U6 t
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
- A2 a4 k; P/ B+ P- ^' B3 T( u }
* L& g( Z- r/ ^6 ^/ V' s if (isset($_POST['notice_content'])) {
. b7 K$ Q. I+ @* Y, }4 G5 o2 P update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));' N. C {" W1 ?+ N' `
}
4 Z9 R# S" z6 v* ~ }, J* U$ t# q3 ^ k* J, t
```! k% Q& G1 Q8 j2 V
) A, t- l3 L0 C' Q* m
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。$ d9 _/ S! \6 {9 H6 K
4 [+ y& c+ H0 `" e4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:1 \ k$ X$ T) g" Z/ V* E
+ x4 l9 h( t G: \. A5 v ```2 F9 C" z3 o1 q+ p5 X9 S7 m4 _. G9 Q
$args = array(3 n! ~# c7 W- @8 m; I# b
'post_type' => 'site-wide-notices',
% y# e. B2 I q1 m" r" P 'posts_per_page' => 3,# I( V' a9 y* G1 }. Y% u. P0 D. [
'order' => 'DESC',
( e5 { t0 K8 W) x3 M 'orderby' => 'date'
; `1 n( R1 E# |, [/ C5 A );8 U- d$ ~5 n# p6 P3 `/ E
$query = new WP_Query($args);6 u7 @5 b4 `; \7 w: o$ R& k
if ($query->have_posts()) :- m7 Q7 ]/ H5 T
while ($query->have_posts()) : $query->the_post(); ?>
6 Z2 c5 G1 b; X5 r' K$ I( T, Y9 r# Q <div class="notice">
: l* O0 h# }# O/ W <h3><?php the_title(); ?></h3>+ C' c5 w Y! t- n `
<div class="notice-content"><?php the_content(); ?></div>
* k+ l8 |$ e2 H1 w5 S. R; k </div>
6 T5 D/ Y6 w C) ~ L4 S/ U <?php endwhile;
9 T6 T, h f/ ]" B wp_reset_postdata();
8 I. c3 Q. ^9 g; C' ?5 L- u endif; J1 l7 V2 P6 n( r
```' }8 d: g2 _ a
& u2 O4 g7 j* ~: W/ U1 @0 H" x
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|