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

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

[复制链接]

343

主题

557

回帖

3637

积分

管理员

积分
3637
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:
4 l+ Q; b) k' H" [
" |  |7 o3 U: k) v& u$ q8 p1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。. s) @5 o* p  a; h6 q8 ]
: [1 I  t7 f" D4 [/ j( f
2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。- r/ b+ h! U; u$ ]
8 q& }5 C# ]& t: I! W& a
3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。
, ^4 D9 W' i5 c! X0 P- U- N
* @, ~* \$ O0 G* r4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。. q" h# [1 G7 U1 I
8 p" ?: }. @$ T! ?8 }4 A
注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

343

主题

557

回帖

3637

积分

管理员

积分
3637
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?: v# H, P. a3 n: p- t7 U+ ?

  I9 S  N, d$ A$ U0 Z如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:# k6 V7 D, _9 @- c0 x& p
* U$ {- w) T, E
1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:
$ ^- t/ D: q8 u4 V2 o2 b5 R7 ^& q4 o6 Y7 W
   ```" Y7 @" p7 \* [1 V9 Q$ h# n% ~
   add_menu_page(
# P1 S: r! L, L       '全站公告',
  C; D! |& {* g0 ^       '全站公告',
) s0 r/ ~2 _& Z: B       'manage_options', //需要的用户权限! R: r0 ^5 R: }( Y& f* y
       'site-wide-notice', //后台页面的 slug
! `0 u7 |3 W* j. x9 _9 F% b# L; k       'render_site_wide_notice_page', //用于渲染后台页面内容的函数
3 K; ]. T3 [. Z# ]( k- j       'dashicons-megaphone', //用于显示在菜单中的图标  m& f& H; }' \1 e. s2 C
       30 //菜单项的位置
+ s( E. p. {! y" {   );5 z( `" M; t% d9 r4 P
   ```
0 c' A) [+ k. N; O7 @: ^
; n% M2 l$ o) ?; y   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。- Z0 H6 G% g! X" _
# B' Q* _; q* C1 Z) Q
2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:2 `. b/ j9 ?. f
4 Q2 M6 f5 L7 a* o1 U8 N
   ```, V9 x$ e: ^5 f" h
   function render_site_wide_notice_page() {
+ F/ S9 K: @+ Y: Y      // 渲染后台页面内容. ?3 z& j% W( k% `
      echo '<div class="wrap">8 t: y& |& [7 K8 z3 ^3 L. D, X
          <h2>全站公告</h2>" u( k2 z8 Y3 G, ]; z
          <form method="post" action="">
5 w* M7 e; t1 t! g              <label for="title">公告标题:</label>
- p+ ^5 X; h6 [9 b6 X5 ^7 V9 d  O2 ?              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>
% p! ^! |0 S; [( e              <label for="content">公告内容:</label>  u0 ~1 @( o9 b0 `% A7 Z& A
              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>4 l( @: M5 }9 q& A# O
              <input type="submit" name="submit" value="保存设置">) ~) r# j+ H) Z& T
          </form>, ~. d' `5 N$ ?9 n; g' Z, }
      </div>';' ?' |- }& U! A5 m; I# M
: o' k7 _, X) l4 S$ G
      // 处理表单数据
# I4 ?/ W' ~" V# Y( r6 ]6 x, R. B      if (isset($_POST['submit'])) {
/ S9 x" D1 |: D1 G* R: t          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));8 J8 o, z& g1 a  `/ ~& n8 Y' S
          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));1 {" {: ^0 s% {3 v/ v( A" b3 H
      }
4 v  X6 _/ g3 A7 _& R6 e( _3 c   }, ?% p' u' ^# k' g3 b8 V* \
   ```
# A; V, E) p% v7 V/ Q2 o& D# J$ L
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。" v# ^( I/ m1 B; J, q

0 e5 o: o; l' p; E0 I3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:
4 @5 Q8 q: F/ }6 C, V  d* P8 U# ]8 O- r! K9 {3 R3 J% W
   ```
* `; m; ?3 I# f0 w7 \   <?php
. q3 h( `6 l6 C7 H/ v6 E* B   $site_wide_notice_title = get_option('site_wide_notice_title');
- a; B6 Y1 R/ T   $site_wide_notice_content = get_option('site_wide_notice_content');
" {7 Z) T  U8 K+ F, A+ v5 N   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {0 j$ y: d4 V# G* x, M. w. N
       echo '<div class="site-wide-notice">';
+ N8 P, L7 f% l9 c* C) {) R       if (!empty($site_wide_notice_title)) {
4 e4 h* \) h. b1 n2 b7 j           echo '<h3>'.$site_wide_notice_title.'</h3>';
. z7 t8 x3 F* K3 W' ~6 z8 n       }: F+ o1 G- |, A3 O: s
       if (!empty($site_wide_notice_content)) {
  t' K, Q1 p6 i  |5 v) D$ y( _           echo '<p>'.$site_wide_notice_content.'</p>';5 y( u! d3 G3 @$ v6 n. g
       }' F) I& ]6 Q' Y& O0 Y# R
       echo '</div>';
2 j+ O; j& F- L8 d7 M: L* V0 A! R   }
' \: `( V+ ^) I* v- ]& j   ?>
+ M  O$ Y4 }. y. Q$ S& A0 N   ```
7 t; @/ s, E* W9 H; r% {/ r0 m( _0 z3 u& d4 {' @% U8 N
   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。
9 Y8 u4 o- `: I% R* k0 e" ?. M8 ~+ A" _  t" y; b, f( I+ w& N
请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

343

主题

557

回帖

3637

积分

管理员

积分
3637
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?0 c8 }: s6 S8 r' P; |

& a4 {: r4 z; I1 g* b% u9 Z5 |您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:" k" }+ i( G7 Z( H- z2 l' X' V4 k

. W+ T$ Z4 x) {5 l1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:6 r. I$ v3 L$ V$ F+ R4 a/ C

  \- f/ ^$ m  G' I% I9 T& v```
8 J0 h* n. h. Kfunction create_custom_post_type() {$ m3 E+ |) W3 A/ H( ~- I
    $args = array(2 X0 ?) f! G7 L) R/ z0 D/ J
        'labels' => array(4 x4 `) q) c* D  i' U' H5 M6 [2 ^
            'name' => '公告',0 z9 g# i& \1 X2 X/ K
            'singular_name' => '公告'# p; E+ H" F8 w, e
        ),
$ i" w5 z' B2 o, v3 B$ m        'public' => true,9 a* [: Y9 u$ p" D! j6 A7 t
        'has_archive' => true,
+ g9 a. f/ x& |        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),* d) T0 p9 B. m' L+ g
        'taxonomies' => array('category', 'post_tag'),
; V3 ]4 U" J9 ]9 O, V        'menu_icon' => 'dashicons-megaphone',
7 l1 |5 \3 K8 o        'menu_position' => 5,  U  ?; c" c; [
        'rewrite' => array('slug' => 'site-wide-notices')- u9 ^, |: ?0 ?/ i4 a( l  ?2 y! I6 L
    );
. u, Y3 }, I( ?! s+ g    register_post_type('site-wide-notices', $args);0 N- A, l1 O6 v+ Z
}6 t. i  }/ b! c" g
add_action('init', 'create_custom_post_type');* Q. ?" C3 ?8 ?! z( B* D
```
' O2 N7 d3 m1 {( b" X5 O$ l! `5 ^% l9 d5 S
在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。
! b. _- e; @, `7 N) V
! l. w' D9 h: c6 _0 J2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。9 G! ^  J& J* v4 e! g3 h+ i
' o1 V) M$ O! P& W
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
! h/ D) Y5 a  |6 T. U0 n, m8 ]5 E. V& K
```
  L" ^' s. I/ o* y$args = array(
2 p: N( n1 x9 Z9 k% _    'posts_per_page' => 3, // 显示的公告数量
) k- H1 o8 s. r; Q. y& c% L    'post_type' => 'site-wide-notices', // 公告文章类型5 h: n" G5 u' H7 U  H2 C3 k" U
    'post_status' => 'publish', // 公告状态0 W& i" s1 r( y' K/ _
    'order' => 'DESC' // 排序方式, T& n/ t$ f' s, V: F4 b- U
);
. N, y' k+ t, k- A7 O3 }
$ p, q1 h4 ~$ ~, s' t7 l$notices = new WP_Query($args);4 m+ B- X% }% X. X
if ($notices->have_posts()) :
; Z& N3 m! ^" g) E    while ($notices->have_posts()) : $notices->the_post(); ?>% R7 L# S" P# \7 p: e
        <div class="notice">$ ~4 j# w. v& |* s) p2 j
            <h3><?php the_title(); ?></h3>3 {- f( V1 c" d( V0 c% N" h6 R
            <div class="notice-content"><?php the_content(); ?></div>
( O, V# L, x6 y' L( G        </div>8 l1 O: }' m  o/ x" I
    <?php endwhile;- p8 i3 Y) o$ U
else :+ F1 l; j5 y" {# |) q+ e2 P
    echo "暂无公告";: m# g9 U5 V; H# f/ o' R8 D$ v
endif;
; S# T0 J4 Q9 s0 O4 {wp_reset_query();
' X. o2 Y$ R% `2 P9 o```
' \# ^0 g  }0 X0 Y4 \# G1 O6 t5 N0 T; v8 j5 H
这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。
: ?6 k2 [7 l: \! @% p
4 Y6 ]( [& l- c3 ]! g以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

343

主题

557

回帖

3637

积分

管理员

积分
3637
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
8 l# d# ~9 S5 [) O( r3 _1 K9 @3 _2 \
# v1 `$ ]% A$ Z6 A% ?1 s. r" x% y如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。# W# O: K5 x3 R% w( N: s, z
. ^" i  Q% x9 V0 S6 p" s, R* I
以下是创建自定义插件的步骤:3 y! [6 s2 I; c+ {* Z  p

: h! ^! y% K) ~6 [3 O1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
0 _2 A% Y% I7 I6 Z6 j; O2 w. V$ E- H" N; H3 q
   ```- a: C$ J4 |! I
   <?php+ b' Q: S* h2 e$ {2 w
   /*
; H9 R! x7 [0 x( s, O4 k6 e+ x   Plugin Name: Site Wide Notices Plugin
% M9 }" `3 d: V6 r   Description: Adds a new custom post type for site-wide notices.
; B7 G' `) y  [- g# i   Version: 1.0* S. U/ {' Y( |$ T, P  a
   Author: Your Name
/ I" l' h1 p# R! j/ U4 M   Author URI: http://example.com
9 j% J1 [* J4 i& r   */
- C3 R' R- I9 }0 v' P* b
# k9 X3 o6 I) v  A) H' ^+ e   // Add plugin code here..., r* r2 M1 K& N' {& {! A% u; U, g
   ```
& \, ?& z' S: a( Q  I5 x7 b" l7 \2 R6 N- m: c3 y: P) Q
   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。: X' w  j1 k+ M8 B
0 y3 b7 P5 V& }6 P: l: s, M
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:7 N6 \1 c3 {2 [' Q
' E0 l5 {* d- l2 ?% l$ |0 ?
   ```
4 A+ {+ C% l; g$ B% ~$ D% P) n2 U4 P0 E   add_action('init', 'create_custom_post_type');
8 |$ S9 o: I% m. Z& ?) e% ]   function create_custom_post_type() {9 ?: c" r- P7 j* j, s+ X+ z
      $labels = array($ G( `4 s& X; {# ^3 ?
          'name' => 'Site Wide Notices',+ R# |, @# M- p# J7 Z
          'singular_name' => 'Site Wide Notice',
7 B8 B+ ^$ h8 Q: u' g* ]* `          'add_new' => 'Add New',  {0 W8 g) g, P- e( B; |$ ]
          'add_new_item' => 'Add New Site Wide Notice',
7 q% |) G, _1 q# u7 A          'edit_item' => 'Edit Site Wide Notice',1 L( q- B: i% W5 ]
          'new_item' => 'New Site Wide Notice',7 f2 J! M: ~( h7 d$ k" e
          'view_item' => 'View Site Wide Notice',3 H( L1 C4 U# K# O( X5 C8 |
          'search_items' => 'Search Site Wide Notices',4 J) A3 T5 F# p- X% I4 a* R' m
          'not_found' => 'No site-wide notices found',! m- ~, Y- ~7 V
          'not_found_in_trash' => 'No site-wide notices found in trash'/ o! d3 @8 s, }
      );% z' ~) _2 A0 p! q/ J" h

3 w8 s- h1 t1 `      $args = array($ D; q! [8 i" ~' l
          'labels' => $labels,
* ]: b/ q; }" Q( f& t) S6 V          'public' => true,4 P# v& {% g9 Z/ b& y
          'has_archive' => true,0 s6 K1 d3 i4 y  j. r. k
          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),' ?  h! H3 n" |9 c/ ?, {) m& x
          'taxonomies' => array('category', 'post_tag'),) b$ L: o0 C6 d$ W* k7 Q3 o4 ]
          'menu_icon' => 'dashicons-megaphone',
2 B; x* s" Q; v$ @          'menu_position' => 5,
5 f, |! a/ T& O( S          'rewrite' => array('slug' => 'site-wide-notices')+ c" c; }! t: @' T( S
      );
) ~% m( P2 ]% i' C& x, J
+ j% d, b% q5 C; _6 y' t* U7 o7 Y3 e      register_post_type('site-wide-notices', $args);- K" f) N7 ^& F" ?8 }! V) R
   }
5 V) A+ C. [, @6 J   ```& L5 q, q( P$ Q$ {: U# z

$ [! _) ?- w$ k, P* j   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
: o* p; D$ H( u% q( O$ L* ~8 L/ B: Q+ {
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:5 M' R) t) F/ i

* ^+ ]" e0 a4 ^   ```0 s1 I  a7 C  y5 J5 Z
   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
1 s! D: P! U- E% G2 L   function add_site_wide_notices_boxes() {
+ {0 D" ?& O! @; @# X: L      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');) {3 ?5 T* G( ~$ `
   }6 ~4 c1 [5 l7 L( H
8 @' O+ v; k9 b" k: \/ H3 v
   function notice_details_meta_box($post) {' O0 e# P3 }+ `  G8 Q/ J$ N' U
      wp_nonce_field(basename(__FILE__), 'notices_nonce');, Z1 ]4 e3 T& a+ J0 p3 k) P
      $notice_title = get_post_meta($post->ID, 'notice_title', true);
- b/ q, Q/ |* n% V/ F      $notice_content = get_post_meta($post->ID, 'notice_content', true);2 ~$ J2 ^9 G, `! C
      ?>: J; s0 T8 B: p# m
      <p>' _. `, E* q6 o1 g
          <label for="notice-title">Notice Title</label><br>
+ u8 V% X0 P3 K' a5 E* h9 D+ v, B          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
& {; [  B- L# u8 P0 I0 q      </p># c, S6 l$ d' L3 [7 V- L2 i
      <p>3 r5 m$ c2 L3 l. q0 J8 N
          <label for="notice-content">Notice Content</label><br>
. r- p! C8 N/ [" Q6 ?          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>7 g# j6 {: u2 L! R+ ^
      </p>4 ~# `; ~5 d5 i) d1 w
      <?php
% {! h9 M! ^! m) q3 O9 C   }
7 A8 v0 i, t% d# C5 o1 E% s/ q4 ?" \& O& {' R8 W5 @, x
   add_action('save_post', 'save_site_wide_notice_meta_box');7 a* T: R$ Y0 [4 X
   function save_site_wide_notice_meta_box($post_id) {
: e3 S9 b! M( w) K7 N( J      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))* ?: x; q+ N4 d9 W3 |6 T' ~: |, t
         return;
7 p2 ~% m: V. S, g  @      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)9 b6 E3 m. B6 `
         return;
9 I9 s0 u, a. z: O9 i$ E6 s
+ i' n: E" `, V; T$ _      if (isset($_POST['notice_title'])) {
- i7 Z' D" U$ J0 I  k6 W          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
! h7 n* g& R& v; N3 p  w$ x) e7 B5 @      }
; M/ V! }  K- T# g, E      if (isset($_POST['notice_content'])) {
5 G( B8 {& c" @/ W0 g- @, S& g# Z4 R          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
9 a9 g, P. N* ^* _9 E( u      }3 y' [( M8 q) ]# @; O: b0 t1 \
   }
. L: G$ g5 M! W- ~! D   ```' m: @* N7 j* t2 r# W2 x
5 s! I' `7 Z" f( ?& p
   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
$ f; x6 c; b, v; m" e8 m  z' l% |& d& g
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:, V/ l; E- V/ m3 @2 S$ ~

8 s3 l- h  e9 H   ```
3 O! t  C- i" f- B5 a   $args = array(2 S% k2 f6 V7 {- O+ \- T2 m4 E3 ?, c
      'post_type' => 'site-wide-notices',
+ q5 k, u0 I$ _1 Y      'posts_per_page' => 3,
. i; D# r4 h" U# H      'order' => 'DESC',
! s$ G1 O8 P( c  o. J$ C      'orderby' => 'date'
, J, x" c0 ^- D# Z0 H& m" Y8 V   );
' b% ?2 X: b( T5 j) @   $query = new WP_Query($args);
1 C. [4 i* v' P7 _2 r0 i: F   if ($query->have_posts()) :
8 O/ L# ?4 u# p* w7 ^      while ($query->have_posts()) : $query->the_post(); ?>4 H& ]4 _. i5 q' M# D
          <div class="notice">
4 U. o) m2 w; x+ x4 {              <h3><?php the_title(); ?></h3>! I) `5 c9 o$ ~* a- [
              <div class="notice-content"><?php the_content(); ?></div>
1 t. y& w" ^5 Y          </div>$ W8 D" Y4 I& y
      <?php endwhile;
; o  |( ~: l8 C( ]/ u8 V      wp_reset_postdata();# `* w" P$ @5 M1 I4 D" ]" `
   endif;
, ?+ V+ ^+ |9 p9 p5 ]   ```9 y- F) l) u1 N; t

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-2 19:20 , Processed in 0.027420 second(s), 3 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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