WordPress 置頂文章是博主根據自己的需要設置的,通常用于展示重要或熱門的文章。
以下是一個示例代碼,用于在 WordPress 主題中展示 10 個置頂文章:
<?php
// 查詢置頂文章
$sticky = get_option('sticky_posts');
$args = array('post__in' => $sticky,'posts_per_page' => 10, // 顯示 10 篇置頂文章'ignore_sticky_posts' => 1 // 忽略置頂設置,直接獲取置頂文章
);
$query = new WP_Query($args);// 判斷是否有置頂文章
if ($query->have_posts()) {echo '<div class="sticky-posts">';while ($query->have_posts()) {$query->the_post();?><div class="sticky-post"><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2><p><?php the_excerpt(); ?></p></div><?php}echo '</div>';
} else {echo '沒有置頂文章';
}// 重置查詢
wp_reset_postdata();
?>
你可以將這段代碼添加到你的 WordPress 主題文件中,比如 sidebar.php 或 index.php,根據你的需求調整顯示位置。
原文
http://www.chudafu.com/jianzhan/7749.html