|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?! @+ F, r6 L: R+ \- H9 C
& `3 B) }2 a! J, j. {" `2 L9 Y* a0 Q% a- z如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
' o8 B2 h i7 i0 ~5 p) l+ X) s) _. n" K9 C1 ^
以下是创建自定义插件的步骤:* j1 U% ]! a- F( i% y: [& t2 _) R
2 p7 Z, _/ J& S8 {- c& J7 i
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:7 I3 e! Q+ ]1 f) A
4 h, \9 @" d, J+ K. s4 T ```! m' _+ T4 n% {7 n* L
<?php4 [8 h2 ?1 j+ G8 m
/*
; C4 e2 i) M4 s/ H9 A Plugin Name: Site Wide Notices Plugin
3 y3 z' S% O( f X Description: Adds a new custom post type for site-wide notices.
0 _$ z! ~2 |6 L3 o x Version: 1.0
# G7 V9 e( N& c# P$ x. E Author: Your Name
6 q" \: |4 M2 E$ } Author URI: http://example.com
0 X; j+ \9 ?* q, B */4 k' a" k/ n* O
+ T8 {) F4 a4 s. n5 d
// Add plugin code here...
$ j. T) M1 C6 j: [, n ```# i7 `% X1 |6 J3 p; n" A! B) `
7 R9 X1 H% M* T0 v! r" P
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。1 J2 {3 j; k" e! [
+ q( C U8 h" J' O2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
% T! \, T9 H! n' D
4 y. X6 C0 L. }. D. d- P# E/ ~ ```. z9 p& e* ~* G4 m
add_action('init', 'create_custom_post_type');6 O8 a- n4 R! _2 e3 h t$ f7 O2 }7 B
function create_custom_post_type() {
4 ?1 D! B% g5 v. W8 y0 p1 D $labels = array(/ y4 _2 I, J7 I2 i! n# U
'name' => 'Site Wide Notices',
! a* F G; W. W$ M7 g8 o 'singular_name' => 'Site Wide Notice',$ C# Z: F: ?6 ?( M" x+ ~* P) l& l; M9 Y
'add_new' => 'Add New',, y6 H ^% v4 w$ n5 k
'add_new_item' => 'Add New Site Wide Notice',
0 m, g5 v' T. b 'edit_item' => 'Edit Site Wide Notice',
7 p# @' `9 Y- W& ^0 Q 'new_item' => 'New Site Wide Notice',
5 x' v7 Q# Z9 w2 E 'view_item' => 'View Site Wide Notice',* j0 K4 n0 h( u% [
'search_items' => 'Search Site Wide Notices',
- g# E; a5 M8 n1 R V3 \; C8 x5 a 'not_found' => 'No site-wide notices found',
$ U3 o0 O8 o" T8 [ 'not_found_in_trash' => 'No site-wide notices found in trash'- u; }+ x% t" i% q+ x4 H9 j
);
$ H2 ?' r: t* s# F/ \4 k# d: K1 s1 X4 \6 `9 w8 l$ g
$args = array(
9 t6 R5 k2 x7 t: u! A 'labels' => $labels,2 u, r9 o4 i2 T; N
'public' => true,
$ [! x) a: T& p; i; o8 _" t) H 'has_archive' => true," t+ y. X# Z/ `
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
I5 K3 H& a& y 'taxonomies' => array('category', 'post_tag'),8 E8 T: N5 Y- u
'menu_icon' => 'dashicons-megaphone',
$ x2 f1 A5 N, f3 d 'menu_position' => 5,9 K5 k, b4 p' n
'rewrite' => array('slug' => 'site-wide-notices')) g( f2 a* ]1 y" p Q6 r
);
: ~9 ?3 u2 O6 ^
# W! o3 w# e* |5 M# W& h register_post_type('site-wide-notices', $args);5 C. ^+ p2 ^8 e
}( w O% q0 g& J' o: w9 m
```# Q5 @; c) ^; i; k
- u1 w; w/ t8 u
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。0 v8 u% N! {3 Z3 l
6 P8 E. M5 l! c4 g- B3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
! ^" L/ c4 d% E) ~ V& V; {- a) Z' y" L/ { u
```; \6 l, V" V7 i R5 D
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
/ y' }3 |4 z6 @0 I0 z function add_site_wide_notices_boxes() {+ Q4 j8 u8 g( I9 o; L1 f) K
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
1 c) V2 n/ Z* l7 {; p3 r }* l- k; ~) o ^. Z. a
( R* r4 a( R3 x function notice_details_meta_box($post) {+ D4 R; [) C) N2 g
wp_nonce_field(basename(__FILE__), 'notices_nonce');: _# k9 j# ~8 _9 Q( F
$notice_title = get_post_meta($post->ID, 'notice_title', true);& d- o7 O5 R8 x. s8 S- Y) |
$notice_content = get_post_meta($post->ID, 'notice_content', true);
6 g* V; E. q- ^. j ?>1 v8 ?0 ]1 H( D. t
<p>+ {3 { N* T% k7 K. z3 ]: F
<label for="notice-title">Notice Title</label><br>
0 A: e# d" H- V# g <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">7 O0 `" |% J+ q$ x
</p># W% |' Q6 N( b" ?- t/ h
<p>
% c" \' i( W1 T7 s <label for="notice-content">Notice Content</label><br>% a5 Z# |/ e# |9 L9 |- R
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
: F! U/ _+ C9 @5 _6 G5 G </p>! x! R9 q; [$ z+ z7 v* I- v
<?php
. e4 u% W% [* ^; |2 m0 A" [ }
) {1 l! h8 r8 h) ~" r* m$ p& `0 k$ w/ U3 z! f A
add_action('save_post', 'save_site_wide_notice_meta_box');
- j" i' H5 Z; N" \( N2 J1 v function save_site_wide_notice_meta_box($post_id) {
0 v' y1 d' n4 z4 i( d if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))( o' W. E$ z7 O; C b
return;
$ F' N: g/ J! Q; v if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
D) x( U% l/ U s: {) j# I" A return;
6 M0 q' O$ V1 A$ h6 g
- `! F, {0 G5 X# J2 P if (isset($_POST['notice_title'])) {
' y5 ~) c1 {$ a$ l/ s! k b update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));; [% t2 ^9 u' d% Y) |4 s, l
}6 u( r+ k% c! q/ o
if (isset($_POST['notice_content'])) {
" t, Z5 o Q; ^$ i" @( @5 y/ Z5 ^ update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));1 _8 D: i+ Y& R+ e* }
}* }( R$ _- v3 D& _( }5 b
}
+ }7 X* }, J+ s& R ```
) r- w8 O, _0 P* v5 a+ |+ I6 I) P1 I& x0 O. k i9 o/ _ U
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
+ T# G8 S, E: _2 V
$ f/ w/ T9 N2 W+ E: z4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
9 }8 w7 i9 e) d b0 E& Y6 g' }/ f
! l% L1 O, f8 @7 v5 ^ ```
: b3 B: o/ C+ W $args = array(
) I) x9 j8 A; Z 'post_type' => 'site-wide-notices',4 [4 i; W. W+ f+ Y6 ~
'posts_per_page' => 3,
4 q5 z- L# S, n: `7 O' r3 G 'order' => 'DESC',# y7 r: ]+ n* j( }# v- q) P
'orderby' => 'date'
3 |. F. r" S K: b$ o/ R, y );% C; Y4 L% o. T
$query = new WP_Query($args);
+ I, n2 j3 ~5 C. i! B" m if ($query->have_posts()) :
1 ~0 k4 X: l3 q1 U+ R while ($query->have_posts()) : $query->the_post(); ?>
' f- o9 L! @& S <div class="notice">
& s2 k3 V2 Y7 d2 U4 {; H! {% y <h3><?php the_title(); ?></h3>
2 W$ D Z+ }4 |$ l- j <div class="notice-content"><?php the_content(); ?></div>
9 K6 L/ W4 h+ r! @, r </div>3 B& h, _# g( s }+ E ^
<?php endwhile;6 e* S& u& E7 v# Y
wp_reset_postdata();+ {) ^& @& h4 p! S
endif;& U) Y$ }+ c+ R
```
2 h8 l; k3 O( L: l3 V, H1 `9 \% T/ I) `! K; M+ }3 g% \
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|