|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
* a8 V3 [1 L& r$ t5 k# o. Z, ^6 m, s J! n8 J4 ?
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。) c; A- b' w9 ]) s# c
/ x, R' x2 \6 c x; C; B$ ~3 q) X! w
以下是创建自定义插件的步骤:8 \. J1 l6 F1 L( X' V4 D
& a- k [5 d% h' E1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:7 j. l8 |& [7 T2 }2 I5 e
5 N7 x! g9 {3 h3 J ```& t6 A/ k8 B& q7 O1 K2 I1 ]
<?php
5 e6 Z/ |% O- L( e' U- k4 J0 F /*
0 k& @$ D; \. Y' M5 J Plugin Name: Site Wide Notices Plugin
) t- M4 ~" Z; \" j U: B4 y0 l1 R* W Description: Adds a new custom post type for site-wide notices.: Z! A4 D# d7 T; j g. F( c7 f$ B
Version: 1.0/ q! F5 |7 G( i. @
Author: Your Name [6 l+ S$ v9 M' b3 B4 A
Author URI: http://example.com+ B( o/ d1 e/ c. m* A% \5 \
*/7 M, a8 h$ X9 J# ]
n; j' I! A# B. j b; f% D // Add plugin code here...
! ~, a, a! |, N/ d" y ```
' S+ v0 E' M1 k5 {- u8 M2 t/ r1 A/ } Y$ U }
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。+ q1 A, ^9 f/ |& i+ O
& s o$ @! M# C) i+ l2 n1 R
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:4 Y6 Z1 u/ }" z4 ~+ w
8 a- P Z9 l& L j6 A; b# ?3 u
```
' d% W; [9 o% H# D# x) n1 q add_action('init', 'create_custom_post_type');2 q0 _4 f. ^7 C7 |
function create_custom_post_type() {& t# I/ k0 x1 d0 `0 e% F
$labels = array(' w5 V/ [# ~, @
'name' => 'Site Wide Notices',
( O- A) u4 c- |2 a" T 'singular_name' => 'Site Wide Notice',% \, [3 `4 K' {
'add_new' => 'Add New',
2 l) e+ |( N7 c9 o- B" a8 ] 'add_new_item' => 'Add New Site Wide Notice',
' ]6 V2 ]' ?2 d' @: D) I+ M( I- y 'edit_item' => 'Edit Site Wide Notice',
4 u* Y$ d' v# I0 Q+ O 'new_item' => 'New Site Wide Notice',
) m8 \3 Z0 `" E8 \$ C/ [ 'view_item' => 'View Site Wide Notice',5 ~& A! M0 w2 I# o, w1 z+ W$ o$ C
'search_items' => 'Search Site Wide Notices',
! F5 D) s/ t, h" D) d+ j+ i 'not_found' => 'No site-wide notices found',. P2 X# o* c1 n q+ ]; Y% [0 I
'not_found_in_trash' => 'No site-wide notices found in trash'
! N) a$ r2 z7 K+ u% K );- e! e8 R; k8 M, e% e6 y- S( C* t
* ?# Y9 r% |& T5 M9 P$ I
$args = array(# D' N1 l, V* l
'labels' => $labels,) `; F1 v& q. o2 u9 e
'public' => true,; b, `. Y$ f( R( ^+ R. L+ Q4 ?) Q
'has_archive' => true,& p; d& v ^. [ M3 X8 M: U, j
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
, }( h3 U" r2 r( ] i 'taxonomies' => array('category', 'post_tag'),
9 |" X, J8 a0 S) O 'menu_icon' => 'dashicons-megaphone',4 [/ Y4 t3 L6 r4 M8 e- m5 t
'menu_position' => 5,
* V/ n9 ?+ |7 M 'rewrite' => array('slug' => 'site-wide-notices')
+ O9 M/ }$ X( [( D, c' Q! X% A! \% h );5 r6 s7 v* q6 n
: G U# {* T1 y2 |* x p- A+ Z: o register_post_type('site-wide-notices', $args);
! D/ t1 p0 z) m; y }
7 o7 L( q4 p" k4 u0 q ```3 Y6 `+ i( H5 C: E+ L" d2 I
% `# q' k1 p/ N+ |+ I$ c; x 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。- q+ U$ W) o! \
9 i. I) Z! Q6 l' F; S; ]5 h9 p( w3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
. ?/ z+ V8 L/ `/ q" S
! i1 `+ E( U9 f& y2 Y) u$ d ```, a: b; M: `6 o. c4 C! Z
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');2 n1 X4 S! B4 I9 P' V Q' [
function add_site_wide_notices_boxes() {5 |/ V% P% D( Z) v5 n5 O
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');2 s% R; X- {0 O+ p6 M
}% ^+ u# o, a* y0 ] U3 C
4 _6 T0 [& ^, f* y8 s/ U# ~. a1 l& q6 c
function notice_details_meta_box($post) {. k6 M9 G% E, {, @' V0 ~
wp_nonce_field(basename(__FILE__), 'notices_nonce');
2 E$ s- v8 @/ G# K6 ?1 m% c $notice_title = get_post_meta($post->ID, 'notice_title', true);+ t* R( O5 n7 u2 Z% v
$notice_content = get_post_meta($post->ID, 'notice_content', true);5 q- p0 @ Z8 I7 w
?>, O. v* v9 v" H4 R" E6 n# C
<p>
* ~8 S( k- {% ]3 J' F3 O9 X( G <label for="notice-title">Notice Title</label><br>
5 [& V/ P+ M, ~9 q, N, q <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">4 O' T% _7 }8 I" }- B
</p>
5 C; ~7 ?$ g, z' p0 p+ n. j3 N. N <p>7 c6 s/ i0 t- D4 [
<label for="notice-content">Notice Content</label><br>8 Y/ q5 o$ ? E! s. r
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
$ C. \! e5 i. {5 p5 ?. D9 S" U </p>
) m4 v- m$ h( b- T, C. q <?php: S7 n8 u% r8 l2 k0 B. a8 r5 L
}$ U7 o9 `" B5 p: Z5 f" Y5 [, ]" B) F
" C: s" Z) s% E4 f6 d
add_action('save_post', 'save_site_wide_notice_meta_box');
5 _1 E0 L! U3 y. h, p function save_site_wide_notice_meta_box($post_id) {6 }( X* T) `- c2 K' ^7 k8 |' J1 K
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))& D8 Q8 R, Y3 V% j
return;
) V/ b% Z: g: C" r4 O) e if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
6 M/ B J' G0 Y* r& S return;
# E# _+ A: _6 j( ?+ C
9 k! C- S4 U: n if (isset($_POST['notice_title'])) {
; F7 M1 x1 G+ N' G/ [% ^: F9 ]: R/ e update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));0 x3 w1 T5 ]3 r4 q% G' t
}
( Z e) h- D. q+ e) b! j if (isset($_POST['notice_content'])) {6 f) C; h7 H# n6 ^% W
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));6 k; U9 j$ K( h/ y
}' o2 s5 S$ l9 H1 G* f8 e
}
0 k0 Z+ c% J; A2 N% q# P ```
% j- ~; t c3 a$ g" m# z2 R+ ` l. j/ V" d' r4 d" H+ x
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
5 t) @% u$ u4 Z- ?& {, I3 M6 W. V' E3 q+ j
+ p. ?5 ~# ?' m( l x8 N4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:$ i* S: i! `8 `8 z
5 G# J' K8 V' E# Z" @" ^
```
1 ~/ X! Z& x2 X9 g; i3 C $args = array(
6 W8 c0 N# _7 q. h1 I2 F* @ 'post_type' => 'site-wide-notices',/ i. A4 b7 ?1 g( `/ Z6 k
'posts_per_page' => 3,7 w7 m* `( v4 t* K# K5 Q: j
'order' => 'DESC',( j1 D; i# _% k# e
'orderby' => 'date': q( L; E* r( L. T; f( e
);
$ e; N) o6 |& q+ J& m1 N $query = new WP_Query($args); X5 M2 w4 K; @7 |8 \0 @
if ($query->have_posts()) :
8 A' j, p: F' ? C: W: W9 W while ($query->have_posts()) : $query->the_post(); ?>
: }0 J& ~& `" Y9 o& y0 f! q! s, t Q <div class="notice">
& P& F& ^! b4 ], t* }2 h k <h3><?php the_title(); ?></h3>
* v7 M/ v0 [' ~ <div class="notice-content"><?php the_content(); ?></div>. x S+ @+ {9 o& Y# o! U7 ~
</div>
) w4 g$ c8 A) v <?php endwhile;
' s4 ~5 r; x! f6 ]' [; G wp_reset_postdata();- m- c, G! @6 }5 c
endif;
2 p8 R6 v a8 @( S4 \; B, N ```9 b6 P- t( R% T# _$ ?
$ L' T9 \, s4 W
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|