|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?" w5 E/ _; Y- a
' G& o0 s4 u2 W7 B) o6 B如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。, L% J/ ^% C3 |
8 l0 v9 s0 X( i( y以下是创建自定义插件的步骤:
: s- w2 Q# x9 p% f9 H& I' v
/ m) z4 t+ C8 T: J4 ?1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
" N6 Q5 M- O2 {; d
; x6 o; a, F) \8 o+ D8 E b ```: E) l3 G+ `/ |& m+ o, G) y
<?php% B5 Q7 H! |# S! h' u. D8 ^
/*8 R- E$ g6 M( C: d
Plugin Name: Site Wide Notices Plugin7 i5 q4 x( X* S' F1 f
Description: Adds a new custom post type for site-wide notices.
- R9 M# Z; u0 V- b% v; ^ Version: 1.0
0 ?% q5 O; M; a6 r9 f Author: Your Name
/ u$ r1 V+ |$ k1 [7 o Author URI: http://example.com2 v7 n( c# _. D* y+ S/ Y
*/
6 I/ o# J: C) D3 Q: x
1 P; k4 c9 C$ |: n1 ]5 H) D // Add plugin code here...7 n* m7 L) p* v. k
```
$ i; J2 \/ f& A9 Q8 F( D0 i- C, i
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。5 t% {) _* D4 z
. h* r2 U' W m" Q2 N
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
3 _1 s: J! O h0 l& E6 U
3 g. {' z1 V# Z3 n% K6 O ```
2 o* j& _8 Y3 J l5 x3 y+ F/ D add_action('init', 'create_custom_post_type');
F6 X7 i* w A: Z: }" K function create_custom_post_type() {3 w1 e( s* A& K% F+ _
$labels = array(& x$ Z9 B5 S4 o8 T j2 o
'name' => 'Site Wide Notices',
; u! ~8 I- c* Z+ x0 E 'singular_name' => 'Site Wide Notice',2 k p# n! |3 f% n- j
'add_new' => 'Add New',
. n5 A! \. w9 q 'add_new_item' => 'Add New Site Wide Notice',
) w$ }' `- n6 b( T/ s" ] 'edit_item' => 'Edit Site Wide Notice',& R6 y x2 _0 F: S/ @3 d
'new_item' => 'New Site Wide Notice',
: s) b! `" K1 j& ?" Z; D5 L, U" b 'view_item' => 'View Site Wide Notice',# c' f' G4 M1 l
'search_items' => 'Search Site Wide Notices',
6 a% Q" C1 t$ V, o 'not_found' => 'No site-wide notices found',
5 e Y [( _, P" G7 x0 ~& u1 ]) p 'not_found_in_trash' => 'No site-wide notices found in trash'
]3 \: J2 n/ a3 E8 n );
) y: N+ B) q' t& M0 @* I' ^6 V" ]$ n* P
$args = array(
/ J" d9 R( V1 S0 b( [: L# N% U 'labels' => $labels,$ Q* W2 f r1 [- J L
'public' => true,* P; j" m H( c! q. h( r
'has_archive' => true,
6 ?! f! ]% t; i2 Q( L 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
# Y& E* \6 H0 q( V, M ~ 'taxonomies' => array('category', 'post_tag'),: W+ Z- |# N5 Z3 l- r4 r$ C2 G
'menu_icon' => 'dashicons-megaphone',
6 e- M) V8 G6 a' T 'menu_position' => 5,' Z& U& [ N6 A( Y5 B+ g
'rewrite' => array('slug' => 'site-wide-notices')- |& E5 s5 C7 t7 O) v! k
);
) i( y( ]4 B6 J
) M* A$ T2 D! h' \, a- X1 v' G register_post_type('site-wide-notices', $args);
1 K: V& S/ ^# J. H9 c3 s }- g0 [1 x0 ?" e1 f! q4 z
```
$ u3 l6 D$ Q# z* l& a" S
" H N1 c2 Y. F, } 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。8 e1 [: P4 T% o- ~, T" W! R
5 w z* N' W9 i
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:7 [' d) |( H6 m: m* x& S3 H* m: c
h) A t8 A: T ```) l8 N. W" D7 |& p& E, h/ c5 T
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
( h' E2 U9 Z6 T9 H# }5 O function add_site_wide_notices_boxes() {
& E( {$ v5 Q5 b add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');8 H1 v% l' f& c5 _+ I5 N
}1 y, N; A0 }$ z8 l! y
* P- h1 A& A+ ?' x
function notice_details_meta_box($post) {
' D- X# z x4 e# l wp_nonce_field(basename(__FILE__), 'notices_nonce');' w. T; k7 |" W4 ]7 s# V# x+ y1 b
$notice_title = get_post_meta($post->ID, 'notice_title', true);( I& }1 x/ x6 ~9 t% I( q- p7 e6 k
$notice_content = get_post_meta($post->ID, 'notice_content', true);
7 E$ `! R) R7 ]) k, L0 [* F ?>
" N: r% l# M- p! r! M <p>/ K6 G- Z; ` p- L
<label for="notice-title">Notice Title</label><br>
4 C- z& h) p2 N! p# Z7 j <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">* [% t, Y4 a3 N8 h$ `: G' b8 l
</p>6 j2 t5 c% [- f. p6 E) @
<p>
, d# ^% N7 t' e/ X <label for="notice-content">Notice Content</label><br>8 f+ ~. O& Q) Z; m7 W
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>4 t; ]% r. R4 e1 m
</p>8 T, h. l+ b! Z
<?php, R# X3 \1 m: D# r
}
1 |- B" Z- H3 D7 }+ ]) ^
9 e3 a( T: j. E4 b6 t7 n add_action('save_post', 'save_site_wide_notice_meta_box');
$ f8 I* N$ @- l$ a) S* k Q9 H4 c5 p function save_site_wide_notice_meta_box($post_id) {% h$ [' F. x. Z, H# Q/ w+ ^) T0 f
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
2 S1 B/ ]( G+ y, B% E% ?- F0 L% v, U return;
& c A- z, I1 n; M. T if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
9 S7 m1 P7 p7 N; E) R8 Q return;
( @' h5 o, [6 {$ }8 T5 V# C3 A6 O6 ~& `
if (isset($_POST['notice_title'])) {
0 r, d0 T$ F# O9 Y) C+ J" v update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
/ |: M4 S$ r/ U* y* j9 g' I/ ~ }
h! j x8 l) L( t8 l9 c if (isset($_POST['notice_content'])) {
# }# |3 k3 m/ k# u- K5 I5 \ update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));5 C f* Q3 g1 u P' I* |8 l$ Z
}
( N( j9 a4 \$ [ }- j' e+ ]+ W7 k$ W5 z
```( y* M4 Q( ^- {$ Z& r
- T3 w! Z S/ ~1 e0 V+ O `
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。0 |+ s, ?5 s' E
$ z7 y) a8 u. ^, _0 J1 R
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:& k3 `$ T4 `5 Z Q4 m! }
% ~" N$ ]6 K3 ]9 l ```
, K$ D& s2 ?& I9 q0 g $args = array(
; k* n0 b( T" M 'post_type' => 'site-wide-notices',
" S$ i) x4 f2 q% S* k 'posts_per_page' => 3,
! u2 [" q1 ~* t. ?0 E& v 'order' => 'DESC',
: P o- e; P; T- s1 s5 h 'orderby' => 'date'
) ]" U, i+ A$ {% U* N* J );
0 [' C( p- e' | A! A $query = new WP_Query($args);
9 v* B4 h( f9 J, C5 M/ L/ X) { if ($query->have_posts()) :$ g. u$ f2 f6 g
while ($query->have_posts()) : $query->the_post(); ?>
' \; K2 q5 x9 ?* q; L( a" Z/ ?( } <div class="notice">
9 M2 j# j6 [. c+ F* o <h3><?php the_title(); ?></h3>
) |% u" w2 l) S( s' ~ <div class="notice-content"><?php the_content(); ?></div>" f( j _# S) h. o
</div>
a. Z6 O9 b. H7 D* G0 k <?php endwhile;; R& O0 j6 t' f* s
wp_reset_postdata();" r2 B+ r3 D) o: R0 W" x
endif;) x/ i& X/ l# [
```
+ b$ x; q5 x, e9 h. @. N* l
; d. V) i$ I- w) ~" y 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|