|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
/ m+ s/ ]6 H$ G4 {2 d$ k/ Y
2 G" G% y' K7 {, I如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
+ Y- K# ~( Q3 {
/ P8 ^6 e+ M# N: ~! {以下是创建自定义插件的步骤:
/ E! [& s3 J6 P2 V/ T1 g
9 _6 \0 x! B+ L- G8 t1 O3 S1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
$ ?6 C. _, I% p& v
. x" w* B& V* p8 z. ?& a ```
3 d# s9 A5 n& s$ B @1 Q- _ <?php* i% l# U. D! ?9 y
/*
! E4 ?8 z" C g/ J# P, `% X Plugin Name: Site Wide Notices Plugin
$ Y9 y" m/ y7 ~1 n U* d' j( Y Description: Adds a new custom post type for site-wide notices.2 w1 z2 [6 c% T; R1 }0 F
Version: 1.07 J. j0 I) E7 W! s6 m( s% L
Author: Your Name
+ [- G5 X" x" @. h- i& v) ?# r Author URI: http://example.com! W! E8 z7 t. ?5 k/ {) C0 |; I
*/
) h0 r4 u/ V& [
) m: z& ~2 J' D5 h% H // Add plugin code here...
/ h8 v1 @0 c7 G/ \ ``` _; j* ^- L+ ^$ K- x
/ U, k* e" M1 u, L
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
9 { j2 M' l2 h1 ~' U3 R% F( f. j( R- w4 y
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:) b( @1 S( e6 A9 Y2 ? B j
- V* M8 L6 Q* {+ U- B ```
3 J: b5 h# Z. [, m add_action('init', 'create_custom_post_type');0 S5 `1 E3 b+ \
function create_custom_post_type() {, b+ @" I0 b: R, s, v6 e
$labels = array(
+ Y& g: M$ p6 @: I% p3 l 'name' => 'Site Wide Notices',0 F! [: A X" f
'singular_name' => 'Site Wide Notice',- H7 S0 J! O2 d$ B
'add_new' => 'Add New',. o/ {( H* R! N* h' \! k1 x
'add_new_item' => 'Add New Site Wide Notice',
" ?! O0 K5 }+ B( G E 'edit_item' => 'Edit Site Wide Notice',
# O1 K4 p+ ~& N1 n( Z 'new_item' => 'New Site Wide Notice',
O% Y5 q+ @4 D( ?4 z( ^/ n F 'view_item' => 'View Site Wide Notice',/ k% j; e8 o( Q$ B" W
'search_items' => 'Search Site Wide Notices',( w* s5 P$ S0 t5 a! X3 p
'not_found' => 'No site-wide notices found',
3 _1 a3 v* b% W6 A9 l- { 'not_found_in_trash' => 'No site-wide notices found in trash'
8 O1 s+ L# M u2 q* | );( s2 v; E4 k& o3 s, Y
- b* F8 V% e8 D $args = array(
4 E7 C, L6 [9 @) y; { 'labels' => $labels,
- o' w; C1 I% P2 m 'public' => true,' \0 m2 s5 I) x" E' _2 s
'has_archive' => true,
- L2 P- R$ D5 @' P! d1 t9 f" @ 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
7 c7 T% V; H U; [( ^- c) { 'taxonomies' => array('category', 'post_tag'),
! i3 ]' m1 h; Q2 ]" A& J 'menu_icon' => 'dashicons-megaphone',
- Y- ?" [9 A4 a0 J0 ^' |/ j$ [5 V7 | 'menu_position' => 5,1 d' \) M4 g+ g: V/ f( ~& @+ k
'rewrite' => array('slug' => 'site-wide-notices')" D! D7 w$ e6 t( f
);
' c6 P( \' j+ y1 G0 \1 Y
8 i( Z1 H% k2 U( O register_post_type('site-wide-notices', $args);
8 l# c/ O) M, x }
% I2 P2 m& g/ ~0 D( S6 @4 a ```% C% y! T- _3 |0 d g. b
( H' I9 Q4 J1 _- Y 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
$ f/ d" l. N# ~6 H+ ~+ F V6 o6 y& }( L v$ T/ h* Q
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
$ }: _' b* q4 V
. \. I1 q* }4 ?: P! i5 H ```3 o' r) S8 @/ l* |# u# e9 t# h+ `
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');; u( g* E4 ^) y5 G4 e$ L9 X
function add_site_wide_notices_boxes() {
; V1 u7 v0 h: K1 _1 ` add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
' N+ H& v, h7 S5 ? }
) [0 X1 }- k- f" C8 h. v. I) P7 Z: |* b. o5 p W2 U f% R5 Q
function notice_details_meta_box($post) {- V1 g ]6 B/ C1 w: g
wp_nonce_field(basename(__FILE__), 'notices_nonce');! K; @, G9 N0 Y# N% o
$notice_title = get_post_meta($post->ID, 'notice_title', true);
O& W' _0 V7 J/ C& j $notice_content = get_post_meta($post->ID, 'notice_content', true);& b5 ]$ g1 r% |6 f* F9 P3 S
?>
/ v ^6 ^' t1 z. h <p>, t0 O1 s( b V6 f& i9 c
<label for="notice-title">Notice Title</label><br>
0 v5 H+ e% u1 n" N6 l8 G0 S: } <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
9 z! e1 S) A$ Z7 c: s! W </p>
% C- S3 V* G* F3 k3 P2 o! F <p>* Y9 z+ `: `1 Y" q0 d* b, |2 E* Z
<label for="notice-content">Notice Content</label><br> v7 B B' v6 |& X+ ^$ i
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?># ^' E; B* t' m
</p>
: @+ Y3 F* q9 V2 \& r2 a <?php1 z7 S2 t& g* v* h( Q3 \4 y
}2 t" j8 M q: c" \
0 m+ p3 P" s3 G
add_action('save_post', 'save_site_wide_notice_meta_box');4 F/ z. g1 t7 Y0 D. V
function save_site_wide_notice_meta_box($post_id) {
5 d- @. l! n4 o' G% E2 ~( L2 W if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
+ E- D* Q( A Z+ s6 _ return;
% G' p2 X4 G7 v; E* B if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
% ?: P7 s: s; S0 _9 ?% D/ U return;" K: ?' Z$ c/ _' \2 U0 Q. E
! q x$ P3 T* b* `5 R if (isset($_POST['notice_title'])) {, s; b; W! U9 |: h9 G) _+ X! l
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title'])); j* g0 q4 w& c8 N
}
: y" e! i( d0 {6 D3 u: { p5 `: A if (isset($_POST['notice_content'])) {
( B8 d/ |# d& Q update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
# Y3 \* O5 Z, m3 x- q; m }* P! T8 j' O4 i5 H& |
}
+ n: _/ f" z" x ```
: f! Z# j7 ?( ~& k) ~: _9 O- l. d8 W% X( B
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。, e$ i, p+ x3 u& E" g9 @8 U( Y
7 t' Q, c5 y1 X! H+ r8 W3 T
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
2 J$ A! W0 f; s' ]( G# n2 ]( s H: o8 u0 r- g
```% Q3 L0 a7 l( n0 k
$args = array(+ x: n+ k, c+ L
'post_type' => 'site-wide-notices',! h1 n, h% V- D5 \. k8 p
'posts_per_page' => 3,) M- Z% ~' d+ m$ _, _" |
'order' => 'DESC',
7 f4 E6 I( l' z7 E3 Y# J6 e 'orderby' => 'date'4 y& s, h. P: Q% F% r. l! h7 G
);
; v; Q' u. p2 A7 J5 `' L! Q n $query = new WP_Query($args);7 N1 D$ J' y! ]" z2 E% d$ ]
if ($query->have_posts()) :) M( U! d) m& o" X5 K* m
while ($query->have_posts()) : $query->the_post(); ?>" {7 q* s# L9 }- }) G
<div class="notice">0 ?! |5 A$ D6 ^1 @
<h3><?php the_title(); ?></h3>& n/ x9 Y! j7 o! z: E8 j
<div class="notice-content"><?php the_content(); ?></div>2 q/ B0 b- e5 s' O" \7 L) `( p6 n) ^
</div>
0 P; e5 x) [5 z4 U5 x& J' s <?php endwhile;
! T( H4 u8 l9 z; U6 R( X( a7 o wp_reset_postdata();6 R6 c: r. g5 T
endif;
; p7 c1 [6 u2 F+ ^8 t( v ```
f; p8 o0 }9 K4 G: ?6 O5 J7 }5 V, y1 z0 t2 U- H# Z& p+ J) ?% {
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|