|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
6 s, {; O/ D, x& W" c
# Z/ E1 p4 B+ O: S8 ]9 T. X如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。. G6 r" F+ Z5 ^) p" Z3 G# s) p# K2 |
, M9 p+ T. Y! Z. ~# C以下是创建自定义插件的步骤:
, C4 g+ }* L9 x9 c4 l
3 w% O6 L% e" \0 ^; n3 d9 ?1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:: R0 m) P- b- v3 s. N
w& P' w' Q# P6 f4 j) C
```5 R6 d/ S L/ f& R- a
<?php& v1 k ~' t5 E' N/ i' v
/*7 b5 d1 u0 g' |
Plugin Name: Site Wide Notices Plugin4 _% j. c" y' d9 J0 y& M
Description: Adds a new custom post type for site-wide notices.; D+ n5 @5 _$ x: |
Version: 1.0
! p0 M3 z; C" q Author: Your Name% o2 z" e! e; r, w! w' {9 F
Author URI: http://example.com
3 b! O: f/ a& W. W' j */. L0 `* l3 N! T; m" j- ]8 a
( A) Q: ~$ B: ~+ p3 f+ d
// Add plugin code here...
1 A" [. s8 f+ Y1 t L$ R ```( O% _& J4 P" q; m/ N: Q
, l0 i3 S+ [9 N* g6 k1 ? 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。( f$ z# \) }8 B$ g
$ r' h% W8 \, Y& J* v
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
# V' F- C9 O; J9 f. a! T, V+ \ c7 w; j
```3 T- g* h- b3 u& u g8 ], i/ u+ j( l
add_action('init', 'create_custom_post_type');$ b5 ^8 C! L; R0 c% N
function create_custom_post_type() { Q8 e- n9 \- K$ A ~+ p
$labels = array( Y% r( r! ~4 Q7 |0 {
'name' => 'Site Wide Notices',
( l+ n+ E8 \: n4 ? 'singular_name' => 'Site Wide Notice',
8 {3 m& `3 f/ H; X. \4 T$ f 'add_new' => 'Add New',5 h; A3 u4 M6 m" w1 U
'add_new_item' => 'Add New Site Wide Notice',0 L2 ?* I! @0 X# `
'edit_item' => 'Edit Site Wide Notice',9 B" a8 I8 q d9 Z4 M+ v, L
'new_item' => 'New Site Wide Notice',
# N+ l$ V2 G* b, ~- Z 'view_item' => 'View Site Wide Notice',6 v/ Q( [* T8 u' _
'search_items' => 'Search Site Wide Notices',6 h6 M$ K# j0 ~, `: ]
'not_found' => 'No site-wide notices found',6 \* y8 D3 Q* q
'not_found_in_trash' => 'No site-wide notices found in trash', v" w9 i( O/ d- W& l8 j
);
2 P" `" O8 I: ^. ]5 n, Z; t
9 m1 P2 U" |; o% L: a: I# R $args = array(' _0 \$ I# f6 T2 B h' C
'labels' => $labels,
% y1 J2 D4 i y- L/ s1 R 'public' => true,
7 }- {! m: F; y( N( h$ q 'has_archive' => true,
7 V, d) M' l) q. q% @ 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
8 I8 V( {) U, d/ U7 y 'taxonomies' => array('category', 'post_tag'),
* M3 h6 b0 Y" \& [! I7 L 'menu_icon' => 'dashicons-megaphone',
9 F7 S4 j1 [- R) m7 T$ {, W6 V1 U& V 'menu_position' => 5,. y9 n2 Q7 q' H9 c
'rewrite' => array('slug' => 'site-wide-notices')
5 p+ Z" k- W2 a5 g e );
' {: t" _" K0 R4 S @+ f. o. }6 p: o1 w- @" U+ M; `/ ~% J; u
register_post_type('site-wide-notices', $args);: L5 o1 U9 j1 n( I5 r1 ~) \
}
2 X1 X' e+ Q9 y ```- J& U+ l- J: m2 K5 U; G, |/ V
. O; k6 z2 u8 b5 W
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
& f3 @5 m6 F8 V+ H
7 E* T, |; m% g3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
* F* @" ~9 R; R! A6 Y; ]( ]
" D, a( f9 m( }% j! K ```
! g, S% g2 O6 s: S/ j6 X% ^ add_action('add_meta_boxes', 'add_site_wide_notices_boxes');. c7 ]1 }+ C1 B# D9 J0 H8 g
function add_site_wide_notices_boxes() {% X4 Q! ? l9 }) }- \) E9 u* q
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
" A& K+ Q# H# U! K' J1 u9 H }# K3 d* Y8 x" @; v
; P$ C) P$ V) {: Y
function notice_details_meta_box($post) {5 r" W1 s: w$ ]8 _0 l6 R; B
wp_nonce_field(basename(__FILE__), 'notices_nonce');0 [% @/ ` [% |( ], }4 Y
$notice_title = get_post_meta($post->ID, 'notice_title', true);( _* r0 J+ |' g
$notice_content = get_post_meta($post->ID, 'notice_content', true);
9 i1 r, n( R @* ~: W/ P, N$ u" O2 `* n ?>
/ c7 p- H: f& J# u <p>
+ T! Y. b8 Y% W7 z <label for="notice-title">Notice Title</label><br>
' j4 V$ X" t$ a9 |. e( s9 ~ <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
$ w1 H4 q' k" n: E' M7 C! {8 h' F </p>
* ]* ^( R' j2 r- q# o+ V <p>: o( b1 x; l6 S* J% X
<label for="notice-content">Notice Content</label><br>
& A5 t5 @8 B, J( M1 @ <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>, k! P" d3 o6 _: y& b
</p># U* L3 r2 b9 }- k( O' u
<?php
; B1 O6 f- M' {+ o E: O# E }
; F6 }% U0 s" t& v% i# Z
; [0 G# w8 E9 v, P' i5 k+ I add_action('save_post', 'save_site_wide_notice_meta_box');; s) n# s. w" | Z
function save_site_wide_notice_meta_box($post_id) {
' G$ B2 z& W1 V, V7 n+ | if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
& [+ ~! X) o8 F" X- l return;
' e' I! s- B; ]8 C, t if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
1 {9 H) C1 M% f3 m2 s return;8 t( \: p6 b8 m) U5 G$ E
8 j, t5 j/ B" D* O0 }( I) B, v if (isset($_POST['notice_title'])) { \% V( J# T! z3 g+ W& G7 f' Y: U* c7 g
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
% h' f4 T) n" b# c# B* p# ^7 z }; W3 k7 U2 x+ S9 X
if (isset($_POST['notice_content'])) {- P0 b: w. i D
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));: P8 E4 Q% g1 r- l6 Z5 K
}
% ]0 q% @6 H5 D3 p# C, g }
0 B, y0 s/ R, j& X3 X* S" B( w+ q ```
7 l8 R8 j! ?6 h5 I; M k/ r
$ x2 w8 Q3 A' {- o) [% }) v 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。 v) {' V& [8 B( o" |6 J: K/ s
. [! e3 l7 k2 C
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:% }% r w+ g3 a+ D4 Y
) w1 d. K1 }3 V
```
3 R Y7 Y- a. ]3 O* c$ c6 a $args = array(
5 R/ E- Q7 r: J 'post_type' => 'site-wide-notices',4 D8 Z8 O6 u! F* T
'posts_per_page' => 3,: ? @3 E# b" E; J q! K1 U
'order' => 'DESC',
' L: z9 Q$ q3 S/ M$ D 'orderby' => 'date'
1 \" J: v+ o8 l/ Q9 I2 l; Y );
+ V% }7 J; f& \6 }& `6 O $query = new WP_Query($args);
# e3 Z+ _2 p3 G$ G8 |: @. g if ($query->have_posts()) :
- _* ~& z& q: B, k) H. N. v while ($query->have_posts()) : $query->the_post(); ?>
2 M% v# R" y0 p5 F8 _- ^9 r# F <div class="notice">
. R; f( V5 L- [, s8 j1 ^ <h3><?php the_title(); ?></h3>+ N# w0 @: m) H5 J8 t k& h
<div class="notice-content"><?php the_content(); ?></div>
) h' \1 a' D& {9 _ </div>1 i2 H. E" R! Z- E0 K- E: }6 \ ~
<?php endwhile;
% H7 K1 L- A# ~, U' o* u& l$ n wp_reset_postdata();
5 L. |, {2 v1 M endif;
9 J+ f. U$ g/ s" R5 { ```/ k1 r d: N- `. E# I8 C
, g& Q: T& r: K3 H2 J+ _1 d
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|