|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
) E C7 x, d3 j. K* c; G! j3 }# y9 y1 [& t5 Y5 L
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。: ?5 t2 j) x4 f$ C3 ^( N
# N& m. s5 W5 s以下是创建自定义插件的步骤:
& |1 z2 f ]# T% e& J
; R, m$ m/ W- F( [1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
! i$ w" I& K& a! Q) g* Y& q: `: F1 D3 Y# ]* ?2 ?
```
" g& ]6 \. X( E <?php. r' N# E+ j( f# i0 h* f3 E% D
/*
+ H) s0 p2 W5 Y. s$ E Plugin Name: Site Wide Notices Plugin7 w7 s4 |6 x* l; z2 j2 ~$ d
Description: Adds a new custom post type for site-wide notices.
1 k4 W" x# d1 T+ H Version: 1.0
0 b) U0 c9 P. H* M Author: Your Name% H& i2 \& }6 b% }8 B
Author URI: http://example.com& `) S$ D) r5 P
*/
; g7 Z0 o5 S% L/ H2 ?
: i, B$ J+ x3 ]( E: d; D // Add plugin code here...
9 t0 G2 R X; \! N" b2 w5 N2 Z ```
& K+ Z1 q* l: S5 W' R/ @% V/ k+ l; w, ], m% z1 n# _1 k P
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
3 S" P4 |3 } [1 E, ?. `' T+ O
, A, G5 b7 j! g5 T2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
2 d. E+ }, N* g1 y0 g" ?
: z6 h, e5 T4 ?5 O& |" E ```5 }. R$ `: o2 y$ |- s" a
add_action('init', 'create_custom_post_type');9 {% [, u$ M4 M2 `6 @& m
function create_custom_post_type() {
( \4 ]8 k r [$ o $labels = array(
4 f; w0 Q4 I' O$ ^ 'name' => 'Site Wide Notices',
6 \/ Y9 s _, C E9 i8 s: h 'singular_name' => 'Site Wide Notice',9 }& C- l) ^. X5 \& W$ [9 x
'add_new' => 'Add New',; J5 D3 X2 A% G( \9 K6 c
'add_new_item' => 'Add New Site Wide Notice',
1 b1 a% G j2 s9 B% f9 f/ G& ? 'edit_item' => 'Edit Site Wide Notice',
# m: F3 U, n Y- Y 'new_item' => 'New Site Wide Notice',5 X/ U% Q& Y& W5 ^* }
'view_item' => 'View Site Wide Notice',
5 v' A. ^, |4 G0 G: N' t, @ 'search_items' => 'Search Site Wide Notices',
* a; r2 D8 u- s$ k! O) J 'not_found' => 'No site-wide notices found',
; ^+ l7 V. B2 [% H9 ]! \ 'not_found_in_trash' => 'No site-wide notices found in trash'
, d5 g1 w& g" y" S* g3 p2 J );
: Y8 I' J* A# h
' n( D. h; J1 u9 w4 }1 |. |% y $args = array(
5 {- O( ^. u* ^ 'labels' => $labels, a1 ?! t# \ A' k
'public' => true,
0 \) z1 w2 N, F, W4 W% f9 q& b 'has_archive' => true,. H! j, U7 S0 ?; d9 T. R
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),. i+ X* ^6 _- R: i( o
'taxonomies' => array('category', 'post_tag'),
% b7 F8 z. s& V* c5 s1 q 'menu_icon' => 'dashicons-megaphone',5 n) }! X( S) w( {6 k
'menu_position' => 5,# E. Z- P2 J. b/ o: q4 _
'rewrite' => array('slug' => 'site-wide-notices')
" B& B- C) D6 A R9 R: M/ K r; S );2 y" ^& ?' ?: Q2 r) g) \( p- ]
5 Y7 O9 x" N: Q' S+ \4 E register_post_type('site-wide-notices', $args);: L3 q! \0 D& r' P
}( k+ z4 t( Z4 e1 U' Y# K
```
6 I& ]' x6 I6 O/ g" Q5 b7 L7 t1 M' y% r: L- I" Z' N* Z9 c. W
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
7 V9 \% D$ G0 u! E( S- ]( O) z* A- o; @$ C. w, m
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
4 I8 a6 g& {- x$ e$ ~8 Z3 h3 \$ m0 c( M. o6 q
```' Z- U: j; D! P+ X; r+ A% F' G
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
9 K2 S5 t1 [0 E4 {" ^ function add_site_wide_notices_boxes() {
$ Z0 ^3 H8 N, R$ v" F; }( E/ A add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');" E- `- V3 ~7 o. j9 g4 O7 e* X
}: M7 M* w5 a. V! j0 j4 ~+ K
6 W8 R3 u! x) X function notice_details_meta_box($post) {
, ~3 R6 h/ f6 h& ?! ^ _7 a wp_nonce_field(basename(__FILE__), 'notices_nonce');
3 J7 r' x6 M: x+ S K5 x; T- x8 t $notice_title = get_post_meta($post->ID, 'notice_title', true);
% q# z; T1 }' F! ] K; { $notice_content = get_post_meta($post->ID, 'notice_content', true);0 o! {) z* [( L& U
?>
) W. F- K! R2 `) U( {) e! D1 G <p>' ^& ] @5 V+ A8 e
<label for="notice-title">Notice Title</label><br>( J, M# i7 W8 m2 _6 E4 N: A! l" b
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
# _- m; `# U2 @2 t$ N/ Y </p>
7 D( Q* p/ m5 t" @6 t2 V <p>3 B" `3 M, c8 K; E: S! ?5 K
<label for="notice-content">Notice Content</label><br>
& b& }. E; U2 I$ r <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
5 o0 W# V. K- c1 B* m </p>
0 o9 o! p5 k% }1 i$ k$ M <?php
t* h: G. f+ M }
% d4 h+ K" l2 d$ ~" V# Z; B |5 }# Q# ^4 }
add_action('save_post', 'save_site_wide_notice_meta_box');
. {7 ?& T$ R8 a function save_site_wide_notice_meta_box($post_id) {% O* [6 t, ^/ }: i6 s( k- r- H. W
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))) [" l( Q! D, V
return;
9 V! a- T+ o0 Y, f0 e if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)4 S8 ~0 V* W# c1 e
return;
# `, @: k0 m) K) T! y Q% j
5 m: r8 Q% j/ p if (isset($_POST['notice_title'])) {
3 o: e" N. x, }. i update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));( [. P1 S9 t5 m( g/ O
}* `% n* V$ }) l- [) j1 A
if (isset($_POST['notice_content'])) {9 H W& v3 q7 Z& Y
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
/ X" Y0 O: y9 V' a }
# A/ L" j) N$ R; T } m: Z$ q# X+ A; h8 Z* S* U
```
0 @0 L9 H, _; V( z. e0 s8 z' B; O. C9 G$ m! W8 n4 U
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
2 ?5 G# s1 {" q# w7 B1 Z% f+ r$ x* j2 K9 O" @
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:6 V0 e j& w: Y3 E
4 B" N& S! }3 V9 f# x ```* q. z8 _, L5 b6 J4 X: y
$args = array(
8 x5 V; Z$ @! i+ e3 y0 C+ j) n 'post_type' => 'site-wide-notices',
1 z5 z4 Q* M6 E/ q9 W( E9 I/ g 'posts_per_page' => 3,
- P5 p: G1 C, d% N' ^8 O' _6 i, A 'order' => 'DESC',; ]" p/ h) ~0 C/ f7 d3 H n
'orderby' => 'date'( Y' Q3 e1 W! K' Y
);
. R' ]$ Y" x, E6 _. z $query = new WP_Query($args);
, H' n, ?+ d" x# | if ($query->have_posts()) :
u% m% W3 \; R7 T+ m( k! ^ while ($query->have_posts()) : $query->the_post(); ?>1 k& _) Z% L# I7 H3 ~
<div class="notice">
' x# H% K$ ~. y/ t6 y <h3><?php the_title(); ?></h3>
/ I k$ A i2 W$ R/ P, m( U <div class="notice-content"><?php the_content(); ?></div>$ [5 A# I1 s3 D2 l5 g2 Y1 h/ K
</div>7 S; l J G) \' x
<?php endwhile;9 R- K- ~0 v+ L" F* H$ d
wp_reset_postdata();
& f* S( G& A! U$ m) a endif;
1 J3 Q* X8 z6 M1 S ```) [. @1 I9 l* J6 ]! k8 O" l1 r
- s3 ~0 i) Q% i% b2 m* X: t6 R
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|