|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?* p# R6 r4 \+ |/ v' {$ {$ R& f
& c' H: P$ I0 o/ x t
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
' b9 S6 `- l) @$ B! k2 }+ l5 a9 E3 N$ ]0 l6 l, ]
以下是创建自定义插件的步骤:
2 U$ y% ^5 l. f% w# g v" B5 l$ M& H1 i' h
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
+ Q& ]7 w2 r2 N1 \8 ?! a1 j& M+ i2 @6 O& F( M& a- d8 ]
```; t/ M @8 `% ~% i9 L
<?php
8 y- d. q& S% ?2 d" @' J /*
/ k2 \. I6 C f" L" o7 a Plugin Name: Site Wide Notices Plugin
+ A2 z: d# V1 N( b' m7 ]. |* h Description: Adds a new custom post type for site-wide notices.- ]' b: l. _3 v, ~5 k% m. Y
Version: 1.0
8 F/ h+ e: R& I' G Author: Your Name+ n' Y+ K, m4 R0 w, }' n
Author URI: http://example.com
/ g, ^1 f5 K5 u- h: x3 p% n' M) } */! o, L" ?+ X8 Z/ b$ d
6 D# v2 C+ N6 A* Y8 F, m8 ^, ?
// Add plugin code here...5 ^6 d+ `7 m* }
```6 [7 c/ g7 N: z' o% g
% g7 A' D! a1 Z& s 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
. {. x+ g( C, r" Z/ j5 ]5 i- y9 @2 N* V3 Q. F; V' Y+ @
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
( H4 Q( Q' s9 w+ i0 l. z, `+ u2 {6 u. Q; b, t
```* p/ L( A# Q0 ]: s
add_action('init', 'create_custom_post_type');- J" E7 l8 Z7 ]- L$ _
function create_custom_post_type() {0 d6 k5 h7 M9 M5 o/ S7 u
$labels = array(0 l/ L' H0 @4 k" H% ^7 q
'name' => 'Site Wide Notices',$ ^- U) e" [4 a& D) n3 T
'singular_name' => 'Site Wide Notice',
+ h0 x, P) E8 }% v6 |2 L; D 'add_new' => 'Add New',5 l: |& r% O% R0 h6 K; h
'add_new_item' => 'Add New Site Wide Notice',9 u0 H3 b# S8 @
'edit_item' => 'Edit Site Wide Notice',- m0 a# O% C+ \1 M z; d, \
'new_item' => 'New Site Wide Notice',7 F6 [+ H" h. O" p: B, Q
'view_item' => 'View Site Wide Notice',) o; s5 h- ~5 ^6 c% G6 i
'search_items' => 'Search Site Wide Notices',+ O; V1 x/ B. d* G- a F$ P2 E
'not_found' => 'No site-wide notices found',( ?/ @ i: P7 a/ T! j
'not_found_in_trash' => 'No site-wide notices found in trash'; z0 p, d# _& [5 I$ t% H6 Z
);% L% R5 [# p5 O4 `
/ s) c1 W) `8 ^: _! i$ Q/ k* f $args = array($ g( ?1 v* t& R( i3 o: B8 `
'labels' => $labels,
' C- g+ _, ~8 } 'public' => true,3 S5 `( O5 }$ P2 B* i. j t
'has_archive' => true,0 r3 g+ X% o% c. B% L) d( V
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'), i% P# y2 ~& G @* M
'taxonomies' => array('category', 'post_tag'), |# O3 p& ]) ^
'menu_icon' => 'dashicons-megaphone',
& }+ [' q: V' s. I) \ f: q4 I; L. I 'menu_position' => 5,
; j' l3 f3 L( r3 ?7 u* u( c4 I+ ^ 'rewrite' => array('slug' => 'site-wide-notices')
# R: p! O" M; K0 c0 e' P5 K: ` );6 V1 G7 |( L# k5 V
$ a6 _% A# g8 g9 c* U+ N register_post_type('site-wide-notices', $args);: D: Z0 O6 b: o+ x5 v `! S5 [# N
}; `5 M% w. [/ v/ v& n9 x% v
```+ B5 X% w9 F4 S1 ~; h% D
0 u8 r% ?8 G1 s! u
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。5 v, X' `, b# D$ l: B4 Y$ I
8 X# K, [+ ^" K* \% p! @
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:+ [1 {* C$ q- i
) p8 \" I( o+ o$ b( B* a6 U ```
2 }6 m# @5 h" [ add_action('add_meta_boxes', 'add_site_wide_notices_boxes');( V/ w {) m3 `" R
function add_site_wide_notices_boxes() {1 C( b2 A. o* Z, `* u% c1 i9 y1 v
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
5 ]9 s- C7 W5 T- _ x0 l }
0 B( r& p% Z! G8 G0 ~' X
5 L8 @! i3 j- v function notice_details_meta_box($post) {% V( R# j+ _. o$ E
wp_nonce_field(basename(__FILE__), 'notices_nonce');; e3 w$ w0 S O; i
$notice_title = get_post_meta($post->ID, 'notice_title', true);
/ X! _9 F& O0 p $notice_content = get_post_meta($post->ID, 'notice_content', true);
% k$ }7 O6 E6 E0 G. i ?>8 A5 d9 D' x4 O2 n1 j" y1 A
<p>
' Z! N+ C. }) n b <label for="notice-title">Notice Title</label><br>" B3 k# X0 o% h. U& N
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">5 p' w3 x$ e: T
</p>' V7 }2 A% z7 l B8 H
<p>/ ^1 C& p7 o! z( X2 c# d
<label for="notice-content">Notice Content</label><br>1 q; C* ]1 S- R5 e! b
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>4 r% }" D* c g( E
</p>
6 v( S; @/ W+ A2 l <?php, H z- }9 h) S4 t2 I' Y
}* U7 d0 U: r8 }
6 s* L1 y! g4 g8 Z$ _3 u add_action('save_post', 'save_site_wide_notice_meta_box');
7 }' M; d( E* Z; I _ function save_site_wide_notice_meta_box($post_id) {+ |" E6 E1 S# u
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
/ j% K# S) |/ }5 q: D/ ^ return;4 k, t; d' s$ C, X! T; y' c1 m
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
* k$ V3 t, j6 D& C9 c" N: X1 O return;* X2 ^" Y* x& ~: V1 s. H
( a* s/ f6 I) @/ j
if (isset($_POST['notice_title'])) {
) u# b. t z9 t! g update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));) @, T% ?1 e7 z6 a7 N. M
}
# C$ w1 o& _" j8 f2 |% q7 x if (isset($_POST['notice_content'])) {" q0 S m$ {5 i/ [
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
& z8 w- c0 Q- j7 h$ x }: C6 U, f9 z3 N& {5 A
}
: }! [* u5 s! R6 x/ ` ```6 |" g5 N% O* [
' H) P }" m) T: ~% r 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
8 j) C H7 D' [3 ^6 B9 b# [1 S$ ~. L/ k& a
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告: ? D8 v! I8 H3 n4 n* D' k
8 n0 N, D6 J4 E2 p$ Y
```
' W" v# Z# K, [$ m1 ?' _) b' d. a $args = array(/ S5 O0 L( v6 M0 o
'post_type' => 'site-wide-notices',0 t$ W) m8 {/ J& b/ i
'posts_per_page' => 3,2 x* A9 Q' U% o/ H
'order' => 'DESC'," e2 R* t. \& g! D0 t( f) E
'orderby' => 'date'4 y% ^# k9 y* g8 \) w) ~
);
! i* X1 u. W; Z7 f $query = new WP_Query($args);
- ~- x! @- d' y% d if ($query->have_posts()) :
. f& y L e: h4 o! g7 M while ($query->have_posts()) : $query->the_post(); ?>, r: w" j( ^8 k# K4 c3 [; h
<div class="notice">; T( g) R& z6 V9 |
<h3><?php the_title(); ?></h3>
0 T/ R0 a! Y2 S3 A7 B <div class="notice-content"><?php the_content(); ?></div>& n0 V3 |1 `7 Z* g1 {! T
</div>( `, H: n7 z2 G$ D3 H
<?php endwhile;
& b h! P/ E. ^1 I) ^4 B wp_reset_postdata(); O T1 k! ]" q0 F
endif;, K1 [8 X2 S7 D, G% H( w5 r4 C+ ]
```3 w" F! u0 m5 ~( w& \
' n& ]4 c' l) c8 Q& A7 f
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|