|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
5 @1 I5 p! _, z- x: }$ [9 b! l7 A8 z5 k2 u2 X
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。- c. N% a' c& P9 }* [
& v5 T8 z" [! T+ U- K" j5 l以下是创建自定义插件的步骤:( g1 ]6 S) C- X! m# z
7 Z) I& R" f7 u0 j8 J1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:/ T7 ^+ F& v1 p; A" q% e; R3 M
/ c. E; l% g" S5 y! B' \
```
s* a6 O0 X; M+ p <?php
! Z; ?" K6 h: [' j( L5 m /*; @/ c: ~. @2 _
Plugin Name: Site Wide Notices Plugin
( f& i; A" }$ I Description: Adds a new custom post type for site-wide notices.
, Q; I; M& ^ r Version: 1.0
' J! g+ Z* g2 |) x Author: Your Name
2 E1 `0 B, D: E; Q& l! t Author URI: http://example.com6 j1 V/ _1 H% M( i1 F
*/
7 m4 B( u' l4 L4 M* x. Q. ~8 s- i! ?6 G. K+ ]4 v# W; j" S9 Q. I
// Add plugin code here...5 f& h4 v H, `
```
' ^6 T" r5 n j
, x9 k6 p" o3 g7 d. ?# Y5 _ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。* s0 c5 z C: s) O
$ L3 C/ W3 w- o6 d
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:9 B3 f5 K6 W7 k5 v( {
8 M Z' C0 x+ ~# Z8 k) M ```+ \$ g2 H# A( E; `. f& P1 k* C
add_action('init', 'create_custom_post_type');7 t Y" R; Z9 x$ C
function create_custom_post_type() {* n8 Z& g0 r* `$ @
$labels = array(
& O" N8 E* P- L" L4 p. N5 c: m 'name' => 'Site Wide Notices',3 s+ E+ ?' ?/ {! L7 S- h* @' Y9 A
'singular_name' => 'Site Wide Notice',# M; |8 K, R& X( O
'add_new' => 'Add New',
" W& y1 F3 ?6 _7 y 'add_new_item' => 'Add New Site Wide Notice',1 R3 T0 x' @+ B, Q6 f
'edit_item' => 'Edit Site Wide Notice',
; ?+ K/ P# x4 ?7 p/ ~/ B 'new_item' => 'New Site Wide Notice',
+ T9 v0 g* p5 s4 P! X 'view_item' => 'View Site Wide Notice',2 s! O$ U% K( T: Z1 Z" W
'search_items' => 'Search Site Wide Notices',
2 |, `" D* L5 o 'not_found' => 'No site-wide notices found',! d( v9 t; u8 g5 ], V
'not_found_in_trash' => 'No site-wide notices found in trash'+ I) q d" A! K, P1 _4 T
);& n/ _8 F8 K5 F6 a* |
6 L9 ~4 m4 w& o* J$ r $args = array(
$ @; F+ H N: A# ]! \* |+ h* ? 'labels' => $labels,
9 @7 }9 T0 t0 G5 {8 Q. {+ J 'public' => true,6 X4 r) C7 U7 A7 X1 _8 F
'has_archive' => true,
8 ~2 o9 i- k+ V0 g# f: z5 u# j 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
5 H6 T9 B" U9 R3 T2 e 'taxonomies' => array('category', 'post_tag'),, U( ?7 v9 ~; g- L1 I1 N& o
'menu_icon' => 'dashicons-megaphone',
8 O6 Y6 A9 h4 W; A- v 'menu_position' => 5,; ^6 p9 @ f1 W$ W0 u( C9 d
'rewrite' => array('slug' => 'site-wide-notices'): _" S8 _; U, G$ P* E
);
! q% ~6 H S# N5 C
+ v2 r3 H* M7 G2 `5 Q9 ^3 Z register_post_type('site-wide-notices', $args);
6 a; J0 w- `1 b( F2 I8 ~ }
' w9 |7 v& W; ]8 U! }3 Q2 I ```: V, u$ j+ Y' G
+ l* F5 Q- E: U
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。* i7 o3 O* B) U" v j
2 v6 M, Y( B$ K& _. T% t v7 D3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:' ?/ H4 q/ k. ~/ G0 {' I
# f) V7 g* v' r, Z# b ```
. Q! U* F/ {) M5 w$ G% E$ o; s8 y add_action('add_meta_boxes', 'add_site_wide_notices_boxes');0 p& c# C: |% r
function add_site_wide_notices_boxes() {
5 ^; t, |7 j3 a+ @ add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
" A, ]7 u, K) d! E- |/ C$ ~ }
: z4 r' a9 W; A$ D
+ W- Y+ y) y8 G3 M- I function notice_details_meta_box($post) {
8 r; R8 ~+ A" ^ i wp_nonce_field(basename(__FILE__), 'notices_nonce');
( b8 [- H4 F. R6 Y! c8 f1 V $notice_title = get_post_meta($post->ID, 'notice_title', true);' W( I/ u) t1 o; I
$notice_content = get_post_meta($post->ID, 'notice_content', true);
v2 {- g2 k e2 G; S+ Z) t8 k ?>5 B' C" p1 w+ i1 ?) D
<p>* \. `* {- m" _8 d, l
<label for="notice-title">Notice Title</label><br>( f1 _% N4 o9 \
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
5 B% m1 a8 f" m) U </p>
3 C3 r3 x4 ]. D$ p# y6 _2 `- g# y <p>- S" }( f2 l9 j' \
<label for="notice-content">Notice Content</label><br>4 S9 e% n% X% Q- t2 \
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
4 x `8 i" h A% _" _$ b </p>( y$ ?: a% x1 q) y# V
<?php. @/ C, h& A& y# Q/ |
}) ]+ D. x$ F8 K+ ~0 m1 s
6 B* M% R5 L1 F% i) A add_action('save_post', 'save_site_wide_notice_meta_box');
2 H" c1 U I* A' r. w0 X8 k! X function save_site_wide_notice_meta_box($post_id) {5 g8 v1 X# x: o9 K, J/ Y1 H
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
. S# M% }, X7 x3 w return;
. L; o! b. N }6 \! D if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
! ~& L4 j: j* C7 R; _3 _: Y return;
( M% |! _. T. r) Y8 O& I* m
1 g6 m" o! u2 }" @ if (isset($_POST['notice_title'])) {8 r5 s; R9 c, R5 B
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));; E2 n7 s0 K' x: `9 E6 r
}
, I% b' S7 ~( }8 P- |! D4 ^ if (isset($_POST['notice_content'])) {
6 A: L6 V; U" d$ ? update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
* a5 \ E& f4 g }
1 a4 G; B! p T* h; Y }
2 g [$ ]( e% P: s4 I V; z ```+ J, ^( \/ B. t) s3 Q. B
6 C! a8 R2 {. @( w& @ 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。: f$ `) e. y8 B
! o" q" s6 Y5 m1 h4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:# _- d8 E9 h+ f) Q! j _
; W( x, |7 R( Y# x
```
6 l3 B# r& i& o6 s( {" R% l $args = array(( O( l& `( m+ m+ X% s9 R3 i
'post_type' => 'site-wide-notices',
1 E. S- L+ j9 z) @( r1 h" G 'posts_per_page' => 3,
- q% V/ Y/ m! s5 Y$ q5 z* U0 Z7 e 'order' => 'DESC',
9 x% [; Z {; O" ~/ I, k8 o+ H1 k 'orderby' => 'date'
) l0 }4 [; L% C6 Q3 K- U );) N7 |" {! ]2 |# p' J
$query = new WP_Query($args); y- E* i, }) V
if ($query->have_posts()) :7 w* w8 n, u/ n+ }2 I% n" e. j
while ($query->have_posts()) : $query->the_post(); ?>
8 V; L, N9 m) I# r8 g, @* d* t6 z9 b <div class="notice">
9 W' L: l" C3 q5 @! u0 \ <h3><?php the_title(); ?></h3>% l: g. d4 I5 ~* e& N
<div class="notice-content"><?php the_content(); ?></div>
' w3 i* o9 y9 D. y2 L. i5 j </div>
. \/ T W2 H. |2 R, B9 @3 P$ ~ <?php endwhile;
g4 z" \4 ^3 e0 i7 O# S wp_reset_postdata();
: ^- g" o2 q7 ^& @$ A: k endif;4 T7 ~$ U6 _ N2 M
```
4 {' W p3 M1 V8 C6 d7 P! g% L" Q- e5 X/ ^2 s
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|