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

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

[复制链接]

347

主题

565

回帖

3685

积分

管理员

积分
3685
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:3 E. k" a4 R( w" S# m. R
, F2 l2 |0 Z. T* P+ m
1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。' P( f: [) Y$ h  B& T# s" `" f
* O6 v1 v; ~2 i/ n
2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。- f1 l. u- K: @

# R+ u: j, }0 ?- C3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。
  G% F$ y0 Q5 g
  u( v+ |! T( [0 _& J: p$ F3 D4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。
; g7 v3 Q2 H% ?& ~6 j; Q' c- `1 v! W6 w
注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

347

主题

565

回帖

3685

积分

管理员

积分
3685
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?
, R, H4 U4 l  j9 Y! Q5 F/ `+ m: Z7 e4 Z8 @0 j
如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:
! u  X9 n( r6 L$ t5 \
& S" V) j- L; c& I1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:
7 h0 `0 B+ N7 L. p: D* N# M3 b) V
7 \+ g$ x3 V: Z0 t1 o- Z) ^  \   ```
. `; E3 u/ x8 [0 e( V: L. m   add_menu_page(
9 U2 Y* G5 `- ^4 y5 S8 Z       '全站公告',
% B) J8 |. |3 F9 x4 r       '全站公告',
% E# ~2 z  x7 Z, z, D2 P. P       'manage_options', //需要的用户权限) V8 ^& L$ g) w& x0 V8 d7 f
       'site-wide-notice', //后台页面的 slug6 r$ |" j4 u+ o2 o/ R, z5 u: B- y
       'render_site_wide_notice_page', //用于渲染后台页面内容的函数/ z6 U3 T( }, B' T1 x
       'dashicons-megaphone', //用于显示在菜单中的图标. g- y! J* O2 i7 x6 M' r2 P) }9 S
       30 //菜单项的位置
# c5 E8 E" C6 _1 r2 j   );6 O; ~& l; D, U
   ```# [  ?# {3 N6 n$ ~' o4 L

! n8 O- c$ v! L1 Z   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。
0 h4 g* B; u1 ~. C2 o2 L7 ?  ]. J; K3 a1 c. c4 C
2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:
5 h2 H5 a6 Q% I4 R8 W7 t, A+ d; p& B2 I9 Q# Z3 a, e; ^, h
   ```7 m& b3 a4 f! v- t
   function render_site_wide_notice_page() {
8 a3 @, `* `2 O8 X      // 渲染后台页面内容3 d) \+ h2 i3 K" v& n' ?# ^
      echo '<div class="wrap">
# t2 G2 W! H) H, y1 r- ?+ [" s          <h2>全站公告</h2>8 ?1 A0 V. e5 G  O! c- y
          <form method="post" action="">* U( v6 W' s/ F* `! B
              <label for="title">公告标题:</label>1 q4 H& N( b" G7 o8 V
              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>
0 K- i% G; ]5 L5 \  p              <label for="content">公告内容:</label>' R, r9 D* q* y8 r! }
              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>
+ T% Q, U, i, D              <input type="submit" name="submit" value="保存设置">% v& {+ d, {1 h' W
          </form>& ~; D: C, z5 h6 D7 D; Z( w
      </div>';
& \* v$ X/ a2 X% Y5 Q' Z
$ Y0 g: X1 X* ~6 O( c% V      // 处理表单数据
' A* K1 A* Y$ ]/ O: @3 p      if (isset($_POST['submit'])) {
6 L8 T  @1 D, p, o& I          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));- J. e9 F- w% L9 W" Q0 T; [( M
          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));
+ z& Q8 @! R0 O; e/ t' e; \      }
+ ?6 }( i. h7 @( n4 R! @$ m$ S1 a   }
1 }1 n7 e: v0 N9 S$ D) m   ```" K: g0 u5 G; Y
$ Q& f6 V8 A2 |! L
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。
3 |/ N$ `3 T$ S1 i' J9 b; G$ P
2 q+ f9 h, A2 h" M8 I3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:
. y9 B3 k' C, ?4 I. c7 u# G* I- Z: O+ ]5 L
   ```& i( C3 y5 K  o2 U0 E
   <?php% D, V- p' \) Y* j
   $site_wide_notice_title = get_option('site_wide_notice_title');2 p4 O5 c$ P; E) T) D1 t/ X& o) N
   $site_wide_notice_content = get_option('site_wide_notice_content');
2 y" B: M7 @+ g' O   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {
; H( d) r4 r2 a       echo '<div class="site-wide-notice">';6 U. B. ?8 }& w/ Y* V' k4 t% A
       if (!empty($site_wide_notice_title)) {* g6 [) M- V- `/ E
           echo '<h3>'.$site_wide_notice_title.'</h3>';- w1 W( W+ @% K
       }
3 \2 }! o* U. _* m. i, u2 u       if (!empty($site_wide_notice_content)) {
* A5 |) u& M5 v$ r9 y) ?1 v           echo '<p>'.$site_wide_notice_content.'</p>';
7 V2 L' {" T% B5 s1 o       }* ]& E# Z, I6 [! M/ a$ H
       echo '</div>';# z3 d! e3 {7 o/ _" i
   }
2 J' ?# ^9 x" n   ?>
+ I4 @5 ?; [, H0 O7 w( R   ```
4 q. d- H0 T# |2 Q, N, S! ]
  O/ f! ~9 c  m: G7 |, F! S   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。
9 f$ C4 U% ~( M1 K0 H
; P) @" N0 B0 B) L* H% v请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

347

主题

565

回帖

3685

积分

管理员

积分
3685
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?0 Z# e% l) t& y. |4 s, F( r+ d# d
& [9 A& D$ K5 ?5 z
您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:$ o$ y1 R( W) C
' Y# w6 R( A- u9 Z6 f& f
1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:1 l" K! g; Y# G( Z( v* P

/ q" ]6 T" T0 b+ e```
* w1 e' g: @; k9 Mfunction create_custom_post_type() {
- v) g+ H# v3 \" Q  Y$ ]* R! S* V: F    $args = array(
! C3 v% @9 [* E) w0 R7 O. k* e        'labels' => array(4 G3 T* i6 z- g# d$ J* L% B
            'name' => '公告',* Y" }9 P: N. C; S" b0 |+ d
            'singular_name' => '公告'
) F( d4 y5 R. R  R! `, M4 V; y' b0 k        ),6 F$ G. f& ]9 K) g8 \4 e
        'public' => true,- u- n: w& e  ~3 Y9 M1 B
        'has_archive' => true,4 R* R; z9 A9 e9 F" Z- X( F, g
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),$ _' Y, `* y4 Y& q1 X) \. |0 J& A5 W
        'taxonomies' => array('category', 'post_tag'),9 {- J5 p5 |; S# C, F
        'menu_icon' => 'dashicons-megaphone',
2 e5 q4 p: b7 |        'menu_position' => 5,) o7 l4 Z( g0 C: @9 a; S1 {
        'rewrite' => array('slug' => 'site-wide-notices')6 Y+ |+ j$ |, R
    );7 a& A# j4 s1 `  {
    register_post_type('site-wide-notices', $args);# i* u2 ~) {# ~$ F
}
- T: [# h! O( Vadd_action('init', 'create_custom_post_type');
% O4 i: g5 W3 h  H; g```% s. o* j, z& B# [. B; K
, W. [  `% A7 b: W6 ?+ P+ K
在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。
: l6 I! a$ M1 c1 ]/ \* X8 r1 X2 Q7 V. r+ u+ J; Q
2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。
4 I6 t# {8 G# c! O3 `" h
) J8 i! J9 E" }- N3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:$ h0 h9 \" i5 N% m1 S

* j% K% @2 d. c```
5 t6 @7 Y6 B( v" K3 c$args = array($ ^* O/ v! m( F+ ^
    'posts_per_page' => 3, // 显示的公告数量
- c" D: }( f7 ?# w& R    'post_type' => 'site-wide-notices', // 公告文章类型7 s  Z( _& ~" M0 s" j, @8 C( Q- N
    'post_status' => 'publish', // 公告状态4 u0 Y0 j% x2 v: ~! b1 o
    'order' => 'DESC' // 排序方式7 A  N' a- I! b: C
);
& B8 z" ]8 o+ V$ P( `8 x. q1 v
  }4 B4 {1 L' X# ^$ e, ~! X& L2 c, h: }$notices = new WP_Query($args);+ l8 T8 q6 ]8 {! z
if ($notices->have_posts()) :" s: Y' z7 j! V* L( W, y
    while ($notices->have_posts()) : $notices->the_post(); ?>. q3 S4 U( c. m: I
        <div class="notice">5 H# ~0 `& h8 Y# E; U2 D6 \
            <h3><?php the_title(); ?></h3>
  ^) \* I9 ~; x3 Y& h$ g) Y            <div class="notice-content"><?php the_content(); ?></div>
( F( q. H7 {& w5 {6 P        </div>
# |) S) s9 p! i    <?php endwhile;
: e7 C+ N2 V6 o% Aelse :7 {2 }2 r- K& D- Y4 \4 X
    echo "暂无公告";0 O3 w7 i+ I  C: ]; j2 a
endif;) u; b2 N6 z# b% R" a
wp_reset_query();
3 |( ?$ m+ f9 M" l```
) |$ L3 ]8 N: k3 `. s8 t* z
2 _* }" I1 s$ d4 ]4 m8 p" v这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。
7 Z& q1 o3 f5 q# I$ N  j( W
3 b( y0 }' M) i# o0 d% w% U以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

347

主题

565

回帖

3685

积分

管理员

积分
3685
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?7 M8 E% i$ y7 |. B1 v& P' {
% ?- l1 d% q% _
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
) a' r$ G' ]- _
* f" M0 N9 p% Q- p5 f; s以下是创建自定义插件的步骤:; C! K( Q% D- g$ \* d
. p2 ~7 |# Q; l  R& [
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
1 `! a7 }* [" k+ s' o: L
" h$ F9 |8 h6 K- v* {3 b. l8 o/ f   ```
( J" W$ i9 L( ^. e' ?, G3 D7 H* J   <?php" t, p7 j7 b3 q
   /*# T9 y8 Y" t! C  Q) z$ j. F7 m
   Plugin Name: Site Wide Notices Plugin# c/ k! t( G. G/ J% i& O' }3 w( L
   Description: Adds a new custom post type for site-wide notices.
0 @9 F; {) |2 K   Version: 1.09 E1 W8 u) p. |- p
   Author: Your Name
' b  Z5 M* J* ~2 m' H, T7 H( z( p   Author URI: http://example.com
/ l" j6 t. j  v   */
5 {8 M  ~' S& f7 Y. d/ C2 x) S
9 x) J2 D  o/ Z$ Z/ [   // Add plugin code here...
5 g8 a! Y- M$ `( B% R   ```
! q3 z4 P. R8 J: M  F, H  G8 `' e4 i! F/ l
   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。) R8 F+ ?0 i5 H2 Q6 X% V& n6 t

7 j3 J( [7 K+ R) ?) m6 I& a" Y2 O. i2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:- L3 n( T. e* k7 `0 \1 \

# o' ]/ `6 X$ N) _   ```
7 `% @. s* H# w- x0 S% H   add_action('init', 'create_custom_post_type');
: k) c% E' t5 l  E6 _8 S/ H   function create_custom_post_type() {; i. ^/ W6 y2 k9 }, \1 c
      $labels = array(. o9 w( p+ M0 M. t7 {' V4 r/ I
          'name' => 'Site Wide Notices',
* \8 p. P/ V- O  `$ Q( f! c          'singular_name' => 'Site Wide Notice',8 l8 z* p8 M5 R# a" z
          'add_new' => 'Add New',
5 N5 D3 K/ w. Q8 O8 K          'add_new_item' => 'Add New Site Wide Notice',5 Q! S( K* F8 Y  l. x
          'edit_item' => 'Edit Site Wide Notice',
& d3 p. E! p' f( g8 f% B% w          'new_item' => 'New Site Wide Notice',
, d# J1 |0 O  n" h2 R          'view_item' => 'View Site Wide Notice',1 f1 x( [, V' u
          'search_items' => 'Search Site Wide Notices',
) j  @6 D" R' z8 I          'not_found' => 'No site-wide notices found',
8 M! X/ G9 c; B/ ^$ W0 y; q          'not_found_in_trash' => 'No site-wide notices found in trash'* x; g9 \2 F3 R& U$ j5 o- e) L
      );, J+ Q  g% n! O3 A4 C) U% F
& q* F/ Y3 x: P3 y; H7 f5 B: h
      $args = array(! G. s; ?1 L  ]9 e: x: e/ _" P% h
          'labels' => $labels,+ f4 X5 k( h0 u; I
          'public' => true,
6 i4 i$ c" M1 O3 X4 G  c; a+ M          'has_archive' => true,; z8 t; h, I/ z; m- f& Y/ ?; N
          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),8 E9 H5 c9 v0 u5 D" A+ m0 a9 M- L4 q
          'taxonomies' => array('category', 'post_tag'),
8 {+ x' Y9 \4 J2 I          'menu_icon' => 'dashicons-megaphone',4 _* ~1 k7 ?% F- X: I+ W
          'menu_position' => 5,. ^, R4 N: W, [5 @& I( M6 o. k
          'rewrite' => array('slug' => 'site-wide-notices')
# s1 e" l: |- p# R% n8 ]      );
& }( B& q6 r: h# f0 C: \
9 V8 l" j7 y' K& R) a% n8 w      register_post_type('site-wide-notices', $args);( f% N( X& p  V& _# j" a
   }
: ?5 X! x9 h8 N  ?$ Z* J: L7 W   ```
0 M2 O$ [7 y  X
: X; V, Y' V$ k( V   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。# t. N7 `+ J3 {( Y" I0 H
% j/ j5 D4 L0 ]: G0 G) p
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:- r/ ^" w& q3 |; i- G
% C/ y" t+ G$ t; N
   ```/ P3 z9 r' y# m" G+ v$ E4 R7 [
   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');0 P& d  |0 ]) W) @0 X2 F
   function add_site_wide_notices_boxes() {
3 K( g1 K0 S1 b/ y! q+ Z, r2 i      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
5 x* F9 m. g6 Y! ]1 X/ U   }
' C9 t4 h2 {4 l4 W' T
% q: z6 A* m8 G" p1 C   function notice_details_meta_box($post) {
; u7 D5 V# N5 N) k  K% p$ v      wp_nonce_field(basename(__FILE__), 'notices_nonce');( z( b" @) g7 Q3 G3 E
      $notice_title = get_post_meta($post->ID, 'notice_title', true);
8 n( X8 K- O2 ~. o5 z) Z      $notice_content = get_post_meta($post->ID, 'notice_content', true);
5 K6 b! j  V- X# x; J" H5 g      ?>
, \5 n) k8 Z) m" u! h* s      <p>5 _3 |- s0 L! |. p8 w; q9 \7 O6 O
          <label for="notice-title">Notice Title</label><br>) H% W, _  R8 z0 g( U& n# J* ?
          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">$ n& J" Z, D& S
      </p>& X$ h8 K1 _- }) r5 S- ^
      <p>
6 q$ k3 h- G/ D6 o+ Y, C9 h          <label for="notice-content">Notice Content</label><br>4 C7 z4 _5 n5 A/ D7 r1 e( u
          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?># A. N/ v# l' F/ S) ]4 D8 E% ?4 H
      </p>
2 @  |* S) x  U: x' N  v      <?php
  Q6 p. u% [! k- O6 M/ Q0 s# w+ A   }
- d$ k$ Z$ {& G7 E6 o8 ?7 S
4 z7 r. V3 K6 r  x4 q) C; o% t+ ~   add_action('save_post', 'save_site_wide_notice_meta_box');
, f9 u' Q+ Y, [! J   function save_site_wide_notice_meta_box($post_id) {; n% X' ]1 b9 V6 @9 \
      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
  P4 q1 |3 X, |1 X& h: R6 f( h0 w         return;
( d- c+ i# M/ h      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)% Y' C; `) ]# Y0 ]5 o# |1 F
         return;( j; {  o7 t. z# ?- }: f

1 @# m, w$ I( f! a1 _      if (isset($_POST['notice_title'])) {
0 a+ P3 s: Z( {; z6 {          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));3 k4 ?9 n1 `( e( z& i# t. k/ b
      }
3 |8 b6 g7 m- H; f# _      if (isset($_POST['notice_content'])) {+ |4 n. u/ U" R/ `/ y
          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
, ^5 \3 b& Q- G0 k9 |( X      }. R. I* w& [6 {) k' S
   }
1 I% S  ^8 ^2 b! A6 u5 Q   ```
# c" @3 K! H) z5 n, z: ^( f' @  X( K4 f+ I: S+ _& {- b
   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
: p, L$ d; {) ?# w' a/ P/ Q/ j# v
# V# y8 ^/ s" J8 h& K% n4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
1 i3 B# ?. F- H, [8 V& t9 S2 b9 c/ `( a5 Q* K
   ```
3 R) ~4 W3 |( W   $args = array(
) Y$ R7 k7 f% i      'post_type' => 'site-wide-notices',
# r) b7 n( y6 @( H* t9 N/ T      'posts_per_page' => 3,
9 L+ e5 L6 J1 L0 }) R; M      'order' => 'DESC',
' Y" e; I3 B7 x" k6 x. _7 x& a5 D      'orderby' => 'date'
* `* T7 {9 I% B: O, h6 C   );8 ?9 h5 x8 r+ ?$ p
   $query = new WP_Query($args);
  F7 @( n: s! J   if ($query->have_posts()) :
# M/ G  D# g. x" V# p2 Q      while ($query->have_posts()) : $query->the_post(); ?>
; B& D# m# \" s+ r% ^          <div class="notice">
# S! y4 ?2 `* o5 h              <h3><?php the_title(); ?></h3># G" l# m1 a' K6 N5 V) a+ }: b* G
              <div class="notice-content"><?php the_content(); ?></div># G4 k4 Z, x& R0 Z- e0 J8 e
          </div>1 m2 I8 }' M" k" i& m/ B( [# ^% e
      <?php endwhile;
% g2 N# }  J3 q) m7 u      wp_reset_postdata();9 Y, {5 B& n5 `% Q9 z: t
   endif;
" v7 c7 i8 W& @' r2 k   ```
5 [/ x+ `% W/ e7 r' N' E5 N1 g6 R
1 J4 |" U, p6 T& |% b! |: C4 w   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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