|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
2 n9 A7 u2 i- q4 |& N+ x! @/ B" \, w5 K0 H
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
. Z1 U/ s$ k( C7 |0 v' C) ?# `+ e) ]. H' V1 S9 ?/ o* D/ o
以下是创建自定义插件的步骤:. L c* q- M9 l; b( h
?8 W3 ~& i, D/ E1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
+ B/ i' v& i# S. {0 E& ?+ u' S
1 l2 X" G: J9 `# X' }- g' q ```
8 D4 x8 Q" t# T* r9 m( R <?php( e$ L1 @3 F( ~) L
/*
# L0 Z0 r3 }: d6 c" Y$ A3 i; U Plugin Name: Site Wide Notices Plugin6 j# K* Y2 t/ E. p- \1 x& J! W
Description: Adds a new custom post type for site-wide notices.
% I; h* R7 V( e$ y Version: 1.09 J Z* y, y' r3 P+ W" @
Author: Your Name+ ^/ y9 w+ n6 {$ ]# o0 Y
Author URI: http://example.com
$ e' g8 J9 t. p: G& ]1 J+ K9 m, [ */2 r" I8 l" s" b! L' ^ \. j
& z5 g1 ^0 {# q; o9 X6 |+ c- D
// Add plugin code here...& m6 c& i* X2 k- v7 h! K4 l8 ^6 h3 o6 @
```
* o& Z! R& L9 [5 c9 s+ K/ X$ O D& @4 v6 N
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
: f" l1 J5 U& n" `
# j& z& r! V8 a) m0 \* R$ f/ c2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:6 v( i: i% J- Y7 J; W4 f
4 S3 a+ t( b; E2 J. q+ c
```
* ?9 e$ j# w8 [0 {( G: { add_action('init', 'create_custom_post_type');3 c6 R2 ]* @& ~+ ?) m. h; P: D
function create_custom_post_type() {
/ Q; i2 W T/ z* Y) { $labels = array($ C s: y1 C4 O# o
'name' => 'Site Wide Notices',
) G9 X: n3 ]: I) F M' ~9 e 'singular_name' => 'Site Wide Notice',
. m- h5 a+ d' c 'add_new' => 'Add New',
9 `& d( Z6 e* b$ N# h3 w/ D3 ` Q/ R 'add_new_item' => 'Add New Site Wide Notice',
9 ~2 f. F& E4 V" n9 c 'edit_item' => 'Edit Site Wide Notice',: ?$ k# z" ?- o. [% u9 W
'new_item' => 'New Site Wide Notice',
4 `$ V7 t8 J: h! r$ C, ?; Z: | 'view_item' => 'View Site Wide Notice'," D: e7 Z2 s& f, U" e6 H
'search_items' => 'Search Site Wide Notices',! a7 t$ Q! V7 X3 K' {- g
'not_found' => 'No site-wide notices found',
+ S8 ~" Q I, ^$ f1 Z- ` 'not_found_in_trash' => 'No site-wide notices found in trash'4 B( r& ] S" W( @7 P4 T+ X
);
- }& K) c5 x5 D p/ S8 V
7 G$ c6 t, |/ O: t1 `( [6 \8 M& b, H $args = array(. q) U4 p' c7 P& f! D* H) T
'labels' => $labels,0 k2 c; ?' b% ]! h3 j4 [3 m& p
'public' => true,
2 H) B# ^; X5 N$ e 'has_archive' => true,( Y: x. |" U5 y" ~6 x7 w
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),* C5 N4 Y5 Q$ d4 ~
'taxonomies' => array('category', 'post_tag'),
5 M' R) ?$ [/ V9 \5 a7 t 'menu_icon' => 'dashicons-megaphone',
/ p% {, d; r/ c' h9 T/ d 'menu_position' => 5,
) V8 N# B8 `0 p9 ]' M 'rewrite' => array('slug' => 'site-wide-notices')$ e: |" F' \4 n7 A% u Q. d. J
);
$ V/ n2 ^# Q" `& E+ h+ N0 `
; I4 k/ E4 ?) @' P, a register_post_type('site-wide-notices', $args);
# s; D w; \' j+ k. z9 p }
; D. V+ a; [; t1 L9 X7 S$ `; s! c ```; E: n- L5 N. y1 V4 A
# K' L( N ~* p. S 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。- e0 R6 z* p0 ]" U/ ]
, t& P1 D: y/ j8 @ w0 r& x& K3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
) U* D6 s) U$ i- F+ a" v, c( y- G8 f8 ?) r% K; q0 O
```
# T+ c7 w: o& c; E0 M add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
% \+ f3 b$ S! D' e& ] function add_site_wide_notices_boxes() {( D( A# T3 B8 O! E/ H7 P5 w
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
5 J/ N# `6 Q# C }
7 y! k3 z+ k0 p+ c Y3 n3 y* R
function notice_details_meta_box($post) {
# D! z; c2 R$ p wp_nonce_field(basename(__FILE__), 'notices_nonce');: t9 |4 a! Y: \. s" _
$notice_title = get_post_meta($post->ID, 'notice_title', true);; q& S* z. n( U+ V
$notice_content = get_post_meta($post->ID, 'notice_content', true);
9 [( R. S8 C; O. s& a# C ?># r- h0 @" \1 A/ p( T" g, @! d3 ^7 A
<p>
1 j, O5 W$ Q5 w( u4 M: a8 w7 u <label for="notice-title">Notice Title</label><br>! G' q) q, v3 w( ~7 Q" k2 b8 x
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
4 w- `$ a% | S4 U% W% T </p>
: e4 z% N1 _6 A <p>
5 v0 e4 n& c9 X- Y, |3 Z <label for="notice-content">Notice Content</label><br>
3 ]$ c# R% t# g3 _; B) G <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>, P. [6 W) Z2 v" K& D7 G
</p>6 s% E# z; [* G* ` s, w f
<?php# n; y4 Q F2 t; x! A0 g& p: E1 w* s
}4 F7 S+ e3 J! T
# o/ c) Z) h2 v. n6 J! D
add_action('save_post', 'save_site_wide_notice_meta_box');2 v, [% [+ r( h; ] V
function save_site_wide_notice_meta_box($post_id) {
G- b# Q- @1 r- U e if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
P- o+ q7 \0 y. H return;. W5 E) b7 e; t- D
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)9 X9 b; E" G( o1 M$ T
return;
$ R. v; x$ N& Y1 A+ N" F" Z( X) u9 H: J$ t
if (isset($_POST['notice_title'])) {
5 b' J- ^2 t; c+ H update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
2 J9 `$ O8 g" O2 t }
; x3 P$ }4 x) z, [8 g if (isset($_POST['notice_content'])) {
0 f' u* f7 F6 x: W update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
4 q _+ Z. z# j }
3 p1 w( g' V# [ }
O- U" K# ^2 G4 W0 N ```4 j0 B6 d/ _5 M0 c
, k1 ? d" P* ^& d 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。$ e$ U" j$ N6 G4 i$ D
; m# L' L& W" s4 J) j' l4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
1 l3 S, t" Q4 ~
) j/ ?* X7 V" I' O7 ] ```
( t2 n) b2 c; H! A" L/ R $args = array(: W! c; ]- S6 G; o. [4 `7 B+ Y0 E
'post_type' => 'site-wide-notices',
; \ _8 P7 N- K8 V8 F 'posts_per_page' => 3,
. {/ x# B/ d$ e7 u 'order' => 'DESC',4 x4 s0 y4 }3 n1 r) X+ K
'orderby' => 'date'
0 |+ k% q X4 m" @, b) L( s );# `8 `2 D, M/ {/ a" q& ~
$query = new WP_Query($args);
5 O3 v# ]$ [& h if ($query->have_posts()) :2 x! v, L' Q& H& C# R7 m# x m
while ($query->have_posts()) : $query->the_post(); ?>$ y4 M. [& F) u3 g3 t
<div class="notice">3 v, q) D( Q9 x4 W& T' r% z
<h3><?php the_title(); ?></h3>
3 X5 b( E( o# ~' o$ h1 b% y <div class="notice-content"><?php the_content(); ?></div>
3 w+ \& [+ ?# q# z* V </div>
! F1 S( ^, G- T/ b <?php endwhile;; O& L5 Y$ r0 q7 v
wp_reset_postdata();
" g% j8 U+ _, A3 S5 d) P4 {/ j endif;+ h( a/ X/ E; R, h6 u
```
1 f- ~; d. K7 a* _
* J8 O- w; u( w3 l! ]! X0 z7 @ 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|