|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
" I. C! P h% d9 o( d+ V: V9 ~3 @' T% h
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。3 I( l, e3 h# E/ n% P: O2 z
2 P8 j- y) ~* |, @$ w& |以下是创建自定义插件的步骤:
" c2 @: o+ n* p$ w
* o6 g3 K" z* J2 h, Q1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
! m8 ^( @) d* d3 ~- x) J
. x: E' v6 l$ B' G ```9 s; H1 s f, p% R' c, I3 N
<?php
( l1 p6 D0 b& f$ n! G7 R) H0 g /*
- E- n7 G6 Q! c9 b0 q0 L! c Plugin Name: Site Wide Notices Plugin( \3 i2 ~: W6 T5 M' j
Description: Adds a new custom post type for site-wide notices.5 \" V) y( y' ~3 A# ]8 M
Version: 1.0% L2 Q! E& X* h9 u) p& J8 N
Author: Your Name
0 I' p- F1 N. B5 D* u" ^ Author URI: http://example.com
8 B+ `% i0 Y3 i2 u" x9 r */
, a0 t L3 i6 P1 c) g" d& V7 Y' f! U! ]6 s3 U
// Add plugin code here...) y: w1 h6 _; r
```
, e, [- q, c r# a: T) C6 \! T" {3 [
* P6 V' D# Q) U9 q& M4 x2 K3 } 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
- W! | `3 o; R& @/ o! q k" h4 p8 o
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
Y H+ G% C6 i4 y
: {2 L: x3 m7 ^: ?" f/ I+ F ```
- V; H+ E1 H% e9 I add_action('init', 'create_custom_post_type');5 }. F; b- P9 w( A7 m
function create_custom_post_type() {
9 M& R) W- W) \: i% V $labels = array(2 }0 P4 J! E1 F# j
'name' => 'Site Wide Notices',9 L' i: W. V& H/ F% U
'singular_name' => 'Site Wide Notice',. ~6 n# N; \' l+ _& u w b @0 b
'add_new' => 'Add New',4 L1 l' j# a# K$ z! ~
'add_new_item' => 'Add New Site Wide Notice',
$ T1 u$ P: P- P0 H5 [- Q 'edit_item' => 'Edit Site Wide Notice',, [* v8 u g- F B! c. g
'new_item' => 'New Site Wide Notice',
; O3 S( A* Q5 ], i+ V! j 'view_item' => 'View Site Wide Notice',+ e8 I* x1 m/ h' ^8 t& n
'search_items' => 'Search Site Wide Notices',
~# c! J1 u4 }- a8 `: W 'not_found' => 'No site-wide notices found',) D. k" M8 Q8 h+ |! Y$ Q7 H t
'not_found_in_trash' => 'No site-wide notices found in trash'* I' l# ~$ S& M- M% `( L
);: ^7 u7 k w# |" T- S; L
8 b$ u, E* `) D9 a $args = array(" [- y8 m1 w* Z3 e2 \
'labels' => $labels,
1 c: z! ]2 ~( F- S6 M 'public' => true,
, d0 I4 Z) @3 J$ r8 W+ F 'has_archive' => true,
/ k) u8 m6 Y+ q; X% X0 D- ? 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),# W* Q& Q# C c7 u
'taxonomies' => array('category', 'post_tag'),
5 x! e4 ^! x1 A) i3 _ 'menu_icon' => 'dashicons-megaphone',
, m; }4 F$ S' H, T W 'menu_position' => 5,
7 f8 C9 n" C! q( s' h! V 'rewrite' => array('slug' => 'site-wide-notices')
+ u- D# j( @" j! V! d1 h5 ~ );8 d: w, v9 ]$ o% o+ W% @
& ~; n6 t& j1 F
register_post_type('site-wide-notices', $args);
r9 I5 d2 i0 o/ d1 s3 g }
9 J+ v' i; D$ f/ x ```
+ E3 W" z- a: n$ x( T
/ F4 K v$ w9 l t% k* _, P 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
, }) C( |5 l1 T+ H w
$ N) o: t* q8 g9 W: ^3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:+ p" I* A0 k& S- N
2 u/ ~8 w9 n1 X) i3 Y7 Z4 Y ```9 u7 \9 b- b9 t$ C
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
5 i' `1 Q# U7 k2 {' c" z/ I function add_site_wide_notices_boxes() {* a1 N! }% W% U
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');' X; J- Z0 E6 I+ s7 ^
}
$ E7 F5 Z2 a4 [( ^# T: `; K4 u8 D1 n* B0 q
function notice_details_meta_box($post) {
! C" T/ @. E* y" c, M# r7 {7 o4 j wp_nonce_field(basename(__FILE__), 'notices_nonce');
. E9 J/ G) M$ W- g% H/ M/ V $notice_title = get_post_meta($post->ID, 'notice_title', true);) ?, B: [ u$ K4 F, l6 Y3 }) u9 o
$notice_content = get_post_meta($post->ID, 'notice_content', true);0 g' D9 w2 H7 A. U5 o" B
?>
. Y4 {' g$ j$ m& J2 K7 t, s* ` <p># Z( R. q& z# ]% j& n
<label for="notice-title">Notice Title</label><br>
: n8 ?0 ^% o& s1 n4 ` <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
0 K3 }- _* k1 s* \% B </p>5 W& N4 h9 Z. u4 A, f
<p>7 Q/ p F- I/ z8 G
<label for="notice-content">Notice Content</label><br>
7 O( N* K" f- |* D6 y <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
% i9 T* E k6 a, r </p>
: Q, D0 d( F: A n& l6 x5 A: z <?php
& K8 ?" g; V5 b$ U) ^; k }
$ G6 w; H: ?( h2 k0 j! k8 {9 ?2 x- L2 \
add_action('save_post', 'save_site_wide_notice_meta_box');( R9 o7 N, }. ?
function save_site_wide_notice_meta_box($post_id) {: s* a$ |5 g% t
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
4 s7 K5 A' J2 A* z- E return;
- K! k" r, n* x& i$ x5 ^ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
$ f' E) {( _; _6 ? return;9 S/ t6 w% ~8 i% {$ A% q
9 x b7 y3 v! y3 P( O& N if (isset($_POST['notice_title'])) {
2 b9 U9 j; ?( L update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
3 p' h0 |/ i3 T% g/ ?, r7 O! i }8 F0 T4 a1 `- }7 p! S& L5 |; F
if (isset($_POST['notice_content'])) {
: R: w$ B8 t3 A! [) h# B2 D) Q: q( a* n update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));; ^, q% @% \3 v
}/ _* B& _( h5 k( j3 T+ ` |$ g
}6 c# _ h% a: i& p
```4 R! w p& ], x# s% S
+ L" y: o/ B7 K* L ]% ~" W9 F 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
% T' e8 D$ y; k/ |3 y3 D
2 N% g& Y0 e% [% |- k4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:% \3 X& Z7 f. m \
1 U n9 h5 c; J: h ~4 y$ O
```" h7 N" a5 D8 r9 y
$args = array(
9 m; K7 V# T: i+ g( J 'post_type' => 'site-wide-notices',
/ T5 Q9 x% h# x1 W 'posts_per_page' => 3,
* F9 w, p8 {- n" K3 t 'order' => 'DESC',2 s; b4 ~/ }. g: i. W, m
'orderby' => 'date'
8 X. t- k9 p! z l6 R );; F" i9 P+ E. Y4 H
$query = new WP_Query($args);# d1 D2 v9 F/ T# G) |& j
if ($query->have_posts()) :& B( j- p% x7 |4 @
while ($query->have_posts()) : $query->the_post(); ?>
0 S1 X9 a: w$ d" w0 f! Z0 A J <div class="notice">
7 E0 U$ ~# o, h* _! r: }1 x$ U <h3><?php the_title(); ?></h3>' q, {6 N3 X3 m, _: {6 L. d; H5 @2 H
<div class="notice-content"><?php the_content(); ?></div>& h' T O# @/ S- q
</div>
0 o Y3 l- ^; q* j1 O" F <?php endwhile;5 X, Z* a' ~9 f* R
wp_reset_postdata();2 j$ f$ i- {! t }; X
endif;; S3 z0 K% W4 X! A4 m- k) e N
```; U# ]$ q2 ]' B% R# _2 B$ F9 ?# \
# |3 _& |0 @5 z# q; c' X* y 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|