找回密码
 立即注册
查看: 4492|回复: 3

如何快速在wordpress后台中增加一个发布全站公告的功能?

[复制链接]

347

主题

565

回帖

3689

积分

管理员

积分
3689
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:& T* M+ z5 m( u; ?, R
- n* C! `- _* Q/ j
1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。8 Y8 V1 M$ [) G5 L, H2 m, a
+ \  H- Y; ~7 Z! w9 }2 U) |9 ?7 Y
2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。
% c% z! ^) q  A) Z$ X$ n8 g
% k8 g) I9 O7 k3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。
+ D1 e4 O3 s; G7 }+ g: e) P. i6 D! O' _; y9 E# ]+ ~
4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。
% V! R8 B1 `9 [0 W! \% F' @9 Z) p9 K
注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

347

主题

565

回帖

3689

积分

管理员

积分
3689
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?4 T; ^; b# x, H$ ]
6 x! b1 u) w2 {3 {
如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:
. Z. R2 P- I9 ^4 N3 _6 W( `* U$ G; Y/ v! W7 D
1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:
5 k/ V8 I1 B  g7 Y& ^7 @: D
2 p/ n9 A: S5 R/ e% }   ```
1 C/ s1 _4 }# A6 I* J   add_menu_page(0 h0 F( J% S; |* k9 Q) c9 b
       '全站公告',2 r) [" n* e: Q2 w& \0 {' }6 q* A
       '全站公告',* t/ e; k, M- F& \
       'manage_options', //需要的用户权限
% Y4 w7 x! f6 Q! _) ?! `+ m       'site-wide-notice', //后台页面的 slug  l, z; J0 c4 `# Q8 e
       'render_site_wide_notice_page', //用于渲染后台页面内容的函数
$ s' O4 ?8 K) c+ @. o7 q" e* ]       'dashicons-megaphone', //用于显示在菜单中的图标
$ R6 c4 S7 `) o4 B       30 //菜单项的位置
' f  f% D1 C  |! _0 {# T% k   );
5 F* r) k& |0 U/ c( U3 Q" X   ```3 V8 B9 }& @1 Z# [( W, |
$ B. ]0 t, d8 ?0 t9 G- ]
   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。
: v" L- g5 l" m* @; e9 |' x& x' u4 T# g7 T8 w: W% T
2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:, K2 @2 T, `* _# D$ ~. w

+ q$ @9 L; I% @' p% K   ```
- b, {6 u& {, J# R   function render_site_wide_notice_page() {2 K* M" S/ q# E0 J
      // 渲染后台页面内容
- W% \' t) t1 a5 R3 N. X4 g0 U- T      echo '<div class="wrap">3 C7 u3 X1 c4 ]  t" Q
          <h2>全站公告</h2>* a, Q( k7 [8 d; r0 t. O( _+ @5 v$ m
          <form method="post" action="">2 k; G7 O0 ?2 Z1 K9 D! ]1 p
              <label for="title">公告标题:</label>
# R: n* Z$ M# h+ z/ F$ L              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>6 ^' x' I' Y! ?; _9 G, n
              <label for="content">公告内容:</label>
6 ]% _! c+ E$ Q- N! P4 y              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>
. U& P- X3 h5 n3 Q! }              <input type="submit" name="submit" value="保存设置">. F3 S% v# F  P: E, {
          </form>
1 K6 ^3 S; I$ C: z4 c1 C; l      </div>';- \6 v% g4 N# T4 K

6 L9 H. r) H% ?. y$ m' ~& B      // 处理表单数据
9 Q$ r4 Y- g8 v/ m* ]1 y      if (isset($_POST['submit'])) {" A9 h- L: V) z2 o$ ?/ `
          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));5 P& ^0 C/ y0 q6 V; t
          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));5 e& C+ n* m, N8 A+ `
      }4 K$ e' v2 C0 Y; N
   }
$ j4 {# _5 X" _3 k   ```0 t: x2 u2 @# K2 p) Z: T4 d
+ g9 {$ l7 E) O5 m% W- L
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。
7 e( N# o( n; H4 f4 C
: u/ }) E) Y& m3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:  V5 q  k$ H* x/ o! P
7 f9 a" J. _  F& o: l% }4 N
   ```! s( B. ^4 i* B& R9 ?* [
   <?php
! M2 e) Z( J" |   $site_wide_notice_title = get_option('site_wide_notice_title');
6 |$ G1 ]0 E1 z( I) n& D) c   $site_wide_notice_content = get_option('site_wide_notice_content');  m* x: B/ E5 x1 B4 b
   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {0 B# ]7 c5 i9 u' m5 m. q
       echo '<div class="site-wide-notice">';& w8 H' T( L+ K) f  ~
       if (!empty($site_wide_notice_title)) {
6 r+ j6 l) z# d           echo '<h3>'.$site_wide_notice_title.'</h3>';
+ q* B; u: M& Z; }1 U. W" b# Y       }; N1 b( L) o" S0 ]3 h* _
       if (!empty($site_wide_notice_content)) {
" d3 S6 j* Y' V6 k# O% @. M" f           echo '<p>'.$site_wide_notice_content.'</p>';% w/ y7 ]8 {- h# i0 m
       }
- V5 E9 m3 M+ K1 C% y6 d       echo '</div>';- N- E5 g# a8 T
   }
# R: O( B9 N" a6 W   ?>& T. P9 s+ A7 P# Y4 w
   ```
5 V5 L" z  \: @4 i7 H7 q) V) o5 Y" Q
   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。
7 y  q, r7 N* v" V) X+ J' _
  o2 V" R7 U) f% f请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

347

主题

565

回帖

3689

积分

管理员

积分
3689
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?
+ P7 Y/ B7 F5 S: A7 w# F, @* a
( \/ i. h/ \( I0 O. b1 t1 Q7 C您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:; q5 K+ |( p- Q" c; r$ K

8 k: r& ^) U& A: @% M# p1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:
& e2 }) i. s0 K5 ^) A" k
( W) @  t: r" S/ ^```
% B% I9 R: r6 D+ ~- l  Bfunction create_custom_post_type() {
2 j  [7 \% k( z" O( T    $args = array(
: t/ N  W) P7 ?: u, H        'labels' => array(
+ p2 B" q8 y8 O2 t6 E/ ?+ K0 I            'name' => '公告',
2 n7 W$ a- ?$ |2 b5 H            'singular_name' => '公告'/ W8 E+ \3 F6 K: a; R0 U
        ),+ W. z- e8 Z6 C- M3 I
        'public' => true,
; u5 u$ Q: V. {# R6 m: r        'has_archive' => true,
4 V5 ^' j/ \* z3 X# R8 [7 f/ t        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),; G2 j4 A0 ^8 G: W% e0 o" f0 z8 `
        'taxonomies' => array('category', 'post_tag'),
5 t' j1 f+ F6 f        'menu_icon' => 'dashicons-megaphone',
  e! |0 r4 {  c  D; c/ R1 f( L        'menu_position' => 5,. R/ a5 n* V" j7 M0 E  C# K" r
        'rewrite' => array('slug' => 'site-wide-notices')
5 v( @* P! M6 \! N2 t* Z, F    );
# }4 A, S$ F: M, y    register_post_type('site-wide-notices', $args);
0 k+ c. V% B# D4 }3 A}
; P+ ]1 n4 n5 D2 Iadd_action('init', 'create_custom_post_type');
! C+ f$ d) G: d" i5 s: j: {```) c( V8 O# T0 I: l8 M' \" i: ?  r: ~

! S) R" M" m7 z- b在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。
# A( A1 H% I/ a4 z
1 s' ?9 b$ |' b5 W% _2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。- D, n5 ], W! m5 G  F; A6 s8 b. l

/ Z! J7 L4 l  D4 L0 y: e3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
( s! b: V0 c- U* Y  h
$ j- [+ E% ~5 s' A, t$ u$ C```$ x" w6 v# u! u) H, d
$args = array(* i6 e. i# c- q
    'posts_per_page' => 3, // 显示的公告数量) J2 q9 f' J% \* w  `% i) F1 h* `
    'post_type' => 'site-wide-notices', // 公告文章类型/ R3 O0 H" g9 {
    'post_status' => 'publish', // 公告状态
: W% @' {  F5 |) \: b. i1 ~    'order' => 'DESC' // 排序方式# D6 G$ n0 N' s/ F3 w
);
6 i) D. B1 @9 i
/ k# r! X4 k" m: S2 N$notices = new WP_Query($args);
6 j! @( O& O9 i! E- `5 {4 o$ ~if ($notices->have_posts()) :
1 J# a* {9 R* P5 Z1 d6 s" `    while ($notices->have_posts()) : $notices->the_post(); ?>
: B6 p+ e/ Y6 G$ `2 W, H        <div class="notice">' D1 w' {8 V' t& E
            <h3><?php the_title(); ?></h3>
! G; G( m6 h0 G7 B            <div class="notice-content"><?php the_content(); ?></div>! j/ L  C8 ]6 A* y+ A
        </div>7 n3 A3 i4 }9 T" T2 q+ P2 Z
    <?php endwhile;
, ]: o2 O' C  Q& T5 L$ Pelse :- T' I% A' w, X: _, H8 s/ G
    echo "暂无公告";
! T4 r8 r4 C3 U( Vendif;
- j% K' s5 O, x7 P  Bwp_reset_query();
( a: I4 K) g2 r7 v9 d6 |```/ ]% c2 }1 T* k, X/ l

4 E6 r' `' c! u& |# r+ m这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。
3 }% h6 b3 a+ s9 b/ d5 d% C, }+ `, s+ Z
以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

347

主题

565

回帖

3689

积分

管理员

积分
3689
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?4 M3 I* @+ ?! K) l. G$ L: {
8 {7 l1 j/ _3 \, _. i
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
5 d3 B$ ]7 p) u$ X  U1 K
) b( w+ P' \  s; N" r8 ~9 b* |. [- _以下是创建自定义插件的步骤:  i( `9 \9 C) h# y/ f( R9 L5 _3 L$ s# T
) c+ b- b9 \1 E6 F2 R# @
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
" P$ p7 C. P. u( G. }/ ]5 r. u7 y. H6 L: w
   ```
" \2 @* J5 w( Y' H+ @& y6 V   <?php$ U& V8 F5 e" Z" R* w% C% m3 `  Y- J
   /*
: T0 }4 F& j/ _" V   Plugin Name: Site Wide Notices Plugin
) m5 q+ F  c, E8 A6 e   Description: Adds a new custom post type for site-wide notices.- ?2 F2 X+ A& h  |; Q3 I8 V
   Version: 1.0
$ e6 |/ R2 e: s   Author: Your Name
7 R0 d. Z% e& f# N; e+ V$ l- @   Author URI: http://example.com2 H# Z; P  J# G- z. p. ^
   */
! F+ f* S+ R% P( Z+ {
- J% M% P7 R& C: p+ W0 V   // Add plugin code here...
' ?, T$ p5 @; [7 d2 D1 W( i1 g   ```, S1 O1 T) ~1 @  l# O. c) w3 Z) X

! l5 q8 E8 R3 H, c   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
9 O, F# `# \% u# k+ T6 J8 F- X* X) C8 T. a
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
  v3 {  d, G  m% M2 F4 t1 N; d9 F
4 z0 z! s+ e3 ~/ Y6 ]" ~   ```2 }$ s, ]  X2 e0 t1 s' T0 r
   add_action('init', 'create_custom_post_type');
: R" P( W, w4 d! Z! L   function create_custom_post_type() {
6 _3 w/ `% f8 c+ ], P) P      $labels = array(
: d& R0 T$ z1 T- W$ D7 d          'name' => 'Site Wide Notices',
1 V% r0 f+ M2 X          'singular_name' => 'Site Wide Notice',
% S1 x- c- i, I          'add_new' => 'Add New',
% z  G; x+ Y( R6 u6 P          'add_new_item' => 'Add New Site Wide Notice',
. O. L+ {0 c, v/ Z9 ?          'edit_item' => 'Edit Site Wide Notice',* A  @3 P5 V/ K, P
          'new_item' => 'New Site Wide Notice',
2 _/ H, R7 y6 t$ _8 L- f6 Z( x* f          'view_item' => 'View Site Wide Notice',
- |, W) s' p8 G" t4 l- _# q9 A3 G8 K          'search_items' => 'Search Site Wide Notices',
2 c) z  ?2 e5 I+ o& l& Y          'not_found' => 'No site-wide notices found'," K7 j* Z) b1 }: X- V/ J0 C
          'not_found_in_trash' => 'No site-wide notices found in trash'
+ s& ^" u; V2 J% Q3 K1 z' n4 x      );0 I" O5 p2 ^0 t$ A1 `8 L" Z

. ]" L# b+ G7 P      $args = array(0 f. j; h9 w6 G& B& a/ ]3 W! o
          'labels' => $labels,9 _+ W( f/ p0 \6 m8 u5 _7 C
          'public' => true,
; }2 s" B2 d9 W( i! C' ]7 E          'has_archive' => true,' e7 g# s% d# m" u: i, W% o
          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
! [) ^8 o: d8 A/ a* l$ O5 j* w          'taxonomies' => array('category', 'post_tag'),0 D1 {& I* a9 @' G, |! }* d
          'menu_icon' => 'dashicons-megaphone',' x, E1 S& _8 @2 |
          'menu_position' => 5,
( P% C. I0 E. h3 n; A" {# |          'rewrite' => array('slug' => 'site-wide-notices')' u' d4 A8 v5 F  ?* _* f3 N" L, \
      );
5 t+ a, z) y. ]4 `$ e* D
5 ], d8 \0 Z# {      register_post_type('site-wide-notices', $args);
) E( ~3 Q) X3 h/ O* F! x) ]# I   }
1 V4 e- T8 W8 A3 h% R( R   ```) T' S0 s! n" R
- v; k/ n/ \5 a1 b1 ~* N
   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
& y2 F! ^/ d8 U/ L; m& O7 G0 B" h
8 c4 q6 g% j, i  |* R/ _3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
0 Q; w+ T7 d( K/ r! ?  [# W+ _/ i! g0 S
   ```6 ?& H" Z7 M4 F( K! @4 K4 _
   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
. G: A! R/ z/ x$ E9 Q   function add_site_wide_notices_boxes() {
- U; l3 r8 y, P, m1 ?) ~  F1 n      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');2 w. @- V' b0 Y6 [5 O! U* t
   }7 s) s. O, T9 }0 e4 y
8 s( K: ^( n0 V
   function notice_details_meta_box($post) {, a2 q  d6 m0 W
      wp_nonce_field(basename(__FILE__), 'notices_nonce');
* j( [5 }. N, w      $notice_title = get_post_meta($post->ID, 'notice_title', true);
$ U7 k% q! ]8 I# r0 u2 C8 g      $notice_content = get_post_meta($post->ID, 'notice_content', true);- q, [  D# a9 l5 Q3 Z4 x1 S
      ?>
- X7 `# I0 O) J& A( S, ~      <p>
4 t( m2 W- s7 n4 }          <label for="notice-title">Notice Title</label><br>
4 L* w( f5 L; I, |: @$ |          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
: p1 W/ |1 |7 `: v! t  |! S      </p>
7 l0 L0 o2 U  I' q/ U/ h( h      <p>
7 ]3 }4 k! K" _" U7 }          <label for="notice-content">Notice Content</label><br>
6 e' K* c- ?( z3 C; Q          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>2 |: N2 _% m- O: V. }. S* B
      </p>
/ B' N- U, [) F+ r7 y      <?php
5 A2 h  p2 E3 V& S# s5 N   }
! u: r1 k3 Z! h# ^- l% T3 D- c) [
4 q5 f! D  k( f! s2 `   add_action('save_post', 'save_site_wide_notice_meta_box');' ?4 v$ K5 V+ G  G
   function save_site_wide_notice_meta_box($post_id) {
5 _2 z: C! ^1 G; l) S$ R& D- S      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))2 ]0 J  u( |0 o2 Z. h
         return;( Z8 W/ Q  y  T9 R
      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
5 S/ `$ g8 Z6 h1 ^( Z4 q         return;
8 Z% j: T3 `1 i; q7 P7 N2 b! O3 G9 p& N. X  v' f3 W4 s
      if (isset($_POST['notice_title'])) {* [" Q+ B; c3 e: d, X- N
          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
  B' X; y9 Q2 n& [+ g      }- L/ K: ?" g8 w+ F$ C
      if (isset($_POST['notice_content'])) {
. u  B  R( W. a- s, V  Z          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
& D% E6 ?2 q/ s! |5 C3 e      }3 |; D4 S) D) P, ?! g: x
   }; R% v7 T; y9 {
   ```6 f5 l+ ]# o* y4 c3 }

2 C) \# ?/ X2 e+ c6 N) s8 n1 M   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。! @& s4 Y, o" G& A
# F# G. P0 Q* J# N* R, T/ \
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:. D& \) r9 K  [0 P
7 j: r& i5 P( I  V/ T
   ```7 R0 r' }+ U. q2 p( i
   $args = array(3 t: e- D' A: S, J$ U" P
      'post_type' => 'site-wide-notices',
+ n5 d  j0 |; c/ ?- k      'posts_per_page' => 3,
( u( `  y6 B6 R& o      'order' => 'DESC',
! u5 _  R" w' `# H& x6 Z      'orderby' => 'date'+ O1 S  p6 Y+ O5 K
   );! I( V6 B+ ~- Y+ I) U
   $query = new WP_Query($args);( Z3 k2 Y# U, f0 K/ X6 g( n
   if ($query->have_posts()) :
6 ?6 ?6 U/ `7 l3 ?' j; }0 J: Q      while ($query->have_posts()) : $query->the_post(); ?>
' [! Z& y; K' v# y; P          <div class="notice">- h, ~7 S* I- x9 U& n, j
              <h3><?php the_title(); ?></h3>& N8 b, B$ L! F# S
              <div class="notice-content"><?php the_content(); ?></div>
, s7 ~# E0 _  t5 i          </div>: W# ^8 R7 R0 s  U4 J1 |
      <?php endwhile;/ L0 Z/ W; K/ D7 ^5 ]1 n( m; Z
      wp_reset_postdata();; c5 ^! n8 K. Q/ g
   endif;
  D6 g$ q2 E8 c( Y- ^; {   ```
& o: ]7 y0 `# d2 ^, ]7 v# f8 `
   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|通达创业无忧 ( 粤ICP备2023021749号|粤公网安备 44030402006137号 )

GMT+8, 2026-2-20 18:07 , Processed in 0.014273 second(s), 2 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表