|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?9 q1 Q1 J' a' Q7 p$ p" I3 S; X
. Y$ U: G+ t$ t1 r5 D w如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。- G! O# @# A. a) U. Q( Z
' O* r3 `% N! m0 \6 }以下是创建自定义插件的步骤:
& [- I- M$ r9 y- _( R+ H# j3 x4 g0 S g9 l- \+ [* L
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
) x0 Z4 \# c+ Z# |# @
# y3 x5 A# d* `/ m& i1 r ```
& O* q5 y# R8 t7 ?" w <?php
2 B3 A/ R8 m u6 W6 t, y0 L8 n) Z3 ] /*
+ u" _5 r/ K7 {4 j9 o) z Plugin Name: Site Wide Notices Plugin A8 R9 F7 K! f1 R
Description: Adds a new custom post type for site-wide notices.6 S3 j) ~& e$ Q
Version: 1.0
% |* j v9 ?# ^6 u; B; Q Author: Your Name% o5 q+ U& p, H: y; e
Author URI: http://example.com" z ~# R% X/ t8 F6 |
*/
8 N5 m2 h$ j3 o8 J& A' j" y8 u4 o3 b* m w5 y7 U" F
// Add plugin code here...
# H! Y9 R' U( p4 t0 \ ```
2 Q t: Y# D( ?+ |# ]# H% |% ?) c0 \6 b% e# j h6 U
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
. d3 j- N0 A. Z, R+ [8 k2 ~- {
. e3 m7 q( h. q! q2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:' d* K6 ?- I- ]
4 |; A! s5 W9 H e
```
1 I6 e9 ?; Y! B+ l4 \4 F2 B add_action('init', 'create_custom_post_type');, D6 B9 v- }8 p! s
function create_custom_post_type() {& [9 t' V B' B( r2 k
$labels = array(
1 l: ] g* Z: |) a2 F 'name' => 'Site Wide Notices',/ h. h1 _5 |' |& {: g, }! q
'singular_name' => 'Site Wide Notice'," b( L. h- w. ]& G1 c5 o8 i( M+ A
'add_new' => 'Add New',5 |9 \( d2 C: ~# d; |1 f6 j( V
'add_new_item' => 'Add New Site Wide Notice',
0 Z: i* V7 H1 b* `/ J 'edit_item' => 'Edit Site Wide Notice',8 q% v. M2 ~. i0 G
'new_item' => 'New Site Wide Notice',
. S- @9 F7 B! M# \# w 'view_item' => 'View Site Wide Notice',
8 S" [9 w: Y+ B+ Z1 e1 r4 q6 w 'search_items' => 'Search Site Wide Notices',6 a9 H- g5 ?& A( c
'not_found' => 'No site-wide notices found',3 @, S8 x; M; C2 b3 Z( s
'not_found_in_trash' => 'No site-wide notices found in trash'
- Y1 ~ Q) E) \' E" g/ p0 n );
) F, i# {" U; g/ L* M
1 c$ O' O0 O9 y( B- B7 n' E$ n6 V" \ $args = array(
# i- j4 ~7 c8 R( |$ [. ~5 G- M# i 'labels' => $labels,
2 R, `( n; _6 {! n7 _. v 'public' => true,
" X. ` \ b3 X+ `# P; W0 B 'has_archive' => true,3 E& R3 _$ A7 j! g. e" `
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),9 p" F' o" T5 Z0 s2 H/ I
'taxonomies' => array('category', 'post_tag'),
3 r9 [. D' _0 C( v 'menu_icon' => 'dashicons-megaphone',
) J7 s) {! \6 I; h 'menu_position' => 5,
* t5 j: w, T+ @: s! i- W 'rewrite' => array('slug' => 'site-wide-notices') Y. }/ u1 f' V6 q. E% `* Q6 U; ?" L
);- v& S8 p; p$ A" w
3 ~. i3 y" t: E5 N- w
register_post_type('site-wide-notices', $args);0 p6 c# m6 Z. E: r
}. k1 g3 x# Q( Q
```0 u& C, Y1 G3 j; @: A
* D' }7 R7 N4 u8 `9 U+ j" G 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。5 R( {* w. r z, E$ ]
9 P: h0 L* ]2 E3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
5 x/ m* b+ R1 ^# {6 z3 ]3 B+ l! y) x& @2 p7 e- x" m9 J+ A3 K
```
! h7 \- |0 z$ Y1 q4 Y/ h3 o# X& { add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
/ J1 i+ p* D6 T0 Q# a! _2 P1 t function add_site_wide_notices_boxes() {* [2 l( ~7 C1 h1 [
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');4 X6 s- F( W( m$ {% p' d3 l/ x$ N6 i
}
* N8 j7 p2 p1 p1 l9 ]
2 y7 n- Z: u* l9 x function notice_details_meta_box($post) {
' N. Y8 ?9 [* ?9 Q# V wp_nonce_field(basename(__FILE__), 'notices_nonce');) g8 I3 k' ^# d
$notice_title = get_post_meta($post->ID, 'notice_title', true);2 a' A1 v* ]$ S! z
$notice_content = get_post_meta($post->ID, 'notice_content', true);
6 a5 k9 L9 `' C4 i/ F/ ] ?>
( k" y. U e- [+ @$ v9 R, S7 U- x; O <p>
% W% u& W7 Y$ p1 J <label for="notice-title">Notice Title</label><br>
8 t" T8 t2 x' e) U) k <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
- q. q# ^ U) A2 [ </p>5 _& H& o, X4 g' k* r1 x' n
<p>4 _4 c8 a. D. [% H4 d7 B {
<label for="notice-content">Notice Content</label><br>2 B' M$ q2 r: |4 x! f) e( b
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
2 |$ d2 ~6 y# c3 z </p>
" p" F6 I4 M% b5 b6 C <?php# a5 @0 d* k2 H C# R
}
" M$ E6 G9 G) N$ ]) P. o! _! L- y* ^' G+ h. T( |
add_action('save_post', 'save_site_wide_notice_meta_box');* p% ?) _8 D4 {: J5 e! [9 ^2 r1 Q( Q
function save_site_wide_notice_meta_box($post_id) {3 s2 e' ?/ M X/ @6 m
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))9 q I5 ?/ l* t3 ]* D8 i* q5 E! ~
return;
+ Y1 B- @4 m1 o2 D) s if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
" J+ t, |& Y0 l* k' E: f return;* |& J; S8 V1 T G) m( S
5 h0 N) g0 s) F# n- {6 |4 k4 l if (isset($_POST['notice_title'])) {
7 e4 l1 T& `4 u- a7 P. C update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
; x* E5 v2 t( U0 a. v4 L! b# F8 P B }
: J, S! P. s9 a5 L4 a if (isset($_POST['notice_content'])) {
$ {' j2 ^1 @/ P update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));/ }7 U- W$ S" u" l9 |: ^8 I
}9 Y, h1 L! e2 I7 L
}
; |: u/ {& u9 Z/ f ```
. x. G; G ? m) _1 X7 t5 v" k) W6 s& `8 B: d( ~
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。% |' L3 A) T3 g2 C( J& H
' w3 G( z+ T8 Y6 Y& P# y) y" H
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
1 w: }' ]. F& I9 M) {' ~" s" q# g1 L. R* S. a" g" n2 L
```
# a5 [ K/ j/ b& R, L3 U* W $args = array(
( S2 f0 o' S' o& p$ V 'post_type' => 'site-wide-notices',& k' L. r6 k5 M1 @% j
'posts_per_page' => 3,
. U6 Z# |2 {( v5 M* d* n/ G 'order' => 'DESC',9 U. z0 a1 T& w
'orderby' => 'date'; `8 q+ u4 j X3 F& J I2 {' z7 {
);
1 l/ a- c3 J; j% b' b" p# \ $query = new WP_Query($args);. W( s! l& }; }5 Z1 S
if ($query->have_posts()) :
* K# C0 u# H$ ~- R while ($query->have_posts()) : $query->the_post(); ?>
( N: P( F v& v$ w) f* S% G <div class="notice">8 X3 E2 k: o, I& {
<h3><?php the_title(); ?></h3>
' s. l1 w$ {6 S1 v0 a \: g <div class="notice-content"><?php the_content(); ?></div># U- X6 E$ l* U5 e* n8 k: [
</div>
, \- W8 d4 R d" I$ p' c <?php endwhile;
, V& {% b" Q: ~. @- |* I; ]& q3 E wp_reset_postdata();
1 P* ]9 s7 m1 }9 B& N9 I# \ endif;9 n! D! l5 ^+ |1 }
```
8 c9 f; ^& R+ p$ E5 k
+ ?* O& q8 E/ N. G# `4 n' I! j 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|