|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?3 r2 ?8 G8 K: o$ N- C' X2 Z5 `0 ^
# v0 Q0 L4 a& H9 t
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
0 S- [+ S: i% T% e0 i7 ?( d; U$ A% }
以下是创建自定义插件的步骤:1 u/ \% v' n. Z% F/ P' }1 i5 `
/ x2 ~! ^- t! h* y8 Y1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:; t4 h" I9 `2 f9 w" j
( P) ^1 k/ f9 B) T
```4 g3 d! g& h6 j- U/ \
<?php
. }+ f0 M2 D/ c0 t' {$ z /*! R/ d1 G x! Y9 Z+ K1 a; Z: f) ^" J
Plugin Name: Site Wide Notices Plugin
6 `" @: D h" B Description: Adds a new custom post type for site-wide notices.4 r! [6 Z x) n/ @- a" b
Version: 1.0
- f7 r$ v |! s$ I% S5 M( u Author: Your Name
2 S& |4 U1 P, O+ x Author URI: http://example.com
$ h* u+ d( D4 I1 Q8 A o' ^2 ^ */
6 [5 r7 ^; X6 w) v) X9 l2 e u3 V c+ K8 E6 s
// Add plugin code here...7 d+ w( l0 D d6 J+ [7 B/ P( }
``` z8 o1 T4 f3 x# g/ K( B e8 O
1 Y7 B- f; e! Y" j/ E- q
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
/ x( `' c# c2 r
" u S1 `3 a; r. F2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:( T* S+ C) O, o/ @/ o
1 G! g" s. j- e; z ```
' q" s' ^/ |5 i) b. D& _3 k/ V9 j add_action('init', 'create_custom_post_type');
5 W& B4 o* Q7 R: u function create_custom_post_type() {4 S3 |( K8 T7 ?& m) l
$labels = array(6 Y W) H3 ~/ o# y( Y% I$ [
'name' => 'Site Wide Notices',0 \# l' @$ r4 E7 W4 ?
'singular_name' => 'Site Wide Notice',
6 A1 z. z# _9 B/ l3 d8 U# L. ~ 'add_new' => 'Add New',
. K" ~; @; G! _2 [; } 'add_new_item' => 'Add New Site Wide Notice',
7 m' r; m9 @9 C# y: b 'edit_item' => 'Edit Site Wide Notice',1 Y# e8 P" Y/ }8 G* d
'new_item' => 'New Site Wide Notice',
6 `) f: }9 M$ f6 n# ? 'view_item' => 'View Site Wide Notice',
. |; V4 j6 X& c 'search_items' => 'Search Site Wide Notices',4 g1 E5 a8 p' J. s3 `
'not_found' => 'No site-wide notices found',
, p% }/ @* S2 E* c$ L, Q 'not_found_in_trash' => 'No site-wide notices found in trash'$ D x. ]$ Q0 z a" [! |
);
3 `- l$ P9 A7 f) g( e! q% ]2 @1 v! q' F5 G7 @
$args = array(; N/ i* O; t5 c7 w' ?
'labels' => $labels,
; r! @* o* C4 q. M6 C# m0 J3 O& f% o 'public' => true,0 e" {8 }( `# s. d
'has_archive' => true,; q4 g! @1 B* d, ]
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),1 M1 a' f- g# ]3 u1 b
'taxonomies' => array('category', 'post_tag'),% H. r" e$ q1 S* r$ [& `1 }3 [
'menu_icon' => 'dashicons-megaphone'," m' B# g5 I B4 k+ h" y e
'menu_position' => 5,
9 c8 u( g, z9 k' `8 X2 O 'rewrite' => array('slug' => 'site-wide-notices'); m, W! T& N# k
);0 f4 G. x. |3 i1 N% _6 P. y4 A6 d$ G
) v' _" q. F2 U% p1 \8 ]9 ^ register_post_type('site-wide-notices', $args);6 p3 n/ r$ F3 @- n' Y! b' [
}+ `& c+ O8 w$ E7 }8 T
```
/ Z3 R/ h. K1 J- v
- m9 U# T# t& H9 t# U 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
8 H- E: P' A; j% D$ R3 Y" C, J
+ `+ Z( J. i0 s3 K3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:6 ]( t* A/ x4 U9 @- d2 u2 ~6 P
V1 l) y& K/ a" n* y! S ```
3 J2 p1 I1 h* [* [- D+ f# o add_action('add_meta_boxes', 'add_site_wide_notices_boxes');% n5 d# C! \9 d9 B
function add_site_wide_notices_boxes() {: K$ Q4 H/ h* ^4 S
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
3 @! f0 O: M% {1 x5 a% | }
* [" B% y$ {( S- f9 \4 o: z) M0 `5 [. [* o7 [3 j/ \
function notice_details_meta_box($post) {
' G, N% v% z3 _# {/ Q/ r wp_nonce_field(basename(__FILE__), 'notices_nonce');
# i" b3 K/ H T0 @0 F $notice_title = get_post_meta($post->ID, 'notice_title', true);
; V2 X: K& L# p4 F7 D, { O. [ $notice_content = get_post_meta($post->ID, 'notice_content', true);& E4 e4 @6 ^, x7 D# W
?>
# S+ ^+ T2 N3 \4 R <p>
( j2 U+ F# [7 C4 R <label for="notice-title">Notice Title</label><br>
! B+ Q6 W' G+ w/ n v9 ` <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
$ w5 y0 O o% P& a! C4 b8 ?3 [- v: [ </p>6 j' `2 @0 y& y3 i8 i! T4 a
<p>
/ q8 @$ s5 G' \/ x! S: B% O5 I <label for="notice-content">Notice Content</label><br>
/ a5 |( b% t5 K7 k) p/ i: W2 i <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
" s0 d( Y7 N9 F; V& |6 A </p>
1 j3 ?9 Z m8 M) z) a* J! U) A <?php
! F" J {7 d' C3 D( U6 [ }, ~ Q% T$ t! u" D6 H& W4 O
. E- `+ B& o& }
add_action('save_post', 'save_site_wide_notice_meta_box');8 W/ A" m+ m6 B
function save_site_wide_notice_meta_box($post_id) {& q, \* u! c& y. _7 q1 L
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))( J- R7 K. H* p. s& W" S
return;% `% Z7 z. Z" f
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)/ f8 a9 X' L( h; q7 s& o: A5 k
return;9 ^ K# q9 e: y; N) r7 R
7 H1 ~0 l* M5 \
if (isset($_POST['notice_title'])) {' r3 \- ~* f: Y! c
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
4 |: x, T: ^ l6 Z }% e3 U! l# d3 ?9 J
if (isset($_POST['notice_content'])) {& m! p/ J+ l7 U5 U9 ]! R( ~1 W: H
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
& a5 e( M6 Q/ [5 c' w9 H% ~1 Q }
+ d6 \: \/ a1 r$ d: ]6 \ }, x/ q' h$ i3 N, |, P
```
& P4 x# l+ X8 J5 ^5 p+ a3 k( F; t# V3 D0 L2 Q) d* Y) A
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。. y9 ~& z) I+ h& T4 d
2 G+ Y" M; l. i6 h4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
5 I5 P; E; m: G: X: h) Q. j6 n
9 p" a6 ]2 t% U$ H% j8 Z ```
, U1 _- d! J x* m $args = array(4 N. k% W9 y, S2 R) H3 G
'post_type' => 'site-wide-notices',
8 P; |/ k6 A2 M6 B( |; {- | 'posts_per_page' => 3," U' ?/ u/ b ~8 d* Q+ G
'order' => 'DESC',
8 t( @% C, O2 S 'orderby' => 'date'
. M0 Z( r: S6 K1 E6 q) z );; @" {" s. |! Z) I7 Y
$query = new WP_Query($args);
. [+ [& v8 X& {: P/ { if ($query->have_posts()) :
' ]6 I1 l5 Y) A" X3 n0 C while ($query->have_posts()) : $query->the_post(); ?>
7 \* p; o( m% n- K <div class="notice">, U3 |$ W" k5 G
<h3><?php the_title(); ?></h3> g: i1 M" Q/ ]% Q) D/ t
<div class="notice-content"><?php the_content(); ?></div>
& f0 Z. x) T1 y- H% [( Q </div>5 W! a$ ?0 `% l, q& Z% k. H
<?php endwhile;! ]" x! n, i# c6 o
wp_reset_postdata();
9 x) T7 m" ^# p% X endif;
$ F& |' U; p t3 Z, Y ```
+ J4 g$ H. J. I+ ?
% d% H/ e2 P8 ]% f5 |6 ] 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|