|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
N$ d& g% D! U; ]; X8 H) Y2 L' \1 N9 ?! }
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
5 b. @4 e. S' O! D8 e a6 ]; @0 b
G. P {7 V' c- _0 ~+ r" @. i' v- q以下是创建自定义插件的步骤:0 _$ u* R2 }' U" h) i2 w
7 p7 S1 h. f; b1 v t2 I! V; A1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
* a$ ~# Z/ g. a1 t' \% _" S
; W z) D3 W+ | ```
# Y4 H! i" x% L+ }( g; V/ p, [ <?php
t1 D, s P3 u8 L, { /*3 C ? O( L* k Q2 t+ G) `
Plugin Name: Site Wide Notices Plugin$ i/ _, G: c& u
Description: Adds a new custom post type for site-wide notices.2 z5 n; S4 ]8 I# ^
Version: 1.0, H3 y) z! b+ }" z- `" N
Author: Your Name
, O5 G# g9 E% |/ f! C9 } Author URI: http://example.com& m# N6 I3 }( a7 y1 x ~( ~
*/# l! c' e2 |- t2 v) c& \1 w: \7 J' m
# t ]0 z9 x9 i+ B; g
// Add plugin code here...
7 p- t. ?# @1 E6 U ```/ c3 W ^5 X( Z- z
3 u3 W) z3 M+ T6 U 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
' p2 l& K% D0 p& `: ?7 [! P; d6 N
5 c6 T3 @4 ~; ]6 e2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
9 w1 V. ^" v" l" i0 b+ h3 i
8 V- i+ j- s' b! I ```
, p& d q: T% a# F4 v' V add_action('init', 'create_custom_post_type');/ h7 O9 Q& H( Y
function create_custom_post_type() {* V# m* M- A, N' n c' T& e. z9 i/ j
$labels = array(
" M0 Q1 _. f6 W' y4 T$ F 'name' => 'Site Wide Notices',0 r3 f4 y$ P+ y- Z6 G5 @! ~
'singular_name' => 'Site Wide Notice',4 w5 m8 L8 J) U+ T. {
'add_new' => 'Add New',: f2 N% ~: i! u8 L. r4 y
'add_new_item' => 'Add New Site Wide Notice',$ s. X) E! I7 V& @/ f0 t* R
'edit_item' => 'Edit Site Wide Notice',
$ n( X, \8 I. d6 |3 B: W' ]. ^$ t& I 'new_item' => 'New Site Wide Notice',2 t' U3 }, n; B4 G/ X0 z
'view_item' => 'View Site Wide Notice',9 i9 a+ P1 H) n5 D; f% `3 Z$ T
'search_items' => 'Search Site Wide Notices',
# Q1 J" C& R* d v1 [0 R; v 'not_found' => 'No site-wide notices found',
$ l# n+ n s- m, F# Z' z( ? 'not_found_in_trash' => 'No site-wide notices found in trash'& m& d. a* O( f% x
);
& R: f6 M& F2 b3 D1 y5 o6 M. @7 Q" P1 R5 f
$args = array(8 u a: z" R& ^) y$ f) x6 j3 M9 N
'labels' => $labels,
# o0 Z$ k# `* h4 G* n( X 'public' => true,+ _. j, w# B% e
'has_archive' => true,
; j; @3 x7 n2 V O 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
* i5 g# j. Q4 i$ U, a! P; o& f 'taxonomies' => array('category', 'post_tag'),: {0 }9 `; X* K& Q/ q# ]
'menu_icon' => 'dashicons-megaphone',3 p7 {+ e( C! @$ f/ {6 U( Z2 o
'menu_position' => 5,- Z5 `" c$ Z7 N$ K
'rewrite' => array('slug' => 'site-wide-notices')- k y9 g5 I: H
);
- v! f c: e, v& ^ x! d, b/ p) T" o n% C5 U: m
register_post_type('site-wide-notices', $args);
7 ` n' F3 w5 o0 U5 z! u$ e }
' {8 L6 A8 `9 y8 S7 c7 @( n8 p ```/ U7 r/ B3 \ \/ M7 W& g+ V9 T1 L
, Q' l T+ b9 _2 C: ~ 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。/ T5 A0 O: Q0 I7 J+ T
/ P! x2 J; w! a) O9 Z M) l3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:% j P: z. w! P4 `5 m/ @* A
Y/ O( N+ D% _+ t+ v ```
5 U0 N* W' G2 B w5 E& k add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
4 B5 M# d* V+ r function add_site_wide_notices_boxes() {
! H0 U/ {( r- \" h) u2 Y. P add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');5 p$ r Q# \& B, C G: |) w' j
}; v% V# F+ X; L# a( @2 m$ F
( d8 U9 e, y2 M+ A function notice_details_meta_box($post) {
) b1 t. \ J# w P; Q& K( { wp_nonce_field(basename(__FILE__), 'notices_nonce'); a2 k0 o# F0 D
$notice_title = get_post_meta($post->ID, 'notice_title', true);* _1 P3 a D' P5 y" t! U
$notice_content = get_post_meta($post->ID, 'notice_content', true);
/ F) @) p- _+ m& ?2 ]& ~ ?>
1 C/ A3 P6 t& U/ x3 ^* e <p>: v6 p7 e9 ]( r4 z$ O7 A% s4 K4 t
<label for="notice-title">Notice Title</label><br>9 O# n7 `1 l! |: e
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
2 q+ N( R# O# w3 _% Q% A6 k </p>9 W( S) s+ Y3 q% F( ?4 ]1 Y6 |+ K
<p>
- J6 A9 h. `9 i9 e <label for="notice-content">Notice Content</label><br>
1 s: r' ^2 f0 e2 D: d <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>8 f8 _% m" }% }% `; _" @! R/ t
</p>& V4 u) m6 _5 M- g+ X
<?php9 e1 M- p) _' P" G7 x" [
}1 H- A8 z( }$ J' D9 o/ r$ b
M* ^( x" {0 P6 v
add_action('save_post', 'save_site_wide_notice_meta_box');0 t" U3 g3 }, y! a6 w7 W/ j# L9 m) F
function save_site_wide_notice_meta_box($post_id) {
. A. o; {6 F" U5 r, b# b if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))+ M: K5 b$ D Q- O' M S
return;
' k7 v i3 s$ g. {) Q1 E if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)4 I( [) N4 j/ s4 F: F+ t0 x
return;" c0 E. i( ]3 I" D- {5 v7 J
0 l* `6 ]+ L7 \ S if (isset($_POST['notice_title'])) {
( b# g2 U, a( R; h& V update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));! L4 J3 w3 |$ ]7 W
}
& N; R0 p& y5 w( m( p if (isset($_POST['notice_content'])) {
. C! M+ G0 N. x update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
9 A0 m4 X X+ `5 `# j% ] }
6 O! V/ o! u6 {! f( a }, { ]1 O, D% Z5 h1 T* z. a1 \; W
```
; W6 p6 R3 n, [5 Y p( @) p J0 U
$ I2 W. X U$ ^+ g/ l 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
d; _5 j. ~ w4 ~) r, R% o3 ~. E
3 V, y1 |2 h6 c) j! T( y7 N2 N4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:7 {6 S* ]% U1 G5 N' I( L
9 S% U: ?. D# @% g1 F
```2 |0 f" L( [: ^
$args = array(: Z4 ~# O: f- ^" j O& \, H
'post_type' => 'site-wide-notices',; ~, \) k+ a& {3 Q: t) \# _
'posts_per_page' => 3,
5 i% Z: g# V& e8 [1 s8 \; m0 c# v 'order' => 'DESC',
0 }# m. R' B9 J 'orderby' => 'date'
# H" h) W0 m! c4 _$ Q c; r+ A );: m8 B' l Z: w0 M; G; Y# J- }% V
$query = new WP_Query($args);0 A. y5 l1 H. o* o1 P+ m {
if ($query->have_posts()) :
! u4 W( |1 t$ B" D- V; a3 z while ($query->have_posts()) : $query->the_post(); ?>2 x1 t8 S* n# r8 E. a
<div class="notice">; k/ l1 j0 G) [- h$ l
<h3><?php the_title(); ?></h3>& p: N, c* C; r
<div class="notice-content"><?php the_content(); ?></div>- X3 i0 W" W1 ~5 m" [; [5 G
</div>
5 m1 v6 n# k% n: E <?php endwhile;
! J3 L9 |! E! `5 F0 j wp_reset_postdata();
- ]" ^4 U, T7 X- p6 Y, | endif;
" E+ g8 M9 y: W9 i/ d4 e. U ```
8 I' [) G" t, Z$ _- {4 H; {
$ \. T& `# P Z" u1 ] 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|