|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?$ B/ s# T' v' p# @+ p- G9 z3 N
{: k0 Q$ M/ W, v7 g, O
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
* n2 Z9 U. g, H7 C0 D5 q; S! k/ P! p% E0 l+ E" k) {
以下是创建自定义插件的步骤:
4 r+ _& d' w, s% ]% q( K+ F
; S: j/ |$ W- F1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
$ i+ G R+ Z. m! n7 w( `& t. m/ H+ y
& x& Z# G+ c Q- z/ I1 A ```, j1 H1 m2 s5 U: W5 W
<?php
- a D: d( q$ w0 m /*
) A* G( r1 _ C/ f Plugin Name: Site Wide Notices Plugin
0 B" t ]* `) P. Y, q Description: Adds a new custom post type for site-wide notices.; m" j: S8 m) r7 R' n0 j1 n
Version: 1.0 v- ]/ @ @/ g ~7 c
Author: Your Name
; L4 Z ~4 `8 X+ m, P Author URI: http://example.com
0 A# `) F& r i1 V U6 w7 t */: M" `; n! U( c" w
/ U6 g/ ^/ O! ~% H // Add plugin code here..." m+ d h4 M8 r7 u
```3 }9 f) M+ D6 q/ c" \! x# G# ~1 i
' B& z/ f. n( S9 h2 p# _ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。2 k0 v. p' | h, x+ P
n# z% M6 ~1 W0 P( C1 e
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
+ r3 j5 I" ~" B- z# _3 ?2 \. ?5 e. M* u
```
+ E: N& Z; W$ b4 Y5 B add_action('init', 'create_custom_post_type');
0 D- ^4 P9 {: y, p# {( L) ~! F function create_custom_post_type() {) `1 K4 U( v+ c2 k# P
$labels = array(4 F" a% p+ V0 e
'name' => 'Site Wide Notices',7 D0 A- v+ S: G# C; }5 s! k2 L
'singular_name' => 'Site Wide Notice',3 l. b8 M3 f k6 g% k! l+ [
'add_new' => 'Add New',/ q3 z, w3 e: J7 N r; \0 A
'add_new_item' => 'Add New Site Wide Notice',
, N, w+ k1 J+ h! h0 X 'edit_item' => 'Edit Site Wide Notice',
! ~3 r3 d' d5 d+ d 'new_item' => 'New Site Wide Notice',2 j8 F' ?9 P. G& }7 C; H
'view_item' => 'View Site Wide Notice',5 a' ]& Z6 ]# ^$ `0 q" c
'search_items' => 'Search Site Wide Notices',- v) v! _% m N9 E: O* W
'not_found' => 'No site-wide notices found',1 F1 \3 p+ f7 n6 A( m0 ?, j. p
'not_found_in_trash' => 'No site-wide notices found in trash'
& B" I+ H+ q/ Q+ q7 M1 { );
D. q' }( ?" p9 c+ Q; i- }6 T& K3 h' m# }. }6 R
$args = array(1 X$ ^8 M J3 Y
'labels' => $labels,8 w- H" e3 O! R
'public' => true,
# @3 b; K8 k* s2 c" J6 J 'has_archive' => true,; i% V0 K+ g2 e( R# S9 Z n
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
0 u7 p* }' o4 l8 ^, ?9 Z. f 'taxonomies' => array('category', 'post_tag'),1 `# O2 }+ i5 D/ c, K) M9 G2 ^( k
'menu_icon' => 'dashicons-megaphone',
+ v' ~: {# ?& }8 `; l( B 'menu_position' => 5,
, j8 U4 r- G4 d8 c9 ]3 ]( \! q 'rewrite' => array('slug' => 'site-wide-notices')" U+ ^2 ^6 j) [& A" ^( f
);
& ~/ y5 c2 j- @+ u6 }# f7 d( w$ R$ Y6 {3 Z/ s
register_post_type('site-wide-notices', $args);
y; @9 L% N, e# j% k1 ?0 @ }, P# i) Q9 n! F5 C# ~0 s7 k- e
```* M( L! x9 b- K) V) B4 t8 Y
8 G9 C- W! D- V. `
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
4 l0 V. H7 r+ y; [1 g3 ~/ ^$ k% O8 x: C; ?
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
9 W3 _& ?5 L* E+ }
9 O: }# h6 n. i$ C/ W) H5 z+ \* {5 M1 P ```
% |1 k j- m9 l& T6 h3 Q7 f" J* b$ W add_action('add_meta_boxes', 'add_site_wide_notices_boxes');4 H* ^$ l4 y& A7 I
function add_site_wide_notices_boxes() {
" b- e; [; P, {: W6 H3 c add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');+ U1 _! v% [1 v+ `2 R6 O5 y
}3 f5 b3 j1 V' q# m& C) \9 u3 q
8 w! O" q1 T/ V4 w
function notice_details_meta_box($post) {
; c+ X4 f+ ^- {8 J) m wp_nonce_field(basename(__FILE__), 'notices_nonce');! q* d3 q. O+ ^" r5 V* s1 ^
$notice_title = get_post_meta($post->ID, 'notice_title', true);; D; Z+ q+ U6 R1 c2 G8 U
$notice_content = get_post_meta($post->ID, 'notice_content', true);5 |5 H: p1 \! Z1 z# r% B
?>
/ W) N( x7 z/ a/ ?/ t7 v+ Q <p>0 E+ G. E9 l/ N. i
<label for="notice-title">Notice Title</label><br>. H9 {9 n' p/ |* R% e8 [
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">2 Y0 Q- l* C8 Y! W
</p>
9 h4 g. z7 M; ?# ^4 v <p>" Q# i/ A0 w$ l. T
<label for="notice-content">Notice Content</label><br>" o& e3 z. N/ F3 r. x. x# x
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
- |9 C# z5 Q; ^' S* h+ M7 {' c </p>
+ {! F% \1 w8 C* s; `) X* V+ s <?php
, k. D5 u! b# ]1 [ }" B: _ c; B. D* p0 G0 ^4 b
4 C6 y+ l3 H1 v3 M
add_action('save_post', 'save_site_wide_notice_meta_box');% ]$ W3 a% ?+ \$ q) E
function save_site_wide_notice_meta_box($post_id) {
! a/ l$ X1 b" o# V7 [ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))); T1 O+ E- @7 ~) }: n
return;, U$ t+ _+ y+ N9 |+ a# [2 ~
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)# z" l! O8 o& F: N% t2 q8 e
return;
/ g, x% ~3 c1 ]
8 p/ ?: A: v3 ~# p: u8 o if (isset($_POST['notice_title'])) {
' K6 L" c8 O7 ~5 Z* X7 Y$ g9 M( | update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
* J. K+ {* n! t& J4 Q2 A }, S @: N, ~- b/ M! N, M1 u- W
if (isset($_POST['notice_content'])) {8 N% c0 `5 s2 |/ y+ \
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
0 O5 y; }7 [* N' Y, L- [1 \ }; t( r* E1 ]+ z/ r9 Y
}# M; U- m# `" p5 p) L% F
```
# i: g7 M4 Q% D/ X6 K" M1 f: [. V% D7 ]& q/ y4 _1 x, S) S6 d' L
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
1 D( T- a: Z# P9 j/ d5 A" m" ?* n9 e; y# M; w4 g. K" X' D
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
% r; O0 |% h8 ~5 @9 O# J& ?- j. m8 @$ Q9 c; B) \
```- U8 X+ w" v; d2 g
$args = array(
6 y7 I- Y. v4 u$ q% ` 'post_type' => 'site-wide-notices',
1 I- J) M) Y7 z' y" \/ _! t2 ~ 'posts_per_page' => 3,4 J' M6 S0 n, J; y$ E$ W
'order' => 'DESC',) I; w* l) Y0 I5 n; q R; J' }* d& F" D8 x
'orderby' => 'date'
: U. q, u2 j0 r2 n$ x ); m' V& J; ]) V* L+ d
$query = new WP_Query($args);( E5 |# f' f) i5 T9 @
if ($query->have_posts()) :: C/ h) r* g, f+ Q
while ($query->have_posts()) : $query->the_post(); ?>
) q1 R3 R, N# z$ a <div class="notice">1 K6 ^7 Y* n' r F; m
<h3><?php the_title(); ?></h3>
% P! Q6 R8 Y3 U+ {5 ]) Z. b3 R <div class="notice-content"><?php the_content(); ?></div>
- q( ^4 ~( _; a4 D o </div>* r+ p, J/ C8 Z- M% K
<?php endwhile;
5 D2 [! d6 j3 i# [, p9 N wp_reset_postdata();
?9 ~( p3 |& n% g y3 { endif;- c# k' @# d6 o, p
```
+ v& s3 C1 o1 _# z5 c% W$ f9 j4 L' \7 I' I8 M% P% w1 S
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|