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

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

[复制链接]

334

主题

519

回帖

3404

积分

管理员

积分
3404
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:
) P% Z+ C- ?% e( p3 h, ~
) H# a8 O0 K1 B# k1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。% G- f1 U8 s+ `% s9 E

! W! I& n  a- S, b$ ~) V2 x2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。
% I/ i# C* D5 K! x5 |) Z! x$ |9 ?8 n" L( o  Y' b) D
3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。
1 \# }7 x4 H% C& N2 E4 A1 b
9 V1 y# P- @1 V/ s7 k9 \4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。. J( C9 {, r3 s" j* y* t

6 L0 l0 w$ C: ?- K& S注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

334

主题

519

回帖

3404

积分

管理员

积分
3404
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?2 X; [% J( y4 i2 @0 W
% x" }( q8 D! z$ @+ O- H  V& i6 m
如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:
$ c, w  J, h! u& d2 y9 O8 T- w# l+ C0 e% U! |* I
1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:
) [* ]2 J! e# ]% u4 @* j1 N4 Q
3 o! q- {. Y8 u  r% ?" g; y# Y( N, o9 W   ```+ J8 \" T6 [2 r/ Y+ U& v. Z7 |/ t0 A& U0 e
   add_menu_page(
! t+ e; i0 E3 D       '全站公告',
, q- z  D; n) i3 s% f/ r2 Y       '全站公告',$ o! Y+ i" u! R# P# p( N
       'manage_options', //需要的用户权限
: Z9 S# _4 V" {) c# W4 m7 `% H! W& H       'site-wide-notice', //后台页面的 slug
" S9 A% x0 \5 f( E( D/ d4 B  w       'render_site_wide_notice_page', //用于渲染后台页面内容的函数
4 C3 S$ l! O8 Z' T( t5 R       'dashicons-megaphone', //用于显示在菜单中的图标' N0 q/ I, S, ?$ h- z  ~  Z+ @
       30 //菜单项的位置
% p8 \2 [. U/ E5 p) G! L   );
" C6 ?0 P0 Y" Q3 J; m   ```
5 M) u0 S. ?) i/ K# a, [  u
8 Z1 z4 m4 x8 l& q" }) ?   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。% I4 G1 [1 t$ K3 N' A9 i* P) c# j

( }6 H. g+ C7 Y2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:; A- t9 r% y/ Q$ |' l' c

9 h- Q; A3 G# j8 u$ J. {& T   ```
3 J$ Q* A$ S/ y  v! @   function render_site_wide_notice_page() {! |+ _! ^; S5 r/ a
      // 渲染后台页面内容
0 o) Y5 D) C+ @% g* n: J. B      echo '<div class="wrap">0 Q# P1 _2 _* r* i+ t3 t8 V
          <h2>全站公告</h2>
) o. d7 q5 h- \          <form method="post" action="">
& m/ p% c6 C' g; {, C( D7 h8 L7 K              <label for="title">公告标题:</label>
- O* ?3 L- e, s' F5 \              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>
  N) p1 s4 D' }1 C: u( l/ F              <label for="content">公告内容:</label>) T* U# _6 y1 E7 L
              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>
/ G% o: p. D) h              <input type="submit" name="submit" value="保存设置">
. i7 f$ x: w6 E% B) G9 K- [          </form>& ?' y' i" d* F, q1 r7 K
      </div>';
$ y2 C+ }& u* F  W+ u- g7 L( r0 B; H( r4 g
      // 处理表单数据5 M5 h, h1 q! S- o5 [/ H0 X, g' X2 Z
      if (isset($_POST['submit'])) {4 D. ^- z+ x+ s6 `+ [4 D/ h
          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));9 Y! L, k! M7 F* C9 ~
          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));" X8 V6 K7 d1 x6 M6 P
      }+ G+ @6 }/ e& V& ^$ n9 z
   }, N4 z5 t# W! W. V2 ]
   ```
4 y+ E$ N2 f6 `" {
( }$ G& {/ V) \4 @/ D   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。
8 g- k. C5 y  t8 h$ R4 p1 o  o
# \2 A; i$ s, U0 c% d- g0 e3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:. d2 ?0 ?% Y8 f: o# ]7 |

# a8 g; U' K0 S2 d   ```
2 x& |6 K" v% f+ P6 D4 ?   <?php/ `: _3 j& l% W2 V
   $site_wide_notice_title = get_option('site_wide_notice_title');
* ^* M7 Y/ V+ P/ y. M% g6 ^- M   $site_wide_notice_content = get_option('site_wide_notice_content');* v- V* I- j2 W: G. b
   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {
& z3 \2 b, }  t& b       echo '<div class="site-wide-notice">';! Q: J/ q: m% p7 F3 T
       if (!empty($site_wide_notice_title)) {0 K) y9 M3 e' o  c: \
           echo '<h3>'.$site_wide_notice_title.'</h3>';
8 \1 x; h4 g: t" i9 |       }1 ^! d/ K- q# r# u) \
       if (!empty($site_wide_notice_content)) {
% F- o* [9 _$ r4 N" G/ I" d& |           echo '<p>'.$site_wide_notice_content.'</p>';
) |' m9 {. _9 X+ Q; }2 B& |# e       }
; h. j9 ~6 W! h; `' B8 z8 f! ~$ s       echo '</div>';
5 J! v$ J: F2 T/ W" U1 f   }' I3 [2 Y) t8 M" I6 R- S5 H
   ?>
9 ]0 P3 [& `' F( Z* f# T( Y0 U   ```: r9 k9 |. Y3 ~* c2 ^$ l5 [- z
* B) ~, [/ i5 Z% Z: y- o" Y
   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。
3 L: G" ~0 Y+ a3 L* H; a1 l" f( g) s9 k' V/ n
请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

334

主题

519

回帖

3404

积分

管理员

积分
3404
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?5 h7 N5 r5 Z8 h
* S. U4 b5 x3 e  H+ U' p" C
您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:. g8 \7 y$ W' N3 i$ O  O) _
  _/ _) ]: _! s5 Y4 Q
1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:$ y3 H3 c* @* d4 @/ c8 ^: X2 D5 U# {

9 t& y, t. W+ j* e1 [0 L% g```
& e' C% l+ u" ^# q! Wfunction create_custom_post_type() {7 R( F' ^* n" E, S" P5 w: g  k& l
    $args = array($ g# V( k, [  ~. @3 l
        'labels' => array(
7 o$ A* L( z, Z1 o% q# X5 B# V1 y) S5 @; W            'name' => '公告',7 \0 g) K- r/ ^
            'singular_name' => '公告'
0 s, t. ?* Z4 R4 O        ),0 M  p* H" X' j5 X8 F0 }
        'public' => true,1 e: D& _) W: R, }" v! }
        'has_archive' => true,
: W* I# u$ E5 M; p. @% q        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
5 C( g% @6 P) W+ J6 H( c        'taxonomies' => array('category', 'post_tag'),
; w4 o0 [8 n* K# V        'menu_icon' => 'dashicons-megaphone',
. s8 E% ]6 c- k2 w        'menu_position' => 5,4 ^8 \, ~' w! c  `4 C7 r# A
        'rewrite' => array('slug' => 'site-wide-notices')2 B1 S6 B% Z& P+ x2 b
    );( Q1 M, [, V& G; U% l/ y" ~( r
    register_post_type('site-wide-notices', $args);
- M* e+ B& N) M3 |1 S6 X3 {}1 C1 Q3 s2 T! {6 K3 z( E' \
add_action('init', 'create_custom_post_type');1 }2 {8 m) S9 a
```
, @7 j# f* v* g4 T1 {& Q9 r8 `$ s; G) s4 ~& V; l
在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。
* T  N7 c4 Z4 Q& _1 S$ `
# F- J' H# n/ ~% n% D- C; }2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。
2 l! U/ `/ L8 }4 Q) r% d. e* l, P7 C9 N8 Z* O
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
8 p7 E! I7 }. h( O
4 P) T2 M( O0 m. U```
) L7 q% v  g; O3 p8 v% \$args = array(
& w8 `  e0 Q9 [+ w2 B- v    'posts_per_page' => 3, // 显示的公告数量
+ _. ~6 e8 l, d4 N" q    'post_type' => 'site-wide-notices', // 公告文章类型& V: H1 @0 g9 S
    'post_status' => 'publish', // 公告状态# D2 p1 M5 ?: t) |
    'order' => 'DESC' // 排序方式+ q  v6 G  o4 x; k6 z4 s
);
  T2 W: [  z  \% H. C7 a- L
$ ], {/ w1 C, X7 @$notices = new WP_Query($args);) K3 k& ?# V# F! _5 m- c$ p
if ($notices->have_posts()) :4 y7 @3 P0 X6 |. @* t7 `
    while ($notices->have_posts()) : $notices->the_post(); ?>
% V# y5 \- c$ N& z! m+ ?        <div class="notice">
5 w2 a- q4 Z  Y7 k/ k1 m4 O            <h3><?php the_title(); ?></h3>
  I1 Q  Z" _9 w; E            <div class="notice-content"><?php the_content(); ?></div>
( d* c6 y, C; P2 W9 M. S6 _% `5 i        </div>. Y) @: h) `2 w
    <?php endwhile;
" g/ R5 U( M& D7 S% s; l+ c7 @else :  s( G5 [2 p( A
    echo "暂无公告";
5 T$ N* ~3 G, ?" j/ ?7 g* Jendif;
: ~- u# R5 V; W, b: b* X6 Vwp_reset_query();- u; ]; {$ C2 b1 J( g5 I
```
0 L& q5 l5 e. N% z# L8 o3 Y0 e: }" E, @$ X! z; E- H/ D5 p
这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。
2 _( k( r; P+ g3 Y. J6 |/ W9 J  a7 R" I
以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

334

主题

519

回帖

3404

积分

管理员

积分
3404
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?' E& `! ?  c/ {
: l9 h: ?! {+ a( Y6 ]
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。1 T- ]" o) L: f
$ _1 C* |  t. ?2 S) _0 O  U! z7 U
以下是创建自定义插件的步骤:) H- n# a+ q- A! [# T. m
% ^- [- \9 S$ {& s8 \' {' o
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
8 G$ u/ o& X/ O+ F/ G. ~& I9 `- o
8 q) E; {- y4 |! s# V5 F7 E7 ?   ```
0 }( {8 ~% }1 U   <?php
: N% K. q  J3 E; x1 a   /*- [. w! A3 ]' m% f; W
   Plugin Name: Site Wide Notices Plugin. n' @0 B1 y" o3 V
   Description: Adds a new custom post type for site-wide notices.. Y0 r/ D7 _, ^
   Version: 1.0$ Z. ]4 ^& {/ X$ l
   Author: Your Name5 C" _" _. z3 F2 l7 q  z+ h
   Author URI: http://example.com
2 s2 V7 p4 g/ M5 z. c   */
2 `0 s  i3 B' x: x2 [
+ Y$ G- M+ w/ Z9 U3 ]; Q5 \   // Add plugin code here...2 A3 i; J9 B% `7 I
   ```
5 O1 N3 J+ e$ X7 c
; `: ^+ A/ @; A; y& y6 U   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。0 U1 ~. ]/ @! |' Q1 N( H) W( Q

* W) b- Y) w2 ~& v2 z" p# Y2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:& G* E) i/ H5 b

& P1 L. o$ ~3 ]! F7 c   ```
! D$ ]) T" a* o1 Y, I/ C) S& D4 `- A   add_action('init', 'create_custom_post_type');! v: ~) E% ]; d
   function create_custom_post_type() {. u7 f: }9 L. L
      $labels = array(& q) A# D2 }' t1 n) @' }0 F
          'name' => 'Site Wide Notices',
4 ]5 b& c) n: ~# v# S1 q* t6 M% M          'singular_name' => 'Site Wide Notice',
/ k5 [+ o/ M1 u4 m( X4 E          'add_new' => 'Add New',3 T# P8 V7 @) n$ B8 o- N4 |; O
          'add_new_item' => 'Add New Site Wide Notice',1 j# q, [' O5 \6 r5 O  b
          'edit_item' => 'Edit Site Wide Notice',8 J: @% B3 ^% R8 a  V& C8 n
          'new_item' => 'New Site Wide Notice',
8 }0 K; K0 v5 @; ?  J3 \          'view_item' => 'View Site Wide Notice',* e# k$ R& ~) |
          'search_items' => 'Search Site Wide Notices',  m/ R4 @6 g9 Q+ L
          'not_found' => 'No site-wide notices found'," h( v) q+ n2 O% O% {
          'not_found_in_trash' => 'No site-wide notices found in trash'# x3 {( J! V( e
      );7 F4 S! L* L: E& K( K- p1 k. V

8 \$ ^5 l6 P! I. E  R      $args = array(
9 L/ v. i6 b- G/ w/ G1 k8 c          'labels' => $labels,
3 h7 I& C3 ?( E# n          'public' => true,* y0 @$ _( I" ?
          'has_archive' => true,
4 I, p% F- a" X2 L; S          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
" O8 }/ Q* T/ R8 U! R& d          'taxonomies' => array('category', 'post_tag'),! Z0 P  N2 F- m, V1 X$ M
          'menu_icon' => 'dashicons-megaphone',
  z; I# t) o6 W& G4 {$ S( ?          'menu_position' => 5,4 k- Z/ ]* H6 u, z% b
          'rewrite' => array('slug' => 'site-wide-notices')
. @! {3 N3 Y. Q( G2 e      );
: N. g' R! O  H# _0 \  m
; t7 X" f. I# R, [$ j4 |2 p      register_post_type('site-wide-notices', $args);* E1 T8 N% ?! M* H0 q) M* b' L
   }0 Y1 _7 Y, v8 h0 n: z/ d0 m8 d: [
   ```/ |) A) R* B, y' q2 b
$ i7 x" Z' [6 E
   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
; h1 m# V: P$ V. m8 _1 y3 h
- R1 j7 ~, E4 d3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:6 f) |2 g2 ^5 ]( X! x/ U
; C# g' \0 ?, W( i3 T; R
   ```
( ^- D) C; o  _- b   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');' \) o6 I; Y$ c# H7 M
   function add_site_wide_notices_boxes() {
, k* T6 o9 ]; R+ |- M* {      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
) m3 c+ ^' G$ G   }
* c1 y7 p" z/ u+ C2 |5 h9 @2 W) X3 n" E, z2 q1 w# I
   function notice_details_meta_box($post) {
- p/ A5 ]6 f. t$ P4 R5 f3 o" b      wp_nonce_field(basename(__FILE__), 'notices_nonce');; {) i  O( w0 \6 ~7 x
      $notice_title = get_post_meta($post->ID, 'notice_title', true);# X' R0 L0 {# y/ |6 r% i
      $notice_content = get_post_meta($post->ID, 'notice_content', true);( Z4 `- c  o3 K% }, d4 T
      ?>6 o# n/ z" X! N3 \9 T
      <p>- R' a4 e8 T! N# `
          <label for="notice-title">Notice Title</label><br>
, ]0 ^1 A$ C5 W- m% i+ v4 {( U          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
5 A6 ^9 M1 l2 E0 I( L      </p>$ l) L3 o1 K! V! t+ q
      <p>9 U6 W% s6 W, a8 l1 t, {3 X. I
          <label for="notice-content">Notice Content</label><br>/ m( U  Q0 ], a8 s- @; U1 `
          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>8 O- n3 W; S( F9 v
      </p>
  n) R3 T5 D: D3 ]* Y0 Q1 b7 D      <?php
2 E# R- y- a9 D( |   }
1 `/ o6 }6 C% A0 {7 h" U6 J
+ m+ l! F) I" V+ E5 g  y   add_action('save_post', 'save_site_wide_notice_meta_box');
1 x% w6 s" \1 e  j6 Z/ A( c$ Y   function save_site_wide_notice_meta_box($post_id) {; O  |. ?- x( G& a! o3 D# v$ B
      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))0 b0 `4 `/ F. E/ v) f
         return;
1 R5 h# }8 Y% }6 S7 N      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)8 y# `; n! N+ \* [
         return;! v" O8 ^/ a/ L: n3 G, K
9 G4 C) i5 }$ g& i% I: a
      if (isset($_POST['notice_title'])) {
+ a& {  l+ x& E9 n  h. t) p          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
) u5 f# b4 N; P4 p  M      }
2 i4 Z+ H2 A9 W) N/ }7 {7 w      if (isset($_POST['notice_content'])) {: m! A8 a/ X5 X- L5 S" f5 W
          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));, Z$ Q) D8 U5 q" x; X
      }0 ?% o% J$ W) I3 O8 d+ n7 e" b
   }
/ c. C7 n5 _/ t& I+ @. v. P   ```
6 T) H/ @0 I5 f
3 [, i) x+ F4 w- Y   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
& Z5 t) _; k7 q  N; n+ W0 e! [1 ?* k( H! j* r6 _( C0 M
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
/ k$ v. _; m# h+ u
7 o* n6 n( `5 v" j: Z" E  s   ```2 {" o9 I- I9 ?
   $args = array(& {4 |' T4 i. M7 d% P7 X$ t
      'post_type' => 'site-wide-notices',
- c& Z2 C3 c3 G: O4 L, d0 G      'posts_per_page' => 3,  J# C: U7 t3 @! |4 C1 q
      'order' => 'DESC',
6 ?8 j* W' p8 H! P9 z% n, T0 _      'orderby' => 'date'/ J# ]! Y) n* T+ A+ Q& s
   );$ u9 q! H, I. y* L% `
   $query = new WP_Query($args);
! _7 ?- L, P* B, d6 V) c3 _9 h   if ($query->have_posts()) :
. j- A5 ~8 }& `1 @8 |6 D% C2 T      while ($query->have_posts()) : $query->the_post(); ?>
& G* R! q9 o4 |          <div class="notice">
( c% U% t- `: P; ^              <h3><?php the_title(); ?></h3># ~$ ~- M( W9 }0 g1 g
              <div class="notice-content"><?php the_content(); ?></div>, O$ n# K# j" l- k- M  a, _
          </div>- f; n1 d% L5 w" b4 R
      <?php endwhile;
8 n2 |7 w8 C* v+ C# F, s6 L6 X; C0 p      wp_reset_postdata();
0 t5 p" w1 v* I   endif;
+ f; B/ u" m& K   ```+ r: b4 d3 A; F6 S% @! \+ b6 T

; q% F# s# V. A   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-7 07:30 , Processed in 0.157107 second(s), 8 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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