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

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

[复制链接]

347

主题

565

回帖

3683

积分

管理员

积分
3683
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:) D# Y) V1 i3 b# y

- M3 f$ J( N0 {7 f, A; Y- }1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。! d/ x" o; o6 c8 v" |/ K+ W+ v

5 b/ ~+ l; L6 n. n9 ^0 _* }2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。5 N- ~$ v% t$ f' a
+ ]- {* p2 s  d
3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。
& l& S; C" ?. E
1 _2 X) O5 C. j7 d1 y& k4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。% V, A- S) |2 q; b( s/ b
' Y, g$ ]: F5 q4 f7 B
注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

347

主题

565

回帖

3683

积分

管理员

积分
3683
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?
$ U6 m  |4 L# a9 Z% u) v6 B! M6 ]' @. t% ^1 \, s& z
如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:
( u" U3 ?% k; A5 d1 @$ _: |. _8 b% c" h1 Y. X. n6 X8 Y- D
1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:% C, w; y$ ?% T$ a+ w- n
& t% g) h0 }) F' R, d
   ```5 A# R0 z% ?. V1 I
   add_menu_page(  [- w1 A# G: N' T; [
       '全站公告',: R0 l: \: l# n  E; ?
       '全站公告',
0 J+ b7 e: p$ h# P6 Z0 N( ?' P, Z- r       'manage_options', //需要的用户权限) y" P7 i8 c+ |  B; @
       'site-wide-notice', //后台页面的 slug
3 [! @: y9 Q* E* v       'render_site_wide_notice_page', //用于渲染后台页面内容的函数
7 l! H1 ~) H: X& a, G( O1 c       'dashicons-megaphone', //用于显示在菜单中的图标' z) h8 G5 [% E% d) q" Y
       30 //菜单项的位置
& e7 X. D$ ]$ M% o3 o1 ]   );
# B- ~  |7 k& x$ b, b' K* Y   ```  ?, m5 ]; n! |6 a* Y
: f) b% x( b! K4 X& l) i  F8 F0 P+ v
   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。* r7 e4 ~0 s# M

& c5 K! v% x* a8 M* N9 E2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:1 a. W7 ]% r  L& a
& f) t% F, ^' H1 s: S) [& `
   ```
9 x/ n* Q8 s+ v, q   function render_site_wide_notice_page() {
- F) C' L* w: v! U. O" Y      // 渲染后台页面内容
6 }% _6 ?) E% Q* d      echo '<div class="wrap">7 Q, n  W! B9 B$ r5 Z  _3 k9 @
          <h2>全站公告</h2>
# a9 q! n( x, b4 f! c7 O/ L          <form method="post" action="">
! G0 y6 F4 Z) f" ?' E              <label for="title">公告标题:</label>' g+ Y6 z& w: T3 Y  |
              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>
( P! U7 h: S& d) U" F" x: K              <label for="content">公告内容:</label>
5 f( P) A+ l( L# N. I              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>
  J6 C& C3 `: h$ |! ]) _              <input type="submit" name="submit" value="保存设置">
% K3 C( y' g9 e- J4 O          </form>
. K' \! F7 j% a      </div>';
# a: t2 V. [7 @9 o
5 I. A" f6 m# {      // 处理表单数据
/ E. A, Z, Z7 l' F% B# k# I1 |      if (isset($_POST['submit'])) {
0 D  `: G1 R5 h& ?          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));
) c2 P' O# g' X/ c$ q3 z6 L          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));5 d0 N7 z( k$ l6 i6 J# M8 A
      }( U# i% Q) B% }2 w1 f8 r
   }
! x) B, ~" ]+ u+ w( r   ```2 W7 a" Z8 \. P9 {9 m1 c% z7 _
7 s! I8 l  d* Y. G9 Z( O3 O1 p2 A
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。
  ^4 l% G6 D$ ?, a/ n; U
( D  R7 l: O  Z( i2 \* A/ n( A3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:
- v1 ]2 m  E9 Z+ L$ }; p) p* S2 w1 V( c
   ```
8 y2 k: C& k4 y1 `4 L# B   <?php
5 t6 U- A. D8 R' X9 D' H9 s   $site_wide_notice_title = get_option('site_wide_notice_title');
& N1 |: y* n8 o( ]" {; @   $site_wide_notice_content = get_option('site_wide_notice_content');' j& w) j6 m7 c" s. {; c
   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {
/ ~* c( L+ W/ e1 c1 J+ G- R       echo '<div class="site-wide-notice">';
8 N! U8 r' a2 ]1 f4 S       if (!empty($site_wide_notice_title)) {
2 A- b. ^4 y& V9 x8 {4 ]           echo '<h3>'.$site_wide_notice_title.'</h3>';: X/ T3 a; q1 w
       }. P/ }3 V" P0 U/ I" S
       if (!empty($site_wide_notice_content)) {
. G/ K5 H' u: e+ L  w9 x- M           echo '<p>'.$site_wide_notice_content.'</p>';# A. w5 V# ^5 @% Z. \
       }
7 s# \$ u. k' z0 j" e$ x$ p       echo '</div>';
. S0 c# ^# x2 r6 A$ M   }
- b- P& Y5 x+ W% g" z! Z2 M" l   ?>; n  k. o( @, z: ~7 B
   ```
+ e9 j7 J; ~. Y  G) t; G8 H1 ~" P* X2 g
   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。! `8 l+ @) o" y
" w& X+ Q0 |: U  e# q# d
请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

347

主题

565

回帖

3683

积分

管理员

积分
3683
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?: b- j6 M3 O* z- B! r. g
* H+ O/ z" D) M0 ~: |# n5 K3 {+ J
您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:
7 `. p5 c& M( r1 [
& ^$ k4 ]: u: \& C: D1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:
& t1 n4 A) w, M9 I6 O8 S2 ~9 f4 m4 A
```( E; H( e; r. N6 o4 V1 |8 ~
function create_custom_post_type() {
% S% {8 S4 i; l2 _: \% p8 y    $args = array(& M) m& J3 Y4 f# ^5 V
        'labels' => array(
4 J! c, Q# |$ u% U! t% o            'name' => '公告',
9 X9 m0 s- b0 H2 p            'singular_name' => '公告'6 ~/ h. \& i' R6 ^3 z( X) u
        ),; x8 \* \8 s) V: @7 S; s* \
        'public' => true,6 R' ~0 d5 ~4 v/ ]: }) W5 _
        'has_archive' => true,
5 l1 Z$ U; {% D$ f        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
4 I6 D4 ^6 Z1 g, y1 W+ y        'taxonomies' => array('category', 'post_tag'),
2 p- U( Q6 \* d* d9 X        'menu_icon' => 'dashicons-megaphone',
& @: \5 ]5 U9 |( L2 k        'menu_position' => 5,
* F9 x- m) j. l& g# ]$ v: Q% {$ u& R        'rewrite' => array('slug' => 'site-wide-notices')
  H1 P$ Y) N3 s% S8 {! N" U    );
, W: D% y, G: Z    register_post_type('site-wide-notices', $args);- B( O' G* d1 p
}
% s; D# U% w; y# U6 C2 ?& I" I0 eadd_action('init', 'create_custom_post_type');) `- u0 p% a# v- h7 j4 L, p
```
  F9 i  V4 F* t( Z0 v( I. ~8 G& b
  g4 M! f: g% D' M! K在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。
. Z! M& T0 g) S* Z0 ?8 S: F
5 _0 A3 n# U7 ^$ h3 s# K2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。: V* k6 G9 P5 I( h
7 z+ M3 [! j9 x% ]6 V
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
" K% w0 J0 _& R4 k( i4 M3 i" o9 x/ B6 o
```% y2 R4 k, ?$ R: C
$args = array(
' E. L! v( s# y9 Q6 M3 `    'posts_per_page' => 3, // 显示的公告数量
' W! Z: i2 G* O, w6 [2 I: v    'post_type' => 'site-wide-notices', // 公告文章类型* Y, I, c5 \4 M" A( X; a& f+ c1 `
    'post_status' => 'publish', // 公告状态' Y; K. E2 @4 y) v: ?$ R. G& L$ h
    'order' => 'DESC' // 排序方式
+ w5 s& H" j7 p7 v& d/ O/ {);
  ^8 l- l: L; A5 B
% {+ A% E; A; k' [/ v$notices = new WP_Query($args);' S* q7 s2 k; J" F
if ($notices->have_posts()) :  h) P% y+ v5 s4 J. ?3 G* w6 q+ O
    while ($notices->have_posts()) : $notices->the_post(); ?>8 i% @4 Q' h  C2 }! _
        <div class="notice">
% X! V+ {$ u1 j' j4 q9 q            <h3><?php the_title(); ?></h3>5 x+ c4 Y% {  \: W/ @
            <div class="notice-content"><?php the_content(); ?></div>
6 i' t+ p- D/ q; z: r% h        </div>4 e  S; j: }, K8 C) P* u. V
    <?php endwhile;
9 P- k; ]. L* P2 N$ v4 h$ felse :
- f3 B8 C7 O* j0 D9 v$ P1 L    echo "暂无公告";8 O: u$ X, V) [$ p/ h& v% J; A
endif;, v4 C# A& \: T
wp_reset_query();8 w- d0 K. g- S4 @
```/ t( v- r8 f- m# }

# l. s6 ^3 e7 O: _# }: S. X这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。& [$ N$ V& T8 E! }

, l* V2 C. I# A+ U, ?- X  S4 K以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

347

主题

565

回帖

3683

积分

管理员

积分
3683
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
) g+ M. P2 }  ?9 Z! ?, x9 W" p# l& ^7 ]! y( y6 H3 |
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。# c- y; ]6 B( t( b$ d- h; X
0 K2 q' L! V, z! f8 `
以下是创建自定义插件的步骤:5 ~# [1 I" c- n; n. `- e
; s8 i) V' o: ^! }2 \
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
/ @& w$ Z  e8 @9 @- y4 e6 K, D4 ^' g& P1 u, t% a" {8 B" [, u
   ```
" m5 n4 A( N; L7 p! T   <?php
2 A! M7 x( D7 M; K* `$ B9 K   /*2 y# N6 d: U2 L3 R
   Plugin Name: Site Wide Notices Plugin
3 n- m: m# O, E' U8 E: a& v   Description: Adds a new custom post type for site-wide notices." k! U. V' ~0 q( ?. N: s6 N
   Version: 1.0
4 W! d5 m# E/ a+ ~5 A   Author: Your Name
3 P& a4 D# {3 L   Author URI: http://example.com
2 _8 J, F9 q1 t- g' g   */: Z  ]# p7 j- @. C

8 \% `' Y7 g9 }) `' l, ^* v   // Add plugin code here...+ H3 f# d; D: n0 G8 B" f' g7 }
   ```/ c; u; B, Z/ L: U
; R) X' a3 {) z- A- e9 }
   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。: l; L8 Y+ ?2 B4 I
" D7 K6 ^0 w. _" P+ C
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
* A: {  x+ i! O% T+ w+ Y" I/ ^2 R/ K. o( [0 x8 N/ R
   ```
9 p; g3 T* x( y   add_action('init', 'create_custom_post_type');
# d9 I! I5 [: q$ g6 S! f   function create_custom_post_type() {
0 x8 }: c) o4 K8 r1 Z6 _5 t3 ^      $labels = array(- k  h4 N* j+ w1 w
          'name' => 'Site Wide Notices',
* O/ c9 R0 B( K# a: A! G. @          'singular_name' => 'Site Wide Notice',0 [( q; L$ M1 @( V! l. X
          'add_new' => 'Add New',
" ^8 G! C# U1 d0 P( f1 N6 H9 t          'add_new_item' => 'Add New Site Wide Notice',1 @7 s  i' y. O( ^" Y* S) ^
          'edit_item' => 'Edit Site Wide Notice',0 l1 d) G' `& A1 ?6 t( s; t6 k: W
          'new_item' => 'New Site Wide Notice',4 U. G5 C4 Z: Q9 e& ^, d% B% K9 G% H
          'view_item' => 'View Site Wide Notice',! V( `. K0 c" [0 q# j
          'search_items' => 'Search Site Wide Notices',) }: F! v- b& q* f
          'not_found' => 'No site-wide notices found',' Z1 e+ }0 k9 ~5 G! E' K/ q
          'not_found_in_trash' => 'No site-wide notices found in trash'
4 t0 B* f. d, |" @& B" M* D      );
7 k/ g4 L) j5 |# M. g5 Q9 j
, U7 T9 |% g) X8 s2 t      $args = array(
- Z) R+ C- h$ D$ {8 z          'labels' => $labels,. o4 l0 H0 N  ^( s* z
          'public' => true,1 `% Q2 @% }3 t1 `% q* X) l2 \
          'has_archive' => true,) j, a. q9 ^0 B
          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),9 F0 y; `! Q' \, l' T" y- k2 {2 p0 d
          'taxonomies' => array('category', 'post_tag'),1 I& ?: ]) \: _, S
          'menu_icon' => 'dashicons-megaphone',
( b; g# c' J' @5 {# @          'menu_position' => 5,  o9 [4 a9 g, _4 X) A- V- m$ a
          'rewrite' => array('slug' => 'site-wide-notices')  E1 H" M) L! n4 t% }3 @
      );* `4 H- c! U' B
' i1 t$ m$ a0 `
      register_post_type('site-wide-notices', $args);( @- k. O( I# h- @4 z  K
   }" }! m' p# S, \: J8 E$ H$ }
   ```
' q( }& h+ k4 B7 U6 @; [7 I+ v  }& I) l5 T
   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。% @4 @7 `( n  M+ A( b1 H( j
  f% i( s# m/ o. U3 z5 ]
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
- ?& O" r4 k6 A( s: B. n
' i4 X, |7 q# n3 D! O. }   ```' _2 _' v( z; K
   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
/ {$ t7 g9 L( m: A   function add_site_wide_notices_boxes() {& S* S( [) O+ t" E( F
      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');; i: W6 A6 U0 n) E/ a: N; w* u
   }
. U6 C6 A# m' K, O4 E& r5 t+ f% Y6 w7 s& r, a
   function notice_details_meta_box($post) {
1 y7 h+ G. R! q2 B# T& q. @      wp_nonce_field(basename(__FILE__), 'notices_nonce');
# H& X0 @- k# u      $notice_title = get_post_meta($post->ID, 'notice_title', true);3 `  R( X$ K) _; S
      $notice_content = get_post_meta($post->ID, 'notice_content', true);
8 o9 q# R% ~- S/ C( I* P! z- `      ?>$ H* n" r* h7 G2 r
      <p>: A/ A/ l, B' n, M
          <label for="notice-title">Notice Title</label><br>8 X5 p  |/ ^/ g6 ~/ k5 B
          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">" ?$ g0 n0 O9 p% c  N0 s
      </p>* [- B2 K  }9 d+ w/ O3 w
      <p>
8 ~: f7 B$ \, u& W& r* v# o          <label for="notice-content">Notice Content</label><br>6 }  ]) B4 x9 t/ |/ `$ B: b
          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>1 b5 x3 ?8 ?' d: P+ q
      </p>  F- f% j) |! l, }! I& o
      <?php
2 O: |' R; s/ h! t* k3 v0 T   }8 p0 y& H. Y6 R4 \" ~4 Z, s
. m  j. ~4 ]7 P
   add_action('save_post', 'save_site_wide_notice_meta_box');& t/ f( `6 w+ P) H6 M4 L* y
   function save_site_wide_notice_meta_box($post_id) {
5 Z) M/ L  C/ B$ i# |. j      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
- h4 p% E, f& ]  b  n         return;' E6 ^$ d3 L# [# j0 H2 _0 P
      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
9 s: P9 M9 X" [: e' k         return;
  e& J2 v; M, y: W8 p' i% F& t$ J/ j8 l2 W) ]1 d+ f' O
      if (isset($_POST['notice_title'])) {8 k- m9 }: J" X  E
          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));+ d! ~) i, c) V4 i5 W' O. ~
      }, C7 j0 C6 E+ h9 H$ b
      if (isset($_POST['notice_content'])) {
0 K" J6 i$ T$ w& S          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));% m# I/ s$ q! g, O+ y# f7 q( r
      }
% c8 s$ J7 W* s: w; @( g   }4 {6 G( t: c# Q& J$ H! l! K' F
   ```1 z) [# x, ^# r$ O  u
5 l2 r3 f' ?! ]. W% t  T' M
   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
- B3 k. f2 k, K6 D' s% b
9 l& V" C4 @3 d. L; w4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
8 _) C/ o! f8 O# h) _
6 b* w9 F' F/ w# n; D& B   ```1 Z$ o1 ^3 p$ i" X' R9 {. i
   $args = array(8 }& n; p0 h+ [& P9 O, l
      'post_type' => 'site-wide-notices',( J; o* J, F/ C# O3 o) i+ y
      'posts_per_page' => 3,, I5 i# C* J7 r! m& n5 m
      'order' => 'DESC',
, A+ J/ ]  e& B8 R. z! r      'orderby' => 'date') @" C0 G$ }. a4 W% m! Y( A
   );
: l3 _) L) {/ @- B   $query = new WP_Query($args);# t  }- [- W- F8 A0 D) j
   if ($query->have_posts()) :
% l5 w0 r6 ?( r9 P0 r1 g      while ($query->have_posts()) : $query->the_post(); ?>
0 P+ v1 |; a' |          <div class="notice">
) K* ^4 N' U+ B              <h3><?php the_title(); ?></h3>: W" y* o3 `) S$ e9 @% ?" E
              <div class="notice-content"><?php the_content(); ?></div>6 O. D% |3 O% H! M
          </div>
9 T6 }7 R8 n9 X% B      <?php endwhile;# F, v# w: T" o0 W0 D( b4 p1 P% G& W
      wp_reset_postdata();  P, y- s( i! T' e' W3 E
   endif;4 X3 {  X  P3 B4 r$ Z. N6 ]
   ```
8 D7 v: Z7 u) J8 X
+ T: H6 j& w  |+ i$ q' q# K   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-14 22:51 , Processed in 0.012517 second(s), 2 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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