|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
- I: g! T [! X) t
+ d5 @3 Z* Q% m T# Q* {* N4 P如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
$ p1 w$ V# v0 y4 c: C7 a8 t* C/ ^4 j
6 h. a& N+ a Y/ P8 F7 H. G以下是创建自定义插件的步骤:+ ?& ?+ Q, @8 g& l
) [# f- T3 P/ P* u* b8 S* U, _. M1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
. {2 _# N/ \& ~1 E. C
0 o% V9 c$ R8 O) H1 W' U ```8 K, O7 s' f7 I6 X9 O
<?php1 d) ^5 W5 g Y9 [& h, c: b
/*, K' d. G- ]8 |
Plugin Name: Site Wide Notices Plugin* W0 A) F* J! r% G
Description: Adds a new custom post type for site-wide notices.
1 O: T. n# T+ r4 A8 e( A Version: 1.0
2 o S2 V" H% X/ } Author: Your Name+ L# I+ G+ a" n# x7 W( R
Author URI: http://example.com- A) R" V! h+ q% _8 A7 `. p5 W
*/5 K8 D9 V6 q$ f. @0 `+ r
# m, ~5 E- V9 i( M A q8 _ // Add plugin code here...
7 b" }: O. `; o( g4 k ```' {; E, O. t1 y L, Z
; S& l7 M M, @% ~+ f% | 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。( }9 R$ K/ C; d% [5 ?9 K+ L! b& e
! A6 T# g9 k5 j5 o5 D
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
$ B/ ^) I7 P$ k, C+ p$ Z- K, x: m' N# O! p, U
```' m1 N% p( l$ x- u O# ?
add_action('init', 'create_custom_post_type');8 ?! O, X9 ?" `4 [: [
function create_custom_post_type() {
0 I7 J8 @) @$ Z' l $labels = array(
; E! ^4 Y+ M/ x3 v: ` 'name' => 'Site Wide Notices',( J8 q9 v" @" @0 u9 c1 j
'singular_name' => 'Site Wide Notice',
$ L- h8 n. w1 r5 J9 p 'add_new' => 'Add New',' ]+ I& }: o8 s# |9 u8 _: q. M
'add_new_item' => 'Add New Site Wide Notice',% W& G4 x( U4 E1 `/ `4 ^' w
'edit_item' => 'Edit Site Wide Notice',
8 w B) e7 G8 O& R, D( J 'new_item' => 'New Site Wide Notice',, o' ?" K" R' g
'view_item' => 'View Site Wide Notice',
e3 ~5 ?% h# s1 B* a 'search_items' => 'Search Site Wide Notices',
2 O/ t( I) r: c' x3 g# w" N 'not_found' => 'No site-wide notices found', n' G) ] h- `0 x
'not_found_in_trash' => 'No site-wide notices found in trash'# N- Z. U6 x, k! O1 V' |
);0 w0 p: I+ M7 ?
S0 y6 |5 X/ R5 H $args = array(1 i: H' c* j. g- r: K
'labels' => $labels," g3 J" P% Y. H% S
'public' => true,
8 L* S) h& O y/ _& L 'has_archive' => true,
7 ~8 B& Y8 V( h5 K9 E+ Q1 S) { 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
8 h" o. W" S9 Y% q: H' x J! q1 e 'taxonomies' => array('category', 'post_tag'),
& E, ^! `( J) e8 P 'menu_icon' => 'dashicons-megaphone'," E; C4 V n s) |8 l" R9 {
'menu_position' => 5,
0 `) R6 t4 \5 ^; @8 h- [5 g' d 'rewrite' => array('slug' => 'site-wide-notices')
! `7 m, X( x3 [3 j/ _' z3 J );
, X W8 a; A* c0 B) s) f) \. j+ w9 K1 J$ g q
register_post_type('site-wide-notices', $args);! w+ I* \. _. N8 u/ |
}
$ ^7 V5 {7 z" V6 z ```
5 |8 i0 @, k" F @- b9 X, S% O0 `6 d
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。9 }7 i9 S3 a# e/ U! j, W
R9 h L' g: q1 b; d8 z4 {3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:; _1 L! B: h5 O) C ?' }- s
6 J& F) F' }4 `' k7 P0 C ```
9 ^: j7 z) c' W+ y add_action('add_meta_boxes', 'add_site_wide_notices_boxes');% W+ p8 M' P2 U- _
function add_site_wide_notices_boxes() {
. N, J- G% f5 v add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');" a! Z( G+ D p/ @9 w' Q, x: a: f
}
% D" ^% j) j* {( t0 i
8 l( K9 D7 n6 o: s% E& R8 F3 Y function notice_details_meta_box($post) {+ w9 K( `* L) o! r0 ^
wp_nonce_field(basename(__FILE__), 'notices_nonce');3 J2 ]( n8 f6 _# T3 Q4 f- z3 k
$notice_title = get_post_meta($post->ID, 'notice_title', true);
( s' m% B% z( Y/ f" [* O ? $notice_content = get_post_meta($post->ID, 'notice_content', true);) }/ G M( J, Q% `7 E
?>
$ f/ L+ f% P6 N# c# ^' s <p>
^5 O" I/ ]5 _1 A* v1 \* T <label for="notice-title">Notice Title</label><br>% m E' U- C( S6 A3 l
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">2 I& @$ K& \% X- l" _4 |
</p>
- G5 [8 @$ P% \; V <p>9 d) M) I; C3 k4 ~4 B R9 t4 ?! {
<label for="notice-content">Notice Content</label><br>/ {# g. M% d& T+ n) V* ~4 v
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?> H7 _+ }, f+ ~: A
</p>
9 n/ v# A& T v, O% [ <?php
7 H, h J0 R! W2 e; P }
1 y8 l' q$ B, c) h8 e, B( X- v' A* z1 z! f
add_action('save_post', 'save_site_wide_notice_meta_box');, ^' E( d) N; w' d0 A( j
function save_site_wide_notice_meta_box($post_id) {" s1 w- P5 A6 \' J: A: z/ S
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
# ~# z& a9 z- m1 q return;4 B$ {' e6 C& K0 l7 o
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
+ r3 r. E% R7 q5 x( X! L return;& ?, M- F/ r3 f* ]* X
" j# L. t0 g2 G
if (isset($_POST['notice_title'])) {
0 w; Q# Q5 ?$ {7 r! S update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
. W2 J( f5 u7 O3 M* @6 p) @+ {& d }
( y# G R# ]: N& R: F) b if (isset($_POST['notice_content'])) {
9 D7 e T7 j% [+ T4 I9 S7 ^: w update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));! @* P5 M; L o8 h! O# ]" i: x
}) n6 L1 J4 V q/ p. L1 y
}0 x# g" z. ?9 \" M9 ~
```
- Y1 y B K3 _+ }5 f+ S$ G
; m# j o/ y7 T) U8 Q$ `9 x 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
0 F- _( _2 ]/ r; ?* l/ Q
$ J+ O6 Q0 C& t4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
: O8 { G& k9 X2 Y Z+ k- Z$ {. I5 u4 M9 n6 \1 b& k$ U0 W
```
2 x( A+ d4 @4 @ $args = array(
' g! J2 k# C% O5 m( a% Y/ q9 @ 'post_type' => 'site-wide-notices',
- E! s5 F: h5 L/ a, g 'posts_per_page' => 3,
2 R/ l( `- u, ~+ U8 `6 \4 W h 'order' => 'DESC',8 k' h4 p6 K/ |+ X
'orderby' => 'date'
- B. H5 L) W& ^! ?8 P );
: f/ {1 L" Q9 x: h; T; @ $query = new WP_Query($args);* Y- i' ^* g2 \& l
if ($query->have_posts()) :
, Q7 a- H; `* U Q w3 ~8 K4 ^ while ($query->have_posts()) : $query->the_post(); ?>
" a7 W& C4 ?( b# g& d <div class="notice">
- k6 o3 v" C: o/ d4 b <h3><?php the_title(); ?></h3>
! g$ O U; \& Q6 N' Y2 f1 i, I <div class="notice-content"><?php the_content(); ?></div>6 s9 t* ~ f5 _
</div>$ M1 I! Q, J( B
<?php endwhile;
9 S; ]2 o5 U9 y2 J+ _ wp_reset_postdata();
4 Y# i) s+ L7 f3 i$ ~5 c endif;; K( S) d/ M* b( y& x8 D
```
$ ~- v' B4 @: k2 B0 ?# i0 p1 ^* a3 B4 T5 b( }! [1 o
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|