|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?) @ f5 U! j& ]
( n J8 @1 W% e% ]
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
; r1 |$ o( |' h2 @! I* \6 l+ N1 {* T: x) y7 u- o1 E
以下是创建自定义插件的步骤:% t; T, i/ g7 }; x+ |
$ p9 m: z: q0 \: x
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:. a, |/ T& d& L3 |& O
0 g- D/ ^3 H3 L3 M ```
+ p x; r" I/ a6 H+ v# N <?php
5 Y9 i1 n* j w* B# G: [6 H /*
8 ~; N" U! j+ q. E2 X Plugin Name: Site Wide Notices Plugin' x; W, H: v) X [+ ~4 b# t
Description: Adds a new custom post type for site-wide notices.
- {9 D& E8 U8 o Version: 1.0
6 x6 z$ K! _) R+ I* M; x/ U; H3 g Author: Your Name9 Z- B' |1 L; [/ t( Z( V2 d8 }3 m8 S
Author URI: http://example.com! h: G: v5 M( d
*/
! C2 b4 D8 @" f% Q2 c& b
' w5 o( ^$ O* ^0 N2 G // Add plugin code here...: y7 }$ O0 [/ z
```
7 q, q7 F& |, E3 o8 N8 |% c8 T. V Q! q- q( P( p
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
" W- Q* z! l" L6 I `; ^& o$ H4 l @4 S# t) g
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
+ O; }4 z1 [8 g) i k+ V) Y; R3 ^$ T# l
```# X) b9 R2 L9 n- M9 t+ A: N$ |
add_action('init', 'create_custom_post_type');. G/ a. e% @+ i% u
function create_custom_post_type() {
4 S+ ~- C$ ?( R& m $labels = array(
' q2 p# p! N y6 x) i 'name' => 'Site Wide Notices',
4 x$ M! L! D$ H7 h2 J 'singular_name' => 'Site Wide Notice',
# d7 J0 f! x' p2 y& Z J9 r1 S8 @ 'add_new' => 'Add New',
* c8 i) E- G ?1 @! k8 e6 } ? 'add_new_item' => 'Add New Site Wide Notice',! d. _3 q! G/ O/ h# w0 _
'edit_item' => 'Edit Site Wide Notice',
- ^1 s+ D4 ^6 g& l* x 'new_item' => 'New Site Wide Notice',
6 c2 w: B4 @; e$ s& B( z 'view_item' => 'View Site Wide Notice',- r- Z- t! t# a3 ?* v' X
'search_items' => 'Search Site Wide Notices',0 |. E- l1 L& D, i
'not_found' => 'No site-wide notices found',
^2 V0 `$ A" Z. } 'not_found_in_trash' => 'No site-wide notices found in trash'
5 S# d3 A) l/ x2 e );/ U$ o8 E! a: R6 G- P* S7 w3 w! v
6 n4 Y! V: [6 E+ @! |. z $args = array(
0 j/ q ^4 r9 @8 o" ` 'labels' => $labels,
3 z' A- }$ H6 @+ ^: o. J 'public' => true,
+ J5 S5 W5 T$ M4 c 'has_archive' => true,& y$ ?4 o) a* g" \7 Z) \
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'), ~: `2 d% b( ]7 }, ?
'taxonomies' => array('category', 'post_tag'), n; H3 C" A' T: O& N0 Y$ B% |+ w
'menu_icon' => 'dashicons-megaphone',6 k& @5 F4 f& k6 \, L- I
'menu_position' => 5,% E0 `0 O; i- v3 c- T
'rewrite' => array('slug' => 'site-wide-notices')! H5 {/ S* S; K% `
);
t9 }- h# K4 Z/ N: J6 {
- v: D- L. g+ B" q) ` register_post_type('site-wide-notices', $args);
2 v& G6 ~1 o# l5 a& Q6 z }
9 P% r) t* M1 g- e0 l/ Z ```( z) R. E7 s7 I# D
3 \3 q$ s5 r- ^* G% z/ |# k8 T: {" e 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
7 s' Z; Z0 F8 T+ v- d" h, a
% N* B9 ~0 z' M- l* L) _3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:# p$ @( I, `" p
. t% z4 L: @/ k! ] ```
- K; w. A4 @' M8 ? add_action('add_meta_boxes', 'add_site_wide_notices_boxes');+ e/ ^- e g6 d! N
function add_site_wide_notices_boxes() {
8 {( w' X+ y$ n" N add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');$ U6 R& J! \0 d1 m' ^
}4 ~) ], ^' c0 H7 x2 {" V1 j
2 h* R1 ^ i5 i7 p. k9 u! z function notice_details_meta_box($post) {
: }! V H' Y8 d; N wp_nonce_field(basename(__FILE__), 'notices_nonce');1 H: o6 ~0 u. e ^
$notice_title = get_post_meta($post->ID, 'notice_title', true);
9 \4 Z' M& _8 m: k& o1 x/ w4 F7 O $notice_content = get_post_meta($post->ID, 'notice_content', true);$ n+ t2 W8 E, c, @. N3 y+ C i
?>
" k/ C! N9 ^. V/ t: Z1 h! l <p>' l# F9 W9 A# J' X! E
<label for="notice-title">Notice Title</label><br>0 M' _. |+ W7 j: C" c' y' y
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
! D. b; v6 ]) ^! G$ X( P6 N </p>& @0 f7 R) ?2 R8 h$ d, z9 ~+ w6 Z
<p>
, @" P1 t( @4 ?5 v/ D- O <label for="notice-content">Notice Content</label><br>$ V$ M( \, h' e6 ^- E' v
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>/ p1 u) P h9 P) Y9 `4 q, n9 g* E- T
</p>2 |/ E/ j3 O1 K9 [/ ~
<?php* b# t8 K* o K
}
* d+ Y8 D" K v% ^' A; l* h* D- ^$ N" A' {
add_action('save_post', 'save_site_wide_notice_meta_box');
7 r" L B6 s, i H2 b function save_site_wide_notice_meta_box($post_id) {
9 B9 n8 G4 @( L/ Y, t$ ^# e if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))" B6 Y% m, E) x. Y: H% N7 ?
return;
+ R* v% m7 e% [4 W if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
) W; ?% ^) v* ~/ T& \: E! w return;
( V4 w: w: \5 F2 R( a
9 n; b+ O0 J+ G2 H: Z# ? if (isset($_POST['notice_title'])) {9 u. [0 x1 o7 u! }9 f0 v4 Q; q6 \
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));& n e6 @( d: u. _
}7 Q7 Q/ [% i. ]2 Y$ M1 {" d' y; e) D
if (isset($_POST['notice_content'])) {
5 p$ f4 `5 J" m update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
: M1 V, L. n- K$ `6 y) H! ]8 R }
9 u) S5 y8 Y8 T- z }" ^; g! S/ y5 j( P p7 l. J1 w
```$ ?+ O8 r! I; a+ j4 T. D1 B* Q
- y9 l2 V- @5 z. p! a/ K* H
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。# l" t0 A$ P" C1 a( ]: O
+ B* Q3 |( A) e" [, |/ ~# \% } W0 _4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:1 w& G) N7 D9 @* ]0 u( c1 s
5 {: H5 m2 e- R$ v+ j: P7 S ```5 u: z6 Z; ?' g: ?
$args = array(; L2 r8 n' W% U, y4 E
'post_type' => 'site-wide-notices',# p, x; Z& X7 t9 i5 I
'posts_per_page' => 3,# B1 p% Y! z' Y Y! N) \
'order' => 'DESC',
; @+ S4 @% j% e! T1 T# k 'orderby' => 'date', w# s! Y3 n9 Z7 {' U
);
/ O- w- X7 S' V6 Y( |6 ` $query = new WP_Query($args);
5 O( D3 {( s- z2 ~' |0 B2 r if ($query->have_posts()) :9 O0 y k% I0 b
while ($query->have_posts()) : $query->the_post(); ?>
" z4 _. ^; H1 i! `5 e' U <div class="notice">
- N: ~/ ~: \% o/ J- r3 Q( J <h3><?php the_title(); ?></h3>- n- F) g. f' j
<div class="notice-content"><?php the_content(); ?></div>
1 k1 Z8 z3 p+ h9 g! k8 H$ L: Y. W$ C </div>4 O5 [7 W! W- X* o; ?; [$ |, r5 r
<?php endwhile;/ X; h) P+ m* J: p& f" Q
wp_reset_postdata();" ]/ Z0 y5 e0 v; T& T2 Z, n
endif;
, y7 |9 U4 ^6 N" Z1 O( Z7 j; W0 ^ ```
. [4 g0 ]3 r. N5 h
7 X6 b0 f" S9 N3 ]9 }# ~ 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|