|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?' E& `! ? c/ {
: l9 h: ?! {+ a( Y6 ]
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。1 T- ]" o) L: f
$ _1 C* | t. ?2 S) _0 O U! z7 U
以下是创建自定义插件的步骤:) H- n# a+ q- A! [# T. m
% ^- [- \9 S$ {& s8 \' {' o
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
8 G$ u/ o& X/ O+ F/ G. ~& I9 `- o
8 q) E; {- y4 |! s# V5 F7 E7 ? ```
0 }( {8 ~% }1 U <?php
: N% K. q J3 E; x1 a /*- [. w! A3 ]' m% f; W
Plugin Name: Site Wide Notices Plugin. n' @0 B1 y" o3 V
Description: Adds a new custom post type for site-wide notices.. Y0 r/ D7 _, ^
Version: 1.0$ Z. ]4 ^& {/ X$ l
Author: Your Name5 C" _" _. z3 F2 l7 q z+ h
Author URI: http://example.com
2 s2 V7 p4 g/ M5 z. c */
2 `0 s i3 B' x: x2 [
+ Y$ G- M+ w/ Z9 U3 ]; Q5 \ // Add plugin code here...2 A3 i; J9 B% `7 I
```
5 O1 N3 J+ e$ X7 c
; `: ^+ A/ @; A; y& y6 U 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。0 U1 ~. ]/ @! |' Q1 N( H) W( Q
* W) b- Y) w2 ~& v2 z" p# Y2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:& G* E) i/ H5 b
& P1 L. o$ ~3 ]! F7 c ```
! D$ ]) T" a* o1 Y, I/ C) S& D4 `- A add_action('init', 'create_custom_post_type');! v: ~) E% ]; d
function create_custom_post_type() {. u7 f: }9 L. L
$labels = array(& q) A# D2 }' t1 n) @' }0 F
'name' => 'Site Wide Notices',
4 ]5 b& c) n: ~# v# S1 q* t6 M% M 'singular_name' => 'Site Wide Notice',
/ k5 [+ o/ M1 u4 m( X4 E 'add_new' => 'Add New',3 T# P8 V7 @) n$ B8 o- N4 |; O
'add_new_item' => 'Add New Site Wide Notice',1 j# q, [' O5 \6 r5 O b
'edit_item' => 'Edit Site Wide Notice',8 J: @% B3 ^% R8 a V& C8 n
'new_item' => 'New Site Wide Notice',
8 }0 K; K0 v5 @; ? J3 \ 'view_item' => 'View Site Wide Notice',* e# k$ R& ~) |
'search_items' => 'Search Site Wide Notices', m/ R4 @6 g9 Q+ L
'not_found' => 'No site-wide notices found'," h( v) q+ n2 O% O% {
'not_found_in_trash' => 'No site-wide notices found in trash'# x3 {( J! V( e
);7 F4 S! L* L: E& K( K- p1 k. V
8 \$ ^5 l6 P! I. E R $args = array(
9 L/ v. i6 b- G/ w/ G1 k8 c 'labels' => $labels,
3 h7 I& C3 ?( E# n 'public' => true,* y0 @$ _( I" ?
'has_archive' => true,
4 I, p% F- a" X2 L; S 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
" O8 }/ Q* T/ R8 U! R& d 'taxonomies' => array('category', 'post_tag'),! Z0 P N2 F- m, V1 X$ M
'menu_icon' => 'dashicons-megaphone',
z; I# t) o6 W& G4 {$ S( ? 'menu_position' => 5,4 k- Z/ ]* H6 u, z% b
'rewrite' => array('slug' => 'site-wide-notices')
. @! {3 N3 Y. Q( G2 e );
: N. g' R! O H# _0 \ m
; t7 X" f. I# R, [$ j4 |2 p register_post_type('site-wide-notices', $args);* E1 T8 N% ?! M* H0 q) M* b' L
}0 Y1 _7 Y, v8 h0 n: z/ d0 m8 d: [
```/ |) A) R* B, y' q2 b
$ i7 x" Z' [6 E
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
; h1 m# V: P$ V. m8 _1 y3 h
- R1 j7 ~, E4 d3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:6 f) |2 g2 ^5 ]( X! x/ U
; C# g' \0 ?, W( i3 T; R
```
( ^- D) C; o _- b add_action('add_meta_boxes', 'add_site_wide_notices_boxes');' \) o6 I; Y$ c# H7 M
function add_site_wide_notices_boxes() {
, k* T6 o9 ]; R+ |- M* { add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
) m3 c+ ^' G$ G }
* c1 y7 p" z/ u+ C2 |5 h9 @2 W) X3 n" E, z2 q1 w# I
function notice_details_meta_box($post) {
- p/ A5 ]6 f. t$ P4 R5 f3 o" b wp_nonce_field(basename(__FILE__), 'notices_nonce');; {) i O( w0 \6 ~7 x
$notice_title = get_post_meta($post->ID, 'notice_title', true);# X' R0 L0 {# y/ |6 r% i
$notice_content = get_post_meta($post->ID, 'notice_content', true);( Z4 `- c o3 K% }, d4 T
?>6 o# n/ z" X! N3 \9 T
<p>- R' a4 e8 T! N# `
<label for="notice-title">Notice Title</label><br>
, ]0 ^1 A$ C5 W- m% i+ v4 {( U <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
5 A6 ^9 M1 l2 E0 I( L </p>$ l) L3 o1 K! V! t+ q
<p>9 U6 W% s6 W, a8 l1 t, {3 X. I
<label for="notice-content">Notice Content</label><br>/ m( U Q0 ], a8 s- @; U1 `
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>8 O- n3 W; S( F9 v
</p>
n) R3 T5 D: D3 ]* Y0 Q1 b7 D <?php
2 E# R- y- a9 D( | }
1 `/ o6 }6 C% A0 {7 h" U6 J
+ m+ l! F) I" V+ E5 g y add_action('save_post', 'save_site_wide_notice_meta_box');
1 x% w6 s" \1 e j6 Z/ A( c$ Y function save_site_wide_notice_meta_box($post_id) {; O |. ?- x( G& a! o3 D# v$ B
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))0 b0 `4 `/ F. E/ v) f
return;
1 R5 h# }8 Y% }6 S7 N if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)8 y# `; n! N+ \* [
return;! v" O8 ^/ a/ L: n3 G, K
9 G4 C) i5 }$ g& i% I: a
if (isset($_POST['notice_title'])) {
+ a& { l+ x& E9 n h. t) p update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
) u5 f# b4 N; P4 p M }
2 i4 Z+ H2 A9 W) N/ }7 {7 w if (isset($_POST['notice_content'])) {: m! A8 a/ X5 X- L5 S" f5 W
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));, Z$ Q) D8 U5 q" x; X
}0 ?% o% J$ W) I3 O8 d+ n7 e" b
}
/ c. C7 n5 _/ t& I+ @. v. P ```
6 T) H/ @0 I5 f
3 [, i) x+ F4 w- Y 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
& Z5 t) _; k7 q N; n+ W0 e! [1 ?* k( H! j* r6 _( C0 M
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
/ k$ v. _; m# h+ u
7 o* n6 n( `5 v" j: Z" E s ```2 {" o9 I- I9 ?
$args = array(& {4 |' T4 i. M7 d% P7 X$ t
'post_type' => 'site-wide-notices',
- c& Z2 C3 c3 G: O4 L, d0 G 'posts_per_page' => 3, J# C: U7 t3 @! |4 C1 q
'order' => 'DESC',
6 ?8 j* W' p8 H! P9 z% n, T0 _ 'orderby' => 'date'/ J# ]! Y) n* T+ A+ Q& s
);$ u9 q! H, I. y* L% `
$query = new WP_Query($args);
! _7 ?- L, P* B, d6 V) c3 _9 h if ($query->have_posts()) :
. j- A5 ~8 }& `1 @8 |6 D% C2 T while ($query->have_posts()) : $query->the_post(); ?>
& G* R! q9 o4 | <div class="notice">
( c% U% t- `: P; ^ <h3><?php the_title(); ?></h3># ~$ ~- M( W9 }0 g1 g
<div class="notice-content"><?php the_content(); ?></div>, O$ n# K# j" l- k- M a, _
</div>- f; n1 d% L5 w" b4 R
<?php endwhile;
8 n2 |7 w8 C* v+ C# F, s6 L6 X; C0 p wp_reset_postdata();
0 t5 p" w1 v* I endif;
+ f; B/ u" m& K ```+ r: b4 d3 A; F6 S% @! \+ b6 T
; q% F# s# V. A 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|