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

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

[复制链接]

347

主题

564

回帖

3679

积分

管理员

积分
3679
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:8 t8 Q; a2 ]% e3 U9 |

/ c. L' H$ P# R! @8 R6 v1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。
; q( X( O) ^* O4 c$ a, e8 I% `3 C* t. F& }9 g$ W
2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。
6 _$ w- ]0 H  x- j! m. t. Z: a
! t0 ?6 T& O- u- u. g3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。
/ W/ x1 t  B) ~8 p# G+ O& X. s; C: y( p7 s
4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。; a# \: r" \9 N$ o

- t) S8 q4 u. U2 b& u, {; }注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

347

主题

564

回帖

3679

积分

管理员

积分
3679
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?1 J/ X* M0 u- R0 _  ]0 w' K4 @

& J5 v& z% c. q' ]7 Z( O( a如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:1 z9 y+ V; ~8 B) K+ ]4 u1 n
2 {' q3 d5 u3 S$ p4 L
1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:9 M6 a) S& X- E! P! e
# i( i: y7 B$ ^' D* Y
   ```
4 r. B. N4 B  l+ ?3 n% M* d. b: C" Y   add_menu_page(
1 Y: w3 A4 f8 r       '全站公告',
0 h5 }8 y0 Y6 j! k       '全站公告',
6 w) w1 W5 a1 r/ S2 {* a9 e       'manage_options', //需要的用户权限
3 W8 [. l! S+ b* [! ^( i/ c       'site-wide-notice', //后台页面的 slug
  c, I; u" N0 F' }. C, a5 W# o       'render_site_wide_notice_page', //用于渲染后台页面内容的函数$ M0 C: D9 x4 ~, \/ Q" O
       'dashicons-megaphone', //用于显示在菜单中的图标
4 ]( v. D/ M5 w       30 //菜单项的位置
( t( q4 r6 \' w9 z1 N& a8 S   );
9 X8 R% e5 f: i   ```* \2 k) h+ ?+ Z3 O$ u

' b* y4 g  u6 ?+ H7 H; Z( C% F; y! J   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。
8 x) x9 r" t( s: G. _% `6 ?
+ }- p( |9 H; c/ k6 `2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:% P7 h* l- b% r8 ], m* E
3 `* |5 h6 k, l! Y
   ```, B; f1 ^! u1 ~
   function render_site_wide_notice_page() {1 W& r5 |$ u# v9 L( U2 K$ P1 V
      // 渲染后台页面内容
. h) M4 g1 t: R) W7 r: Q      echo '<div class="wrap">5 h+ }6 [6 F+ n4 H' t
          <h2>全站公告</h2>
5 J8 @6 J% b' m. S# B) M          <form method="post" action="">7 w: |( E* y5 T2 ~
              <label for="title">公告标题:</label>
2 g1 s4 Y( b4 d, }2 C              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>( ]& C  ], F* i) \1 E
              <label for="content">公告内容:</label>
9 S, i4 A5 O) i& l5 o( D  X7 X              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>( \5 L5 E8 f: y! j( i9 N1 K
              <input type="submit" name="submit" value="保存设置">
3 }2 H0 @* l" L9 c6 c9 ?# L$ M  R          </form>) i* s- S9 m% ?# ?& B% b' t
      </div>';5 r* `$ M9 s  L% \& r% E1 s
5 D7 [5 l. Q8 E6 `
      // 处理表单数据
5 v4 a8 N% |5 g      if (isset($_POST['submit'])) {7 w  ]: x' W/ a& {9 ~) h3 y
          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));
2 p: \; X, g) s' W          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));" H! F* {" b* S) O* r
      }
9 L% j" a0 i! ~5 `   }
' c8 @1 L! }0 }7 m& R  W   ```" E4 C  [0 T+ E7 i  S# B
" q1 h! v, Z; B
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。9 s2 ]7 r0 j6 j9 E. q
5 @$ ~- C: n; t. N, }, W
3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:
6 \8 {; s" @) ?/ \5 `3 q! O% T5 x7 M) w/ ?9 A# w7 p7 D% r
   ```
4 u& f5 q4 }* n4 ]. E5 L9 r4 X; a   <?php. A8 V! u4 @) L5 O$ b% N4 e" d' w
   $site_wide_notice_title = get_option('site_wide_notice_title');; ^3 n7 d7 h( ?( T' Q3 L
   $site_wide_notice_content = get_option('site_wide_notice_content');
+ A8 F6 ^8 v9 u/ U   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {; Q% c# h8 ^1 n; V8 H# b
       echo '<div class="site-wide-notice">';
" K) _3 [9 ]  }4 e       if (!empty($site_wide_notice_title)) {
- @+ O; i/ B4 ?           echo '<h3>'.$site_wide_notice_title.'</h3>';
9 v, m4 F6 k0 C! S5 P       }
3 L$ x4 e! P8 q9 \5 i       if (!empty($site_wide_notice_content)) {8 Z/ Y6 M* Y* v
           echo '<p>'.$site_wide_notice_content.'</p>';- Y7 {: N/ e& s% f" z, W
       }( t$ T1 ^8 Z  H; _
       echo '</div>';
. t+ M. ]" @3 q   }
9 b$ _% A. d# ~( ?9 b; b   ?>
+ Z9 K' ^. n/ O3 u: E& _( r7 Z. B' E   ```
- r$ _, L% T' d. B+ X
/ q, v4 H' U' c3 d   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。' Z8 j0 t" f0 H% K$ a. X
& p3 U- z* l% v# B8 G7 z0 d) \
请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

347

主题

564

回帖

3679

积分

管理员

积分
3679
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?
3 B' G+ \; r) n$ c% u4 Y, D
% B0 w  h  G* F0 u( u您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:+ h2 m$ f& T4 L9 ?/ A
' _: G/ l% Q; ?6 z) B2 Q  k1 d
1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:: L% z* d% b1 n- E: [; p! X0 d! R
; ~  Z( i0 c. W3 s
```
8 K/ O3 |; ~5 M9 q8 g: _function create_custom_post_type() {
  e. `" E: o* N+ C  G1 F7 E    $args = array(
; k: i) Z# Q( o0 V* @) `* o1 d        'labels' => array(
3 f5 B% W2 S' Q* ]            'name' => '公告',
, ]/ }* A+ ~/ f            'singular_name' => '公告'& z. H5 O# O. m. r0 ]: n% b, h$ x) }
        ),
0 D) P' T9 U. H6 @& }1 w        'public' => true,
% h! q, i; C5 Y% G, o5 b# `        'has_archive' => true,
) \+ ]4 p1 S3 a0 L  \        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),3 q: v1 a' T0 U: h1 g& J2 r9 x
        'taxonomies' => array('category', 'post_tag'),
7 L5 X! V* ^0 l$ o        'menu_icon' => 'dashicons-megaphone',
0 m4 i7 j$ V' x, [; Z+ ?8 `' M        'menu_position' => 5,
9 a  X' G; e# D$ N6 A        'rewrite' => array('slug' => 'site-wide-notices')
# \$ {+ q, _: Z+ y7 s9 w) ?    );* m* G) v* O& z0 z4 m  R2 g8 F: H
    register_post_type('site-wide-notices', $args);9 ~( A. A5 T. R6 [; x3 j1 C
}
% {$ ~+ _: [3 W! V2 N: Vadd_action('init', 'create_custom_post_type');2 s6 _2 s/ R, A2 v4 \
```1 a9 |, F# m! Y% }# ~9 |  U7 P

9 B6 t( s/ S$ E  |  L0 N在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。5 r/ U- v2 w4 W$ D

! g. C" D  n0 w  `+ i" L9 C& P# V2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。
$ N9 Q  j' m$ d& D' l0 m9 z7 |( n  D, R9 \6 p3 h
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
$ Z0 F5 }' S% j* c8 g
5 e9 P. A7 \3 k4 S- I- i" z! c1 X```3 P0 n% N2 P# ?0 t0 t9 I. ~' S( U
$args = array(4 @# e/ p, I9 G" `( J
    'posts_per_page' => 3, // 显示的公告数量
4 j' n, W" R5 I( b" O    'post_type' => 'site-wide-notices', // 公告文章类型) b! {8 k3 k2 Y& C  V0 Z8 Y
    'post_status' => 'publish', // 公告状态2 w1 L( H6 g! P9 W+ t, c% [
    'order' => 'DESC' // 排序方式1 b8 c/ i' o5 o3 @6 z: o, H
);
: q' Y" S9 F9 u, X$ V* M
0 b, u. @4 Q  B! r7 R$notices = new WP_Query($args);
/ E% O* u# R: aif ($notices->have_posts()) :) c( m* L6 h0 `# ^: o) \% V2 w
    while ($notices->have_posts()) : $notices->the_post(); ?>
  j. P" i& K2 F: L! ~5 j' P        <div class="notice">
$ D: k8 ]. k- ~/ e3 c5 H% |            <h3><?php the_title(); ?></h3>
$ O8 S% O7 O* J/ d$ z3 M            <div class="notice-content"><?php the_content(); ?></div>
3 u4 ^+ W& E: ?$ f6 v$ j6 g* |' i        </div>
0 f) @/ _3 r% x    <?php endwhile;
5 X4 l, v  j3 G: s/ _else :
: h3 D1 Q. ~5 v4 a; T    echo "暂无公告";
2 H; f3 h! u4 g  Pendif;" ?  h5 p- W3 [9 i+ B2 E
wp_reset_query();
3 s% t7 F" _4 Q8 ?```
" @* a5 m% W6 s* W1 q4 B. @/ g1 q) X- s: J7 c! g' Q: r5 K. K
这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。
  J, y' }# R8 u" J
4 Y: y: r% h/ a# Y* o9 c+ V0 o以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

347

主题

564

回帖

3679

积分

管理员

积分
3679
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
) G" y7 N! f0 r& n5 G4 f2 K& }4 O7 g. {" i! Y
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
+ ?" {% h! d) ~; m* t
- H  g4 I9 o) M$ C+ W以下是创建自定义插件的步骤:4 c; P0 H. {* D/ Q
  U/ U# R2 }5 |# \; g
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
' @" ~# Q% p( B
: X& Q+ }" M$ a, f( ]0 t, M9 u   ```
2 k$ C! r# h; o* j   <?php2 C* v* I/ s& y/ ]
   /*
* f5 u) c5 G8 ]# x   Plugin Name: Site Wide Notices Plugin4 p( V: x9 q- j' J
   Description: Adds a new custom post type for site-wide notices.# k$ S1 j& E' s" n5 s
   Version: 1.0! ]: w! C8 B" k
   Author: Your Name
4 @, J" s1 n5 b9 l# N) P* L# g   Author URI: http://example.com+ B! _; I; o  t4 }! ~/ h" h0 f
   */' z. X( J6 }, A) ]! V, k' J7 `

& N: p, P/ g% |   // Add plugin code here...$ b) j6 |" |" j. ^3 e- Z, Z
   ```
, b6 O+ r: ~* }$ C1 x6 V* `7 p5 u+ k4 [, u
   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。; V" Y7 z! @& y' d- t7 }

! v/ H* x. M5 b* d+ O2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:8 y6 X9 f& t& Z2 o% l# O% u

. P: a# D6 g, k   ```+ S' p* Y  P# `: n9 W6 N3 J
   add_action('init', 'create_custom_post_type');3 r+ _# I" V% r( a
   function create_custom_post_type() {
( @4 F9 s0 l' W) X0 A: r- w7 e& z      $labels = array(
8 x/ m, G' V. J+ V) Y          'name' => 'Site Wide Notices',
9 j) P. G2 ^( D: w7 e          'singular_name' => 'Site Wide Notice',) ?) x2 h/ @" Z4 O" `5 e$ Y' M
          'add_new' => 'Add New',! ^  v. |% L0 p1 ~; b9 Q+ y
          'add_new_item' => 'Add New Site Wide Notice',
# B5 z# [& X3 K' A5 \# @          'edit_item' => 'Edit Site Wide Notice',* o! E4 N6 u' N* P$ Y/ P' R
          'new_item' => 'New Site Wide Notice',& b. m0 S! d! [
          'view_item' => 'View Site Wide Notice',4 w8 V2 Q+ l1 L( t' d
          'search_items' => 'Search Site Wide Notices',
" ]8 Z* ?+ s7 P1 Q4 b          'not_found' => 'No site-wide notices found',
) ^+ N4 N! I& |) b3 G          'not_found_in_trash' => 'No site-wide notices found in trash'3 w3 X0 n9 \9 }) X. J3 C. k
      );7 ~' s. p& I6 i$ _
% a, K' G- q! d1 S/ v; l
      $args = array(" M6 o+ n  P# s, v- x
          'labels' => $labels,
+ L, |* [. T0 T" e6 Z4 `9 J7 Q          'public' => true,
8 [' U( X* ~8 L4 K7 Z. n7 Q. N          'has_archive' => true,
9 g+ M* u0 \6 O. z. Z' b          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),2 z" _4 O  z! D. s
          'taxonomies' => array('category', 'post_tag'),( `2 K6 w  P1 F! W; ~+ m
          'menu_icon' => 'dashicons-megaphone',, p$ D' b% f9 b! S' p# s
          'menu_position' => 5,( V& Z0 s$ [$ D0 K% ~
          'rewrite' => array('slug' => 'site-wide-notices')
! x/ ~: b$ }- R4 F5 v6 R% p5 J  E      );
5 g- n6 ]' y0 \* M9 Z' r* u6 @+ R: p
5 |( b& k: o9 G4 ?      register_post_type('site-wide-notices', $args);
+ h2 ]+ f& i" J4 J" R0 J   }# V" K* M) h0 ~4 ~! H% ~; ^
   ```
0 {1 T+ M5 a) H
9 W% o6 d6 n: X9 a   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
' F" [2 C; u2 \0 h
0 E( n( V8 u+ Q* v( H+ O3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
" F+ R5 `3 w' O( H8 i% ?% H; X: {" f  Y* p3 L2 L$ H) N3 \' G$ k
   ```# t, K1 b0 ?) c( G- H9 M
   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
! B  V. g% y' C6 J+ ?' H( ]+ N   function add_site_wide_notices_boxes() {! f& }4 W) L* `; l4 m  n
      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
9 z' V- j4 W7 x0 E6 }1 o   }
) D, |, V8 z: I9 Q. l: K1 Q. g3 J8 Q* ]! @* B
   function notice_details_meta_box($post) {
" @7 E- t  I3 g/ p0 ~8 G7 q$ N/ x1 o      wp_nonce_field(basename(__FILE__), 'notices_nonce');
1 g5 ^, n0 ^) E5 v- |1 v      $notice_title = get_post_meta($post->ID, 'notice_title', true);
0 f: d4 i/ m( f      $notice_content = get_post_meta($post->ID, 'notice_content', true);
! ]1 a! t4 f  j0 X# o7 _      ?>- n. Q6 Y- h; x
      <p>
: E3 W$ p7 x; F3 q# [7 }  f6 u. ~4 `          <label for="notice-title">Notice Title</label><br>
! r# }" p( D7 M0 V) p( {0 x          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">8 M2 G8 @  Y& q7 T/ k
      </p>
4 F2 n& R+ Y: z" a. a" a      <p>) J9 }. }; \5 z1 P/ w
          <label for="notice-content">Notice Content</label><br>
" a# M4 `) l2 x: r$ l          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
/ ^2 w% o) @: e& b4 g( H( o: m      </p>
# }: Q/ F1 g; I) ^4 c/ w8 Y( A      <?php1 }0 p' e) z! R* a
   }# ~0 U  b/ H* M$ m7 {; ^  M) X
9 m' u3 `0 k1 ]$ L
   add_action('save_post', 'save_site_wide_notice_meta_box');0 o7 n0 o$ F- z5 @4 x( G5 E
   function save_site_wide_notice_meta_box($post_id) {6 s4 H  x3 U5 w) ]! F
      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))/ _. @% j; s4 \. l6 y  |) _
         return;& R* `+ Y5 k2 s6 S. @- o) B( |
      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)* Y. l4 m6 K+ h& z
         return;% b5 t8 o7 B0 d3 E5 ^3 N" z
4 d, g3 z! \4 }; h
      if (isset($_POST['notice_title'])) {$ a- T$ ~" m% M! v$ u, S1 p
          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));6 n) T" Q) K: H4 }4 Z) r
      }* i0 l) e1 h6 s0 M) T/ R4 O% {
      if (isset($_POST['notice_content'])) {
0 u& {! k3 w1 D( t          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));# b# ]1 W# g. M; O# u/ C
      }
+ z- n  X. b2 t2 e% I3 t9 l5 w   }
2 r- C' J% E# U( L: G   ```! h( r; ?8 x8 }3 j9 i
5 ?0 H- B3 R* J
   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。9 Y; P1 z  ?; V0 s
% D0 ^( [) g+ c" _- e% b
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:! H8 s& J; [  F; j4 Z3 I6 ?; u, V

5 p9 V$ Z8 ]  ^% h; L9 W   ```
( H$ I+ E& g( t1 ^; S# y% C1 E+ ?   $args = array(. \1 J5 d$ [% X; u$ s  M% x4 J+ E% W  h6 E
      'post_type' => 'site-wide-notices',
7 H% |* k$ L( Q- J      'posts_per_page' => 3,: c5 e2 n2 Q, `: c4 b' X/ w( g
      'order' => 'DESC',
$ x0 `% e  h; Y2 R& g% Y  R1 H      'orderby' => 'date'. C7 G5 U! L- S8 a) K; o! X& a0 x9 P
   );, t$ Q! G9 R( l- g) y
   $query = new WP_Query($args);
) C2 [# U$ _7 V5 ]: H   if ($query->have_posts()) :1 v; [) H( g0 A0 y" S) X
      while ($query->have_posts()) : $query->the_post(); ?>
- o8 L+ [' w6 K5 c          <div class="notice">( f4 G& m  `$ N. Y
              <h3><?php the_title(); ?></h3>
; o; c/ e/ Q! g5 r8 W              <div class="notice-content"><?php the_content(); ?></div>
5 s: E- M  q$ l          </div>
4 g4 @% s/ a  J" I! z! L4 t# K4 x      <?php endwhile;" a% {3 W, M' |
      wp_reset_postdata();
$ y, p  ^5 \8 D- d# _   endif;
+ B2 P0 O$ G4 W; i/ ~! D   ```
4 H% a) n9 W5 m$ e  o& o
# l  P  G  z# L( U! D/ S; }8 S   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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