|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?. P+ T$ a7 U* z% f) ]- G/ j! h/ \, I
+ ]) ?' }2 q" ~% n
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。( q$ s, S# x( D' G1 {
$ y4 L j9 b- e$ ^2 p以下是创建自定义插件的步骤:- C/ \/ k3 K6 L+ d/ W9 f2 C) g0 t+ T1 x
2 t! z( `9 V& O: f" F1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:+ s. f& \4 c+ E
" l9 L! a* H5 z" O ```+ V, G! k5 M# a2 U! z, z
<?php7 N$ U( R8 n; a3 \2 n
/*
4 i8 Y* H- l0 w4 U Plugin Name: Site Wide Notices Plugin j" l- P4 l7 q/ r) H |( q
Description: Adds a new custom post type for site-wide notices.
) X6 B3 H" T( z5 j Version: 1.0/ S# j; F: w' K2 X3 c7 j! L
Author: Your Name
0 m+ u/ b4 k3 {1 X% ] Author URI: http://example.com
: Z/ z) s D* h' D */, {& j8 w$ d% O* [* x
; M d: s ^8 { L, D7 O
// Add plugin code here...
" }% z) \- ^/ r3 p2 t8 s1 L3 y ```+ h, ]1 S9 H# O8 h) P
" u4 c2 k: F/ P) ?
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。! h7 n% Y/ ^3 c
2 V' o( h2 D. E3 J2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:8 V! t C; v% Q8 J; H" {5 s2 \
+ `# I6 N# _( }" N' V
```
, D5 e3 p8 z% K" l8 b W add_action('init', 'create_custom_post_type');7 C) T' w3 f: G% {1 @
function create_custom_post_type() {" Q3 p/ q7 R# R, }
$labels = array(
6 c2 i' i8 j* a" X K! _$ ` 'name' => 'Site Wide Notices',. j$ ?% `4 X% h0 T6 Y- I7 g: N8 H* u
'singular_name' => 'Site Wide Notice',0 Y1 q$ t- m$ D" ^* W
'add_new' => 'Add New'," f2 H5 L7 ?6 Z. s
'add_new_item' => 'Add New Site Wide Notice',
; q# n1 |* R/ m1 y9 D* _ 'edit_item' => 'Edit Site Wide Notice',
g. x2 }. m% W) k: B( ~9 \, {% i8 A. A5 e! j 'new_item' => 'New Site Wide Notice',
8 O, G0 _. `. j* c0 v 'view_item' => 'View Site Wide Notice',- n1 n! o+ C$ L9 n% }$ [, f6 f# n
'search_items' => 'Search Site Wide Notices',
$ @6 t) d2 l$ V 'not_found' => 'No site-wide notices found',
' o0 _( d2 Y8 Z- ~7 C0 q( T 'not_found_in_trash' => 'No site-wide notices found in trash'
; l6 Q, K; z4 o0 S( U& w7 N );% u" s! b( L8 V# q" y% ^6 Q
0 K6 F( V0 M' g% E ^: r' o
$args = array(/ F4 Q" z& e/ k. c9 M
'labels' => $labels,
4 Z/ v$ m; @3 \$ l 'public' => true,6 A W4 v! a# H( t4 a
'has_archive' => true,
6 q$ z0 ^, P# F+ a2 K6 C 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),' Y; c, B3 f4 g. ?$ X
'taxonomies' => array('category', 'post_tag'),7 v5 f+ v% x" u/ @6 B
'menu_icon' => 'dashicons-megaphone',9 t& L _/ T1 T& A1 }5 Y0 u
'menu_position' => 5,
% l) \5 A8 E: \$ n: f 'rewrite' => array('slug' => 'site-wide-notices')
4 x% m" d' p0 K, o4 c );; w; i" u% W2 l( z% M# i
! O6 D' Y+ T' z
register_post_type('site-wide-notices', $args);
" i9 K6 |; j6 `1 y* f% [7 X P* c+ y }
o3 t" S6 k# J, Z# {! X ```. p# V. b: Z/ q* e( X9 ^
i& ?) U& F! o# ?1 p* { 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。+ d8 K; C: ~" l/ N: |4 k) n* k* q
' F) z5 \# C8 P3 W+ T9 y; Q3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:( l7 ]% m5 i, F8 Z/ ]0 x/ @
5 r8 P0 r# d# }5 x+ u$ M ?$ d
```
4 H1 F7 i9 H8 Y: B" G add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
8 x0 K* y5 Y" g- l) W8 a; w: M2 p function add_site_wide_notices_boxes() {# t* X) W2 T6 p, N
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
! C1 S9 W8 n3 x M l5 o7 P }
% Q( l9 p: z) D$ ]$ h$ M; F, [' ?; P( O$ y3 P9 R9 I7 X
function notice_details_meta_box($post) {9 m; z" d+ h" T' Y; ~9 i7 O1 @! D
wp_nonce_field(basename(__FILE__), 'notices_nonce');/ _: i( H7 y2 J* I3 P
$notice_title = get_post_meta($post->ID, 'notice_title', true);6 N' F+ b3 _! \5 f7 I; H
$notice_content = get_post_meta($post->ID, 'notice_content', true);
, g" n: z1 i& [9 c* J! k! u ?>/ q: `5 Q6 K& l
<p>+ A2 F* p+ f1 @) q/ e# u! i% t
<label for="notice-title">Notice Title</label><br>7 S/ K* T2 {- |: @! @! t3 i
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">1 e( B: x, f/ z6 i4 o
</p>
) C6 u3 g2 K0 S- y$ `7 J1 U <p>
( g Y; ~4 S& [7 p7 q& S- |; f0 ^" g <label for="notice-content">Notice Content</label><br>
. b: B- V! G/ |2 A( A7 U& C* c% @ <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
; O0 \. i1 V T0 U5 c. N </p>
: X! e7 @. |. Y8 b3 s' U <?php
3 g6 a o% l( m$ Q }( U! K3 z1 E. K6 y, d
. _+ K, M+ N# ?7 ?" u4 c
add_action('save_post', 'save_site_wide_notice_meta_box');! s2 ~: H; i+ f* m: x
function save_site_wide_notice_meta_box($post_id) {
' e) r4 }: b3 B4 _, ^- T if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
0 [. M& x# i) H1 |* T return;
$ _8 F& W3 N+ {, t4 | if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
" c& ^4 c! }0 \, Y- V return;8 r. |+ M j8 w1 L
T5 M# K" s( I if (isset($_POST['notice_title'])) {& I( g* ]; a: l2 N# G1 ]2 C
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));+ B3 e- F, i+ W; `- D! v
}
( Y, O. b2 o- O! a if (isset($_POST['notice_content'])) {
+ v. P+ \, K. [2 v$ J1 A- c update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));# u, O* z0 n$ E- U
}% ?* H$ s9 V& a8 |
}% @# Y: L' B- Y
```
% q1 b7 E' y/ H+ `5 y( @# ^7 _
. t. Z. r- e D9 c4 E 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。5 a% @* |: \) E* l \3 ^
% u \4 C! h3 ]2 T
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:+ u1 w4 b0 f9 k! l6 j0 @$ l* V
' {0 V1 V2 W" W& r8 _ ```1 c7 I" ?( J' P9 f! K
$args = array(: t! _. N. [" E0 x
'post_type' => 'site-wide-notices',
9 a G' Z; J) _. L% j$ Y) M. x 'posts_per_page' => 3,
) ]6 X) i0 A: r 'order' => 'DESC',
6 z" W7 {: w0 z( Z8 t2 \ 'orderby' => 'date'
$ o4 ]6 j0 j# n* ?( B );
5 f" R ~1 N4 v9 B $query = new WP_Query($args);
9 k- l1 \4 j" N if ($query->have_posts()) :
+ }. X+ n# x2 V3 `* _ while ($query->have_posts()) : $query->the_post(); ?>
* R/ N# @0 U8 X' X <div class="notice">
5 _( a |! ~+ G+ o/ v; c <h3><?php the_title(); ?></h3>
9 O# U: R$ f, U% E <div class="notice-content"><?php the_content(); ?></div>6 P; } Q! z* i
</div>1 H$ y9 `+ u0 D5 L" ^9 k( h& j
<?php endwhile;
: D+ y# o+ U5 ?0 N$ z' d( v5 s wp_reset_postdata();7 u( J" [" g5 \" H- q
endif;1 B3 O5 {3 ]: [! D, |* Z# o. U1 k7 i; k
```
- G' w. X0 [( i$ M
{/ r$ }6 ?) e: J1 U: l: a 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|