|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?: a% k# f* M8 B0 S4 P& j
2 U% ?3 r. r- v: z
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。: m: o3 I2 c6 d) m
' ?' x" O5 s( Z, F8 o' i
以下是创建自定义插件的步骤:+ Q$ t% s/ n6 }/ b" g0 L
6 G9 h1 b- K5 m, Q& U. B1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:, J" ?( s2 O+ ~% h/ H
9 c0 N( T' c! y ]
```0 j# j$ A+ x( T# ~5 K2 P# W3 X
<?php
) W+ s' Y# S, u6 [& ~+ _ /*- Y _0 R: I7 k8 `- [
Plugin Name: Site Wide Notices Plugin7 ~% ^, Q4 x* R
Description: Adds a new custom post type for site-wide notices.$ S4 U, {" B0 Y' X$ p
Version: 1.0. q% R2 r1 I( o
Author: Your Name
7 ]. @6 q+ h7 q Author URI: http://example.com
( g9 q( b8 A- D7 i */
; h1 ]! V- j+ g- S( u
. m, V0 Z. V# b. ^% b" g* O // Add plugin code here...
' y2 _) q+ {0 A+ t8 [ ```$ b ]( M/ j0 y% W7 [9 Y
0 a! N0 G- }9 ]! ]
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
+ c9 E1 P/ {3 i2 W6 K9 J
( n8 X) _5 P3 s- C2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:8 o# I3 z% Q3 o1 T# j0 R
/ R; a6 y0 z! C" Z ```
, _. U+ E( e$ I. s" `( J( N3 v add_action('init', 'create_custom_post_type');; M1 M' ~: h1 l
function create_custom_post_type() {) e2 |7 y, M) a
$labels = array(
( I7 O. I$ M5 G5 V4 N 'name' => 'Site Wide Notices',$ f! t E% g3 D7 N8 B" c" ~
'singular_name' => 'Site Wide Notice',/ w B* H) |4 j2 e) x/ g
'add_new' => 'Add New',
/ j) v% Q( x+ T6 W2 K" X0 \$ t 'add_new_item' => 'Add New Site Wide Notice',
; \3 K* M- j+ K 'edit_item' => 'Edit Site Wide Notice',
4 R% R( ]: N8 A 'new_item' => 'New Site Wide Notice',3 i6 i& O( _: M& |1 H/ h
'view_item' => 'View Site Wide Notice',
9 h( B: Z: i7 q& v$ H+ ]. `- b9 Z 'search_items' => 'Search Site Wide Notices',
: \( ^& r: i+ g6 g& Y 'not_found' => 'No site-wide notices found',
0 x% G. K3 a& ? 'not_found_in_trash' => 'No site-wide notices found in trash'! s2 R% _9 F) H1 ~, [) O* ]
);1 g( D$ p, A, }1 }
# O0 a! Z. a6 F( @; E $args = array(7 O* b; g3 Z8 L9 l% l
'labels' => $labels,
2 s+ I* o$ ~$ A5 d" e 'public' => true,
8 q- w/ B5 L& ?! s5 E; u 'has_archive' => true,
7 h; d* e+ ]1 E( J" g1 Q4 Y; u 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
1 i! t/ f% e! A. k; B/ s- D 'taxonomies' => array('category', 'post_tag'),
) N @! ~* t; m2 h0 h9 F8 [ 'menu_icon' => 'dashicons-megaphone'," X. @0 E' n1 I3 Q# j* t
'menu_position' => 5,
3 ~ n+ ~8 O) X: [4 @9 y 'rewrite' => array('slug' => 'site-wide-notices')
! E9 r+ U" Q* g, q );
; {+ ~# P/ y' |+ z2 S: u9 i
0 y0 B0 ]; {8 L: ]) f) j; g register_post_type('site-wide-notices', $args);
7 |" F1 F- _& Q! o }# e( W2 N. h7 i6 S c
```
8 w9 b/ o1 U: w# o2 i% ?6 Q6 \+ Z b8 y
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
5 y7 F6 ?' T' H; g" [4 v" D, C5 D" v# C5 S: I" L
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:9 T+ h* h+ B. X4 f8 `
. F5 Z, o9 a% o& Y& R: h* f7 V+ D
```
: Z1 ]# ]" t+ X$ E5 G3 { add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
+ ]: k! d$ o$ g0 s- B% v function add_site_wide_notices_boxes() {
0 [4 G% P1 J7 \" N8 w add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');/ g8 y% G, t$ ]3 u$ r% w
}: S3 g p. x0 [# {0 w( a! H9 u% _
G; L, I& [, K+ T' Z1 `- u
function notice_details_meta_box($post) {* o1 J: {( I" b2 ~: {# G A- W$ b
wp_nonce_field(basename(__FILE__), 'notices_nonce');
3 `2 j7 b U: O+ p. S $notice_title = get_post_meta($post->ID, 'notice_title', true);
& m6 I2 f; h* E $notice_content = get_post_meta($post->ID, 'notice_content', true);. |2 G+ E- l" }- E5 Z2 d5 N
?>
# R, M |* L# f2 w <p>
6 C+ s" q$ y- D: {: ~- j: c) P R* X <label for="notice-title">Notice Title</label><br>
5 j9 Y1 T1 X8 l <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">4 O% Z! p" `8 m
</p>
9 B6 n# ~: D6 _9 F <p>
* {6 E# u% r3 r1 y7 g2 b) y <label for="notice-content">Notice Content</label><br>
; r( r% D/ c. \& ? <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>6 ~: I$ j' P5 W, @, S
</p>$ u& }3 e: }3 b- \" M6 L) ^- ?3 }
<?php
* a+ Q1 [+ j$ x0 a }
" ^' J# t8 S7 v: G0 P$ P# {
( k4 l* j- O* z% e9 o1 y% p add_action('save_post', 'save_site_wide_notice_meta_box');0 R: ~ r5 y+ m) g9 b
function save_site_wide_notice_meta_box($post_id) {
. N; X" r9 Z- u$ o6 W* X/ Q if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
$ O. [" ~; E5 p/ ~ return;
8 E: M" W6 k0 a! e2 a if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
& H3 T1 }. P N8 V& H* k2 N return;
& E. n+ T5 O+ n$ `5 z+ d+ A7 e1 P2 a, u: R, h. N
if (isset($_POST['notice_title'])) {/ z2 a/ U7 B4 | w- S0 G
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
/ D$ F. g8 K# I$ L( L }: u3 k9 s" d p* |4 o' ?, _
if (isset($_POST['notice_content'])) {
& z! {$ b3 X: X( |7 v/ p update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));5 |4 A* y+ I5 p# c- L' c! U9 ?5 c
}" [* `! ^+ C8 Q( Z0 p; G4 q& O+ Y+ z4 i' D
}5 ^7 y! @. t" c5 q+ r! O* V
```$ N5 ]. V2 |. g; }' S( i" s' D' Z
9 T9 Q0 Z# q: G- t
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
9 t, j U8 [6 N' Q5 O& h& H- A! l6 @) c% A+ D8 W- D
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
- N+ A3 Z5 D, ^4 @8 i
& _3 ~- X ?% N# J ```* z. c* @0 O) z. \
$args = array(
* S! ~" M# p5 i$ H e0 ? 'post_type' => 'site-wide-notices',) e3 J9 ^- _$ e7 T" c8 L6 a1 K
'posts_per_page' => 3,
) D& l+ C7 U/ v: L' v 'order' => 'DESC',
& W n' t/ n& e/ ? 'orderby' => 'date'
, e( C* Y1 w- ~/ `4 p6 E N1 D& @ );
. E+ q% R8 f0 w5 L# ^ $query = new WP_Query($args);
@3 B+ ^" A/ u1 T; N& X if ($query->have_posts()) :+ p3 L6 Z' Y1 u3 r
while ($query->have_posts()) : $query->the_post(); ?>
+ d0 Z. _' e% A i) K% m L6 H8 A <div class="notice">
: W @5 [' U- ^. D <h3><?php the_title(); ?></h3>
3 r3 y$ ?, z& Y. Y- ~; b+ w <div class="notice-content"><?php the_content(); ?></div>
- f% x. D( I: n' I5 R6 {; Y </div>
1 V- w( z: E) A3 v/ k$ U4 O <?php endwhile;
9 L% q3 B" Y6 m# H8 O wp_reset_postdata();9 `! X9 n3 K* l( v5 I
endif;/ ^ p7 v6 ~5 Z$ Z
``` d+ N' t8 g1 N+ G% @% O* O
0 t- W' C3 G" j
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|