|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?, x' m4 b9 w; m
9 h) h4 B C+ Q) V
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。* R# z% D5 Z0 d. D- O/ u
& M: k) ?$ f! X- u& E N
以下是创建自定义插件的步骤: J* p, h1 o, G( N: \7 A8 C6 s
4 \4 p+ X) d+ I1 P/ g$ x1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:, |: O7 i1 f, a* q
, Z S3 `+ F8 C0 p% l, ?1 z ? ```
! R: b5 l$ Q* J h' a" `! C! p I <?php7 W1 T: r# v; r8 j: G# @
/*
8 W& J$ K7 K4 u Plugin Name: Site Wide Notices Plugin; r/ Z# b9 m" l# ^# M& ^; M
Description: Adds a new custom post type for site-wide notices.) }) D$ b( q( e6 p( t* z
Version: 1.04 \' _9 H. u8 G* t+ s$ c8 `
Author: Your Name
7 I( u, b; ~9 c9 Q$ J Author URI: http://example.com3 F; `7 i1 k+ [1 ]
*/6 v4 @ `; W, S L( O) q
$ r- }% d+ v% a/ |' O+ T
// Add plugin code here...9 F7 E6 a6 Y. a
```: k, F# }. ?* G% i0 a! J8 m9 K3 x
6 L( g' l- n& h
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。6 E d9 v1 H1 I# k F: L
" t0 c h( g( s: j- k7 P$ u2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
* X3 i8 D9 }) z1 ^* C
, X/ r1 Z8 C6 v ```+ I1 B" i2 i# r- h( U7 R
add_action('init', 'create_custom_post_type');) K3 H0 E& v0 Y( n" `, q
function create_custom_post_type() {
: l) I4 Z6 [: j. Z9 U $labels = array(
! p9 f+ e H7 x 'name' => 'Site Wide Notices',
* C! y* b- ^" |9 @ 'singular_name' => 'Site Wide Notice',
# c4 D0 [( y& a8 S; ?% r 'add_new' => 'Add New',
. {) i) A& k/ i% w0 ~ 'add_new_item' => 'Add New Site Wide Notice',6 P7 Y# \) G {! j
'edit_item' => 'Edit Site Wide Notice',: o$ Q$ x k% v" w5 J/ @ K
'new_item' => 'New Site Wide Notice'," n# }$ r8 B. f+ z, l
'view_item' => 'View Site Wide Notice',; G6 ]- b, p& u& {0 Q% v
'search_items' => 'Search Site Wide Notices',. z# y& k1 h% p$ S
'not_found' => 'No site-wide notices found',
+ x) D- o/ n: R1 `' p. N 'not_found_in_trash' => 'No site-wide notices found in trash'
4 a& P& P+ M' j7 M* p );" x/ B1 v0 O3 G3 v
/ {5 M; X$ ?& S1 ^; t' M3 I( Z- ]
$args = array() s* C+ \$ h7 l5 Y9 ~: r
'labels' => $labels,
2 p" z/ C' z- S, z! H- r 'public' => true,- q; z% S2 c0 ^% ]3 x& h
'has_archive' => true,
# h1 V# g% T5 f! n" U) L. S0 B 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
F# _& p8 S& L: k/ e( B 'taxonomies' => array('category', 'post_tag'),
' `7 c* t" @& Q$ q 'menu_icon' => 'dashicons-megaphone',
# H7 e3 p7 M: J 'menu_position' => 5,
) p$ l: R3 @& Q( k 'rewrite' => array('slug' => 'site-wide-notices')* ]( S. _ U9 D3 p7 h1 C3 L
);+ Q" M' r4 f( ~7 i
5 ~8 R( T i/ K* x register_post_type('site-wide-notices', $args);& G. o+ w2 y6 M' S5 c
}
! ?% q5 V, }7 K0 }! ? ```$ W; ?. R" s. ]: i; }7 D
6 W2 o R$ X+ x/ {( p) v8 [7 F
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。 I. w3 X6 q) G
0 h3 ^3 K" G8 T4 ~# P6 Y. E( J3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
- [+ G" I# W; o/ M. U
! C+ n+ i; S5 w A1 D: r/ k+ U ```0 d0 W% H7 C" I3 Y* r& Q: N
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
) x s5 l' R& e- v8 Y4 } function add_site_wide_notices_boxes() {
6 m" M( ]6 n+ `; E- h, e add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
( P& L5 O$ b" i( w6 c1 n( L& p }
! k( f. |9 `( N2 f4 Y- f3 X# `4 h3 k
function notice_details_meta_box($post) { Y0 `5 [. N& Y) C4 o
wp_nonce_field(basename(__FILE__), 'notices_nonce');0 Z+ R3 q* m1 r
$notice_title = get_post_meta($post->ID, 'notice_title', true);
& N+ q' }* W; d% h$ O $notice_content = get_post_meta($post->ID, 'notice_content', true);1 y8 d: u# ~2 c0 f) F
?>
O* A2 G2 y& V: n0 r <p>- d; H6 R' R% `8 `6 b* w
<label for="notice-title">Notice Title</label><br>& k/ m7 ]6 c$ z; B- G h8 [
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
) k! Z. \, U+ `& P </p>/ Y8 z4 z5 P# i( z2 T8 s. U9 v2 j
<p>
% s, w- C5 W- ~0 Y5 l f& C) q% _2 O <label for="notice-content">Notice Content</label><br>( @1 Q f% {% U$ S5 M, K0 Z
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
$ @- R; I+ u0 f- T) y </p>
1 Y$ I* Z/ r) f/ A <?php
6 S1 C# O+ w3 O0 V }) C$ R0 _2 J; x+ K: ?3 u
. W- Y7 K4 ]' K% ~
add_action('save_post', 'save_site_wide_notice_meta_box');9 w( m) ]% i& y: c+ S
function save_site_wide_notice_meta_box($post_id) {
$ `# y' D* J3 e* D if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))# z) x& g. _+ e; d* I
return;" r5 H( d& d& a! h. w6 C0 I
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)/ {# s1 p1 C0 D
return;
/ H& t4 `( L0 \/ ~# C8 U) L6 x) n0 N+ L/ Y+ [7 P/ t/ Q2 r
if (isset($_POST['notice_title'])) {
* Y2 f+ f2 V' [$ I update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));6 l1 e; I2 r7 O7 L a) F1 l
}2 x Q3 d. d. ]( w2 _ a& W
if (isset($_POST['notice_content'])) {
7 n5 m$ f4 l( K/ e) I. [3 Y update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));; ~1 U, B3 O& i/ {. M2 ~
}& o; z& ~9 A) [. k# P
}
2 _; T1 {' u- |+ s2 c ```" Y* T' H5 o0 d1 b% n
3 ?$ Q0 }0 J" I y7 e& d- A
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
5 i9 h! z4 Q. U2 f9 v% N
: @7 i: z* v+ _9 W& S4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:( _) {' r. t& t0 f* K3 r
! z$ ?& A3 V* S0 B% }3 t ```0 Y, O' C: n5 N# e& t; V7 e! Z
$args = array(( o8 T. g4 a* Y% }
'post_type' => 'site-wide-notices',
- q! j( a1 X% r% ?8 {: ~- z' d* c% w 'posts_per_page' => 3,/ z" P; p" M+ Z/ e
'order' => 'DESC',2 c6 n. s8 K$ ]- X: m5 j1 |
'orderby' => 'date'/ s3 t0 d3 B O, d. X
);# U# @' @: z) [& T: z
$query = new WP_Query($args);5 I& o5 a+ n% ~3 o/ K
if ($query->have_posts()) :
( C0 k6 p2 u _' _ while ($query->have_posts()) : $query->the_post(); ?>
1 k b& X2 e! e, z1 j/ @ <div class="notice">; a3 b# H" I1 j+ S9 T' l
<h3><?php the_title(); ?></h3>" `* |1 `5 h+ y6 |) M5 L- W
<div class="notice-content"><?php the_content(); ?></div>
! N: V1 r& m- q: D9 j7 s( W </div>3 z6 f$ ]+ D9 J0 J* ]
<?php endwhile;- J, Z! s' G: R8 q! n( ~; ]! C
wp_reset_postdata();
. j1 h& B$ D. Y" m+ j& h; E/ K endif;
2 S" A- Z3 T9 {. w7 Z ```
% e/ |) I7 o0 ?0 `
2 ^( q. m* ~* B! U( B 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|