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

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

[复制链接]

347

主题

565

回帖

3683

积分

管理员

积分
3683
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:
2 s- S! {9 m5 ]# Q1 B3 a
) E3 }8 }0 u7 r: }5 }1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。+ s+ M, K% r  o& m, x$ H# e

6 \; U2 \+ y2 j/ @' K* m* g2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。
' _( M2 [, P+ K+ |
1 k: |4 E5 P6 K1 @4 l" F3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。
: g% k! L" \3 H0 k% o. T9 F" s& y( o" m
4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。
4 P, J5 H1 `* Y: o6 a) M! g, h' \- b6 K0 H- c9 p9 p  G
注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

347

主题

565

回帖

3683

积分

管理员

积分
3683
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?5 G9 J, p3 H+ ~& ]
* e' H* [) N9 Z. }/ a, n
如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:
- |$ ?- j( j) Q5 l  [) E1 l
# A% Y1 ?+ T; D! u  l' D1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:* j6 d/ N: {2 K. d
( R2 e& L, u7 a( r0 `: {7 y& K  G
   ```) r* Z$ Q: g+ m/ \; h0 T$ f
   add_menu_page() P5 g. Z! p, F) b3 X$ ~
       '全站公告',
( K: j* G+ l- w( z$ ~       '全站公告',
! G* c  x2 i: I       'manage_options', //需要的用户权限: m3 ^% ^. T3 r6 j7 c+ t
       'site-wide-notice', //后台页面的 slug
# R. I* n4 w! M* o: O. D! u8 P       'render_site_wide_notice_page', //用于渲染后台页面内容的函数4 j, m# u' K5 t
       'dashicons-megaphone', //用于显示在菜单中的图标
/ T  |8 y) I* b2 [, p5 `       30 //菜单项的位置
8 F# o% [  z. _, G   );
% E! q, w$ B  I) R% e   ```& J8 C0 z6 D8 |/ ]0 P0 t; J! o0 f
7 Z: `0 ]8 ^% [7 X$ F
   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。
: d& D- M5 g3 s, J/ Y
5 h* y7 w- t, `( P4 K+ s2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:; W* P# L+ `7 l9 X

2 e) w4 `9 P. R/ z   ```5 O" h3 |, r0 @# A2 n! \
   function render_site_wide_notice_page() {- y0 h# P* T7 C7 a& \7 e
      // 渲染后台页面内容0 [% N2 _, N5 M+ b1 a; J: D( P
      echo '<div class="wrap">1 W  W, ~# v. T+ S" e+ `
          <h2>全站公告</h2>
% ?: i. j% x  f& j) }          <form method="post" action="">) c2 Z) \# p/ J% j  O0 k: w
              <label for="title">公告标题:</label>
& W6 l# I2 Y+ L& P/ Z9 f# s              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>0 t6 k& i  H! N1 a: G, F! X  Q* `2 A
              <label for="content">公告内容:</label>
, J4 \7 G! K2 K: M, `              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>$ ]% L* U2 \  J( K6 o- `" B1 ^& I/ e( V
              <input type="submit" name="submit" value="保存设置">
* R5 O" j  h# V          </form>, ]" C3 |8 C) o
      </div>';
( ?, f# P- J1 h0 q' q; L4 l% p# J" P, k. O' _
      // 处理表单数据
) p$ G8 h. J5 h+ T/ A/ M% [$ t9 b      if (isset($_POST['submit'])) {
# ]3 X" U7 @/ v5 ]! T/ G# p          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));* j% T# N& M5 P( c- k& Z! O# D/ x5 G
          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));
1 y) ~4 A. `3 v: ?5 E      }+ C& L0 R, U, `: P2 ]9 l
   }
9 M' \0 r0 p# D! I   ```
$ g/ R5 E: R* N) H. C7 c, `/ t8 M) R, n
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。
# N/ ?# X( K+ y! X4 u
, Q- O5 M" E$ k5 y- O& U3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:
: U  f9 V0 z) ~. [9 a
: f! w, s$ ?$ w9 c! ?   ```5 k- j, D- Z$ f/ ~2 j# {
   <?php/ U4 A/ @8 X5 H, u
   $site_wide_notice_title = get_option('site_wide_notice_title');
5 L' h$ U+ s/ c* l& I6 ~' M   $site_wide_notice_content = get_option('site_wide_notice_content');; K2 m# ~; O' W- V! J
   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {
# x$ J3 J& ^0 f/ f0 |       echo '<div class="site-wide-notice">';2 s, F: f6 v9 l- |  h9 J
       if (!empty($site_wide_notice_title)) {
: U( D& x8 R9 y           echo '<h3>'.$site_wide_notice_title.'</h3>';4 ]" B; i# l! t% d4 B* A# v9 Y+ l
       }
" `2 I- i9 Y8 }/ P3 t% G6 U6 y1 R% _       if (!empty($site_wide_notice_content)) {, V+ B8 n, K, J& b9 m6 D" ^
           echo '<p>'.$site_wide_notice_content.'</p>';
3 {- a! @5 `$ @( M       }
2 H+ q' d, R+ L% [9 L       echo '</div>';; k2 `! }* h" y/ G4 C, ?
   }& U  w4 u0 \! Q
   ?>7 x. X2 f  t" F$ D4 @5 h
   ```
+ N( ]( U7 f  }( y- Z  B4 I7 q: s, z; e
   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。
( m/ U, t* ~$ V* ^' F# ]9 g0 H+ R3 d" v! h+ {8 M8 o5 |
请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

347

主题

565

回帖

3683

积分

管理员

积分
3683
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?7 v" |. |( b6 \" n6 N& E! v

* V+ V& Y! H- c您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:4 i7 i7 r" L+ {* b

7 N$ A9 {3 F6 W. D1 G, E1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:
( z5 R! J* g7 y- v
8 E6 E" z% o" H& t8 ]```: P- T6 g5 e  ?  Y# @6 [0 H
function create_custom_post_type() {
% K9 j2 f6 c' u3 T9 d# K# X    $args = array(# c+ Z- D" f8 l0 F8 B
        'labels' => array(& V, k; ~4 K+ W9 t- t! |4 |7 b
            'name' => '公告',) z& u% U9 x9 I  j0 A$ P) w+ A4 j
            'singular_name' => '公告'
0 _) ^: Z3 S9 `/ N+ o        ),  W: D  M  R% _6 z; P% h; @: H
        'public' => true,
- W9 s4 X0 v! f+ M# r        'has_archive' => true,
+ c5 M) g; Q9 N8 p& b4 K        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
$ D" b- t: K5 F        'taxonomies' => array('category', 'post_tag'),
8 s1 B6 o1 B) l" g        'menu_icon' => 'dashicons-megaphone',
  d5 r4 K# g" D0 s0 d, X5 p! U        'menu_position' => 5,/ O* f  T6 p% I: g% C4 s, G
        'rewrite' => array('slug' => 'site-wide-notices')
2 G2 L6 z3 W5 P( _4 w& t$ x: i4 b    );
* D9 U6 ?' |9 N4 H* Q    register_post_type('site-wide-notices', $args);  B4 P& I7 t3 k7 K# H
}
* b$ l" q' }/ `- z: \: F0 m! ladd_action('init', 'create_custom_post_type');
% |9 W$ B# Y& p2 C' h( a```8 P% A7 z9 F; N. x  T
$ \, C% A8 o) r
在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。+ o: r' F( p. x9 v& V6 g

" D) J- Q7 a9 M7 u0 H( @7 Y2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。
) R- R0 d5 `  ?! x4 J# u* \- L) C+ o) N4 p  t/ q' o
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:6 |7 b/ g6 T0 W. W

. y8 q" T2 H1 O6 ?' \1 r```
$ b2 J! D& D0 x$args = array(
! t* o. b' K  e2 q8 u1 u    'posts_per_page' => 3, // 显示的公告数量
$ Z' t% x; C( P2 R) S  i8 I2 q0 g    'post_type' => 'site-wide-notices', // 公告文章类型
$ k  a; `- g( U3 Z" Q    'post_status' => 'publish', // 公告状态
8 m( Q# x6 V0 Z7 O: d    'order' => 'DESC' // 排序方式  U  ^. O% Y9 F- T1 A
);9 _* t% o) Z8 n- f$ B  |

% W5 u  c& H& U' j2 J+ x6 @9 ^5 x' V$notices = new WP_Query($args);
; q. y: d/ e5 I2 f4 s1 Y5 Lif ($notices->have_posts()) :
9 ~, _+ ?* |4 D    while ($notices->have_posts()) : $notices->the_post(); ?>
  O" Y) a2 d- s% G  ~7 a        <div class="notice">
) q  J# Z* {6 \4 w            <h3><?php the_title(); ?></h3>4 G# c+ C. X; B" w6 s8 d: Y4 L( R
            <div class="notice-content"><?php the_content(); ?></div>
! z( \* P* d0 ~9 n        </div>: K9 x* ^+ i% `5 T3 s, l$ s
    <?php endwhile;  a9 t; u" O* w; f0 a! A
else :3 e3 g) f, ~- C7 ?9 L) H0 c
    echo "暂无公告";
% K' Z/ @! l) sendif;
$ a3 E. w/ F- D1 X2 c/ Qwp_reset_query();
( ]6 X: D- `3 j; b& s) u' d6 J```
" i( {6 Z; I$ i9 A, @7 C
: F. t( W9 P( }  B+ V这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。
( p& A$ I/ e8 B! Z6 n$ I0 K
* ?4 g' W4 n  q以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

347

主题

565

回帖

3683

积分

管理员

积分
3683
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?5 N, T% P) ~: x. D+ S. ]
7 r* g4 T4 w+ S
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。; t& I4 `4 ?5 |2 z) k/ U

- F, B# N& w  u7 f. i4 |6 P以下是创建自定义插件的步骤:
$ ~9 Y& B# n; d8 O2 e6 N
: ]5 w8 T5 I6 m- M. m2 t( R/ C1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:# O* \& L8 x& B) x
4 y* h  p8 r$ ?4 ?% Q' y" ^7 K+ G
   ```6 _- o/ O! S9 w% x4 O% j( ]. @" h
   <?php
1 c+ z( b( t5 K) w9 C1 ]4 P   /*
. X3 F+ |3 x; h9 c( z   Plugin Name: Site Wide Notices Plugin$ ?* M9 n& ^" p; X1 X/ e
   Description: Adds a new custom post type for site-wide notices.$ L3 y6 K. j: d8 }+ C% M* J
   Version: 1.0
6 F5 Z" {# b2 u9 G( P/ |   Author: Your Name- Z  p: l$ @4 K) P1 L- S
   Author URI: http://example.com
  f7 f# f, A7 y6 W1 |% v2 w   *// y0 t5 @7 I( X! p% ~4 h# b+ Z

1 q" _' A0 ^+ ~- |' v8 [   // Add plugin code here...
0 h: @$ A/ m. a3 I4 W4 {   ```% {% ^8 ?" A& }6 d0 J

' `2 r* x! \5 D3 ~& \   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。, d( E- L) T  N: z0 l  i9 w+ O
6 q0 t5 R+ X5 _2 O
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:+ X7 x0 H2 ]2 {$ O2 J$ a
) X; v9 J! n% y) w/ L7 ~3 h( J
   ```- |" x) D  r# ~$ P2 J9 ?1 c
   add_action('init', 'create_custom_post_type');
% \  e* ~. {3 ^7 L$ @+ Q) ?' o; i   function create_custom_post_type() {+ a8 \$ e" C- C. M, l$ u5 T5 g
      $labels = array(2 g$ V+ ]. V9 `9 [
          'name' => 'Site Wide Notices',
# k4 D4 i8 V1 d8 `          'singular_name' => 'Site Wide Notice',
( X0 `5 P8 U, x          'add_new' => 'Add New',
8 W. F/ n# [! I4 g, _' u" v+ z          'add_new_item' => 'Add New Site Wide Notice',
2 K1 m* k% R/ N* S& s/ V4 H) J          'edit_item' => 'Edit Site Wide Notice',
) ?; V' ?: l' o' a& ^: N; m' p          'new_item' => 'New Site Wide Notice',4 F: x. n; {  d+ o- Q/ f
          'view_item' => 'View Site Wide Notice',4 s" S2 `& T) a) ]$ v1 _
          'search_items' => 'Search Site Wide Notices',: x, I; ?- ~0 `$ j! ^) L) @% q5 V( O
          'not_found' => 'No site-wide notices found',
( W; L0 V; M. N1 P, s, L( ~! N          'not_found_in_trash' => 'No site-wide notices found in trash'
( t0 d8 H6 T& \3 b: N      );
  l  _0 c3 {2 @) x% l1 _9 t5 X% s+ U1 `% d
      $args = array(* D. @4 n- x* k+ g: q9 c$ P
          'labels' => $labels,5 x6 o4 S) l. R- ]
          'public' => true,
$ X4 ~" T- ]8 m% }' R" H3 b          'has_archive' => true,
" B# W" F* X, `2 c3 G$ h          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),# Z; c& k* s4 x: N5 y
          'taxonomies' => array('category', 'post_tag'),
1 [! }$ ?2 c7 Z! G  }          'menu_icon' => 'dashicons-megaphone',
2 g) s: t. h2 {, {7 x8 s          'menu_position' => 5,
: w2 p( |, X( u9 R  f; {4 C          'rewrite' => array('slug' => 'site-wide-notices')
# S, t* K; p/ b, e, N      );: x) e) r; F8 [9 ]  _. ?
4 E" `  e! b8 Z6 \: `/ ^
      register_post_type('site-wide-notices', $args);
+ t0 U* Y$ Z% B. b# ~  q5 p2 q3 G+ w   }
7 M3 n" O! M% i4 p* a   ```
( f+ Q) X7 s+ t6 o: S2 ^, f, F" i/ V- O* X' P  \$ [
   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
) n# \; J% G3 e4 l. x1 s
0 i' i) ^8 f4 I  v  [+ }3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:' P5 j" j/ `* J& o- r# R3 H

- A4 n: {2 Y( I& P0 E6 b/ m   ```
* c9 J0 q: P4 w, T   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
: l; J5 L  B" y1 T' v% M1 X# n( A9 [( q   function add_site_wide_notices_boxes() {
" x& U2 V0 d2 i, h) @      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');& T# a, t9 z6 _# X7 d' G( v
   }
2 v' ]. [8 \, T+ i
: M" E/ P$ s# q9 _0 A& ]# c   function notice_details_meta_box($post) {
( T3 j. T/ Y, a6 c/ E      wp_nonce_field(basename(__FILE__), 'notices_nonce');4 Q  g, l# S, y" |1 {1 ]/ a
      $notice_title = get_post_meta($post->ID, 'notice_title', true);
* ?( R3 s7 T4 B6 M, `, c0 m$ _% R      $notice_content = get_post_meta($post->ID, 'notice_content', true);
+ C- f6 S  D/ |  F- S* L' l/ p      ?>2 D" c, \) C. A( l
      <p>5 d- Z1 Y! G9 e4 w: F( l4 H
          <label for="notice-title">Notice Title</label><br>) H1 X' G" k7 [- P( f2 z! o* ~$ a
          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>"># N7 A' b: U2 q9 O; i! ?
      </p>
6 V  a9 m8 @: @      <p>3 |6 u. P5 H6 {- `& M) r0 D
          <label for="notice-content">Notice Content</label><br>
2 c' i# o4 O5 ?/ x3 c          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>% F5 q4 }7 L7 f! U. N
      </p>
: n9 V& u; T: L  l      <?php: _, A+ \+ q" v  q6 H& z
   }' ~$ ^+ e0 v5 I) f
3 D1 {7 j3 P! F4 D
   add_action('save_post', 'save_site_wide_notice_meta_box');  z  L) A9 B3 n' B7 v: O8 C7 t& }
   function save_site_wide_notice_meta_box($post_id) {$ q( L1 b3 D! r: j/ ^# t, S
      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))4 f5 ]/ v# k' l+ s& U3 L2 A9 v) O
         return;
0 r0 b8 J2 t1 A8 B6 n2 r      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)% A3 K5 R5 \$ Z& o/ i- |
         return;
$ K/ l& i$ r5 D% x8 x
, X! m4 X$ j! q      if (isset($_POST['notice_title'])) {
  Y7 B6 w; a6 }  }/ q7 W          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
3 ~1 B" L, X. C9 c! {* n2 e2 ]      }
% Q' t1 I4 l5 b+ L: }9 S      if (isset($_POST['notice_content'])) {  I8 N+ U. v. g$ r8 n
          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
8 Z/ R$ c; f  W8 U0 R7 F      }6 F, ^- z$ q% ~
   }( P2 P5 p8 f  Y# c& o9 V1 b. {1 g$ r
   ```
) c, {3 a1 Q1 i9 }7 e* Z
" ?3 z# a9 ^% U* D  g+ [3 [   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。, C/ I# `* R" b6 i% l7 l
# ?. s7 [* t9 Q4 T  s3 k) E
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:: i& j. ?/ e& t: W

' C2 m. k) _5 l0 s$ _& O   ```0 k" ^/ `1 \+ L+ y$ w) F4 C+ H
   $args = array(. w, T( k2 ~% d
      'post_type' => 'site-wide-notices',5 D. r( P) r) O- A6 W5 Q' ~
      'posts_per_page' => 3,
! r1 Q6 }4 w$ r4 x* K0 `      'order' => 'DESC',
# W# z" S( N2 y/ ~1 r3 u* A      'orderby' => 'date'
- c0 A, G0 g- H8 \8 c, Y5 {   );  e* x2 H3 e" K; i. r9 K7 U) d5 M
   $query = new WP_Query($args);; G! D! `0 _; Y  h
   if ($query->have_posts()) :6 [" D5 d% A8 i$ N$ R
      while ($query->have_posts()) : $query->the_post(); ?>+ ]; E# ~" W6 T* f$ s0 P
          <div class="notice">' X( i! n$ R' k) g+ S+ I, r
              <h3><?php the_title(); ?></h3>
' I+ e9 U  r% i8 a2 t4 `& k) w# E              <div class="notice-content"><?php the_content(); ?></div>" u7 c$ s. \$ h; w! u
          </div>; b1 S: _2 v- C' t1 Q! \
      <?php endwhile;9 I( j3 {; Q( n; j9 i
      wp_reset_postdata();
/ T+ W) m+ Y; G; k9 X+ Z   endif;* [0 W3 o0 d+ _) p
   ```
- V1 z, c5 b/ M+ J0 H% a
8 Y: v  n% m9 x) F9 H3 Y   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-13 17:58 , Processed in 0.012337 second(s), 2 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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