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

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

[复制链接]

343

主题

551

回帖

3619

积分

管理员

积分
3619
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:* m) O$ d: X$ j9 W* p5 @/ }2 v4 c& C* f

% }- C# m- ~7 C. B, x) Q1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。
% R8 C  o9 s( z/ e; H* X2 l  A8 C' k, s1 V
2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。8 {6 i1 g% A9 t0 K. \% q
& L4 A3 ^+ Z1 [1 G: m% P" q& |
3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。
) G! S& V& E: {* R% w1 B7 u1 ]/ A) K# x! Q  W) i
4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。
8 u5 d* ~9 ]. \' \
% u( X& g. I5 b) \( s- D6 J注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

343

主题

551

回帖

3619

积分

管理员

积分
3619
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?
+ G3 ]% i3 d+ P5 ?- p. p5 o/ s' a, t+ S: X7 y1 |5 L) H
如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:
6 a) c( ~0 w% j# {6 K) e* N
% j+ G+ }0 f4 N6 r( P1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:9 ^5 }2 f/ `) v
  J1 X: K1 q; z  W
   ```
# I, F/ r. P0 V* |' X% d, W   add_menu_page(
6 j! l3 I' ]) b+ C7 M       '全站公告'," I9 C, G. R  O) K/ f3 `; U
       '全站公告',
! t& j, o& I6 |2 g- S% `       'manage_options', //需要的用户权限
+ c: N! W* m' C4 s2 _6 m/ i; M0 A1 d  X       'site-wide-notice', //后台页面的 slug: k5 h1 [- P. S0 s9 e
       'render_site_wide_notice_page', //用于渲染后台页面内容的函数$ p% K) u, C0 T3 i- d# |( F
       'dashicons-megaphone', //用于显示在菜单中的图标
3 V# i6 b7 Z# w3 G2 A' E1 d( V) Y+ u       30 //菜单项的位置
& z3 D; p9 z" n( j% i0 q: B! ]* ^$ y   );$ b1 p7 r( G" i* j. c6 P
   ```
# x9 x) ~8 T$ ~4 n  k" D6 D
2 G) T$ d" e% N2 x; Y   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。  U# V& i7 o$ X( j

( Q2 U: o2 [* t! Z, N3 m4 @8 v4 \2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:
; W' ^3 Q: p/ i# N9 g8 n/ w( U9 s- D, \/ ?
   ```6 I# {& L5 z% J$ `: B0 J
   function render_site_wide_notice_page() {
' z( n, B+ k% s/ A# H      // 渲染后台页面内容
# ~2 n/ i( U, I, C$ i- _& w      echo '<div class="wrap">' e, X5 y/ Q! V1 y1 t% {6 I
          <h2>全站公告</h2>
% f) T, H+ A( q2 h, T          <form method="post" action="">& U1 P% U0 r8 m
              <label for="title">公告标题:</label>
# x: g! v) m& u              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>
1 Y- S$ @1 q7 l2 A1 J) l- r              <label for="content">公告内容:</label>
2 i$ y/ V$ F/ `4 t6 r* A% V              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>
, r9 u- w/ e' p  S              <input type="submit" name="submit" value="保存设置">7 Y* w( l0 R8 y( x" V  q
          </form>& n3 {8 T7 {# P
      </div>';
* s" o( m5 G: e/ x" y1 G
- Y( _6 ^& p8 p5 j, z; Y      // 处理表单数据1 o. G, H; h5 Y' N8 z/ H6 C
      if (isset($_POST['submit'])) {+ v/ Y9 y& S9 j& C/ F+ V# \
          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));  `$ S$ @, j+ B$ I, d5 r3 s
          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));
; H0 e5 U- d7 o      }1 `. n" ?  p) }& [
   }
+ L! e. p8 Y1 `7 t0 J* \   ```% B+ G/ t- a( }. R& \4 H
2 l+ o1 e6 Y% w8 I5 _- S
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。
9 U6 F! g1 C' O$ Y
- o: b6 h( ~7 ^2 d3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:
; ~  H7 ~; n" L' w8 J
: g5 H4 i2 f+ v* r9 A8 M   ```% W# r" T  B, i' ~  J" c
   <?php
7 Q' J) M8 W5 L: s% c( l4 I: v  `   $site_wide_notice_title = get_option('site_wide_notice_title');1 G) S; V  M3 u3 l6 S* n
   $site_wide_notice_content = get_option('site_wide_notice_content');" ^+ e' O: @( ?' X
   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {
8 K- T% [$ K/ G. N; o" e       echo '<div class="site-wide-notice">';" L! {. u, \8 Y$ `7 N% Y4 d6 \
       if (!empty($site_wide_notice_title)) {
+ o' G1 a5 }0 ~4 Q$ d" U2 G           echo '<h3>'.$site_wide_notice_title.'</h3>';
% h# _: y# A2 n5 `       }
+ [3 P2 v8 D: |. \' c3 D; ]- x       if (!empty($site_wide_notice_content)) {! Q9 P0 R3 l1 j3 J, @/ I. r6 J" e
           echo '<p>'.$site_wide_notice_content.'</p>';
9 \0 k& D+ E- }) ^       }
8 [4 `6 X# x& O4 d: n       echo '</div>';
! ]# w8 Y$ o# B1 N# k   }
- ^6 D; g/ T8 Z5 G' X   ?>: g  C! d8 I, B! J
   ```
) v# K( G  H5 @, L" s  ^- d: J: w3 g0 @+ [* }0 C
   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。
) f; F- V3 Z  c- V
6 i' K: b0 d; W$ n请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

343

主题

551

回帖

3619

积分

管理员

积分
3619
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?
# B0 v* L0 w# A9 ^2 l$ W7 K9 U
8 X  o( L" _* e您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:, J6 R4 M- V/ S0 [2 G) o* H& O; `
' G, M3 c9 G- ~7 D/ h8 H: ~
1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:
2 l( T* H' t* }5 i3 K; f( Y$ G- q$ m: L6 Q2 F
```5 [, f. {9 n) Y' S7 F8 f
function create_custom_post_type() {
  E  F" B- w  E7 n) q- O    $args = array(
' B, ^/ o0 a$ l- i        'labels' => array(/ w3 P5 \' `/ }: Y( T& _% K9 m
            'name' => '公告',  l/ C3 G% u/ Z. h: ]- H
            'singular_name' => '公告'5 o1 x( m! J4 c6 {& i2 y
        ),( v$ C7 q& \2 l" k' O: T
        'public' => true,$ e( k# Q6 K! u5 M3 H! H9 y
        'has_archive' => true,) g' Y) U/ n% t- W" j& i
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),' D: V8 Q& T$ e
        'taxonomies' => array('category', 'post_tag'),. K/ o1 s# f7 v
        'menu_icon' => 'dashicons-megaphone',
4 `3 M2 G* |  ^+ T3 e  i        'menu_position' => 5,; N9 \7 j" o1 p4 N
        'rewrite' => array('slug' => 'site-wide-notices')
4 |" ^# s3 q% S& w/ m! @, s9 v3 g    );8 N# M! i. m' j: T# h
    register_post_type('site-wide-notices', $args);% W7 A, {8 X1 }
}: S) M4 `8 H3 \
add_action('init', 'create_custom_post_type');
/ Z( p  T: [; ^2 H9 J/ v- x0 E```, u4 n$ D! Q# z3 ~) y2 q, s- J1 k
6 L9 X0 d* P: c* e# K: W
在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。$ A! R7 [: C3 o! @1 U$ Q2 }" g
1 D, ?0 ?& H1 n/ s3 Z
2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。1 G/ C) g' C) J& m; h. Y

+ U3 l1 M0 F0 {3 i, z3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:, K" F& w& N' R4 {% `
# }3 i; t/ h4 ^: W$ E" H# i& n) \. y
```
; R% o8 E* L& ^* U7 J  w* D$args = array(# D$ M8 {( O6 J- A2 _
    'posts_per_page' => 3, // 显示的公告数量
, n3 n0 V! B) u3 C6 o    'post_type' => 'site-wide-notices', // 公告文章类型% p0 l; K6 L( b
    'post_status' => 'publish', // 公告状态! O4 d4 \0 s' L* J/ L4 F
    'order' => 'DESC' // 排序方式( A' P5 P, r& V
);* x) A2 j$ W3 i; Y8 \- G# e) ?
6 m) C3 H7 v0 j6 e1 @: s+ K6 h
$notices = new WP_Query($args);
5 |- O& X; c% p! o) `if ($notices->have_posts()) :  A3 g+ m: B9 }1 B( x
    while ($notices->have_posts()) : $notices->the_post(); ?>
9 M3 v1 L( N- n4 ~! b! \        <div class="notice">
3 q1 r* s" e, j- m7 \2 ]            <h3><?php the_title(); ?></h3>' n4 k' r5 {; o4 H/ B
            <div class="notice-content"><?php the_content(); ?></div>& q9 V3 q7 u$ S+ T& R
        </div>
# H+ p# P( Z; Q$ g9 P    <?php endwhile;
% E' i3 `4 L/ Z7 N) j& E) Kelse :4 _# v* ?* k+ G" e3 u) W
    echo "暂无公告";0 h* c9 y4 E9 r+ s& X
endif;& {4 }8 n; ^9 y% f
wp_reset_query();' h9 {# U- G# O# e, C
```
( S; @: p5 s- |# q6 L3 b
/ c+ ~7 D1 C1 h7 y/ E, M9 C这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。
1 X0 [  w" w  K- Q( s3 H" N* ?3 y+ u$ h  K
以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

343

主题

551

回帖

3619

积分

管理员

积分
3619
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?0 s  X3 F. {- C# V

" A2 P1 Y) v5 Z2 o2 I# T如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
) O/ ?3 c5 k& n1 ?$ a3 f* e, i
# F1 g* {5 g5 y7 j" V4 P2 j5 h以下是创建自定义插件的步骤:  r; b$ i4 S9 i! K! J( ~- b6 ?" n
7 i0 a, O% F) n5 o. O# E
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
: x1 E6 @7 [! a  w3 z) e" T
8 @; g& ]5 g. V/ D# k   ```
+ j, J3 N$ t0 V; z- O2 j# O   <?php3 U2 J6 L! F0 I' H
   /*$ U2 g9 @! u9 K8 j( M! H& g+ j8 A
   Plugin Name: Site Wide Notices Plugin) T: s) V; D6 t: S
   Description: Adds a new custom post type for site-wide notices." k8 G4 J( t8 c! G
   Version: 1.0  Z! Y9 K* E- P) q, u6 e
   Author: Your Name5 x' g3 m$ T8 e2 \
   Author URI: http://example.com
  l& ]2 w. x' r5 a; J   */  t1 M9 U% n. U8 q
7 |! @5 ]% ]! y7 r
   // Add plugin code here..., D; S2 J+ |8 h0 y7 H% G+ ^2 R
   ```
; f% ]5 K1 s: ~; ]9 ?
" o0 Z% @- Z7 @8 T' l9 t   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
9 B, U' R. Z+ R+ E8 `$ h1 P% e  T+ K6 r+ S, L
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:1 I. Y  @- ^/ l# V

9 B! j0 w: a4 _/ H# F" Z. j( T7 |   ```3 W# _4 I# l$ D  [) ^; N' f2 P
   add_action('init', 'create_custom_post_type');  |2 U" ]5 s1 @) }% r/ z$ Z% |2 Z! }
   function create_custom_post_type() {
7 D% b8 E- }4 |9 n" s/ x. [) ]      $labels = array(
, q' p) C, k" U4 s5 N; Z          'name' => 'Site Wide Notices',& M2 K( ^/ t# p) `- ?- i' s; j
          'singular_name' => 'Site Wide Notice',
& @1 B5 e4 f  e" w2 u          'add_new' => 'Add New',
* N, N/ L: a0 x1 H  g          'add_new_item' => 'Add New Site Wide Notice',
0 u& B2 z  H- S4 \9 }+ u          'edit_item' => 'Edit Site Wide Notice',8 b- [' K4 X" ~* F7 i; E! }
          'new_item' => 'New Site Wide Notice',
; {+ {/ @' k2 H/ i1 x! b) `          'view_item' => 'View Site Wide Notice',
) x+ n; Y% A# \5 D          'search_items' => 'Search Site Wide Notices',
$ C6 f$ @  q- ?' L9 d2 k9 W$ ^; Q          'not_found' => 'No site-wide notices found',. b# ]& |* n% ]
          'not_found_in_trash' => 'No site-wide notices found in trash'
) }  a! |# L4 c" E2 e      );
# ]. }1 c# a& @" M( M# Z$ m* n: ~" {6 s) m7 s, ~
      $args = array(
1 _4 t: G/ X  \+ w, n1 K" N          'labels' => $labels,4 V$ w' p4 @: \$ u" I7 e  Q& A
          'public' => true,
* o9 ?/ S8 @& m5 ^5 F2 Z; @) p8 |  i          'has_archive' => true,/ k9 A) R' x- P
          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),% M5 Y9 h0 }: m. c  z8 w
          'taxonomies' => array('category', 'post_tag'),3 c8 r1 M5 Y. q1 q( v, y/ z" s
          'menu_icon' => 'dashicons-megaphone',+ u6 w6 u: N$ {0 h0 b3 J# h, @: d  l
          'menu_position' => 5,
9 [2 d% `& g  L% ]. V! B          'rewrite' => array('slug' => 'site-wide-notices')
- d7 I+ Q- u2 V2 a) [. A      );. E% j4 T7 Y* G9 H/ p
" b; w8 y1 l# w; D; _% ]
      register_post_type('site-wide-notices', $args);
* z4 y! \& t* w5 n. S+ U   }8 ~+ G4 a8 L/ |  O. A. z
   ```
; Z- {; P0 U7 d7 u
1 o# |3 g2 T- B! G   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
1 Q& l  h. v% y( O# f  y; r3 |( W$ E4 S% W2 Q# h% a
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:$ R. e6 r  ^3 m& ]5 e9 K2 Y

5 c6 g! `# T: b  n   ```
5 x0 e: O; M7 I% [* `+ d   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
; G! s1 o9 }: v5 Y7 R! _   function add_site_wide_notices_boxes() {
  ~( y; D) \: h# E' n9 z      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
' \1 j% J, C3 W1 u   }+ D1 i0 H3 r4 s+ z: x5 _
' z) N$ @& c" O  t
   function notice_details_meta_box($post) {
# e# n; B5 u$ c6 q! G, q      wp_nonce_field(basename(__FILE__), 'notices_nonce');8 [* f* e1 H* k7 T( Q9 Y: x
      $notice_title = get_post_meta($post->ID, 'notice_title', true);  t$ s5 f  Y" C  z! e8 P4 O
      $notice_content = get_post_meta($post->ID, 'notice_content', true);8 w( X2 J& r8 P& A, \2 M  y3 @
      ?>+ C; [0 u4 d$ I4 Z. Z2 o
      <p>+ N' C8 G+ r9 c1 h; _+ a
          <label for="notice-title">Notice Title</label><br>
$ U- e4 a5 ~, k5 }          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">9 S0 K" S5 `$ b4 ~1 U0 m
      </p>
4 z7 X9 p3 W" b2 F& G0 i7 j      <p>
& ?3 l! |6 y$ R2 D          <label for="notice-content">Notice Content</label><br>  a, [1 O  }9 l( H- u
          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>. n- [( S3 }2 \* c" J5 i% F# ~! z
      </p>0 a; Y/ F; Y; M; f' b$ e0 S
      <?php
+ H; K& h$ L0 [& h9 V   }. ]) z4 W( a7 v; r% y
  B$ G/ t0 f" v5 D; `( O
   add_action('save_post', 'save_site_wide_notice_meta_box');
0 m$ o- b! G) ^: o   function save_site_wide_notice_meta_box($post_id) {
8 a, ~3 H! H; ?/ W4 j6 n      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
. l" w3 J+ F# N- k9 q- n         return;. s' H! {' ^! s! B* e& P* J& U
      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
1 ?* [  M4 X2 y, {4 m         return;. P: {) \3 F3 W( `7 n+ i

: O' E9 V' Y' X5 Y1 i, d      if (isset($_POST['notice_title'])) {
& @$ ~# J+ `  \          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));' t' m1 n) A, h% C, x  X4 K4 M
      }) h. X$ w: Q8 K/ v9 H3 W* r" m
      if (isset($_POST['notice_content'])) {4 A% A0 G( ?& K* y# K  Q
          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));; a) f' I/ V2 P* c: G
      }
+ M1 _+ M) Z0 F& E6 |   }
1 g( y, L7 g# J# r$ c% y' w   ```2 I0 s; V7 A: r' @1 i0 t9 }
" x# h3 i: D" d$ k0 |' U1 A. D! W
   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
- A- r# e6 e. }3 T4 I$ X( @0 M4 k& U- d3 q9 j+ q# V# `
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
/ H  L, y; x9 ?7 h# O
5 ?, P+ Z* T8 Z- e3 j: @   ```& C5 j/ p, ?3 {
   $args = array(
# R) J& r, U, L: @7 X5 N* ^# C% M      'post_type' => 'site-wide-notices',
( E7 v! e3 B9 _" m9 B      'posts_per_page' => 3,
' k$ b% T, |4 C* H      'order' => 'DESC',, I. k/ I. }" {$ m- }- v2 }: q: A
      'orderby' => 'date'" g  L; R& P5 q# b: |% R/ {, P
   );
/ w. V( v9 v* X( G; y  S, t   $query = new WP_Query($args);
/ s1 m# }& {# ^1 Y. j" C7 d3 u   if ($query->have_posts()) :: E) l% S9 N( n0 ~8 z7 Y+ K
      while ($query->have_posts()) : $query->the_post(); ?>( ^: ^, ^6 m. `! M' K  o
          <div class="notice">$ V5 ]: w4 \; F( v1 _5 O
              <h3><?php the_title(); ?></h3>
2 l. \6 U* p- E0 n9 y              <div class="notice-content"><?php the_content(); ?></div>& l8 \3 C" x( S, P2 Z
          </div>
( q) G: H$ f6 F. }5 Z  V      <?php endwhile;
# w- H8 K- E* W/ R3 i; l' N      wp_reset_postdata();: D/ v* O' _& }1 \1 c5 W- \! t
   endif;
9 `. V+ i1 E( j/ `$ e9 M) E   ```
; [) [0 r% n; W5 L( }7 O6 }) d; S  e$ x
   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-31 10:11 , Processed in 0.019236 second(s), 2 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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