|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
( t, w5 _$ }& t, m- q
4 ]* }3 A$ l( v0 Z/ U' K如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
$ ]9 E) k6 u- H) n* ` k
( @4 ~1 r* \- O- N% x$ B2 e以下是创建自定义插件的步骤:
; O h* y7 E5 |/ j8 j& K0 k5 i) C$ U7 _
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:! U2 z @, t. r4 j( F4 v6 Y- ~( V) i
5 u$ H3 B7 v. h( \0 ` ```
8 e' D0 D- z& _8 q1 w <?php
1 P6 S5 a. J+ V& w /*
( ~4 u8 b! t* V; ^: \5 q Plugin Name: Site Wide Notices Plugin
* @* _% I" ] i- a& G Description: Adds a new custom post type for site-wide notices.
4 y P. m' R3 z" c% D Version: 1.0
% w: u$ T4 e+ y0 Z Author: Your Name
7 e8 L- _+ L; c* i7 d Author URI: http://example.com4 A" K6 \0 h" I) h2 g; u, B& Z
*/
# O& L9 A, R, b4 \8 G& g& q9 a8 O* L$ Z" L- F
// Add plugin code here.... Z4 E& }9 w4 a' g
```
( @) W( a0 j' \; }( N/ |' V& b9 G: x
- {# v& A, T2 p* A2 G 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。3 [9 e# A. E L7 t9 J6 [( _5 `
; U! j; L; X, G1 N+ i9 Z* q
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
, `4 J; O7 _7 O0 l6 U/ M; a: D* H: p5 }( Z5 l$ P3 Z2 k
```% d5 n# ~/ D0 O5 n( {/ ?
add_action('init', 'create_custom_post_type');* C' L2 i! S4 ?1 {; [- x( |. L
function create_custom_post_type() {8 h [" ~* x0 H/ _9 m
$labels = array(
$ P: i$ ?+ r; |) T) o! m 'name' => 'Site Wide Notices',
3 J& f) D8 o- l* k- R8 W/ Z- ^ 'singular_name' => 'Site Wide Notice',
) Z1 s4 D6 S' M3 ]. J; O 'add_new' => 'Add New',( j* p: J% f" E( l7 A# u l9 f. Z
'add_new_item' => 'Add New Site Wide Notice',
- n3 T$ D' w: h( d0 Q1 P 'edit_item' => 'Edit Site Wide Notice',% a& y/ ?* N! R- Y
'new_item' => 'New Site Wide Notice',) \; A( M7 y5 d7 Q X) C
'view_item' => 'View Site Wide Notice',/ g3 Z$ x. A( F" e& T
'search_items' => 'Search Site Wide Notices',
/ O3 y) G- Z6 u- ^9 C( U. o 'not_found' => 'No site-wide notices found',3 E; c0 ]3 o: ]1 {9 P* @7 b# t, R5 \
'not_found_in_trash' => 'No site-wide notices found in trash'
" N$ u2 d6 d/ P# T/ R/ h );
; {+ {. s5 Y- A0 H" o6 w3 W) d9 [3 ?7 H- s
$args = array(
7 V9 o2 M% d: E! H ]/ T' x/ @ 'labels' => $labels,
! D* W- B0 a% I! b; E) ? 'public' => true,5 f* x5 }; I1 W& }0 x) t
'has_archive' => true,
4 I, p3 o5 d5 N 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),% \) a1 {9 u7 U! x
'taxonomies' => array('category', 'post_tag'), \% ~- ^$ g% n1 N
'menu_icon' => 'dashicons-megaphone',
: K% W. E# n+ d0 r6 V8 q 'menu_position' => 5,+ O" j* }8 X* m$ N9 M
'rewrite' => array('slug' => 'site-wide-notices')
, m4 y; N2 f7 C4 }% ~ );
. w# V" |* r; d, b E; G8 r( }* f a! T3 X# R) ?; X
register_post_type('site-wide-notices', $args);
# E- A7 m: ?9 C& t2 C; | }
; o5 k1 q9 |% h ```, H# a, ?* Q1 f" W6 N
6 X3 \* A/ P g2 M- W 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。' c' X( N+ ?6 }$ V7 G) v
. w8 d$ L: f5 q: u Z3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
3 v' L9 t; d3 b$ i; ~% r! _ R( S9 R& ~+ c/ E* i% _" H" i
```
* j3 D# m: k' Z4 N q% y, s1 t( g add_action('add_meta_boxes', 'add_site_wide_notices_boxes');9 y; f L( w+ C. S* ]: B
function add_site_wide_notices_boxes() {
9 |+ o( }' y) g' z/ S add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
, g$ z" Z( ^8 n/ K3 z8 y }3 F- u5 @& S2 S; }
$ y9 n( S3 q" ?& t& V1 n function notice_details_meta_box($post) {, T& W3 l8 Q/ p# ^4 u" c1 m
wp_nonce_field(basename(__FILE__), 'notices_nonce');) I, _# r6 K+ A4 b0 l8 K1 G
$notice_title = get_post_meta($post->ID, 'notice_title', true);
* f+ Z$ J" X% i+ {0 T* m7 D $notice_content = get_post_meta($post->ID, 'notice_content', true);" o) _+ z; m2 a# H
?>/ m4 H4 w6 U4 f% Z! N
<p>
: W% v; M u# Y6 m# V p <label for="notice-title">Notice Title</label><br>
9 g1 F8 P% J1 _' ~# r$ { <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
/ |" h7 U- m/ f' p! V+ V1 j </p>$ {& N2 i* [/ e0 P/ `2 z2 B5 D( @
<p>
! O+ H, A4 c0 Y# w <label for="notice-content">Notice Content</label><br>
/ h1 B6 W. h: }4 ~& |8 f- h( G) @$ s <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
; @. `* J5 V$ ~( D </p>! o3 F5 Z3 k" t8 ^! b6 H; ]# K7 U0 _$ A
<?php
% r4 {/ f1 P. l1 W- T: E' { }
7 H6 @$ H+ y5 V5 [2 c& ?9 s6 w1 W' ]* a! X. y$ O( }9 g
add_action('save_post', 'save_site_wide_notice_meta_box');& j* B+ f8 Z5 c, i/ w$ S9 ~0 }* N. \ x
function save_site_wide_notice_meta_box($post_id) {. w# Z$ `4 [$ M* ?1 M
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
: H2 b O9 m. @. @ return;" s/ o9 |9 l& K* m( r
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
2 n2 O7 h1 j3 C, C. M9 W return;
8 S( S1 K7 z# c4 N6 o$ i/ J9 t. [7 Z9 G+ p: J2 Y
if (isset($_POST['notice_title'])) {
6 o" l# i$ U& @5 n, U' b5 L update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
3 T5 r8 I$ G# x2 j! R9 I }
. }% V$ S9 |3 X9 V4 @ if (isset($_POST['notice_content'])) {$ ]# s& h$ T5 D W1 n B) h/ v- \
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
) \+ ]: n3 z4 P2 N }# E$ N3 _3 d8 T; z* B
}
8 ~9 U6 t# _! G6 y0 y" |: p/ t" n ```
2 E# Y% G5 z& ?2 N P6 A4 k7 P* x2 D% {& a" j( l+ {
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
" ]/ r- [. o: `! C' B# ?& W. }7 o) j9 `& {8 n8 i
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:; B! N% G4 H4 L- ~) W" o6 O) G
# [* k/ r8 {9 o" s- T6 r
```: Q' `3 G0 s Y" H7 F7 l+ o; m
$args = array() [% j" p' c. V3 c5 p O
'post_type' => 'site-wide-notices',3 G- J5 p. Y4 v% J
'posts_per_page' => 3,( Y, K/ z7 h% c, @& `! Z
'order' => 'DESC',
; o. C% S* t0 F6 j' D; D. J8 u 'orderby' => 'date'
) P; v5 t3 E8 f) ]' D. c# S );6 M2 F6 f7 @; F! k. V$ ?; c' h
$query = new WP_Query($args);
. ~& S& E6 x" W if ($query->have_posts()) :3 N8 n5 f4 ^7 x+ u: n0 O
while ($query->have_posts()) : $query->the_post(); ?>
, ^ T7 S" ]! ? <div class="notice">
! X6 g- T# x( z# [ <h3><?php the_title(); ?></h3>- _& |6 v$ m1 q( S
<div class="notice-content"><?php the_content(); ?></div>6 ^3 q% N5 k9 b2 h; U
</div>
9 N2 {: ~; p# m7 p/ K4 s <?php endwhile;
% s: F. q J; R+ F9 C* u2 K wp_reset_postdata();% h/ r+ ]" u: |; p4 C
endif;
2 }# [4 S8 |% @ ```
+ C. S# [" h7 f# J1 d1 _3 m2 e4 D' D
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|