|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
- q, ?. [; c) y0 M" |( ^* c+ U1 a2 ~$ N+ K5 S% o0 [" U. q
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。$ q. n. F9 \2 P( e+ j& G [. N W8 v
: {8 H% z# I4 i% ~7 {: ]
以下是创建自定义插件的步骤:+ \8 a: Q, J5 g: ^7 \' H
0 P4 n/ q2 s" x% Y
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
e* k- }' n* E' h" z+ A+ H/ u4 t1 ]2 @5 ^
```( W" W) y7 c p' E/ m
<?php
' a: b* f7 n; L) B; p4 r /*3 A, M: Q$ \$ u) N9 {6 M
Plugin Name: Site Wide Notices Plugin- y4 O- ~& v2 h, ?& ~
Description: Adds a new custom post type for site-wide notices.7 a! f5 y# [* K+ [
Version: 1.0
' l( f# u0 _+ E3 Q* b# `% j3 q Author: Your Name! I) l V) J8 C
Author URI: http://example.com0 O5 [! l: q& d" T
*/) O) r/ b$ _3 E; ^7 Q- Z* L: w
7 y9 Q% A& a0 h+ ?( ~2 R
// Add plugin code here...
8 k/ _ H3 ?# @& l9 D* C# w4 c ```- f9 ?( V6 A/ d3 s; ?+ k
' p2 J7 F7 O# T2 y5 h
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。- R9 o. T, y( H& o8 n
- |6 [3 H8 b$ L* _: r, s6 Z6 a8 X
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
& h4 o K. V0 k
3 [+ H8 ~$ h9 n) A0 }2 @% k+ c+ L ```/ g0 Z4 ~( K$ D" [9 [4 r/ h
add_action('init', 'create_custom_post_type');1 Z: W$ p& D$ i; r- d4 j8 Z4 U3 o
function create_custom_post_type() {
. ^7 |9 L) j1 f $labels = array(9 o+ d i, B4 _1 k& z+ ^- Z$ z
'name' => 'Site Wide Notices',
3 a7 ]3 u9 i6 m: v 'singular_name' => 'Site Wide Notice',
4 I& ]) r2 y' D+ O/ r 'add_new' => 'Add New',
1 B% z. q# o6 b2 y' Y, T 'add_new_item' => 'Add New Site Wide Notice',
7 ^8 b! Y2 e8 c! { 'edit_item' => 'Edit Site Wide Notice',/ E1 ~$ _' T; r g2 |$ Z6 T) @
'new_item' => 'New Site Wide Notice',) P q. |& R" `) H5 g
'view_item' => 'View Site Wide Notice',
; r+ A4 I S/ v) R! X5 c& y 'search_items' => 'Search Site Wide Notices',
) F P& U6 M$ M; q0 x. U 'not_found' => 'No site-wide notices found',
, n# }9 a" b f0 F6 s% q0 ] 'not_found_in_trash' => 'No site-wide notices found in trash'
# Y3 j! U8 E3 w );
* H( z% a8 G' o7 h2 f2 _& Y! M! Z7 c, Q; G* N# i
$args = array(
5 Z! i. ?. n8 [$ e% N* H 'labels' => $labels,) h8 {3 N& v/ F, J. G$ K: t
'public' => true,' u( W( f' v- e$ N. ^3 L4 E( T' W% U
'has_archive' => true,
+ n1 }7 s1 d! V% J2 G/ \5 D& D 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
$ Z8 r4 Q) }) K( z) w0 @ 'taxonomies' => array('category', 'post_tag'),5 W6 {/ z- Z7 ]4 H9 j
'menu_icon' => 'dashicons-megaphone',
- _' H9 C$ j- F4 w 'menu_position' => 5," g( { t( o5 i# T
'rewrite' => array('slug' => 'site-wide-notices')% d0 l4 N w9 ?( n3 Z0 R& P8 D9 Y
);
! w2 y( M7 o$ R- U t3 k7 k4 A. c2 y( `( _0 w
register_post_type('site-wide-notices', $args);$ S( H. u D! G9 O4 T
}
/ ]! x' N2 D7 F2 F! m8 e ```
, M% K7 J$ y) I+ S4 y8 O( g" S8 O6 ~; O+ W
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。; d& k8 Z+ s/ m
6 C4 k7 u' |1 T" X4 |1 O4 A
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
5 O0 H6 |) Q# d4 d) A2 U3 X1 r1 [; B! b
```
: L. _( X/ T6 j# s& S0 g* ? add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
3 z# t- b/ B! X# d function add_site_wide_notices_boxes() {/ q0 p: T& ]+ k, A, } H
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
" M) C" D( v1 t/ z! r3 Z9 y }
3 w& a! \% o- q2 E/ b& Z y9 C( `2 d% {
function notice_details_meta_box($post) {
7 }' X4 S# R5 t4 T' | wp_nonce_field(basename(__FILE__), 'notices_nonce');4 @' P9 T1 T7 t: c A( `+ G' x
$notice_title = get_post_meta($post->ID, 'notice_title', true);" A1 ]5 G% q: ~1 q y
$notice_content = get_post_meta($post->ID, 'notice_content', true);
* l/ V" j# D7 D, k, c5 A w! Y ?>3 P* P$ w o* W5 T" q. E1 C
<p>, U/ {* F; w( Z0 ^3 d! c+ D
<label for="notice-title">Notice Title</label><br>
0 P/ u2 ]0 w* h7 h <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">( ~9 k1 Y+ x p
</p> \, d0 O* ^: e; A! B
<p>, A% ]( x* D8 a2 ~3 n
<label for="notice-content">Notice Content</label><br>
: o# m( m+ r# @0 h l; ? <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
8 S# w( x) r4 m" x# l6 m3 r </p>
' r- `! p" [5 U( o <?php7 f& w, E/ h1 Z) V: u6 e1 e
}( d5 q+ C+ K9 H0 z! X% W
( p3 n4 ?& F. {
add_action('save_post', 'save_site_wide_notice_meta_box');; }1 |* O0 L V" R2 e d6 ~4 G
function save_site_wide_notice_meta_box($post_id) {
$ R9 G- B. B8 {! H if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
3 ]2 Z4 Z# O' g( h return;
5 s; z: d( Q# M7 p if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
# c6 W7 P% `. f% y% _2 O return;
4 w3 C, s7 l2 R$ B4 P
% i! i! `4 E2 T7 u! z if (isset($_POST['notice_title'])) {
$ K. Y2 K, D# k5 E6 T update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
7 D2 z3 Z1 T/ c: U* E5 ^1 u O }
b! W( ^2 g, |( e9 z if (isset($_POST['notice_content'])) {
) _. }, ]* V) L7 Z. t! N update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));$ z( i" R. A+ ^' y* m4 B( ^
}
& Q1 ]2 G7 l* s: _0 l7 { }' `4 | G2 a8 m
```
2 C& ?3 E7 |& f3 ~" `
+ v9 o3 ~2 D/ x0 i' p3 S" k7 i$ I, d 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。$ G9 o: {6 [+ u% f
3 r+ |6 v6 l' ]+ U- z4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
5 G. v6 B5 s4 t' E6 n
9 e- G. `1 n1 e. V- f+ E3 N ```
+ Q6 [% y' W6 A: u1 ? T0 L8 [# Y $args = array(
7 B$ I( J2 ], [% t 'post_type' => 'site-wide-notices',+ X) t& Y. e6 Y9 r4 E0 h, o& l: M
'posts_per_page' => 3,
0 J# w5 J' v A' h! a! x, \ 'order' => 'DESC',3 m7 m# v* T# p8 O
'orderby' => 'date'* y. z; Q7 r* B9 g+ H
);
8 D# ]! a5 g6 L t" q z: Q $query = new WP_Query($args);
1 q0 A, _ a% B1 w$ r7 { if ($query->have_posts()) :+ c3 Q3 H0 j" e4 P- M
while ($query->have_posts()) : $query->the_post(); ?>5 }+ z* T+ {9 B8 y) s0 n/ d) q( J
<div class="notice">
8 l- u6 v0 E/ ` <h3><?php the_title(); ?></h3>; Q/ ?0 x i. a7 O8 V6 O
<div class="notice-content"><?php the_content(); ?></div>% V' W, i, k0 G3 n8 Q
</div>
$ a @: T$ l+ p <?php endwhile;
, L* C3 @" o: u' U$ M1 o" r2 _ wp_reset_postdata();
5 N/ W- S: Y# [ endif;
6 w: ]6 F8 i( q; _ ```, [: F% m3 A- L
$ L3 V( ]+ |1 Z2 Z/ u
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|