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

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

[复制链接]

335

主题

520

回帖

3433

积分

管理员

积分
3433
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:
6 Z1 y/ A3 m5 \: z* t7 e5 i  T' V% e0 @6 y3 v( U* H
1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。$ {) C! b/ {; B) P# Y
) X9 @$ A! s, a0 u7 y) X0 t8 a) V
2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。
. U: p( C$ q6 X  p
0 Y# a5 z; g( Z3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。% D& V1 f6 Q. r7 G; c# U

  l- }. V/ k( R. K4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。( _& X0 W( g6 q9 V2 j7 ^! o
( g& p% d/ ?5 l( h( G8 ~) B
注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

335

主题

520

回帖

3433

积分

管理员

积分
3433
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?
% t5 z$ U9 ?* o: o, \
+ Z& P- t+ U* O4 I如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:
4 R$ q2 \% Y4 P( g1 |, U/ V7 y3 u0 N1 M* i$ l! G
1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:1 c3 x  {. K# F) ?' H0 \1 H
- G1 h0 N5 \, e; `) `6 ]
   ```
, D% O, |0 c7 C7 d   add_menu_page(  Z( ~/ ?1 w$ ]2 i. f+ F1 J4 |0 g
       '全站公告',
& r+ R4 H5 F0 q7 A       '全站公告',# V/ X7 x8 t: V1 Q9 p# N0 v
       'manage_options', //需要的用户权限$ H; X! E, V1 Q/ d5 P" F
       'site-wide-notice', //后台页面的 slug
7 k0 ~( u9 K& T8 ?       'render_site_wide_notice_page', //用于渲染后台页面内容的函数
4 ^7 L- v; ?& f3 B       'dashicons-megaphone', //用于显示在菜单中的图标3 k$ f8 s' R% [* {0 J
       30 //菜单项的位置
3 H+ @3 C$ ?. `8 ?6 \   );. g8 g' g; I" }) i
   ```& \8 D, h/ o% k
' y6 ~9 H7 t7 s' ?
   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。: e2 `0 y9 v+ x2 r7 l, }

' e+ E6 o( N: z5 P+ P9 k7 m2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:
* z) X) v, b5 x* g- e5 S0 y3 Y1 Z1 K* R, m# X( g
   ```& @. s% c5 s, s/ }0 H6 B
   function render_site_wide_notice_page() {
* f, l, ?  A: B/ H% r      // 渲染后台页面内容! \! ]! C9 P5 \$ d
      echo '<div class="wrap">1 b0 D! L! L6 O+ ^% j
          <h2>全站公告</h2>) v- N5 A- i" Z
          <form method="post" action="">  w2 z' H0 l3 Y
              <label for="title">公告标题:</label>
8 m: g, N" v0 ~: F! c; \) ^# e              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>  e$ `! W( N% S) S
              <label for="content">公告内容:</label>
! |. R+ _6 X1 t              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>
% X7 U8 h/ ?$ [& [              <input type="submit" name="submit" value="保存设置">% f. \- g; y) H$ k
          </form>5 i0 j2 e* \/ C1 B0 |
      </div>';6 N8 r+ l2 n4 Q+ O( q7 l$ o/ V3 t- A7 b' J

, u1 H& x( _7 E3 G' k/ V      // 处理表单数据2 e$ W* \; x. }: a( v
      if (isset($_POST['submit'])) {5 F" G4 T6 \' l! e: k
          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));6 ^  I. v( r( f. Y4 X9 E. l* o
          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));% E$ ]3 D0 L. q, ^4 [$ v- {3 R
      }
8 f% @/ w2 A9 v+ ^5 K   }
: ?1 L7 C  {3 t1 ?   ```, G0 T; d* \3 ^( L
5 A+ t$ d2 e4 V5 n& l2 ]( |
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。
4 U' q4 G3 W5 Y6 K+ }- O! e8 m3 e& q/ _% r8 h+ o, Z
3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:
% W0 w9 l' H4 M9 s/ L2 ?
9 _7 x' b- n/ L9 C5 C* Z9 n# Q   ```
) z- g! s% }, ^! i  R0 ^   <?php
) D% {8 \7 B+ M7 Q( ]   $site_wide_notice_title = get_option('site_wide_notice_title');
# M' B$ Q1 M8 ~/ v% m   $site_wide_notice_content = get_option('site_wide_notice_content');1 }, F$ Z$ l% Z$ @* N
   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {% A) O8 |: J7 J' l- G4 C
       echo '<div class="site-wide-notice">';$ G9 Z; _* r; [1 v3 ]7 Z& R+ q
       if (!empty($site_wide_notice_title)) {/ j2 I7 Y" t0 m  L& Z: S; {+ @
           echo '<h3>'.$site_wide_notice_title.'</h3>';
1 b8 q/ n3 \5 p. ]; j! t  A0 a       }  f8 f" y: c2 A! O5 E% N
       if (!empty($site_wide_notice_content)) {
' y3 c& u$ I. }+ T; S* A0 ?5 G           echo '<p>'.$site_wide_notice_content.'</p>';# Z4 J1 {0 m/ t2 z2 b! v, S
       }9 ^& e& @9 Y. c0 ?
       echo '</div>';0 R+ |) R5 `- b
   }1 s4 I. A" j" y7 p3 H
   ?>, d" @3 ^- U0 g- p- M7 u
   ```$ W5 R: \' a1 ]7 l+ q1 t, m. W

/ n5 u1 o) ~- a% l  b2 q   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。
  L* Z- i! _; {: g
" P0 c' _( U5 f3 k3 x" _请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

335

主题

520

回帖

3433

积分

管理员

积分
3433
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?8 Z4 P5 x) v2 t2 m# F, O8 {

, C( E* c" x0 z9 b& p  |您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:* \% ^  {) K! ]( M/ ?; d' I) i2 H
3 I( m  Q) b5 d  m2 E2 [2 H  j
1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:
3 d9 L# |# J* O% e
" D( h  y  ?0 e. @0 K```
9 l- l0 G2 C! a/ E- Jfunction create_custom_post_type() {# Y* W4 y! [. t: S, E
    $args = array(' c( N- t) ]3 j, M
        'labels' => array(6 d4 C0 J4 E1 s7 ?# x
            'name' => '公告',  a6 e7 m# T7 D2 M& w* O( E' r
            'singular_name' => '公告'
/ Y; O  p1 e) J' U+ B; n& B# ^        ),! f( A$ x" j# E0 n! k
        'public' => true,
. r( V$ v! o8 X' Y' ~        'has_archive' => true,; D0 T) T# |! l3 n+ B7 f
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),' M7 {1 ]; |* T
        'taxonomies' => array('category', 'post_tag'),. N& }1 U% q; ~+ G
        'menu_icon' => 'dashicons-megaphone',. A8 D# U: [1 Q3 L0 y
        'menu_position' => 5,9 Z( {# u5 e! T$ K
        'rewrite' => array('slug' => 'site-wide-notices')4 K# h! q# U" @- x5 v! J
    );. F: S1 S, S3 \: A/ H2 t6 J
    register_post_type('site-wide-notices', $args);
: D* Y& F8 b5 ~6 {1 N}3 n9 X2 Q# }2 X# ?4 q
add_action('init', 'create_custom_post_type');
9 G( |& x4 O! o( _, e% W: i```% g  w* y, x# x2 ^

. v9 v4 X) X6 [! u( a在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。
5 j. F+ p) c* O" D( u1 W, d4 W- O2 n1 ^9 M, T, }( h) i; M
2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。
9 M& z+ ]" H) E  p4 C) w" c' t4 Q& S2 H/ |9 v$ R4 E
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:1 `4 j% I: P/ Q6 X
$ Q1 O' _' H  _" u- s- v0 @
```
, E: a! R8 H& R$args = array(
5 }. {  |$ N, C    'posts_per_page' => 3, // 显示的公告数量7 L& _3 V7 s9 I7 D3 x
    'post_type' => 'site-wide-notices', // 公告文章类型5 W# I1 s+ \  {& H; O
    'post_status' => 'publish', // 公告状态' Q0 z: @; r. o- a
    'order' => 'DESC' // 排序方式, R) d# g6 r# L- y* U8 [
);
- a" G3 n8 U& S' ^3 {3 S0 X, P- ]- T7 v- A& x0 u: Q# H
$notices = new WP_Query($args);. k) m1 A& N1 n3 V
if ($notices->have_posts()) :: h% C/ a! T- `8 k: {. B2 M
    while ($notices->have_posts()) : $notices->the_post(); ?>
5 l0 Z: \8 a- W) j        <div class="notice">4 K9 {7 D; l: f/ G% N
            <h3><?php the_title(); ?></h3>
7 M8 O4 y* m* Y  \0 W2 o9 ?/ _* S0 A$ W            <div class="notice-content"><?php the_content(); ?></div>, ?. V3 D  H4 ^8 P3 I0 r- b" `
        </div>
7 }" x8 M3 b0 g! A1 S    <?php endwhile;- G' i# y& F$ H! e$ X  a4 D
else :
0 R' [5 K9 {6 c) B: x    echo "暂无公告";
9 q" b, n# J6 q* p- X8 W, o: mendif;
2 U2 d5 a, M! [: j3 v8 _wp_reset_query();
$ A( B& O7 p% [" N& h# t2 r```( w2 p0 b( S( Y  p
) x" y* g8 \6 d9 v3 Z
这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。
. f  Z% E2 ^+ r0 h. |6 A
. a2 w9 @0 I/ \& Y以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

335

主题

520

回帖

3433

积分

管理员

积分
3433
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?! P( P9 t3 G; V5 P' X
1 K/ ~6 `$ A% f  o& `9 ~
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。, c/ i5 V- r" c4 J+ \$ K' h  \( T& t
( J/ n1 C, U1 C& u' u0 m; a% M/ i
以下是创建自定义插件的步骤:! M7 h# |4 \6 v8 `: I& {3 |, R& n! t
7 l4 q$ G' ?4 Z+ F
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:, P$ {# A2 i$ \4 Y4 o
2 x3 b0 k* s  ?  q& p
   ```; L6 z5 R' P  ]# J& g6 f& K
   <?php
& C$ c. \' ^6 L' ~- `3 A   /*
3 v; d! @" c9 w) h   Plugin Name: Site Wide Notices Plugin" T  |; E6 l3 K+ _4 b) h' t3 @
   Description: Adds a new custom post type for site-wide notices.
1 x% w2 j: k1 a; O6 Q  e) r! N7 t5 @   Version: 1.0
6 Q# i/ \0 n0 A9 E/ @  _' w, D. x   Author: Your Name
/ y% Z. E" r5 ~9 u# ^' |/ M' g4 W, x   Author URI: http://example.com* s6 \9 m/ v4 y2 q% p8 Y! R4 w
   */( \0 g$ L$ J% t3 _0 o; w4 Y

; w$ C; x& v8 B- M5 E$ i   // Add plugin code here...- i/ ?. d1 t$ B( x0 t& c
   ```
6 ~& y9 N+ E) o7 q! ]  ?
3 D" I. w% g) n0 n* _   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
4 ^; P* ?' v" ?
- _5 H+ \/ |) E& @3 i2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:) h* Y- s* _+ ?- w/ i
2 F. p$ V/ X" y: \% x8 L& R# b: A
   ```7 X/ ?3 ?* \  p' Q
   add_action('init', 'create_custom_post_type');" v5 v2 e4 c0 }% C9 @
   function create_custom_post_type() {
" t( C: y# H3 z9 G+ t      $labels = array(# ^: X" `' c9 }
          'name' => 'Site Wide Notices',( D' C; @3 m* y2 v1 {
          'singular_name' => 'Site Wide Notice',
" M/ O+ m+ j$ c5 C, ^/ s          'add_new' => 'Add New',
; Y: p% n: Q" R( V, ]6 |" x5 c          'add_new_item' => 'Add New Site Wide Notice',2 ?9 n  i! W8 ]' ^
          'edit_item' => 'Edit Site Wide Notice',
# a' P$ e& o. M# D0 Q          'new_item' => 'New Site Wide Notice',
* u1 ~! N+ p$ b1 a+ _1 Y- g2 m          'view_item' => 'View Site Wide Notice',
; ]% j( {' O1 k          'search_items' => 'Search Site Wide Notices',
; z& b1 H/ {( r* a2 C          'not_found' => 'No site-wide notices found',$ ]6 c  L3 {( ?7 H9 q
          'not_found_in_trash' => 'No site-wide notices found in trash'9 O# J! q8 d& m, g+ S  J
      );
- G+ w' }% q; a0 Z: n
- O4 k8 }+ M# T5 M6 s4 @$ M0 n      $args = array(
  c6 ~( B( [1 A! B0 [( E3 g0 T          'labels' => $labels," x# |, \. E4 m0 u# |
          'public' => true,
1 q8 _- u7 g- ?+ j5 ~          'has_archive' => true,. L- a3 I  P: K- j7 T  m  v3 Q1 N7 u* u
          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),7 `/ u3 y) n) B. l- N6 _
          'taxonomies' => array('category', 'post_tag'),
) ^/ l; l! u0 m7 ^' ~7 n0 J0 C" e          'menu_icon' => 'dashicons-megaphone',
! x( c' k. e) [' n7 s( }- z. }: ~          'menu_position' => 5,
+ j  Z6 r3 L8 m( h) I. J! a" y$ U. e          'rewrite' => array('slug' => 'site-wide-notices')4 j; n* ^8 e& T+ r' a9 a
      );$ h, B5 Y7 ~! m* ?7 s8 H
$ \9 ]7 i6 q; F- S, N
      register_post_type('site-wide-notices', $args);
+ c3 }! m+ k9 W( M  P   }
" c' m/ h' j, K   ```
% O3 E% u7 M) l4 T* `# ~2 G# ^2 `" d4 p4 g
   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。4 h0 a! Z9 k  N: s

6 E4 t+ w  ^& u' I1 |' o3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
6 s3 U; ]' p9 K5 m/ g- g9 J" l* }* ]' @0 I+ x! d5 O) n8 O1 d/ V5 t
   ```0 u' G" b7 k3 B
   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
1 b6 i$ l  V5 i! Q6 h   function add_site_wide_notices_boxes() {% J1 b* g7 p" ?: E; d( d( U: j% p
      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');. b& T1 ~6 U3 E0 e
   }+ u2 `1 j4 O* i4 ?
* g! X8 d1 [; Y3 F
   function notice_details_meta_box($post) {! n+ A! N: \3 Q8 p7 J. g* u
      wp_nonce_field(basename(__FILE__), 'notices_nonce');
/ l9 k% g: ^. ~      $notice_title = get_post_meta($post->ID, 'notice_title', true);
2 R* z+ v: d7 ~" e$ L0 m1 {      $notice_content = get_post_meta($post->ID, 'notice_content', true);. [( g1 `! p* m. Z  V" [
      ?>
2 j9 \/ m6 L9 u9 i7 b) o      <p>5 q' Y4 ?% n2 p- r8 s: t
          <label for="notice-title">Notice Title</label><br>
. T/ `, s; m7 n( A+ g+ k          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">! I5 [( a2 D! `
      </p>
0 v1 _2 }# _- p' u! r, @      <p>
) ?+ g- V8 R! u1 {7 \          <label for="notice-content">Notice Content</label><br>
2 j3 [# h; i8 K( w% L( D          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>7 V" c7 M) n  p1 x; M0 y3 v. L
      </p>
, G% a3 E! r$ Z/ F. P4 [      <?php3 m' }2 \; U, Y  r. P- F
   }2 k# i) }4 I! F
3 W# P1 w- `8 i+ x1 c
   add_action('save_post', 'save_site_wide_notice_meta_box');
& `# x7 D" c$ B. Y! ?) R! \' j& Y   function save_site_wide_notice_meta_box($post_id) {
+ E9 T6 [; F; n8 ~      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))8 {/ g8 A" t5 e; @1 O
         return;
" d! K- M+ B0 `' I      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
8 x6 `' i5 ^" }6 O2 N; ?8 b         return;
! l5 G. |9 v6 X- H. s! [; ]) b# d; b7 B
      if (isset($_POST['notice_title'])) {
4 N6 t+ d) s$ ]8 @          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
* m- H1 u4 g$ P7 H' V/ n  \) w  j      }
8 u; @" R' y' m; t( E) q8 ]      if (isset($_POST['notice_content'])) {
/ V: n9 g: f+ w  s          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
7 U8 h9 o9 Y' m; k! Y- x2 |      }  S+ G" A3 e# m9 j% j# M
   }6 H3 F& U" w1 H% ~6 y; K7 g; ^
   ```
) s; T! p9 g0 [0 s, j" Z& {& p  z) s3 t: a5 S3 H
   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
" ~+ I& j  a( p( {! f( g; h
; I# Y) s- c' @, C4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:8 R+ B( i# g0 J- [6 v
# w( N* h, w+ D" O- r$ }; A
   ```
  c& m0 K  u6 e3 l7 T& Z% o   $args = array(
6 u1 N( c5 j) w" R: L/ i: B. z% O      'post_type' => 'site-wide-notices',8 [9 F' D* x/ a! h( H4 V
      'posts_per_page' => 3,
8 H- \6 {# C0 k- T) N! O6 ~! c3 {0 W      'order' => 'DESC',3 j9 |. O! O8 n
      'orderby' => 'date'8 h& h7 q* O, m2 I5 v2 _. l
   );
( f; p) q. }, _6 ~; I, _; w   $query = new WP_Query($args);. y6 y# ~0 p4 p0 C1 \
   if ($query->have_posts()) :
4 P# ]# ^6 ?, G      while ($query->have_posts()) : $query->the_post(); ?>1 p% z2 Q, s3 |
          <div class="notice">! o* [" y( h+ L! a% c
              <h3><?php the_title(); ?></h3>7 s9 Q; l1 g6 W4 G
              <div class="notice-content"><?php the_content(); ?></div>( t. g6 X! U7 L9 ]% ?
          </div>" v6 q! T) O8 G+ m
      <?php endwhile;! D! S8 M- ?0 t6 A0 k
      wp_reset_postdata();, l- U! n' u3 U# b& L0 \
   endif;0 {0 I$ Z! p6 H( l" J
   ```
0 Q  i/ Z8 G. U  T( [' `, w$ W. w7 {( E2 `
   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-16 13:19 , Processed in 0.086910 second(s), 4 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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