|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
8 u! W* Y: V8 }$ P6 L. q7 V! `" j$ Q4 a) t7 P
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。9 _5 j" Y) f( o& y' C6 s
/ H* j { E( J( }
以下是创建自定义插件的步骤:3 Y1 S |1 f& i
7 }1 t b& w0 l6 R( o* T
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
3 i, ]/ T+ q% Z5 q* q7 t8 f/ K# ?* b0 n1 e- C5 b
```
$ ~- y/ P$ l- [# A <?php H5 Y, X4 J3 L( _
/*( ]* A4 H7 Q; J
Plugin Name: Site Wide Notices Plugin, r. a9 l0 [7 R8 u
Description: Adds a new custom post type for site-wide notices.2 N& ^& i. [% s. g
Version: 1.0
! |+ T1 K: o7 K! D/ E2 e Author: Your Name
' P0 {' p! y0 ^7 M) y) G$ k Author URI: http://example.com% B9 Z R2 }$ u
*/( e6 @$ ~) ~3 y" V- n6 f' G$ U
4 l! V: E; `7 H0 t
// Add plugin code here...
3 y% D3 ]* u) V ```: g7 Q7 E( g, r5 }6 d* a1 L. S
' Q* P% q3 Y7 _3 U8 M) `; @ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。* ^! c ~4 ?2 L$ E& F# Q: f
) [, _5 F9 Z1 A7 K& G2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
6 P5 V" ]+ e" u* E0 m) [
i6 X, f: `( s. d4 f$ h3 k ```9 Y" o! N- V$ ?
add_action('init', 'create_custom_post_type');6 H+ S% \0 a% v* F
function create_custom_post_type() {. Y H4 v# C) P' s) F, _
$labels = array(
! c4 \9 t& z0 w8 d+ G$ c7 a 'name' => 'Site Wide Notices',
: B2 Y( z/ ^; t 'singular_name' => 'Site Wide Notice',% K! R2 ]# J8 `6 M- v8 ~( ^
'add_new' => 'Add New',0 h. n, g4 M; l5 V( N4 Q
'add_new_item' => 'Add New Site Wide Notice',' w, e! r& f1 }) G
'edit_item' => 'Edit Site Wide Notice',
- z3 r$ A, R7 f/ u' {) h, R 'new_item' => 'New Site Wide Notice',$ j5 e# Y) y, V e
'view_item' => 'View Site Wide Notice',: J2 g. Y, |) P" d
'search_items' => 'Search Site Wide Notices',
: i" M& X& x& a, c9 P2 h/ a 'not_found' => 'No site-wide notices found',
/ A; J/ P: _( b/ m8 R$ L 'not_found_in_trash' => 'No site-wide notices found in trash'% g# z9 i! t S6 Z
);
# H5 [1 ]( j) }1 E5 q
$ N1 R, {- @- |( ]7 D' r$ ^ $args = array(, O: N' g6 O% U0 |+ Y: F
'labels' => $labels,
0 n- X; |8 Y: c) I+ w8 D 'public' => true,6 m- I" B4 ?8 u- |8 M
'has_archive' => true,! ?) }! X9 V8 N: h! ]* F; m1 B
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
9 i$ z2 {% i# f$ [, t: k 'taxonomies' => array('category', 'post_tag'),
. T ]' p" P4 Y$ c$ Z, e$ D 'menu_icon' => 'dashicons-megaphone',
9 ^) B; L7 v4 [ 'menu_position' => 5," C1 m- N) p2 e, \; F6 I
'rewrite' => array('slug' => 'site-wide-notices')
4 u5 @. a+ _/ b" {, H3 S+ f/ n) |4 q );% I0 T$ k8 `( u* l
. J V$ Z0 v" ?: r7 m: Y! z
register_post_type('site-wide-notices', $args);" q) p, `, @$ w
}
1 W5 A9 X; D( S) q+ d) G1 O+ y. T ``` f& t% K: Z# F+ T$ Z$ Y. }% g
/ I: x! w. s, {' z+ }& V 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。3 C& O# W" k5 Q0 [! O
/ H- r6 b/ h$ J1 t( G3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
) y4 E f" p! A( |; b0 p) }$ ]& N+ a# ?4 L7 a Y1 q
```, _" K4 q4 u6 N; ^: Z% m" O
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');) H& O$ D, K1 D/ R7 m& L
function add_site_wide_notices_boxes() {
: a0 [: }8 S" x3 }" ~ add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
2 Y- ~: v8 v0 x9 B1 a% V }
8 e! A* [+ V- q8 V: U6 U) s a/ ]. v' G. `
function notice_details_meta_box($post) {
. ^9 C' |; ]3 i wp_nonce_field(basename(__FILE__), 'notices_nonce');
( A1 w3 O5 R* z+ n+ q $notice_title = get_post_meta($post->ID, 'notice_title', true);
* l& f$ _( q+ T6 o/ j0 ~ $notice_content = get_post_meta($post->ID, 'notice_content', true);0 B8 {, `! S" s; h* L- S
?>% ?- h8 O# d; O2 H# ]
<p>7 E0 O: g& F6 ~
<label for="notice-title">Notice Title</label><br>$ f' S2 x0 T- v# I' X( l
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
* o* h/ Q* Z8 P' t$ A9 O% | </p>) k' H7 l7 t- B, P+ j
<p>
/ ?5 P2 w' d6 h. h <label for="notice-content">Notice Content</label><br>5 p- J( c+ F" r l
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>0 k3 ?. n) G! @9 O# D6 t
</p>6 F3 J' _+ j$ D. H
<?php
9 `2 P" X! ?$ x. U. d% \ }2 ~% o, S* J) B* C# {! b
( O6 c* T# B4 F! F a* e* } add_action('save_post', 'save_site_wide_notice_meta_box');
/ l6 ~ K+ |/ h! Y5 {% |. n- E0 z$ B8 Q function save_site_wide_notice_meta_box($post_id) {* N3 c. r# b, [. I
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))" |/ U L# c2 \, L$ H
return;
; m# t6 |$ O9 `% x! d7 s if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)( _1 r* c4 S3 B* r
return;) m) c: r; W4 ?$ N' U: e$ l
. V. Y# Y i! h3 Z1 X/ U. C% Z ~
if (isset($_POST['notice_title'])) {. N+ l; q' \0 t4 L6 y: V3 Z& y
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
8 Q' T1 [) C1 V. |- e* g }
" I3 c+ j1 A: C1 d, w5 o3 V" S, A if (isset($_POST['notice_content'])) {
: }: _2 Z7 T5 H1 r; m3 L" s update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));* k$ s& D0 L. u
}
I$ r$ s4 B0 \3 `+ x* O" Q- v) W# r8 X }+ P: `+ O' h U0 b- l6 Z# S
```
4 _0 |- r, u4 @7 q7 @! V/ P+ s3 L* I; x( _7 v9 G P
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。& o0 n3 g; I+ R# B
' l2 o: k" x* X7 V$ ~4 n4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告: q5 C" ?, c, p1 n" B. H
; R' Y* _+ ^6 O4 a0 t+ ~/ O- Z ```
+ X+ E8 b% G1 y) c; g* ?7 Z $args = array(9 T4 D9 _# {$ q% \- A
'post_type' => 'site-wide-notices',
- a9 C7 ]3 p3 A 'posts_per_page' => 3,$ `# K" T% i2 L: D5 C" X. k
'order' => 'DESC',- x" E& S+ e R8 y+ `; t1 H
'orderby' => 'date'; O1 I! { m1 \; t" N' y
);# Y, @2 T# b% Y' h( \: B
$query = new WP_Query($args);0 l4 e) c1 P% z7 O- o6 H& a
if ($query->have_posts()) :
7 q0 w6 O9 w# h8 U e while ($query->have_posts()) : $query->the_post(); ?>: t( j; e) K% W6 \" l0 {% i1 U. j
<div class="notice">
3 ^( F" g/ k8 y2 N <h3><?php the_title(); ?></h3>" J5 ~% g& j4 z/ h
<div class="notice-content"><?php the_content(); ?></div>2 B# ` a3 _1 i! c# ?
</div>5 h$ n, n% w2 d( z" N, k' T
<?php endwhile;2 k" e5 K9 ^( K. L
wp_reset_postdata();
. B6 o! C- i7 v- r, a endif;
( b m7 p9 P6 `8 I3 d ```
4 R! _7 d1 f4 Q& k- p2 n( ]) \, e, x. g5 e7 l1 N; d! m
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|