|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?$ r3 ~# ` s& G
3 o! ]: w# G7 l, w, q
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
3 o+ X2 L7 |, @% m* E9 K, G7 J$ m* j+ W3 `
以下是创建自定义插件的步骤:
# k3 @- Y: Z8 T7 z2 i
0 Q3 c# L. V3 ` U# m+ [1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:, B) x, x" D6 e
% i- I+ R- F$ X$ M" ] P ```: Q' @6 W* o$ r+ M* _' W
<?php4 f1 {( s: c; O: p; k$ A; a
/*/ m) F8 {4 S i' h3 z
Plugin Name: Site Wide Notices Plugin( g b' }+ _, b( V4 I2 i
Description: Adds a new custom post type for site-wide notices.+ |) R" g V7 \8 q* n) R
Version: 1.09 e7 F3 R% p' q, P0 w3 N
Author: Your Name
" x) y3 a5 \& [: J0 B Author URI: http://example.com( q, K$ z3 K0 ?0 y" o& X+ d
*/- s$ p" S4 u# h( G, L
; V F3 n8 y8 i. B% [ // Add plugin code here...
# v7 o L, a" ` F ```
; q% i: ~/ |/ A" ~3 w' L1 {
- P% H f! V/ e& L: T 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
" O. V `5 L, C% S6 @0 q+ I. i* |% D9 g3 \ J
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:' U2 ?" @2 q1 X. j3 H+ {4 t. B
8 J. `6 [6 a3 b4 Q ```
& e: s* @6 f2 u$ X* ?+ \8 ` add_action('init', 'create_custom_post_type');: j, k: j+ G7 r3 }7 U
function create_custom_post_type() {) L$ k; ?2 w/ e7 }3 ~" D% n
$labels = array(& r" q- v7 X% s) Z
'name' => 'Site Wide Notices',
9 A. V2 _. g* t2 V9 v8 P. G5 A, p( V 'singular_name' => 'Site Wide Notice',+ I8 j n+ U {- O1 @
'add_new' => 'Add New',
8 L; [/ s& \( K: d: G( I+ ` 'add_new_item' => 'Add New Site Wide Notice',
8 v1 F) m& `' N9 L. O8 U4 T4 N 'edit_item' => 'Edit Site Wide Notice',
6 m6 c+ u1 L9 S5 t7 I+ ]' l9 \ 'new_item' => 'New Site Wide Notice',( `6 {, G0 [8 g
'view_item' => 'View Site Wide Notice',& N$ @# b9 ?+ g' J9 R5 p& T
'search_items' => 'Search Site Wide Notices',
$ y- C; T% f2 z2 C* Y* T* u' I 'not_found' => 'No site-wide notices found',# G' _1 z3 W2 c/ _. a
'not_found_in_trash' => 'No site-wide notices found in trash'
7 m2 ~+ f; G1 } );
$ e! q# G: A& O% I( w. a4 Q; H" o6 E7 h/ E. B- j9 r8 H
$args = array(
9 B$ C3 z0 d3 k0 m) ` 'labels' => $labels,4 ]9 X- u, f5 p& i# c8 l& q( T
'public' => true,
- W) g% G0 e1 Q0 h/ U' _# t 'has_archive' => true,
: ~) q3 ~; ?4 T; W$ L$ [ 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
: q5 h9 c, V( T) G& [6 F 'taxonomies' => array('category', 'post_tag'),
1 p- Z, d" l6 ?8 o 'menu_icon' => 'dashicons-megaphone',
0 W9 R# J! l3 K6 i; X9 z Q0 b 'menu_position' => 5,& J! d- g6 {* c: h
'rewrite' => array('slug' => 'site-wide-notices')) ?9 J4 _+ o$ a r% K9 q9 ~! I
);
2 n+ Z* t. K. y3 `) d! ]1 ?- [% a6 J+ I0 \# q' f- Q
register_post_type('site-wide-notices', $args);9 J: s0 F( E1 g8 T, Q' ]+ x
}
7 z2 X6 I% G, Z ```" A. ~9 v9 d( k O! u5 D
3 Q5 m0 F( Q: a9 M 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。9 E5 p( D# s" z! H# r
0 y& {# H' u/ g H8 J
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:% G7 J) o1 a, D. U9 ?
9 t# O1 @5 Z6 @/ t& \ ```
# q: Q/ Z- s, d2 p" _ add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
4 @( y S, u& M2 ~4 l! e/ X function add_site_wide_notices_boxes() {
1 r& a: P( M' L( e9 i, ]( F- b add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');# B# P1 [5 G9 G2 n
}
$ H# D" I/ f( A) z8 ` Y
; F/ ^& ]% @0 f. ] function notice_details_meta_box($post) {- ^: C2 z E4 f; b! I1 o
wp_nonce_field(basename(__FILE__), 'notices_nonce');# E* i" A- q) E1 [2 n
$notice_title = get_post_meta($post->ID, 'notice_title', true);
! P6 d6 q8 U. v$ H1 ^ $notice_content = get_post_meta($post->ID, 'notice_content', true);
! ^, x! g2 ^0 B2 ` ?>7 f) Y f% o& u3 n) ]; ?) N
<p>, D4 g! F/ U3 p' g
<label for="notice-title">Notice Title</label><br>
" c9 L! b& t1 x7 f4 e* l* T <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">6 x2 {# L% T, G: o0 N
</p>
4 V9 |/ A& m) Z! n7 N+ X <p>& z" m4 R5 l9 W" k/ M, m
<label for="notice-content">Notice Content</label><br>1 N# d+ @+ M+ l
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
" j z# {1 ~& k5 Z1 u </p>: U4 H3 C1 G0 @1 D, Y
<?php
5 M4 ]6 m" R6 w1 p. D }
. E) Z1 q& y4 w! l
2 v0 u1 A8 ^! ]+ V, L& B5 v add_action('save_post', 'save_site_wide_notice_meta_box');5 r. d( d3 ?% ^( E( [
function save_site_wide_notice_meta_box($post_id) {' H* R7 T! O- M2 Q
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))+ Y6 [/ N o( M4 C- ]' a5 G/ g
return;6 l5 I/ g6 O% F# q
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)& [: i$ a0 P6 G) ?* W6 g4 R/ f5 W. q
return;) Z: R3 u3 B) \) N2 \( C0 T" s+ I
( B7 p. L7 Z# o8 U* U3 {$ D/ H) l
if (isset($_POST['notice_title'])) {
; d4 }, Q( b2 e6 I" o; z update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
! s* p5 t! Z* x7 C8 V, N }6 T( \. T" {/ l7 L
if (isset($_POST['notice_content'])) {
1 H; T( [2 T: o. t" H update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));8 s5 v9 l, |. ^4 ~9 Q( s4 T
}
2 O0 d2 B% ^5 i( z8 Z; I8 [5 H! ?% H* G }1 N, `/ n" ^; |- Y" T( ?4 N7 F3 N
```/ F0 V @$ U# z; L6 e
% ?+ F4 ~( o: H! H; y5 J 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。: p! Z( `" e( I) X, V# c
3 y! Y9 F. o0 Q- C' o4 ]# W- C
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:7 d# O- S9 Y5 A P1 J# n+ m8 x
! }* B$ @! k6 c+ B% f
```9 @# i; s" U* L, m# g
$args = array(
5 u4 |( q! A4 K' l2 {# S! M& J7 Y 'post_type' => 'site-wide-notices',
7 J. g% }) h; p- u! ^7 Q 'posts_per_page' => 3,0 b2 ]' X0 M* ^$ w* [$ Z, V: h
'order' => 'DESC',0 |! k6 s! I! i% s. r- e! W
'orderby' => 'date'# N8 F- a. i$ n: X; L% ?
);
& Y2 H: G8 C1 c: f9 g. B $query = new WP_Query($args);
0 R, f, f3 h! O/ g4 Z+ y if ($query->have_posts()) :5 M% g3 l! Y* T% d( l, Q9 T
while ($query->have_posts()) : $query->the_post(); ?>
9 h6 n2 k9 ^% b: _# u <div class="notice">3 f. W0 U1 \: A' N, K
<h3><?php the_title(); ?></h3>3 G) J2 F* f* y0 F: M: H, w! v
<div class="notice-content"><?php the_content(); ?></div>
/ ]& F9 v1 d3 b& v </div> M3 b, f3 ]+ l7 P; b
<?php endwhile;7 q: T# }7 k2 b" A/ f
wp_reset_postdata();
* q" q& w, x% T2 u# _5 K, \ endif;# `5 s' E1 y& ~- a, Z; c: G$ M
```
8 ?5 F& s1 \( O) q% u, t1 i5 P" p+ H
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|