|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?- D, p7 K+ O- t% H/ j+ i) D
h! n* P' ~" h2 L6 }9 g* ]如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。( a5 P. i q& _6 Z# I6 ?
! b5 T5 B2 {0 V% S) G' u以下是创建自定义插件的步骤:6 N+ e+ g. n8 n9 E' J6 i" {+ }
. S2 J1 s$ ?* K5 c3 w9 ^# r* M! X' \0 ?
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
2 r+ g2 |, i. t. x7 N* N) w, k8 \% R
```
7 x( z" Q* R8 P% s <?php
( M# h5 c* a. U# W; ~ /*
& j0 b7 w9 X1 [" g0 X% X Plugin Name: Site Wide Notices Plugin2 `- D5 v* ~7 E
Description: Adds a new custom post type for site-wide notices.
# e. P0 m# L! F0 b2 B% f Version: 1.0
7 k' p- p4 a) v$ R8 A0 \ Author: Your Name5 U1 A0 x! I. o
Author URI: http://example.com' C0 Z4 `3 y# j
*/# u7 M0 X: M7 e% v& C
; J* Z5 J, k |5 }3 { P // Add plugin code here...
1 F' J# T0 @! ]9 d: K ```
) C' u0 W2 L* j, [" a' ?
# S- u& |9 }+ l+ P* G9 ` 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。% l" Y9 g! j4 O+ L, g3 l
# Q# E9 q( V' K8 z+ N) W7 w
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
+ i8 x: C u( G6 o1 \) X
: r3 Z- ?1 o2 Z. P3 S ```" C. H" [4 [3 l; ?& G
add_action('init', 'create_custom_post_type');
) u! J( N4 o* ]. b" { function create_custom_post_type() {9 t+ z" ~& Y5 {1 d# r
$labels = array(7 ~: p1 l$ y& [! a: ^. i
'name' => 'Site Wide Notices',( M' l6 o+ i! v/ O6 G0 B9 r
'singular_name' => 'Site Wide Notice',2 H6 l- o! ?: @% R) H& P& n& `4 j
'add_new' => 'Add New',7 N9 f( ], J4 W3 S1 D" s
'add_new_item' => 'Add New Site Wide Notice',9 }/ _2 B- H% ~( {4 Z
'edit_item' => 'Edit Site Wide Notice',
# d% _8 B5 r; ^! D, @( p! s2 E$ D% \ 'new_item' => 'New Site Wide Notice',
% n( V# n. q& \! S% M5 { 'view_item' => 'View Site Wide Notice',
" _, M* e& n4 J 'search_items' => 'Search Site Wide Notices',
5 h- N$ N7 V" ` o2 j 'not_found' => 'No site-wide notices found',
- E M6 K8 d! T2 g+ w( s 'not_found_in_trash' => 'No site-wide notices found in trash'
4 I& ~* p6 f4 a8 W4 g0 Q );
; J+ A+ _; P7 |7 I/ _8 S7 |: z' Y5 ?/ r
$args = array(
) f- i$ ?. r* K1 V( i+ C+ O 'labels' => $labels,: o9 b$ m7 n6 s% U7 |- }- u! \
'public' => true,
) r* t9 u C* R( t0 R& ~8 l T0 b 'has_archive' => true,
0 e9 \8 c- ?& @0 `! m 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
; ~' }. O+ O' C+ a$ g 'taxonomies' => array('category', 'post_tag'),
6 L& Q- L7 Z6 J2 b \+ _* U 'menu_icon' => 'dashicons-megaphone',) ]+ f7 q# J/ T! v$ y/ V! @/ m
'menu_position' => 5,& ~6 k% ` f' I# h/ U- H
'rewrite' => array('slug' => 'site-wide-notices')
$ F# O' c3 u0 j );3 U0 P" F% f; N V! c) p& s. G
. r8 |, l) S6 `& |8 R! D: f/ @ register_post_type('site-wide-notices', $args);9 B# T$ C o" O( B2 b) Q3 k- \
}
5 T/ Z$ Q: |5 f! p5 o5 x% J ``` Y0 ]/ m5 J2 i+ |. N; \9 X: S
8 g ?, Y4 S6 L' w
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。( b6 @4 q, \+ @% o8 ~
: Y" |6 L6 ]. d4 Q/ w0 x3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
$ D6 _/ f( C* s# |* f, }: T. f* u: |
```
- \, x4 S6 h" q& ?- t8 _ add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
4 A) F. O5 i6 k. l9 W( q* o function add_site_wide_notices_boxes() {; C% e/ h+ d: r8 g; l' S
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
5 }/ b/ k4 N1 n* ^+ L$ X5 h: j }
# S( e, s! O6 o3 Q1 r3 k
; S- V5 r7 m; x$ U n$ L& ]# Y0 L function notice_details_meta_box($post) {0 K! j. ~, G1 `. }9 i
wp_nonce_field(basename(__FILE__), 'notices_nonce');
4 R) C* [0 m/ K% E6 }* g7 C $notice_title = get_post_meta($post->ID, 'notice_title', true);1 l1 Q% c. n9 e# h' e
$notice_content = get_post_meta($post->ID, 'notice_content', true); w8 C3 O+ F: s0 ?0 [
?>5 I# k2 x' l6 _/ i# n, {& o
<p>
3 p+ g. t! a7 H& ~& i9 s <label for="notice-title">Notice Title</label><br>8 H" d5 P; u# Y
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
( L( v6 ?; V* E( K7 S* B2 t </p>3 E% F8 Y8 U; z2 Z+ l
<p>
! t) @- d" _ X <label for="notice-content">Notice Content</label><br>
& k1 c0 _/ r- b3 V; S5 ^" F <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>0 Y. i$ k) T; {" W9 e
</p>
: {+ m& |, L0 K3 f6 h* A8 \6 r$ T <?php
1 x9 H* T/ L7 a, Y$ W }
7 ~2 _% ]0 w0 n D$ u7 a# k) `
5 E; [! Q( i3 a2 j8 K add_action('save_post', 'save_site_wide_notice_meta_box');$ F5 v( a& Y }9 U0 P
function save_site_wide_notice_meta_box($post_id) {8 D( i% Y2 A. x% k9 t/ |2 u
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
( H0 ~3 Y% O8 E6 ? return;
0 z/ F( p8 J* Z% ]2 g, H if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)0 s! C4 d# F* A+ A$ g4 A
return;
5 I/ M0 j8 Y! B# `* c- g v1 N7 ]6 P2 Q2 V2 f
if (isset($_POST['notice_title'])) {
2 E0 j5 W, q8 }2 o8 \ E update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
1 g2 b1 y; p3 |' M, N; j, D }: }/ A) E; x% y. F" d
if (isset($_POST['notice_content'])) {
; l+ O9 H; y) I7 J' f& R update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));" K0 g4 a* O% f. w
}
B% X$ `1 j C }9 D7 X2 u5 a" c) L! }; A5 ]2 }( [
```9 [" t" s9 w' a+ s% r! L' |1 v. G
% B8 M2 e4 J6 f. m 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。8 d% Q1 X& q Y0 O8 i0 K
- d% L y) _" E, {; w2 @* a/ C) f
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
$ i1 Q' s! N7 z3 k0 n! r4 d
# [/ h% N/ F; Z5 N- }% u5 l* ^ ```
) n: T; I( x0 L6 g4 N( @- J) f7 ~ $args = array() {+ d, @* g1 W' D
'post_type' => 'site-wide-notices',3 I$ j0 U+ T, z0 e
'posts_per_page' => 3,
; \) y, K6 V9 _ 'order' => 'DESC',
" }6 N7 I( M4 c+ ~5 } 'orderby' => 'date'
( A& _% E5 h- Q7 q7 `( A5 U );0 t, [: s% d8 O$ g7 |
$query = new WP_Query($args);
7 L7 i+ } x) a( x( |4 b8 k, a) O if ($query->have_posts()) :( d4 x+ r( y0 y" u3 E' _# Z: M+ u, X0 O
while ($query->have_posts()) : $query->the_post(); ?>. z" e. C, H! Q& W& {) ]0 ^
<div class="notice">' L8 _/ z$ L$ S0 N v3 N5 G
<h3><?php the_title(); ?></h3>
8 [, }1 M5 M/ J" x/ y" n# F <div class="notice-content"><?php the_content(); ?></div>
, _/ q. A3 V& g2 D+ d </div>( r6 V/ I& j- e. l# |% P
<?php endwhile;9 C2 ~# l$ R' |! u1 f
wp_reset_postdata();1 F. V$ v, m3 J: e w0 y, l
endif;
% G/ r, p) m! Z/ w) a7 {% y ```
1 r1 s- I* u; ?: ]' Q \4 v
0 z3 q1 O0 @( ~: a/ g6 f6 M* Q 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|