|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
. G6 W! W" C: p; X: r. Z/ k2 U! P. j: i0 x2 O% N
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。9 i+ `0 i5 j4 O$ j z/ }" `$ b. g# L
* G- {" D& J3 K, P2 T# y+ d3 ]
以下是创建自定义插件的步骤:1 p9 s! [7 r. [, |
1 L/ ^# l2 n n8 R5 H6 F1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如: h# Q( m' R: [! T% }
9 R0 G4 `" D9 l2 y( G
```
& ?3 x# g% q: g <?php
9 C& r( B6 o7 t0 I /*) e. U, J! Q, k( ]
Plugin Name: Site Wide Notices Plugin% Q, _" o% ~: w+ e
Description: Adds a new custom post type for site-wide notices.
& \$ k5 K, p$ t, i) Q. r4 f Version: 1.08 d* c3 {4 [8 n# \
Author: Your Name
$ i- A$ {0 z& m. N+ m Author URI: http://example.com
6 P& @; g }9 n7 w4 y */
5 J& C- K0 ~% n/ C' v4 y1 ?9 v4 x. D" z
// Add plugin code here...
3 w, Y+ o5 E" W. H- i ```
) R+ g% m* v' k* Y1 b' \
4 k7 N, m( I4 W5 x5 R o 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
, p' W! h) d1 q
5 O8 W; y" U2 [" C, w2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:- [/ Q0 m% E5 o7 L ~) N: O$ L2 Q
0 Q- W `/ _" w/ v' p3 _ ```
7 r8 b) k7 [% W# b% L6 I add_action('init', 'create_custom_post_type');
0 Q+ Q* z! P2 x% Q$ h% M0 d: l function create_custom_post_type() {0 m3 \; ]3 z1 U. ?7 r
$labels = array(4 L" s7 o1 J2 r6 U
'name' => 'Site Wide Notices',0 u. ?! @6 `' K$ [( W' _! X, Z
'singular_name' => 'Site Wide Notice',
6 e$ a5 [+ L1 } 'add_new' => 'Add New',2 m. p3 N" Q% h; C0 S% c* A$ j
'add_new_item' => 'Add New Site Wide Notice',
! l& i* I! C/ R* f) T 'edit_item' => 'Edit Site Wide Notice',' m; c* T5 Y* J
'new_item' => 'New Site Wide Notice',6 E4 Y. g; F1 p) K6 W
'view_item' => 'View Site Wide Notice',
9 m/ _2 m7 B" a$ k h5 [; ~5 @ 'search_items' => 'Search Site Wide Notices',) r& U% x1 f8 d4 R: o$ E
'not_found' => 'No site-wide notices found',
4 n8 m5 h( \# ]( A4 h8 M 'not_found_in_trash' => 'No site-wide notices found in trash'
# f. }1 ?2 H9 O. p" U! I# ` );
2 ^# T$ e0 y7 ?/ L& q2 P4 p# T, c( `( T
$args = array(- ? o4 D0 Z6 \# _/ J
'labels' => $labels,! I) j4 U6 x0 f8 j: R
'public' => true,
8 f- G( ?5 @ Q: U 'has_archive' => true,
& n7 V. C4 R# n6 }; T 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),! ^, ]8 T" h) k
'taxonomies' => array('category', 'post_tag'),+ A2 `1 s' G* g6 V: ~/ V, h
'menu_icon' => 'dashicons-megaphone',
Q& c" i2 j& e6 z( [; n 'menu_position' => 5, S, w" E. D% G! j
'rewrite' => array('slug' => 'site-wide-notices')
* b/ J+ ]* Z; r# t5 y2 j );( m3 g3 u7 P0 f1 J+ V& k
* j' A. B8 w& | _& X$ P register_post_type('site-wide-notices', $args);" U' e0 m9 v( C/ U! h) e6 I: D
}, k6 \: D! f; m0 X$ U4 y
```
1 D. {' D5 L7 Q) z5 H3 S( }2 @- H2 [9 Q2 j+ P9 L+ }
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。4 I/ m- Q3 a7 @# x
* l* Y+ |/ g: @- k
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
& {" \$ a% C9 k; _4 i+ J. b4 h/ M4 ?; i- P0 M [( z* _# G/ W2 M6 X1 K' {
```
+ I: E$ ~2 R8 j# c( g: r add_action('add_meta_boxes', 'add_site_wide_notices_boxes');( v- z5 _# e; J1 t
function add_site_wide_notices_boxes() {: s |2 ^6 U. ^( ?
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
6 s g: c4 ?% f# ~% O }5 @5 o6 X& u! D- D/ u4 M+ ~
! s$ }6 s2 M4 E2 k+ P& d: P function notice_details_meta_box($post) {
4 G& Q$ n2 k: T! I( W* U2 j wp_nonce_field(basename(__FILE__), 'notices_nonce');3 w! x0 I% M! ~/ B4 ^
$notice_title = get_post_meta($post->ID, 'notice_title', true);* v% K" l+ D- u* q" Y
$notice_content = get_post_meta($post->ID, 'notice_content', true);/ e( C8 s' D! _
?>/ u' F7 S, D8 O
<p>* N V) d% _* E2 L1 z( P. ~' Q
<label for="notice-title">Notice Title</label><br>
1 i2 f4 H5 W& f! f <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">, K6 D @* `' S2 `: X
</p>
. r- h7 z1 P8 H8 A7 c <p>
8 i; [/ y- R+ V9 y' L1 K I <label for="notice-content">Notice Content</label><br>
" f; E6 s, \+ Q) f7 g <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>, W$ @% r/ k3 X# f
</p>0 \+ V4 g- Q4 ~/ D0 z
<?php
4 f$ E1 b0 E. A8 ~ }
9 t) j/ a* K- [/ P$ c6 o: |2 j) K# c7 L: {: e7 B- Q
add_action('save_post', 'save_site_wide_notice_meta_box');
3 B' A$ u" Z" n6 o$ C4 [ function save_site_wide_notice_meta_box($post_id) {/ P, V' f! k' r2 a! i0 d5 B0 R
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))" H1 C# }( N! R2 ` r c
return;) U4 C+ P' k5 n/ o5 `* Z
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
9 _7 n4 E( V7 B3 a- i; e return;
8 N9 J! c7 |0 ~+ l2 Q- `0 D* G* C$ M6 k1 N
if (isset($_POST['notice_title'])) {' [4 t; L! P2 k; n
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));) q: r# `9 J, f
}1 W* y% u0 ^ T1 s
if (isset($_POST['notice_content'])) {
6 g3 v% ]+ q3 U- M, J update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
) y: M# {/ M% V5 v$ ~ }
6 V; E0 R% a3 G }
. x; U' J; i2 W" G; v ```
' L4 ?, |9 ?6 H3 n3 }; {: }" F5 t; a: ^3 p: A
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。7 V3 d, i; H8 R8 X, U. r& D
9 Q. K, }2 R7 W, c, E; Z4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:7 c. e z) A; J
+ b! l( v6 }1 j& g/ q1 u ```
! O/ @ X, O6 z( g/ R( y $args = array(. ^5 N" m* R' k" p, W6 L
'post_type' => 'site-wide-notices',) m$ Y( K( \* s" O6 a3 _8 G
'posts_per_page' => 3,
- I$ T9 K+ r1 y# I4 o% u 'order' => 'DESC',$ m0 R+ F8 g6 |& L% k0 [
'orderby' => 'date': y& a4 W3 b" `) {: x& a |
);7 C# Y* d) B2 E5 a! O
$query = new WP_Query($args);
* T0 l" d% z- w- A; t if ($query->have_posts()) :
* Z |. }5 g7 d! L- | while ($query->have_posts()) : $query->the_post(); ?>
$ x0 g* I/ k2 O <div class="notice">8 n. ]+ w0 o1 j8 H& H3 u2 Q) L
<h3><?php the_title(); ?></h3>
& G6 c' X" P A* t/ f! ~4 m <div class="notice-content"><?php the_content(); ?></div>
1 `6 S2 h p3 F* e) S2 J' C </div>
1 L0 ~1 v3 A3 ?* {$ k$ L# D. g3 { <?php endwhile;
- ~' Z/ D/ C- t. i1 C3 v wp_reset_postdata();
) L; f, f$ Z: b( {$ e endif;( _: s( E, b0 T$ e/ k
```
7 O1 C/ Y2 {2 Q& p
3 `9 S2 b8 l3 @ 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|