|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗? k; t( a8 p M" H
& _* y6 Q, ~ ^ d如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。$ A# u9 z# F/ a ~/ k; h
! T1 y% F& x9 o* K" K4 y5 \; S以下是创建自定义插件的步骤:
; [3 @: q' c: t( u% C( m+ a2 ]
+ [* F/ X( e0 G' V0 K$ @1 I1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:0 B# B {2 h7 ]3 f) l
6 F; z3 b/ E' a" D9 x" T! f4 T ```( y$ R$ e" T/ S
<?php
, Y& L; m+ h: I3 H2 Z9 l# F y /*! B/ M3 A% d% D
Plugin Name: Site Wide Notices Plugin
' c, m8 O6 n! l* E Description: Adds a new custom post type for site-wide notices.
* L$ z" `. Z7 T/ ~ Version: 1.0
9 t. j6 d: ]; k2 v Author: Your Name5 H1 |4 D5 Q/ l7 |- ^7 h
Author URI: http://example.com+ e3 H t( R1 k1 W3 C5 O7 D- H
*/3 s$ {; F6 J- j. I( Y
$ s9 l" m' e2 r; l* m8 x
// Add plugin code here...
c! z: e; w- s4 R: N; O: R4 @- I) f ```
3 E# F. d; S. D) V, p' m2 o+ z* J/ J5 H' B. K3 A$ S ~
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
9 P/ A% T' q$ S. D$ L/ P% ]1 o; ^; b' [
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
3 n6 j1 r5 M, P c W. s& ~+ H
3 \5 q. p& l8 b8 M$ g ```
2 J; m! P2 L! G( c [% c! J# A. C% y add_action('init', 'create_custom_post_type');
- G7 i7 W; s7 h' H. ~8 u function create_custom_post_type() {
: g0 `) D" Z( t$ M( M8 D $labels = array(
$ k7 H3 @1 |; ^! w 'name' => 'Site Wide Notices',% ?7 h; i- u1 Z" t; V
'singular_name' => 'Site Wide Notice',9 ]. i- H. t- m/ N6 j& b% R" C* G
'add_new' => 'Add New',. a' q# E4 y4 I" Y) U, R' M
'add_new_item' => 'Add New Site Wide Notice',/ N3 k; B) l! e {8 b! @2 l
'edit_item' => 'Edit Site Wide Notice',( l& N% f1 z4 J8 t S) Z2 X
'new_item' => 'New Site Wide Notice',& k7 W. [: J, n
'view_item' => 'View Site Wide Notice',5 ?9 O, L& D8 G9 @; {
'search_items' => 'Search Site Wide Notices',
4 ~; G/ X- H( N& u* N3 ^1 k0 _ 'not_found' => 'No site-wide notices found',/ `( {) g# Y: W T( u
'not_found_in_trash' => 'No site-wide notices found in trash'
- V+ g5 { G! v );
, }3 ?$ I( v' a) }- d% K# C
$ O5 y' h) Z; q' e: ^$ O $args = array(8 x v) G- H% ^
'labels' => $labels,
& n, m! z/ @" M 'public' => true,0 K4 w2 S0 S5 z" D
'has_archive' => true,
6 Q4 [8 o& c& G. P9 v9 k 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),& M" O" I |' ~" a5 ~
'taxonomies' => array('category', 'post_tag'),
# U4 ]# X& ?8 \6 t/ U- c$ g: I' Q 'menu_icon' => 'dashicons-megaphone',( O& S( n S" @# g4 H( k: Z% ?
'menu_position' => 5,
2 N1 i- @. F7 [1 Z/ l% j$ h; Q9 M 'rewrite' => array('slug' => 'site-wide-notices')& @' { @/ e7 ?$ p1 i, x# I3 D
);7 q: q) y. |: Z! A% A) S& M. P
3 e2 i1 F5 f. Q6 q' J register_post_type('site-wide-notices', $args);
* f5 \5 o( c# k7 S) G: A- N }
' N' B% f1 N4 u( M O% b* D r# M ```. ~- x: W0 @9 s$ ?4 g. f* n1 J
/ p7 y1 p. S) K: M) s
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
% k- g4 O/ ~1 t/ a, d# p7 N5 j$ K! I, n7 [. G
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
2 ?9 x) R$ U: F/ y( k
9 d* ?+ v6 I9 I$ Z/ l ```6 W! g! `& k3 _
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
+ l' }7 U4 ~* ^2 L2 q function add_site_wide_notices_boxes() {
# K+ q% ^2 P, i- }/ E$ A add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');' y- {# ?/ b8 Y1 E6 Y' g* |3 p+ o1 [! B
}$ A; Y6 @: \# I; ~/ V
: C& F9 d$ ~8 Y& [* ^
function notice_details_meta_box($post) {
1 F' d% d) G; @: u2 U3 ~' s9 N3 n wp_nonce_field(basename(__FILE__), 'notices_nonce');) X! t9 S, z, {3 x- O
$notice_title = get_post_meta($post->ID, 'notice_title', true);
) w. f U! J. {$ h6 V $notice_content = get_post_meta($post->ID, 'notice_content', true);% O6 m: L6 Z, ~2 f( ]. w8 e
?>
. L7 _) ]- f* I( [9 u3 t <p>
! Q9 D v' y" u6 Z# D8 s5 A! l0 u K <label for="notice-title">Notice Title</label><br>: b, O' q" Y' Z* h/ _9 s f
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">& ~% n- R) f6 T/ P @2 H
</p>5 O5 S& f$ S: Z( z' D
<p>7 }8 p. ~/ k1 x: x7 C
<label for="notice-content">Notice Content</label><br> i) x, p N' _4 M
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
# H8 i/ D3 X: G' z! G( ] </p>
5 Z1 L2 B3 L' i' O <?php
) T! O8 `7 h- @1 `7 ]- D }3 }# ~* d# u. J: m& l( w
$ S1 m( ~! r* d" _% p2 Y* h" [
add_action('save_post', 'save_site_wide_notice_meta_box');
3 _7 Q# d* M! x0 j; V7 v d4 y function save_site_wide_notice_meta_box($post_id) {
. _8 G. |+ x" ]- {; } if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))6 |8 y4 A% ]/ t1 G/ r
return;2 ~) a' h! @9 P4 m
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
1 \* a6 u! x, @! z8 Y' @0 M$ O return;
, J1 F. a7 c/ r7 u
8 f: V8 S7 Y3 C d0 N, v if (isset($_POST['notice_title'])) {
9 }* q( p! h! l: A update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));+ L# i: f# M( b: I
}
: ^$ \( H9 D1 p6 X if (isset($_POST['notice_content'])) {- ^# N9 c- i# \ @% S# X* F% V
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
' v5 m' |; D9 O% l6 d }
2 J) Y% R1 }3 P5 V- M$ [2 D }. h' d. p, n5 z ~: j7 ?
```4 I& a4 c& F: Q- _# s
; m. @8 I- _8 y: ` 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。- C( W. L* i5 t9 i! s2 V; H
! f8 X4 K1 q) X- b3 f C
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:; k# ~& u8 L! L& g
, V( c9 ]+ z# s6 C0 k% B }& o
```
* D7 A7 _9 x9 b4 z $args = array($ P4 N2 r0 B+ b" L: ~2 g" n
'post_type' => 'site-wide-notices',
8 {7 ?+ ? E# T 'posts_per_page' => 3,
$ S5 j, y. {) m; A" _ 'order' => 'DESC',
5 B9 q0 {, A7 I3 M* ?" k! T 'orderby' => 'date'5 ?: r) s& ^# ^! v! Z; \7 S2 \
);3 j% ?) ]! V4 C
$query = new WP_Query($args);; v: }$ N) I# [6 c+ Z' p% h' R/ |
if ($query->have_posts()) :) l8 H- M( @; n, V+ O, N
while ($query->have_posts()) : $query->the_post(); ?>( E* \0 l; n1 b# e
<div class="notice">
9 I& ?' c( `5 u! [ <h3><?php the_title(); ?></h3>
2 L; z& n! b; O& p# z; f* \+ U |# A* V <div class="notice-content"><?php the_content(); ?></div>( I( J- ~0 }6 E6 _* m+ G
</div>' s8 e; Q& c7 w# B
<?php endwhile;
: n1 b5 R" K$ }% [9 \# u! l" M, K wp_reset_postdata();
5 O7 A2 d8 S$ q$ W endif;& ]$ _/ V) o! m9 ?
```- K5 O! l9 h- V7 A) v4 d- g: t4 t
0 A. ~) m* Z3 Q0 D. \4 C. e
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|