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

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

[复制链接]

340

主题

530

回帖

3542

积分

管理员

积分
3542
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:( J. Q0 F, v) y3 e3 m; l
+ N  U, \2 }3 o
1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。
5 p$ n* h) D1 K' q( b3 N7 A+ I
$ p: N  r9 ]5 \$ J% c8 a! C/ O5 A2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。# s7 R6 H/ K4 o: h! k1 H  H# W
  S1 B3 f6 f7 J
3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。9 r/ O& g& V2 k8 m

2 T. v: {+ v. V2 L: C4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。8 x& M$ ~( H% [8 L
$ C" `, A# @7 l& d' H& @4 d
注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

340

主题

530

回帖

3542

积分

管理员

积分
3542
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?
# ~0 b2 R. T' T3 d- Y  }  _* C# s2 x
  M& x$ J' }6 P/ I9 ]: c# o' \如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:
. ]# h% W8 `+ p) H) d
5 }4 r' x% i7 I. P1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:
- v' i" }% ?! D9 r4 V( {3 Z! \9 y: a) S
   ```
2 \4 W. X) ]7 R3 I8 S) x   add_menu_page(
% N1 e3 t- i6 w; S% g& Z       '全站公告',% z7 \9 v* Y( J. }
       '全站公告',: k* V. R, Y# O' e6 j6 }
       'manage_options', //需要的用户权限
, e- c4 M# V  g/ X2 ^" e) W       'site-wide-notice', //后台页面的 slug
/ v% n& m, A1 l       'render_site_wide_notice_page', //用于渲染后台页面内容的函数
* `2 L2 p+ P" ^" U1 |6 [$ W       'dashicons-megaphone', //用于显示在菜单中的图标
* V: G( ^: }2 V* F5 p7 ^       30 //菜单项的位置
) T% l; N: ~: x) j0 }   );
+ ~3 Z( G" o7 i$ x2 K$ j% t/ E   ```$ f  J$ v3 _, S7 r5 g* f! D- |

9 c( T7 T# R. b+ v   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。& Z9 L" ~6 _$ C6 ^" k
! X/ D9 ^- }: {! ]
2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:) f' J4 w4 ]8 G5 Q

; h( V1 L: W" ?  Y2 K6 @. i$ }   ```& w& m- u0 Q' O: K: B. M" p! F
   function render_site_wide_notice_page() {
- f  P: v- H1 G+ w& Q0 V      // 渲染后台页面内容) Q4 H& J. e3 H% u6 l# M; H
      echo '<div class="wrap">  |( p* t" O9 @. G5 c' P
          <h2>全站公告</h2>8 k0 H5 {- D! [
          <form method="post" action="">
; |; N/ o) {% p1 z+ |: ^              <label for="title">公告标题:</label>
" x7 ]: u; e6 W7 a6 G              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>& k# q" A( j+ q* c: W& v" K" i
              <label for="content">公告内容:</label>
( I# J$ l, Q; N- v, g$ j- T              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>' F6 O8 Q( B  |4 s, a# y
              <input type="submit" name="submit" value="保存设置">/ U- i' M* n4 V/ _3 b( `. {/ F
          </form>
/ ^* u- A, U6 v7 H8 D      </div>';
1 d6 f, i6 q" N) q: ?& @$ w: O
3 M# i% D3 w4 w3 F, c      // 处理表单数据
& h' H6 E) E0 Z( p$ R5 J) c3 `      if (isset($_POST['submit'])) {
! V" f/ p7 F4 G# D2 M8 U          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));
* S# N: O& n' y0 D9 u          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));$ H. ~5 l3 ~% D7 B' Y7 {
      }2 t9 G6 M3 s) b) T+ w: z
   }5 D% Z# N, u! g2 ]2 ^' n" e
   ```. b$ H4 g& [9 {- H+ a8 n9 l
: y; _6 Y2 m) g. l1 [7 L* O
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。
  R% M. {- y& `' S! O8 v( d+ @6 V7 u. k
3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:
7 G' c, }3 [* U4 q0 V& J) W0 n) Q! t5 Z+ v% x  Z, X& w% e' D
   ```
4 B2 L/ s! v9 }: [% g! T   <?php
# ]! C* k* K% G6 f, z6 ^( X* b5 k   $site_wide_notice_title = get_option('site_wide_notice_title');! ]* S3 q: M, [2 ?1 b! ^
   $site_wide_notice_content = get_option('site_wide_notice_content');: z' A1 u8 I; V3 F
   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {% S/ o" e2 r1 E, L
       echo '<div class="site-wide-notice">';' d" o: q( p  C5 s& Q; R
       if (!empty($site_wide_notice_title)) {2 f8 |! {3 D+ i# l$ s
           echo '<h3>'.$site_wide_notice_title.'</h3>';
! H' E" _( w& m6 [       }
% R3 A' [( ?. O       if (!empty($site_wide_notice_content)) {
# M$ c8 _" a0 O% @2 D           echo '<p>'.$site_wide_notice_content.'</p>';
  r# s3 Z; j# A/ Z- W  ]       }" s' y9 |# I3 Y  B( Q
       echo '</div>';
# A  k2 T9 S) v# m; n+ W- `! N   }1 U" _, D3 J3 r$ M. m
   ?>' j5 H' R% m6 l" i7 g) R7 G
   ```
- H* P2 D. r8 @4 R& |4 _; V8 U- i) x9 T6 _" ~
   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。$ R1 P* O9 G4 ~# ^+ X3 E

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

使用道具 举报

340

主题

530

回帖

3542

积分

管理员

积分
3542
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?: H, d& |1 S4 _: r3 i

' {5 N' j/ q6 A您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:
9 s) [0 m( D8 ^, ~6 N" }1 A/ c& M' p$ r
1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:6 Q$ f: y/ Q/ r8 }
$ F) f' `( l& i# |0 i# ~
```
+ A: v6 Q6 Z6 c% Z# s6 Ifunction create_custom_post_type() {8 v. `' l  p# F3 P; b* [2 V9 b
    $args = array(/ I) ?1 m" K. ^; |
        'labels' => array(
9 p9 C: {* N( U- K" C            'name' => '公告',
' y6 f4 y# H5 F. ~8 X+ u: P# F. N- R            'singular_name' => '公告'- Z5 e" F' o1 ?. Y/ k9 q
        ),, m+ D' v1 k& G4 Z
        'public' => true,2 E$ q, m# s  ]/ A3 a
        'has_archive' => true,+ V" i: T4 ?. Q
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
$ a7 p% c0 c& p        'taxonomies' => array('category', 'post_tag'),
1 h9 \* ~  @- _, R& \( q3 s3 ^        'menu_icon' => 'dashicons-megaphone',
4 T" B1 [3 Z! j" J3 ~0 V        'menu_position' => 5,
$ y5 S5 B3 m. B        'rewrite' => array('slug' => 'site-wide-notices')+ s; V) o, q; \( j- Q- T
    );5 a. w, ?, O1 x8 ~
    register_post_type('site-wide-notices', $args);
+ ?; l% C, }# X1 V% x9 O}) l- q$ \/ O  o9 P
add_action('init', 'create_custom_post_type');
  h6 g7 o; H1 A* _, q```! F2 ^2 i* r- q

. W) L4 J8 R  l% S+ c在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。8 f3 f3 o/ ?' S; Q; S, g# _
6 c! z. c1 f+ n! A4 V
2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。
6 n- G8 _# f" ]; N8 L( M0 {8 b+ b
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:9 \5 F9 Y/ {( T7 c" c
3 }: g3 x4 c. c6 [
```
. [# K) `$ a- V, e7 n$args = array(
. E/ v/ h3 a& S' Z    'posts_per_page' => 3, // 显示的公告数量
# h% i# F: {& H    'post_type' => 'site-wide-notices', // 公告文章类型( r! g/ U) L- i9 O1 L; ^- t& [' z) T3 J* Y
    'post_status' => 'publish', // 公告状态
/ c  M, `' {& B% ~% Y    'order' => 'DESC' // 排序方式
% ?1 D( G$ Y6 M+ ]5 [3 k$ l0 |/ `);" v0 H4 x7 X! \8 O7 v3 ]

- d7 Q) U8 {! `2 K+ D4 }$notices = new WP_Query($args);
  Z0 \/ O; F( i: q) C; mif ($notices->have_posts()) :2 d2 J0 B5 x/ t$ Y" N3 F, u
    while ($notices->have_posts()) : $notices->the_post(); ?>
) D% a2 ^2 L6 ]8 `        <div class="notice">
8 ~& k9 `, W3 V/ T( r3 Q+ I/ c- U$ Y* c            <h3><?php the_title(); ?></h3>! C% `, }) g7 W
            <div class="notice-content"><?php the_content(); ?></div>
1 v1 i" {5 t: }" |' B1 F0 }4 ]        </div>8 p: i( N$ L& _1 V$ c! |
    <?php endwhile;
7 J' N+ _& C' K# Pelse :
, S: G0 }7 C3 v! S    echo "暂无公告";: X% `( c' C2 Q- l# y
endif;- U, v1 z& @8 q' j- e
wp_reset_query();( G4 F1 }2 w: [, Y
```3 C  K! j9 M( I6 }# K6 k* R

  Q0 `( ~) ?5 X8 z+ |这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。
1 `' c6 Z% `6 q$ _( k' s$ |) ~: E) A
, V* t" R5 Y. ?0 K" r( L. u4 F1 H以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

340

主题

530

回帖

3542

积分

管理员

积分
3542
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
) h! T! c! D! d2 o; e2 ~8 J! X' V3 X4 R% |+ x: B6 i  _7 \
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
/ ]+ F. e* n2 K* ?: e5 Z& x% M- j" {5 x' ]
以下是创建自定义插件的步骤:( S8 H1 ]% |- p' N: w: s- \% b* t

3 J4 s" q9 r' y4 }! r1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
; U6 a- H6 Z( K* D2 J. J2 \& ~3 Y4 _, W" }2 N4 @, G/ [- y" l' G
   ```
8 v6 h$ Y% r  R2 s" R   <?php
' D6 I: m! ~% D. b5 W6 L0 c4 N7 ]   /*( M  ^6 @: [. _
   Plugin Name: Site Wide Notices Plugin
( k- V# ?- z% Y! g0 P7 c   Description: Adds a new custom post type for site-wide notices.
. c; P6 l. `, \" h   Version: 1.0* |% w# {6 i. Z' m$ k6 j
   Author: Your Name" w+ u1 J; s8 K2 q
   Author URI: http://example.com7 t* o7 \  V( B+ C) k/ ~$ O4 R( Z
   */
" ~" j: [9 w* Q
& v5 L/ `- r7 X. I   // Add plugin code here.... O9 z0 D9 m: R
   ```& G# _5 Q( ?9 d! j# g# h. u' Z
+ p% K+ v) `' c' P- h: I# ~
   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
4 U" h4 c1 r3 S
3 T$ ]( H; k4 s# Z) Z+ I, a5 T2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
: d( E  g' M" {4 Y$ ^4 R) |3 D7 c! J" y
   ```% u6 D5 L8 P/ R) D
   add_action('init', 'create_custom_post_type');; Y' s! n; J! x' Q7 D& ?1 o! V* t
   function create_custom_post_type() {" b" a/ H: c' }/ I/ t
      $labels = array(
( p$ J" Z2 Q, X          'name' => 'Site Wide Notices',, d1 D, i+ e* j8 u. _
          'singular_name' => 'Site Wide Notice',
4 I" i1 u/ V) U  r# z          'add_new' => 'Add New',
' _: l+ H7 P9 ]# m          'add_new_item' => 'Add New Site Wide Notice',
, g3 v7 d+ P) y) ~% a  t5 Z) z          'edit_item' => 'Edit Site Wide Notice',
( n( G% Q4 P3 p! C" ]. \7 m          'new_item' => 'New Site Wide Notice',, F9 f8 D! b2 z, L! R; f
          'view_item' => 'View Site Wide Notice',
; I2 f4 {5 L: v- v2 Y8 r$ K# A. e          'search_items' => 'Search Site Wide Notices',
1 }9 Z8 e& G" G+ m* r          'not_found' => 'No site-wide notices found',* [& a5 G$ n8 N2 `! ~
          'not_found_in_trash' => 'No site-wide notices found in trash'
7 \* m! t% B3 e* [! l: S, r      );
; h* H/ j" K6 y; u7 @" r: O* }* r% {0 C2 q
      $args = array(
2 U0 e. t) t/ t- ^3 j          'labels' => $labels,
: b8 V) D5 U7 A          'public' => true,
/ }5 V' P7 {2 [5 G& G, r          'has_archive' => true,' K, B, j- [% H4 s' C5 h
          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
  F+ m8 `% w8 I9 Z          'taxonomies' => array('category', 'post_tag'),
, S. U/ |' c4 n& E2 y$ u5 L5 C          'menu_icon' => 'dashicons-megaphone',2 z( `* `: y6 _( \7 V6 f
          'menu_position' => 5,: m4 t% G4 w, J- [9 J4 S* P3 R
          'rewrite' => array('slug' => 'site-wide-notices')/ f! |, d6 D+ r0 Y3 x+ ~
      );
* ?) [2 {9 c. v
9 {+ H# g; V) p8 B7 }5 ]      register_post_type('site-wide-notices', $args);
2 n# d/ a, i4 E8 H7 b5 [4 ]6 X6 r   }' a( t9 q/ ~- W6 C3 S# [0 O
   ```/ E! @' ~; e- Q. n& _+ \9 {
( h: V9 u7 y, N, J, z- x9 _
   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
* x8 u/ f- N# B1 G1 Q( C) V  k" ?2 P  I! v& @8 l
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:$ b0 X. B. q4 a/ a
4 W) W! x4 N- O2 Z6 h$ K. Q
   ```
: K; L* K) S/ @+ m0 w% a$ v   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');  C' a1 D+ R/ s
   function add_site_wide_notices_boxes() {2 H$ ]9 V: K7 F: O
      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');( ?/ R! Z4 K& E/ O& S
   }5 A# v( W" o1 v. t
% e. }- e) y/ `$ p
   function notice_details_meta_box($post) {( `& g" s- d) ], c8 }8 R5 J1 H
      wp_nonce_field(basename(__FILE__), 'notices_nonce');
. L& m, T0 r6 _$ U3 k6 h+ J4 E      $notice_title = get_post_meta($post->ID, 'notice_title', true);
1 X* }- u) d* m7 a- e% i      $notice_content = get_post_meta($post->ID, 'notice_content', true);
/ ^+ S3 u2 Y9 ~: I      ?>! P1 [4 X: F/ p& @$ G! j
      <p>6 f. B" D  j4 m  a* x& C
          <label for="notice-title">Notice Title</label><br>) R5 Z* O; y& G, ?, \
          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
' ]9 M/ |7 E& |      </p>  H# F/ h9 E: |' z
      <p>1 C" @+ r* d/ a
          <label for="notice-content">Notice Content</label><br>; L& `5 L6 Z9 p. G! v# p
          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?># ^' `& N: I0 ~9 H6 L7 p  ?( r5 {, r8 e
      </p>
- ]  v+ P4 b; P6 [      <?php2 M) }. Q7 E& c( j. ~1 W
   }
9 T+ U/ U0 f' c8 c
' r$ m1 b& r$ r; l7 Q7 @, A! v3 W   add_action('save_post', 'save_site_wide_notice_meta_box');
6 o7 \2 v$ r7 Q. U; p9 l& L* e   function save_site_wide_notice_meta_box($post_id) {$ P1 q: m  R$ r# j$ s% k4 r3 J
      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
, j5 ]$ v7 E+ `2 ?( g" o         return;7 C5 Y+ Z; i% c2 t
      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
- v  }7 B# y& V. h         return;
) ]' ]: [: p4 l2 I8 K) H) H+ f0 y0 \7 I4 J
      if (isset($_POST['notice_title'])) {0 @8 @% c/ t; q& T& ?9 W: e
          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
5 ?! B6 d% }* N) e      }
7 ?5 Q# ?9 J! P$ E% [      if (isset($_POST['notice_content'])) {
  Q8 ~1 f: M! _0 n          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
4 S7 W- `! S( G. G& Z2 h      }; T, r7 ]& T5 K8 P
   }. ~: S4 E. D! K* Q6 l+ v
   ```8 V2 ]) o6 p: A- j1 `: g; }
. d" g% y) E2 f; s1 ?; Q
   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。/ s" T4 z4 P& W! I* D  x
; q; Y- |( N7 K* v. i
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
- b# M- K& N0 i1 g& T. _
0 c. |9 W: D% ^1 a1 \% k- F   ```% L. J/ f/ M. Q2 N  X/ o/ G
   $args = array(
! Y4 p$ @+ H6 f2 m; }( C( p      'post_type' => 'site-wide-notices',! q0 n$ g+ I  z7 @% @3 M, ~% j
      'posts_per_page' => 3,
# S! a4 ^( T% U5 o- X# V1 H      'order' => 'DESC',. o- n- v8 t' V7 z0 U9 E; n
      'orderby' => 'date'
1 h! T- t7 D3 [" a) {- \( Q; }   );
8 ~7 O' B4 l; f& Z% X   $query = new WP_Query($args);+ c" _' C2 {5 b  i5 W  h% P6 V
   if ($query->have_posts()) :, K0 q2 H$ E8 q' G; {  w& u, u
      while ($query->have_posts()) : $query->the_post(); ?>
/ T% z" M, J9 V% u8 G1 M8 j( ^4 F0 U' s          <div class="notice">
- C; ~1 L2 [& c' S              <h3><?php the_title(); ?></h3>. y+ m; M3 P  F1 d! A
              <div class="notice-content"><?php the_content(); ?></div>
6 }8 Q* z  ^/ J          </div>, @( ^- p6 x( v) x; m+ F
      <?php endwhile;% S3 f' L' l1 O) b8 M- B
      wp_reset_postdata();( S. K8 G+ W1 A/ H
   endif;  {& m8 D  t/ D1 G) @
   ```$ a/ i( K) k% h' p9 s. G" m

6 H9 W) y2 z! `   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-23 10:54 , Processed in 0.020560 second(s), 2 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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