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

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

[复制链接]

333

主题

515

回帖

3385

积分

管理员

积分
3385
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:) f% ]/ U1 `3 G6 U$ H1 D4 z

+ N/ t2 [: ~3 ^, z. g1 p& F1 a1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。
( K  ^8 `8 D! G% m6 ^$ b: e" n, C2 D' U' E4 v5 _. q* ~& T
2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。- m- u0 C9 t) I' \7 z# [) {
; m) O8 [$ l3 }$ p7 D- `
3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。& b( i2 V1 [4 O: [9 T
, W+ ^, G$ r9 R+ }+ d
4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。: i, v9 G! n8 d+ M& `. F
. U  N. x" i  \6 [! K; E
注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

333

主题

515

回帖

3385

积分

管理员

积分
3385
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?) l2 E0 q3 l  `$ b
/ L- H* V9 c2 I8 z4 @+ `/ N
如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:
! Z6 Y! P. k, i% @" k) G
5 F/ L3 k8 B+ B( J1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:
& E8 f- s( l7 ?% j: U0 i6 c
$ r+ L& u3 _4 l% J% G2 j+ z0 S, R   ```
  I  _3 I' N1 h. L3 B  P! v5 s+ V% \& I   add_menu_page(
1 m% [3 q% _% q) B. F- O! {4 z* |       '全站公告',1 W1 N. b# Y8 G; ^. s) b% n( l
       '全站公告',7 _: \. i/ @) g% l
       'manage_options', //需要的用户权限- Z7 n5 r* o2 j
       'site-wide-notice', //后台页面的 slug7 w; l; a8 b. K# t0 w/ C: N) n
       'render_site_wide_notice_page', //用于渲染后台页面内容的函数: u& N+ |! c/ {: H! H
       'dashicons-megaphone', //用于显示在菜单中的图标
; b& n7 _4 o. R) H       30 //菜单项的位置
% w* V( L" |  L  X6 e4 [, Q/ j# @# g   );8 @% C# K# a( m2 S
   ```* p1 j/ d! G. J6 u( p6 q* C5 I

3 n" E  p! H: j8 i   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。" O5 V! q# s. q& h2 P
1 C6 h% d/ r1 ^3 [) s
2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:# i0 d) m. w$ ^3 v6 ]% g

( r$ N* |# F# G9 n. j. Y7 b% ~6 ]$ V   ```! K+ D% ^# a  L6 c! X. M' P7 e
   function render_site_wide_notice_page() {
( c# F) N) `) Z/ v      // 渲染后台页面内容
. F, f/ D4 K' n! s      echo '<div class="wrap">6 {2 o8 y; J% e$ L+ d9 `+ v
          <h2>全站公告</h2>
, t' z1 r2 l# \          <form method="post" action="">5 u8 q) L/ d+ D0 h
              <label for="title">公告标题:</label>
/ p: A4 q. D- E              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>
4 E5 ]* I0 E; w1 g5 U& `              <label for="content">公告内容:</label>& T8 k% g# Y  U( u* ~& L
              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>
  X1 ?8 f2 Q% N, ]" o              <input type="submit" name="submit" value="保存设置">( ^3 k2 F9 k7 k0 D. A# c: ?
          </form>
$ U2 \$ t. M) \      </div>';! K0 v. w* n# ~6 o. q

9 q/ o3 W# E+ D& ?# O2 p+ x- [1 Z8 m      // 处理表单数据
& Y0 Q1 [5 s- w( S& I2 L$ \3 Z      if (isset($_POST['submit'])) {' ]0 i5 W9 ?/ v) W- `
          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));
/ V5 @- W" d8 K, `          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));; D2 ]& Q  J" Q2 g. \
      }
5 C0 t6 T8 a: A! ?# W: Z   }
5 l; X+ |3 h# w   ```
5 P2 ~# C: I1 [( n0 B0 U! _& M( L4 |
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。
0 `) }2 u9 A& n/ H7 {1 H  G, Z7 t
! j9 h; [# t+ i3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:
2 N0 ~! {+ k6 P- S5 f" n- ~0 k, ^7 R  w
   ```4 Q# d% q( O( U9 d
   <?php
% E/ |- H1 S% ^* Z+ D   $site_wide_notice_title = get_option('site_wide_notice_title');
2 J& T/ j5 o( s; K, Z! Q   $site_wide_notice_content = get_option('site_wide_notice_content');
2 G! x% Q: a' ~8 x8 ]1 y# J* X   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {
6 j  O  e- C' R       echo '<div class="site-wide-notice">';0 |5 v% S3 n& {5 W/ s
       if (!empty($site_wide_notice_title)) {. r' A1 C0 n  X% W1 r& H+ U
           echo '<h3>'.$site_wide_notice_title.'</h3>';/ f6 t: D( o4 d8 n  |& q+ O9 j
       }" b# y2 t9 {% j' @
       if (!empty($site_wide_notice_content)) {
3 H$ e  f7 f" j) Y8 U           echo '<p>'.$site_wide_notice_content.'</p>';+ ]* |- z3 R1 ?# R4 N# D: A
       }
4 P+ l) B; ^; d5 w# l; |       echo '</div>';: B: q; @7 w: ]' Z8 G# V* x1 [! \, U
   }
& f, J3 z& {  x) V7 Y8 j$ H% y+ s0 y   ?>
4 p; G; k+ i7 I" D7 Q7 F7 m   ```
: E! s) [: ?/ F5 P' N2 T/ T! v" p& }$ Z4 m0 w' b. R$ {
   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。# x( m) V" @0 I5 G% L
8 x- [$ C0 c/ _0 |
请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

333

主题

515

回帖

3385

积分

管理员

积分
3385
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?* j+ r3 a; {% Y+ p) c: S
2 S6 Y6 l; H1 T! m! u' K  {
您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:
: E+ R" c3 Z: v) |# Y# z8 O
; ]  g9 [) i9 f5 f. b$ t1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:% l0 O: H: i1 r

) K4 i5 Y1 D+ Z' \, V  l8 ]```
7 ^/ L5 ?; h9 Pfunction create_custom_post_type() {* I1 E' R% ~: _& c2 s4 g. K# |
    $args = array(+ Z8 ]3 d5 N) ?2 Q6 s) m
        'labels' => array(; o2 X$ u$ g4 s9 y# \
            'name' => '公告',4 a% q: l3 v0 _- Y) G* V# G; F
            'singular_name' => '公告'
5 p* D# r4 b8 v7 V0 u' @        ),
1 q/ d- L- j0 R, m        'public' => true,% E% S) h% ?4 p
        'has_archive' => true,1 l8 i7 _) z, `$ ]+ h( C5 Y/ E) }
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
/ O" K  ?& J2 _& |# j        'taxonomies' => array('category', 'post_tag'),
( d0 N4 I$ b$ d. r$ N8 Z7 K( T0 M        'menu_icon' => 'dashicons-megaphone',
# N, B; X6 U& _8 O) J  f        'menu_position' => 5,( c1 H, C7 d) u- w  j! A2 }* B& d
        'rewrite' => array('slug' => 'site-wide-notices')
$ l' S- R' S9 a! B    );
( @+ ]7 X7 P+ s3 C2 u    register_post_type('site-wide-notices', $args);8 Y6 K/ z0 u+ m" Q% b0 a2 h6 `3 B
}# G' J/ T0 E, L! _+ t$ q
add_action('init', 'create_custom_post_type');8 v/ y' l4 a/ K. p
```
2 P6 Q1 n" L# @, o) d$ }) c) T, `& O
在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。
- b3 r' \0 g6 N2 P! N9 G5 H3 M0 _" k! w
2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。
- h7 Q, F$ Q; b0 y5 F0 p( x8 M, I2 g: l/ H- J
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
% q, p9 X7 t! b" L& i1 f6 H# t+ [) f2 E4 g) D  ?5 K
```
  n* \0 p& z3 G$args = array(! I, \9 T" i2 ?' P: N' |) p/ r
    'posts_per_page' => 3, // 显示的公告数量
3 c7 J4 ^3 b6 @# |    'post_type' => 'site-wide-notices', // 公告文章类型: L7 Q' Y, K  K0 {3 B& e
    'post_status' => 'publish', // 公告状态' S1 K6 @) L0 g( P7 }7 Y
    'order' => 'DESC' // 排序方式6 y9 e8 G+ E" s# m. {4 J5 M+ X
);
$ z5 a3 I& |0 f' ~5 K' l4 D6 }* v! Z# M
$notices = new WP_Query($args);0 J( k( T# g; t' d4 W: B
if ($notices->have_posts()) :
( L# u4 A) {0 a2 `# }    while ($notices->have_posts()) : $notices->the_post(); ?>
1 x- T) T  j9 X8 R1 e( v        <div class="notice">  h, ]0 D! x; S0 n# m0 ]
            <h3><?php the_title(); ?></h3>. g% }# O* _( n$ i+ v# E' \3 w
            <div class="notice-content"><?php the_content(); ?></div>
- T, q2 {1 |6 h# H2 d        </div>
! c( f" ~* C6 `. v8 @" h% P    <?php endwhile;
! j- K$ {! q+ lelse :
  _: _  f9 X, o: R) M8 d. ^# R    echo "暂无公告";, X" h& S+ V# n, m1 d3 u- i( s
endif;2 D. {3 ]9 J6 w3 N5 a
wp_reset_query();
# g  d  L" S( z; X) w2 j* s```2 e3 a0 I( ~! u1 W, x, h  D
, a  N8 r1 _; n) c7 |: I
这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。
& F3 Z) a" F% D) x9 W& z4 y1 J$ B
- i) i- j( j" x7 p3 x9 n! @以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

333

主题

515

回帖

3385

积分

管理员

积分
3385
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
+ H2 q' U' B+ m9 S
9 G$ u5 q& Q- Z! k如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
0 _  g7 [+ q3 S' `! J
8 N5 Q  w" T' W7 f9 z以下是创建自定义插件的步骤:
% Q2 l; e7 j6 |/ X8 M% A+ }: J% {# E3 f2 ]0 u3 s3 P% z
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:, H4 d8 |2 W! G

5 ^% ?+ T- M, {5 h   ```
0 b( C/ J% e' k+ L6 y   <?php0 a6 G# r. [. Q* w
   /*- e5 Q% u6 q# J" z# d6 `8 Y9 ?
   Plugin Name: Site Wide Notices Plugin" _4 H2 g/ Y2 }- H( i& M8 v& x
   Description: Adds a new custom post type for site-wide notices.
& L) p# q7 D# |' ^7 N/ Q   Version: 1.0
& A% c/ ]$ _5 d   Author: Your Name
9 B- K9 v8 A7 d9 S+ X   Author URI: http://example.com
. e5 N7 u% \$ T6 V0 `   */; H5 s0 N1 M6 S: _- f2 n! W

3 ~6 _, x* d! H7 _8 f( D3 j; f   // Add plugin code here...9 S# t4 ^2 g( F: e! N4 n
   ```
8 p3 n2 i( W1 Y1 f: u
3 p  w& @% @) k, c6 E9 X/ q, F  R   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
) Y6 E7 L! P) T2 c3 T
- b  {- v4 G% ^9 Z1 V7 O) f2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
+ I! k2 k$ o9 K2 n3 i- }% P& W( x+ w2 r9 [9 {" V( A
   ```
8 }0 M7 x1 s$ r0 I   add_action('init', 'create_custom_post_type');
4 r  @5 _( T# t4 t  V# W4 _# d   function create_custom_post_type() {
6 i" `: Y" z. g' _& F2 E- w      $labels = array(; W& e; f# `" Z/ V
          'name' => 'Site Wide Notices',
+ x  R3 r1 h$ o$ J( t4 _          'singular_name' => 'Site Wide Notice',
8 Q/ A$ v2 W: m          'add_new' => 'Add New',
$ t( l  }' c* ?          'add_new_item' => 'Add New Site Wide Notice',: M2 m8 v) h6 [  o8 W* H" n% M
          'edit_item' => 'Edit Site Wide Notice',$ s! L" |9 z* t0 _4 Q5 ?
          'new_item' => 'New Site Wide Notice',6 a7 x$ O3 T8 A1 g; n
          'view_item' => 'View Site Wide Notice',+ y. U1 Q: S$ S+ }( V0 {3 H4 A
          'search_items' => 'Search Site Wide Notices',! ?5 Y4 {5 K* o; o3 R
          'not_found' => 'No site-wide notices found',+ |  I4 p- P1 N, w3 p! Q8 g
          'not_found_in_trash' => 'No site-wide notices found in trash'
2 D  F; u4 K1 u5 [* s8 i0 ?      );( F- e; `- k" ^6 E9 \3 A/ Z/ N
! A* W- s, l6 V: q! M$ S* q* x
      $args = array(( M3 z* O, N! Z4 H! N
          'labels' => $labels,) a% F" D+ N7 _: O& D
          'public' => true,
4 }" I, S6 A" Y          'has_archive' => true,
5 d3 @. Z. R6 I2 ]$ |4 m3 M' p6 `          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
4 D# p1 i2 W; `: E: [          'taxonomies' => array('category', 'post_tag'),
# g6 \4 H1 R$ T+ l          'menu_icon' => 'dashicons-megaphone',- f& t0 k7 V5 r6 {; K4 N
          'menu_position' => 5,
) p. ?: w' K9 J          'rewrite' => array('slug' => 'site-wide-notices')
! L' M- `, Q2 k! D' @: p& L      );
( I$ a9 S1 o* E$ y3 W6 [; j9 I  u6 J. H1 k! S3 l7 N5 }  G1 w
      register_post_type('site-wide-notices', $args);
% f$ w. y+ Z* G6 w+ b* H% [+ e* P( N   }
  |. ^% [+ A, ]8 ?9 x+ ?   ```! U2 f3 B) ]! m$ _, j) p8 [
5 O0 N4 i5 D7 y6 V
   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。6 d9 _0 N9 h, L% [+ W( A
" q1 v9 J: c' x8 @
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
7 g& P  h, Q" u' N% O+ G
2 d4 R7 F3 P3 d$ L   ```
' }+ ]! a5 R) ]. z! I" U1 e+ Z   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');( t' B  x0 H! ]0 }- h6 q4 \: f
   function add_site_wide_notices_boxes() {
! [3 \! [/ c" J6 e      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');/ T3 q1 O! ~4 D. G6 o( S5 ?
   }
' F2 f% Q0 H! Y- r8 }7 H/ s9 U5 Z! k, \" @6 ^3 T) P) W, e' N
   function notice_details_meta_box($post) {
4 O$ P3 W" ^: K# r9 b      wp_nonce_field(basename(__FILE__), 'notices_nonce');' m4 h/ n) t6 f5 |) S( M
      $notice_title = get_post_meta($post->ID, 'notice_title', true);
6 A' e1 ^+ _) P7 F  v      $notice_content = get_post_meta($post->ID, 'notice_content', true);/ z' ^4 X" ?' {5 b- Z6 ~$ l
      ?>
1 l9 V: U/ G6 p9 h8 w      <p>: s) Z; O5 R7 u3 H, `7 |% i
          <label for="notice-title">Notice Title</label><br>$ s& r1 s( {. x& Z
          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">* Y- \$ F1 ]6 c  r1 w
      </p>2 k/ w$ t! M$ s. O" J# K& v
      <p>
4 T! a0 E( H$ m; B/ q* t. U          <label for="notice-content">Notice Content</label><br>* i  Q, ^5 I& A* p( x
          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>; U6 w# W1 c2 F/ x% k8 ~6 J9 \8 W
      </p>. z& H4 R$ R% j" D) J
      <?php8 T, O5 M2 A* I
   }6 ^6 G# |1 C- }& u$ Y0 ~. @2 g

! t0 ]) [3 S" |% _( X   add_action('save_post', 'save_site_wide_notice_meta_box');8 ^3 N/ \" u0 C+ Q! A! I/ m5 E- @; u
   function save_site_wide_notice_meta_box($post_id) {
1 v* w$ E, Z8 ^) G& }, T0 A7 v. G      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
/ X! Z5 P$ J! A6 i- y9 U( p         return;4 Y5 ^$ p' k0 {& Z
      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE). P- D3 _8 K3 e; F4 ~/ R* i
         return;
' N- G& v- |5 g7 f' Q) y, ^& Y3 x9 m6 b# B6 @' [" W
      if (isset($_POST['notice_title'])) {8 W( N, w7 @2 W
          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
+ P3 J4 U, H. e% a1 x* o% i* c  Z      }+ I6 f7 J$ `6 \
      if (isset($_POST['notice_content'])) {
% Z' `) m& T9 C* t2 l          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));( z& d0 d( L; A$ E' P
      }
4 I7 [+ b8 G2 L% r, P   }
& F$ [1 S. \7 g   ```$ X/ d# k2 Z: B( v4 c

% C# s5 @( g" h7 T   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。6 }1 m' R5 J& |* {1 _4 U8 r- _

; q" J* w5 {5 C- h$ ~$ v- l/ R- f( b4 l% O4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
9 D$ D' c" A4 p' D3 F
0 {5 h& S& Y( E5 H2 s   ```' ]2 t+ q% m' I7 A( p$ c7 x& _
   $args = array(  D  S1 I% i9 E* W
      'post_type' => 'site-wide-notices',: R4 \$ q& g6 S5 \
      'posts_per_page' => 3,
3 N. @7 a" d% \% {% Q6 [( k$ t$ d5 _$ Z      'order' => 'DESC',$ e( {( R: N1 p
      'orderby' => 'date'
- p2 i8 X' C3 X4 @  M' Y' I   );) \& J% h5 [$ R& s
   $query = new WP_Query($args);0 ^8 E$ f6 h9 }
   if ($query->have_posts()) :: |6 C" R6 {4 ~* x( h
      while ($query->have_posts()) : $query->the_post(); ?>
) d' V4 q  I4 |) o          <div class="notice">
1 \) I, ]! g) P              <h3><?php the_title(); ?></h3>0 }2 T$ s$ N) n) D
              <div class="notice-content"><?php the_content(); ?></div>' ?% e7 Q# ]; m3 Z" x/ h/ V5 [0 E
          </div>
5 K: V9 A: n$ [$ W) c7 I      <?php endwhile;
7 b# |1 F# d: l1 e" _  J' N      wp_reset_postdata();- D$ V7 x1 ?: z  C
   endif;/ }; a4 s2 K) A8 H, O4 l
   ```5 e* X, I" i! V* O6 Y+ V* p* h
% T' g6 [6 h7 Q, b' E* F! T
   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-29 05:46 , Processed in 0.080201 second(s), 4 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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