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

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

[复制链接]

339

主题

521

回帖

3487

积分

管理员

积分
3487
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:8 G  H' @: W1 g' r; P% Y$ S

) j2 }2 X, f$ C! D2 c- N* q5 k1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。8 g" `3 e7 s" W: Z) e* ?* }" K

- t% @. I5 K! w4 A5 V2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。
; o/ s) _1 J1 |
' c9 ]( i2 `2 `! ?  D, ^2 g4 ~3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。1 T! Q; q0 K$ C$ g+ e9 r% ], k7 e
  i  ~$ }* U5 D5 s9 V% i9 ^
4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。1 l' y  ]- {* r. ?% X3 \

  c, k! p8 t( H8 V$ n注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

339

主题

521

回帖

3487

积分

管理员

积分
3487
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?0 L2 L, m/ h* O1 p6 b' E7 p
; ~6 _! \. p: Z* T; D# d, [
如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:
1 B) N; t& V) S- {+ k; F, g" d0 L9 p+ @1 L" S: C
1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:) S3 M. I- w3 o' l+ v6 F2 N$ j
1 L, ~+ W) Y$ N, V/ e) d# J
   ```. M$ [. \# B1 _* e5 H% O
   add_menu_page(' K1 H! k: Q' }8 f0 ]/ h; `
       '全站公告',
* G1 K! B( X8 {, ?, O# h       '全站公告',
# h0 b$ I8 c, z9 M; d2 H       'manage_options', //需要的用户权限
2 @( K$ m! e+ z6 ?7 o& b       'site-wide-notice', //后台页面的 slug
) o3 T! T! {; F+ G* l       'render_site_wide_notice_page', //用于渲染后台页面内容的函数9 l2 f. O. G, Z- U1 G  h
       'dashicons-megaphone', //用于显示在菜单中的图标4 p& V3 Z$ V/ C( J) J
       30 //菜单项的位置
1 r$ J/ A9 c. `   );
, _  A' l5 K/ m5 T4 g8 ~   ```
" x4 J3 v# s7 G% Z+ w8 c5 X6 r. X/ C. O* V8 H- I" z' R; m7 k
   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。2 T* a1 O2 B; G0 Z8 N* {5 z
1 h, l$ d. v3 y9 D9 I: W
2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:# \1 d8 U! l1 h$ B6 z( a5 L  m
( K* g9 @2 x- H6 N% G
   ```* h" {( p; Q4 `9 ^- ^
   function render_site_wide_notice_page() {
9 K: ~8 q  ?% J* J1 V& g7 ^& X# {+ T      // 渲染后台页面内容
1 ~% k6 l' y$ `- Z7 I$ ^      echo '<div class="wrap">
3 [  _# z+ c; a# }0 j          <h2>全站公告</h2>
, W. v: u+ T- n* x" L% R9 W& `- j1 f4 R% Y          <form method="post" action=""># S: C$ C( L" c0 R; u
              <label for="title">公告标题:</label>  t$ ?* ~1 P/ h4 m/ ~  }, n
              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>
- e, K- q5 b: }9 {& v5 W- A              <label for="content">公告内容:</label>7 _9 `/ B" G( n* x: l0 \' O! ]1 Q# k
              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>
  i8 t. c5 j, T# j7 }6 s0 j) @              <input type="submit" name="submit" value="保存设置">8 u- W. D4 _1 ^7 f7 o8 e! _# s
          </form>9 m( \9 k& v* v/ D0 o, E: o
      </div>';7 t3 G; e$ c4 A6 [9 C
8 \) V/ Z0 e8 `' M2 m% {
      // 处理表单数据# E! N5 h2 Y- U+ \1 [
      if (isset($_POST['submit'])) {+ W  K& G1 `# H9 K4 b* K: d4 C% p4 Z) O
          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));
! h0 U( d% R  R3 L0 q          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));* N7 z8 B- B& D( ~0 n0 h7 M; ?
      }
# V: W" d1 _) j0 Q3 j   }
! b( f+ W' D7 S; a4 b   ```9 z: A  A/ }$ l, q; R, U8 W

( f0 t: i. X$ Q/ V8 ^6 d   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。
" d7 K1 n6 r2 p4 f# Q! V  C' @( x* c. a) b
3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:
8 [! Z2 X, e4 V0 [% v, S
; X+ B1 \" ?5 q, H0 U. E' w   ```
3 }( C; @3 M" _5 \+ M   <?php# j8 i$ g: `& }: B4 Z4 ]. P
   $site_wide_notice_title = get_option('site_wide_notice_title');8 y' U6 k% T% x; i9 D4 G5 d4 Q
   $site_wide_notice_content = get_option('site_wide_notice_content');
+ N/ c! C- x8 h& d6 H% _8 d2 a1 I: C   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {
# r$ Y) z1 G) n5 g       echo '<div class="site-wide-notice">';) G: ^1 T" ~# X/ N% i8 q  @3 y
       if (!empty($site_wide_notice_title)) {
: G( |& V" q0 J/ Q- M           echo '<h3>'.$site_wide_notice_title.'</h3>';
% e: R# y! h. K" U9 }- I3 g       }. |: W1 ~# ]/ [1 E
       if (!empty($site_wide_notice_content)) {
, K7 L) G$ ]1 V5 h) i) G* Z, C           echo '<p>'.$site_wide_notice_content.'</p>';
+ f' C/ S6 o2 k. u       }& Q7 h- {$ N) f' O) M3 u: U
       echo '</div>';# ?2 O9 j6 r5 i: {$ ]
   }+ q- Q& q2 Z( `! f
   ?>
! F5 m; v  o/ N/ h# a/ G   ```8 U/ Q+ E* i* j' G, E4 ]
$ ~# e; t  R. [9 y3 w- W
   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。5 z5 L7 C$ g) i6 I- D

0 y7 i, d! h* |+ D请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

339

主题

521

回帖

3487

积分

管理员

积分
3487
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?# H% C6 n# C; L: l' G& i) N$ y

9 _6 q5 @5 j% ~( \您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:( Z4 X- l/ C+ S, T
: s3 o, N8 C+ L. [7 X) y; P/ u
1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:1 g/ ]% p' t! W6 `1 E  c

  G0 P1 s. Z0 Z5 z+ J- k```* x( u5 Z5 I' b  m
function create_custom_post_type() {. X2 j3 M* n/ j( ~
    $args = array(
# a) a8 N# \' X        'labels' => array(
0 O6 j3 ~8 e1 W: C! S; K* w            'name' => '公告',
7 }" D' K/ B% p" R            'singular_name' => '公告'
% P; K9 q" Q) h7 d. r9 K        ),( p3 q; ]' e( `% Y" Q, g
        'public' => true,' d2 N4 V6 C1 B+ R  A6 Z
        'has_archive' => true,
; S* j# Y& c; c+ y7 L        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),) d% b4 ]( f. c9 w9 ^+ |
        'taxonomies' => array('category', 'post_tag'),) m7 J; X! e- J% z5 T) C
        'menu_icon' => 'dashicons-megaphone',. u6 o! O3 b' z$ H
        'menu_position' => 5,
/ H1 v1 P0 I+ Q6 R        'rewrite' => array('slug' => 'site-wide-notices')
) g. J: c* F0 X. ^! G0 E    );
2 S' }2 @, B; s' t# x7 [1 G    register_post_type('site-wide-notices', $args);) ?3 }  V: ~6 D$ O/ a9 ]" R
}" }3 y! f1 c$ [% r4 T- f
add_action('init', 'create_custom_post_type');
1 M: W6 W& b- @9 m9 ]0 K```5 x6 x) F, v$ o+ g; |( v6 N

$ ]9 t2 u, o; {# H在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。( G* M; O/ h9 C  j% B+ {- z

. e8 w% }7 d! B2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。
- i+ L0 V4 s4 v; @
; A: ~! T+ T6 M6 U3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:1 o; D0 u/ }; q  G
0 v2 K# l* t0 v) @) C9 B- ^
```
: }$ I+ q# R# G- ~2 D$args = array(+ L$ M2 }; b) z
    'posts_per_page' => 3, // 显示的公告数量; Y) z- ?: V' O2 H8 Z
    'post_type' => 'site-wide-notices', // 公告文章类型
  W, U( Q7 t/ `! A, ^4 B    'post_status' => 'publish', // 公告状态5 X4 o" y: h! V, `* r) y  q  M+ J6 |
    'order' => 'DESC' // 排序方式
0 T! R- I& I7 C( K);$ f4 U/ }5 m) W4 n
* B3 Z( v1 [3 [& r- N  B( S
$notices = new WP_Query($args);- N. ~6 i' C2 v9 H* Y: W4 {! w
if ($notices->have_posts()) :( h: W( u6 |5 s; g
    while ($notices->have_posts()) : $notices->the_post(); ?>; y+ b9 C" W$ v3 J% R1 `6 [0 j, Y
        <div class="notice">- H% B0 Z  ^) d$ C5 f. T$ A
            <h3><?php the_title(); ?></h3>
% E8 G6 C$ ?) p0 V            <div class="notice-content"><?php the_content(); ?></div>
9 ^, M5 ~2 u( U        </div>
) k& P0 r- O6 @9 \! p    <?php endwhile;6 K& P+ Z/ Z3 C6 F7 J1 @( `7 `" e
else :& [& [7 l/ \7 x% B
    echo "暂无公告";* j  _' t* v! }- J- i% D3 {
endif;
0 `) S2 N1 V1 k8 g$ {$ Jwp_reset_query();5 T8 S; @7 R$ E" B9 T1 t+ u$ t/ Y
```( S. R8 M% s2 T6 M5 ]  f1 x" o
( i/ j) c2 t1 [
这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。8 W2 t7 i+ M0 P3 ~; S
* f, i9 p5 i8 _0 t( h  }7 ?6 R
以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

339

主题

521

回帖

3487

积分

管理员

积分
3487
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?, u( k3 \* ~1 P5 d. }* [

, Z4 x5 q' y! G- x如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。3 G+ |/ `9 P4 x3 m# l/ r3 l& E2 ?
( a( e  c* l5 {& r$ H$ S
以下是创建自定义插件的步骤:
8 x! I) A& N! Q9 G' C3 n  R5 o: Q, Z7 R
' @# K9 [( I* l& `2 }  ]9 E8 g1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
3 m2 ?4 K# e- Q3 @' Q0 T% T! g" a' o+ A" A" @2 p, F5 N9 |
   ```8 l! I" m; A4 S" C
   <?php9 u( Z, ~- E7 T1 d4 g1 M
   /*1 H: T# F/ r; U
   Plugin Name: Site Wide Notices Plugin; E$ t( @( f- i5 s% L4 X
   Description: Adds a new custom post type for site-wide notices.+ v6 `# J7 v) j& N
   Version: 1.0
$ E' z# V1 w1 |+ e0 W# n# H   Author: Your Name1 a+ s! m) t9 X2 _7 b
   Author URI: http://example.com+ K7 i$ N0 Q6 L5 m6 p9 |
   */
3 F; e) p6 ?0 k& s5 r% O/ u( d! P
$ y: C4 n# [$ C: L9 s1 O   // Add plugin code here...
4 k6 K  G2 g2 r* V/ a2 q: ]9 A   ```/ S, v  a6 u  c! I) G6 }4 @
1 I2 R& n8 L1 @0 u) O( }: g' s! }
   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
% H5 B9 g7 A0 W8 W% [% d& Y7 |# G) I7 Y6 f
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:, ?  u0 e! F5 A1 \
: T5 c/ V2 I0 `6 \7 @8 \
   ```5 a1 r3 w2 E8 p( ^. q" l5 V
   add_action('init', 'create_custom_post_type');! P4 `& E$ D1 ]1 B. A! v
   function create_custom_post_type() {
2 H1 u# J) ]  M+ ?4 `* M      $labels = array(, P0 z; z% X$ t6 P1 `+ {
          'name' => 'Site Wide Notices',
2 d& F0 Y7 ?1 ]. B% l+ y          'singular_name' => 'Site Wide Notice',% s% X( d7 h  |: t! e9 q
          'add_new' => 'Add New',
& g$ [+ i4 P% [8 c          'add_new_item' => 'Add New Site Wide Notice',& Q; I' g. }" T& O+ _9 p3 ^, l2 R
          'edit_item' => 'Edit Site Wide Notice',
0 P/ S" W, U9 w6 {2 v          'new_item' => 'New Site Wide Notice',
( \  S  b0 Q5 M& k% m3 M          'view_item' => 'View Site Wide Notice',. K0 k& b  X8 Z+ v" z* s
          'search_items' => 'Search Site Wide Notices',; D" c, {- T/ C0 I# i  M
          'not_found' => 'No site-wide notices found',( ?- W/ }( K: B, ^6 f: R3 G
          'not_found_in_trash' => 'No site-wide notices found in trash'
, N8 u4 I9 B/ g      );( |2 d( p6 [3 D! \; H

  z: v0 G- q  ]! o      $args = array(
" p; S) ?9 q/ ?, M2 L  C          'labels' => $labels,3 R% e7 |+ K$ u& y% X1 [
          'public' => true,! H; s5 u5 [; y# ?7 q
          'has_archive' => true,/ a& z, N* Q( i0 o
          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),8 o8 H; h2 q4 J
          'taxonomies' => array('category', 'post_tag'),2 r6 L/ ]0 ^  S9 U: ~
          'menu_icon' => 'dashicons-megaphone',
9 O/ Z8 I  u( R# ^& p0 c          'menu_position' => 5,- N7 `( f8 G( ?: ~( D, m! |
          'rewrite' => array('slug' => 'site-wide-notices')
, F& {, O5 \5 y) U+ Y' {      );
& S3 g3 c2 h8 F8 f
- O+ N* X( e- _; Q. `% L      register_post_type('site-wide-notices', $args);# u# K4 ]! x- \& Q
   }  P7 s3 w2 i6 b7 z. E6 F( Q  X& l7 M
   ```* H/ N9 V5 I/ W

0 H! _( V- W% C4 S   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
. [, w1 ]# ~% e- |% b
( X6 Y4 k* o9 U: N0 Z" B/ ?& h3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:: e- r7 X# w0 ]: \

- z$ o6 \  g* w9 r8 i- r! @   ```2 L5 W" q0 D% _8 i4 V9 v
   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
' U; Z) W# E" Q   function add_site_wide_notices_boxes() {
  Z1 b* \4 f6 ~, H1 d$ _      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');8 i. B6 \$ t) T/ a' E) X
   }
5 t7 f8 k9 w! M( ^' C4 a' ]7 }& g1 j) ^* R- S- T' m& M& |+ X
   function notice_details_meta_box($post) {
6 K0 s# C; Z( o  k) R      wp_nonce_field(basename(__FILE__), 'notices_nonce');& |( R& \5 A' o% {& l6 }) Q" I
      $notice_title = get_post_meta($post->ID, 'notice_title', true);) i: [+ K' ?6 e, _! z
      $notice_content = get_post_meta($post->ID, 'notice_content', true);
1 M/ P( Y" Y( J2 I      ?>
  x4 U4 I: r0 e& x/ s1 K/ }8 n      <p>
; U2 n- w) \, b  }$ A- |          <label for="notice-title">Notice Title</label><br>
  k6 m1 ?; V* A* }* Z' m3 W, j7 C) S* I          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
% C3 A) H+ n! F4 L0 A! ^      </p>2 v2 a) n+ E$ b! {2 @
      <p>% y% g5 h+ l0 m: m' c% H
          <label for="notice-content">Notice Content</label><br>
1 v1 m( m4 l( W2 U/ [' e" D. H          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>! Y2 a, i& Y* ?( T7 M. K4 y
      </p>
/ p( b$ ]5 S8 t6 K. _5 ?0 q      <?php# e' M7 c4 L( j6 a
   }
: }% K; K# w# X- _
, \7 w8 n+ Z3 C! r, i   add_action('save_post', 'save_site_wide_notice_meta_box');. j3 S1 a  n( a& m3 D  M" k: i
   function save_site_wide_notice_meta_box($post_id) {8 M7 Q7 `% D/ _9 K
      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
9 C; n  Y+ d5 L; F+ I$ B         return;
4 T3 }9 O" L+ k- \: n      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
, o: P' ?5 \% t- B4 e. {5 G5 N8 h         return;7 |4 {1 w5 s- z! r4 \& m8 Y
; A! A3 j. v" w. O' g! ]
      if (isset($_POST['notice_title'])) {
; u# m1 C4 ?5 b0 y  G          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
/ U. Z* ?4 S7 T4 I% @8 o      }
1 @. F. l% N& u; A      if (isset($_POST['notice_content'])) {
1 e  i3 E. O8 U. N, D4 W          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));- C3 o+ G1 b2 e  c5 G+ S( a% M
      }: X- h  _( ~8 B
   }& w  u2 t2 P6 K5 z5 e3 V9 ^/ V
   ```0 [" e) P5 k/ j- g' m1 R7 `

/ o/ R, s# ]1 ]. r3 q6 B7 I- E' S   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。$ {. a, N4 H7 r* \  _

; a; A+ K9 o7 S% f; x* x6 s! ^4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:6 ]& q- X4 w9 Y2 l, m; C7 `: @3 |

& B3 @% |5 y$ [$ a0 m! S9 N   ```4 Y9 o1 y3 r7 B, G
   $args = array(
1 m) Q7 M! u9 h# `7 R2 `      'post_type' => 'site-wide-notices',
; t! A2 q9 O( j) f. B      'posts_per_page' => 3,. z4 v/ o* b5 X" j: \
      'order' => 'DESC',, E/ Z0 y' i/ M! r* U
      'orderby' => 'date'
8 U( W3 o: x# b) v   );
# R. |$ {9 {2 M   $query = new WP_Query($args);+ N) X1 c7 b$ @& B  R' w
   if ($query->have_posts()) :
6 _1 W. a  U  P% q6 |) b9 J      while ($query->have_posts()) : $query->the_post(); ?>
1 g) a4 X2 M) z. k+ _          <div class="notice">% m# e5 D7 B5 q, |; m
              <h3><?php the_title(); ?></h3>
+ k" c( D* E8 w3 Z2 a( N              <div class="notice-content"><?php the_content(); ?></div># ?7 ~5 G; o4 _! R
          </div>
- T5 ~: o+ G: ]; k9 m8 e3 _( F      <?php endwhile;4 E8 Y' t4 L' P% F  x. i; m
      wp_reset_postdata();
5 l, J0 ~+ [/ J0 H1 G2 C   endif;8 P1 z& q) v5 V! |. w2 j$ |
   ```! D; _/ N; C# f

, Q/ x" a5 b' {  k   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-11 06:39 , Processed in 0.019712 second(s), 3 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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