|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?/ k- @ T' s$ X# H$ x
- P9 P0 @! _6 E7 C D3 a+ I* Q
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。# N, z7 W& ^) \1 ]' r2 i
; [+ }5 E A2 {; x2 J% o9 ^5 v7 z以下是创建自定义插件的步骤:2 w6 j1 L! {, X% l" H) Z
* u' o9 _1 F7 _9 ~: q+ C
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:; x) V' t" Z7 V9 M
+ \, {. O1 l$ Y2 F7 w1 s2 }6 I
```
' d" _4 s5 t& B5 ?$ d <?php
+ H9 }' y: m0 y* ?9 x4 J /*
% u% S% Y; [0 U' f: m, l Plugin Name: Site Wide Notices Plugin& ~" }. k. Y4 ~5 K0 c% e
Description: Adds a new custom post type for site-wide notices." }0 \) u) H% P% a/ X- O
Version: 1.0
/ K# B0 @, ^, J- V C Author: Your Name2 D1 k0 z k9 g" J7 g' g
Author URI: http://example.com
8 j' c8 q1 {, Q5 K" w* B */
) }2 F/ M2 u0 l! D. f, n4 r0 d- x/ a3 b$ c! S, l# H
// Add plugin code here...* z5 z0 p% f3 \6 E+ L
```, C! G) G2 D4 o) G$ a: b3 E; ?& l3 j
* j, r+ W% `% r. F$ |9 N 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。9 \7 }% [, B- Q. r/ T" f' r
3 T# {6 P4 ?& s& ^2 p
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:3 U) h% ]* j4 E8 s- `
9 I3 ?- A( n7 y" o) x& x* O1 q ```
; h' j& J* s" o9 m4 l$ T- \ add_action('init', 'create_custom_post_type');
( u8 |$ u4 H! U; x function create_custom_post_type() {6 J! ?+ z; y8 n! n
$labels = array(
' c! z7 M" \# K! m+ G 'name' => 'Site Wide Notices',- A& X* M' P4 n) m- N
'singular_name' => 'Site Wide Notice',* [' |( u/ }- a( B) \
'add_new' => 'Add New',7 Q- c* |- S8 |, t' O
'add_new_item' => 'Add New Site Wide Notice',
2 E& W+ e6 S4 \, J @6 z7 v 'edit_item' => 'Edit Site Wide Notice',
6 g8 S+ V! y( m8 V8 j 'new_item' => 'New Site Wide Notice',
' f$ n h4 k9 _+ W 'view_item' => 'View Site Wide Notice'," S1 s5 F* E) {: i- f. D
'search_items' => 'Search Site Wide Notices',
1 ?: R8 D) m! H s6 } 'not_found' => 'No site-wide notices found',# k# B4 x6 Y& K, N" Q
'not_found_in_trash' => 'No site-wide notices found in trash'7 b! {) N9 b) @; w! R: D& s
);
6 s7 T% M$ _* D: f" H' h
) `8 {8 @1 i% D6 Z) ], x3 A2 \ $args = array(& Y! d Q L$ o
'labels' => $labels,4 f# m. t3 H! e0 u6 K
'public' => true,7 {2 Z1 {2 r. X X, P' g, {+ ^
'has_archive' => true,
4 d$ s% G; C" u0 n) F" H 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
1 Z& f# O, u6 _4 R 'taxonomies' => array('category', 'post_tag'),
3 N# P8 R# j$ o* y) v% C 'menu_icon' => 'dashicons-megaphone',
8 E! E% Q: h' x. I# E' Q% K7 } 'menu_position' => 5,
7 J) J6 s' I k- j' Y$ }) @' ? 'rewrite' => array('slug' => 'site-wide-notices')
! N, Z, \- g6 P, w) i );
" f, g: P" B( |4 T ^
& Z- P/ _/ U6 r3 _ register_post_type('site-wide-notices', $args);
3 y3 r+ r3 y6 t7 e5 h) S }
5 j' E1 Z, C Z5 l5 V% R0 ] ```
# e8 O/ A5 `$ h4 u8 O- t( T' [ c% ]# b
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
: m' D4 f) o, I+ Y# y0 Q8 a; J1 t
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:5 x# o, r' W' b
6 S6 m3 V' d6 m* L8 y
```1 U: w" m- u: H5 H7 P- [2 u) K
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');1 |6 E7 N/ f V
function add_site_wide_notices_boxes() {
9 Q' n8 O) L/ T( T, I add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');) _* d" s8 z- p4 P0 Q. R
}4 K7 I3 r: d( M5 |
- Y8 _: p5 P7 i- |# h9 k function notice_details_meta_box($post) {
; c" R- |1 r% \ wp_nonce_field(basename(__FILE__), 'notices_nonce');
3 G' a. f$ ?% A% _3 W9 a# Q $notice_title = get_post_meta($post->ID, 'notice_title', true);1 b' ]& ?, p- _/ s" h0 C! @
$notice_content = get_post_meta($post->ID, 'notice_content', true);
4 M6 I8 m( f2 G ?>5 p" s* h' p5 u* i" @( G# S
<p>
4 y# P0 p0 n _- q. I- U <label for="notice-title">Notice Title</label><br>, _; P: U" a; e0 ^/ Z* B
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
" L3 J5 R5 J" M4 e, D& X </p>
7 ]0 ?* K* i) m) p: F& T <p>
6 A' R; N4 J; }7 M <label for="notice-content">Notice Content</label><br>4 b; w \9 q, d8 I9 s2 {& x
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
4 {# ]- X; a& t- m </p>
$ A9 l) Q. o& k7 L- m <?php
$ ^# M0 c: u8 m. H$ k }
! \' R7 ]/ p' t7 Q. i4 _- F8 X% f) j! |
add_action('save_post', 'save_site_wide_notice_meta_box');5 _/ i- _ I. u* a& A( B" ]8 Y& ?8 p+ G
function save_site_wide_notice_meta_box($post_id) {. C* F) Z1 L. \7 o% v4 Z6 x% `
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))- C: ^) Y+ Q5 r! @6 }
return;8 j$ H/ N8 y: N3 C9 q+ d
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
7 l9 Y4 B8 A$ }; N& O) i( Z return;
9 J1 G6 T# d) F+ J( h: Q1 D
& |" U; J5 z( x+ ] ~% H6 D if (isset($_POST['notice_title'])) {6 v1 m, M* i0 l7 J9 E1 z: H
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
; Z: f+ Q* ~" Y N) B }
2 E8 \% [* E/ ]7 Y if (isset($_POST['notice_content'])) {
" m# Q R/ ^% ]3 R- D update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
: [% O/ R- b8 N$ q( g ?6 k7 a }1 n* Y9 w% c' \: i, B! O
}
+ ~( Z) }2 g2 V7 S' K ```! {: q- V5 F7 p" I! r) ` ]
6 [" I2 n7 U6 s4 G 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
4 @2 G: M9 g1 T" j
5 l& ]+ J j4 E1 o4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:. o- X, l* B! k+ t) Y( K
; i* W; h' ~* y9 K; e$ e; T1 G ```
# _) _, `* U4 E# ~ $args = array(
6 D! q' t+ j( N' \3 ^ 'post_type' => 'site-wide-notices',
/ o% J6 Y; h3 E 'posts_per_page' => 3,
+ H! L K2 ^& y8 c# W, R+ g; V* w 'order' => 'DESC',' P6 M& k$ A6 K+ k6 N0 D
'orderby' => 'date'
+ E$ z2 |0 u7 w) l );3 R' W% x8 ~! Y7 e1 @
$query = new WP_Query($args);6 f2 N1 {( b7 V* n; W) w3 L
if ($query->have_posts()) :
! v- Q1 ^$ c2 E0 H/ ?% {" @ while ($query->have_posts()) : $query->the_post(); ?>, [6 r- R& ?6 H. z4 c) a
<div class="notice">1 b% o" f! n2 O# ^5 d. _4 h
<h3><?php the_title(); ?></h3>
; ?( t3 c0 \, }) R+ p, A <div class="notice-content"><?php the_content(); ?></div>
% Z# M1 Y/ K2 Q6 m: C3 E' N q3 ] </div>
; a( Z2 D/ _7 H3 {7 r <?php endwhile;! Y) P4 g6 i) t8 N, I
wp_reset_postdata();
: V( R9 y* o# K$ b6 L7 q A0 f endif;6 c4 }( F* T4 P- @4 y
```
* ~* z" a$ o" f5 N5 v/ d# S P5 a2 _8 ]
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|