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

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

[复制链接]

332

主题

513

回帖

3376

积分

管理员

积分
3376
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:  [* `$ Q0 Z0 L9 X6 }) o3 v

! t% R7 `0 N4 Y, J% d/ U1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。9 @' {+ U. q8 `  O% ^1 K

. ?0 s* t- P# B7 H$ Q! f0 i% ~: A2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。# L. d3 v6 @/ b8 s- n

$ m* L) f% y8 C# R5 A3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。
: M/ k. l4 I, G6 @/ Q/ u+ ]$ _0 N" t$ W3 i, Z
4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。7 s0 Y5 G* S" Q0 o8 ~
, ~% ?$ c* i( [( q+ t
注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

332

主题

513

回帖

3376

积分

管理员

积分
3376
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?8 V, |% u' L  U+ S" A* |
8 Z  U, e" C* y& R
如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:
( ^) e1 E) f' {, _- L% n* Y. K+ d7 w. _: T
1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:* {# ~4 O8 \+ w# n. Z4 g

5 i3 Y2 m" b3 _1 a! R   ```
4 b# V. E$ G- i4 N. Z   add_menu_page(
; F. j  }+ L: `6 m  a       '全站公告',
! c5 a" w* @% k* [! r       '全站公告',
3 F# P- _! q0 f& O, Z9 Q2 Q       'manage_options', //需要的用户权限
7 P4 F6 V: r' s       'site-wide-notice', //后台页面的 slug
: M& r4 `/ V% y       'render_site_wide_notice_page', //用于渲染后台页面内容的函数; H3 a2 Q  d9 f/ i; g
       'dashicons-megaphone', //用于显示在菜单中的图标# J% T( x% k4 I3 y& j* h4 n$ K
       30 //菜单项的位置
" B! N- s3 x% k4 s; C( m# A   );
) s  y% v: {/ ]   ```  X$ M% [( F$ I  \( k
5 i& f" I; i* u" r0 y- b$ |  [: y
   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。8 `& Q3 B* L1 W* }! |/ J: ?5 `
* n; ]1 \$ B  X! T7 T0 F- N
2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:
, I* o/ X0 W3 f7 H2 ]5 E, C. l/ y3 B7 j  l/ H; D0 s% F
   ```/ ~( E- v, F& p2 ?3 T8 V
   function render_site_wide_notice_page() {
4 O0 q  r" ~5 R: M) a5 n      // 渲染后台页面内容
$ P8 r7 O6 {# }; e% i7 q' t      echo '<div class="wrap">* \6 Z  I+ x/ C- v: L$ Z
          <h2>全站公告</h2>
, p% c. V6 O( W7 u% W; v          <form method="post" action="">& x( L; z* E! `$ R
              <label for="title">公告标题:</label>
- v$ `. E+ b8 L5 f6 C1 A              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>
/ h; Y3 t1 U  J4 w, ?& m( O$ T              <label for="content">公告内容:</label>8 ], L$ S2 x* [& V' S" }+ k; R; s- R
              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>0 i" I! I+ n. Z7 n
              <input type="submit" name="submit" value="保存设置">0 }; f) L9 H: J: R
          </form>0 ~4 E9 \7 V( _4 M6 j! l
      </div>';( m( n- s* J& a/ f/ o5 f) \
* M1 @( C0 B' f# i- E
      // 处理表单数据7 }0 c# g4 T. e- }
      if (isset($_POST['submit'])) {
, h7 T+ e9 |# X( I, s) N1 q          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));
& V% S3 u/ ?4 Y0 Y          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));* Q+ z8 L* H* }+ |& v- P/ k6 {
      }% O* p8 ~) |* P  g/ _
   }% j+ E1 {! @. _% t. K9 P6 a5 C1 O
   ```4 e8 B7 S  S( i& C, z  \

6 d4 {% K+ V- ~( j   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。
+ _3 V) w3 s' v7 o# q: m% w; @0 t5 i% J# i! b: l9 o4 b. a. f
3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:
: q1 a. q. a: J% u  T) K3 B1 Q! ?5 t; W3 J& M5 [
   ```
& h$ E7 u; e- M) m" X   <?php. o% ~3 p" s* U; `1 A* Z2 W
   $site_wide_notice_title = get_option('site_wide_notice_title');
# D. s" x: ~5 W' c   $site_wide_notice_content = get_option('site_wide_notice_content');
, X+ |" z4 L/ X2 T4 B; _   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {  E) f6 h  `2 c
       echo '<div class="site-wide-notice">';- u/ V; ~! r" b1 Y8 Z5 e2 A0 a5 E% I
       if (!empty($site_wide_notice_title)) {
& k8 t/ s: @% {+ b7 ?" E6 V           echo '<h3>'.$site_wide_notice_title.'</h3>';
" T( ~# c6 H/ B0 J; t$ [1 O       }( e  y& L/ J6 w) @9 f: k' g8 q: h
       if (!empty($site_wide_notice_content)) {
5 A' P( _' w1 i4 j: G           echo '<p>'.$site_wide_notice_content.'</p>';& A& X3 w: L7 s# a
       }
7 R# _' @5 D% Q5 C2 C! p* Q6 y       echo '</div>';' |# {5 j/ ?! o0 q, g
   }1 S: n' r) ~  M9 S5 l3 x' ]
   ?>
, d8 o+ Y0 g( h/ q; V   ```
! M+ X, H' d. ?+ F' \$ G3 l% O1 s9 K0 N0 L3 o/ l, |9 M  |
   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。
, p9 j6 F( F) A' l6 s% @6 j( w8 Y0 Y- d5 T# P$ [5 |5 b
请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

332

主题

513

回帖

3376

积分

管理员

积分
3376
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?
& c* m% i0 ?0 V
- }6 ]3 D) ^% A* c; w1 m% B您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:
* R3 h) y( `) J6 u# D$ x5 [. W
( P8 z2 ~; p) ]! i8 Y% D1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:2 N- ]/ B  N5 ]1 v

) R2 y; X8 V$ `, H; B% }7 U```3 X+ J; L; i3 O  i* }/ f& r
function create_custom_post_type() {
" y9 x) j) ~8 `' X- j: H9 [/ d    $args = array(
+ w- Y, ?! F# z        'labels' => array(
1 W) w/ |9 l% K6 L. V            'name' => '公告',2 o+ R. ]5 ?5 P: o6 d# A9 F. Z/ Y
            'singular_name' => '公告'
: R, F. G, `( w  v- U9 C. B        ),
. j0 w& K+ K5 ?  f; o        'public' => true,
, o) t, L) i$ y- U        'has_archive' => true,
+ F! F5 ^0 a; G/ z) v        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
) T6 f. `: B$ L9 @4 g+ m5 Q6 @        'taxonomies' => array('category', 'post_tag'),
0 H$ I* s; v8 R* b* X: I  h% B        'menu_icon' => 'dashicons-megaphone',
: z, i4 v5 ?# b7 [8 A9 F& Q3 n        'menu_position' => 5,
/ ?) i% q+ Q8 r1 W1 P9 Z" @        'rewrite' => array('slug' => 'site-wide-notices')% l( X, K- C0 `
    );
+ [+ a$ p7 ]' _) z2 \7 a7 M: _    register_post_type('site-wide-notices', $args);7 `& _. d3 A: |0 D
}' x  B# k9 @4 @# ^5 {
add_action('init', 'create_custom_post_type');. i% K: m3 g1 I% T7 W: H2 z
```7 |0 n2 G- m0 V) r8 H
/ @* E) J- M, E
在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。8 }- Y: q0 I$ O+ [+ b' g: m8 K( \
$ `$ p# L: F* v- {3 @+ y; c- Y' w
2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。
8 u) s: k/ G. Y- [; ]" h" D  x/ \" U* b; R( T" [
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:5 v; R' f! Y% w
7 j3 Q, U6 {' o7 o4 Z- [: [0 `- J
```9 m# |1 y7 w  Y4 g  W  J
$args = array(
* |# U. _/ |' O7 q. U    'posts_per_page' => 3, // 显示的公告数量
2 S" N, m: ^6 a% ?9 ?    'post_type' => 'site-wide-notices', // 公告文章类型
# p1 D# z$ V3 b4 t! w* L5 s    'post_status' => 'publish', // 公告状态
% Y2 C: t# m8 R  B    'order' => 'DESC' // 排序方式
0 P3 R) W9 j! w2 R* _+ ~);+ f, D7 ~  g& n- e3 Z

  x, J- z* v7 _$ G$notices = new WP_Query($args);8 t, |8 B5 U7 Z- N( j
if ($notices->have_posts()) :
+ r" l! v0 D# Q' c2 g4 M    while ($notices->have_posts()) : $notices->the_post(); ?>% o) a# c2 S3 J; u" }8 L1 U1 F
        <div class="notice">4 B. a3 K) l7 d! {8 h) E
            <h3><?php the_title(); ?></h3>
' L8 E4 o: k; c8 B3 |6 \+ H& [            <div class="notice-content"><?php the_content(); ?></div>
- m( L- Z  X- G3 a8 m% Q0 V        </div>
' D. c3 G8 d( `    <?php endwhile;
0 t% z% V2 I# P2 z) X% Relse :0 O7 i7 M/ P6 S) p. p
    echo "暂无公告";( l+ h9 I. p; A: @- u% m% ]+ j
endif;+ G7 R0 o+ B) O$ ]7 n7 K
wp_reset_query();
( q/ E. _2 T. G2 H5 c' u$ T```
  b5 B/ g2 H: ?) O+ _& ~& Q9 |" K/ w0 A% B3 k( h3 _
这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。
8 E1 m. S: V$ U" F5 P& t- h
7 X4 V$ ?$ l& R0 w以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

332

主题

513

回帖

3376

积分

管理员

积分
3376
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?$ c" K+ q4 y: x2 \. }

# }" F2 _: _% @% S如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。6 ]3 a# Y; w5 ]

$ _9 r0 q7 u- E3 m8 Z8 \! T- d以下是创建自定义插件的步骤:5 }1 ?* k8 b1 M; @

& C; a) i7 |2 Z1 L% s, `" }* }1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:" [6 ]- e+ E  x( e' Z0 V% u

3 R/ w0 Q: J; [8 k5 y   ```2 q; @6 V. x1 m
   <?php# V5 s' _0 I8 {
   /*: O1 H. ?% ]% t6 e2 W/ {, b/ W* f
   Plugin Name: Site Wide Notices Plugin
' r# B7 l6 I1 z* |# R4 m   Description: Adds a new custom post type for site-wide notices.6 i/ j; a: P! @! E/ y2 D
   Version: 1.0" C" P, N3 M$ t4 B
   Author: Your Name
0 ]! G; h, [) k( a% D# m   Author URI: http://example.com7 n8 b9 o, T* d+ B# E
   */
& G# \3 Q9 c& ]/ }' B( }4 e7 A$ ?1 f3 A/ f9 Q- T' y/ R
   // Add plugin code here...
5 p2 a: o  K, a9 V3 l) H   ```0 B+ l7 b& f# W9 L" ]

* M! ~9 x& ]* w   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。0 ^7 E# C  u# S4 a( J1 H6 w- F

  G' n5 v! t, i9 |0 _* a/ e2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:. K0 Q6 N0 n2 u+ U4 x' o
5 ?* W+ T' V( D+ I# T, J/ D
   ```3 W/ a" g  p7 q! \3 h
   add_action('init', 'create_custom_post_type');
! D$ z6 e% h( N; b$ p" e! V   function create_custom_post_type() {
) \! _! w& g1 m9 l1 _      $labels = array(8 s; Y( w- {+ x! v" Y. i! z0 f# ?
          'name' => 'Site Wide Notices',
5 u4 c7 O& X! ~" C1 h- x3 P( e          'singular_name' => 'Site Wide Notice',, ^1 c# @) _( z# {8 A7 e8 I
          'add_new' => 'Add New',
0 S, V  ^+ `7 C; ~' q          'add_new_item' => 'Add New Site Wide Notice',4 ]" h, ~# W/ X- s5 d  w2 e: P
          'edit_item' => 'Edit Site Wide Notice',
9 M) B$ Y8 h$ p4 K5 o          'new_item' => 'New Site Wide Notice',: }; Q" J9 D* N6 ~* _
          'view_item' => 'View Site Wide Notice',
" f; G: o' p  p          'search_items' => 'Search Site Wide Notices',# X* D8 Q' A- y# r( m3 A7 L6 s
          'not_found' => 'No site-wide notices found',; {- \5 W8 j# ?/ L# x8 b
          'not_found_in_trash' => 'No site-wide notices found in trash'7 O( s, T2 u( A# e8 {: I& L- z! `. o) V
      );
% Q4 Y4 ]( L, _; J6 ^" |" c$ H' A& Y
      $args = array(; b4 ?8 _1 ?" E7 H8 M1 g
          'labels' => $labels,+ o0 L( r0 Z4 s6 G4 [
          'public' => true,( d3 X3 U  B3 U* x
          'has_archive' => true,
+ o6 O) B$ X) X2 j' x4 k; o* ?          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
) `; h5 V( j3 o. g, g5 r: [          'taxonomies' => array('category', 'post_tag'),7 n4 W, H& V: V3 l8 K0 w# W
          'menu_icon' => 'dashicons-megaphone',
+ Z! j9 m/ e/ B4 J          'menu_position' => 5,
1 s9 Y8 W( t- l5 h0 P8 I" V          'rewrite' => array('slug' => 'site-wide-notices')
% E3 r! j& E. B8 n$ ?      );' R1 }+ E; c* E4 _7 v: y3 Z+ o( u

7 l! U( U+ l# V; H& _      register_post_type('site-wide-notices', $args);
; G+ j6 K# @# V, u   }- Y7 I/ R. o& ~2 |" J0 j
   ```
. E2 h, e" I" Z0 p! G5 X" P  y% h* l" F! u" z
   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
+ W% U5 _! ~, Y. P* j& c
/ y1 I1 |  B; f- r7 e$ r3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
% G1 f& _$ @- @% h8 Y
  O' x- Y2 D- }- {  c   ```3 w& t; f4 P/ P
   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');  e! F* z5 ^! V6 p. f: ^7 D
   function add_site_wide_notices_boxes() {9 R( ^2 i' k8 S& P
      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
0 J( ?" r; g+ [6 a% N4 I# Z' f+ z  _   }, F' ~4 e5 b3 R5 L3 n! ~0 O

% v' n- p6 ~+ Y) _4 R   function notice_details_meta_box($post) {
9 `; C, n, R' H      wp_nonce_field(basename(__FILE__), 'notices_nonce');
6 b2 a" Y8 P% Z4 E4 @5 b      $notice_title = get_post_meta($post->ID, 'notice_title', true);
$ l( u" v& s! a- h2 D      $notice_content = get_post_meta($post->ID, 'notice_content', true);  S+ c0 n- v8 O; e. l" {) Y6 ^
      ?>* g% s! J: @. j6 t
      <p>
! t$ K2 i: W  k- x+ S$ G          <label for="notice-title">Notice Title</label><br>
# S. b) m  c0 p          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">8 p# T7 X1 ?1 D; F
      </p>" f  d% t; R2 }1 g
      <p># J  ?1 P( W7 M7 E  v6 c7 N8 s( }# B
          <label for="notice-content">Notice Content</label><br>
5 Y) i" m' |- y/ H! q          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
0 ]) `, c( t" P7 _. p# ?$ ~      </p>/ n3 y  C0 x1 ]& l, Q
      <?php9 M+ G6 i$ {# M8 E, c: X. X
   }. \" G+ h% m1 l2 E

. S( {+ @3 C; v6 z, G( C) X+ Z( Y, ?) i   add_action('save_post', 'save_site_wide_notice_meta_box');; D7 o; M9 Z3 v- Y) \
   function save_site_wide_notice_meta_box($post_id) {: j/ o* h: \8 W3 m; ?6 R9 N% N9 c! ~
      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
+ z% C8 t% ?9 S$ U. S6 M! T% O1 l         return;
) Q- Q, r$ R" Q* ^/ w' D* K      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)0 j/ M9 X0 A: t/ u- u2 _2 J
         return;
# B. X. C& F4 C. L+ e# W
5 v2 z+ X: D9 m      if (isset($_POST['notice_title'])) {
  ^6 w- r* g- W& W* E$ Q          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
+ N1 d, v7 _+ V8 c      }1 g: N! }2 b0 Q9 `4 i
      if (isset($_POST['notice_content'])) {
& Z( ^6 t' M7 h# s' K7 p          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
3 I3 e0 j1 E0 G: P" {5 x) v; P      }
6 q: M/ b; q, J, X5 R/ u# |   }7 o6 z- P( y8 F  x4 c; {4 N2 u; z
   ```1 p* @: a! I. q" |* S% d7 ?: k

: T+ E$ h3 c* L8 G8 {3 [   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
& i/ y* h8 E7 l/ p' ?2 b
. P0 ]/ p- y( P3 I2 o4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:5 A. B8 M6 q8 D+ J/ \, @
7 U7 r7 \7 q% S- X& Q& H8 g# e! D
   ```
. ~1 E: w, p$ S# w   $args = array(2 S2 W' i7 S8 J  n; l+ U8 f
      'post_type' => 'site-wide-notices',
, t- w, H; _9 {; {& `      'posts_per_page' => 3,
4 I$ _) ~# R' l2 Y1 M9 X      'order' => 'DESC',* a9 X! W1 u1 v# c: W
      'orderby' => 'date'
5 A9 e. H. \# v$ u' f   );2 h, D+ t$ G& R- a# ]6 z6 g5 C. ?
   $query = new WP_Query($args);
  g& r! b. `, @$ I  Q+ m# \( ^   if ($query->have_posts()) :
6 m8 ]! M2 T& Q2 M( y9 T      while ($query->have_posts()) : $query->the_post(); ?>% \; Q# }" j" Q" J! M7 e
          <div class="notice">/ r; u' x4 q( G9 w8 |, O# `0 P; q
              <h3><?php the_title(); ?></h3>
1 h; V4 {5 Y+ h" q# y2 V              <div class="notice-content"><?php the_content(); ?></div>5 l& U! m. ^0 D" H% r
          </div>  J3 H: c- ~, ^% N8 }6 f" O
      <?php endwhile;
6 J* t7 ]6 K; E& t% b$ z) G! P# G. d      wp_reset_postdata();. a. M! N  e; c& U, q, ~$ E
   endif;
3 H6 X) w, L4 I$ d   ```3 x0 F: V; k9 K
2 f5 V% ^1 p3 c
   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-26 23:31 , Processed in 0.080469 second(s), 3 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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