wordpress - 仅在主页上显示来自特定类别的最近帖子
问题描述
我想显示特定类别的 3 个最新帖子,并且效果很好。但是只要我想限制只在主页上显示,我的功能就不再起作用了。
add_action( 'um_theme_before_content', 'extranet_header' );
function extranet_header() {
// Get the posts form the "Communiques" category
$the_query = new WP_Query( 'category_name=communiques&posts_per_page=3' );
if ( $the_query->have_posts() ) {
// Render posts link and title in an ordered list
echo '<div id="communiques">'.
'<h4>Les derniers communiqués</h4>'.
'<ul class="administration-notice">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>'.
'<h4><a href="'.get_permalink().'" title="Cliquez pour en savoir plus...">'.get_the_title().'</a></h4>'.
'<p>'.get_the_excerpt().'</p>'.
'<a class="admin-notice-button" href="'.get_permalink().'" title="Cliquez pour en savoir plus...">Lire la suite</a>'.
'</li>';
}
echo '</ul>' .
'<div class="communiques-footer">'.
'<a class="communiques-button" href="/communiques/">Voir les anciens communiqués</a>'.
'</div><!--/.communiques-footer-->'.
'</div><!--/#communiques-->';
} else { // No post found
echo 'Aucun communiqué';
}
// Reset the original main query
wp_reset_postdata();
}
当我添加 is_home() 函数的结果不返回任何帖子:
add_action( 'um_theme_before_content', 'extranet_header' );
function extranet_header() {
// Get the posts form the "Communiques" category
$the_query = new WP_Query( 'category_name=communiques&posts_per_page=3' );
if ( $the_query->have_posts() && $the_query->is_home() ) {
// Render posts link and title in an ordered list
echo '<div id="communiques">'.
'<h4>Les derniers communiqués</h4>'.
'<ul class="administration-notice">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>'.
'<h4><a href="'.get_permalink().'" title="Cliquez pour en savoir plus...">'.get_the_title().'</a></h4>'.
'<p>'.get_the_excerpt().'</p>'.
'<a class="admin-notice-button" href="'.get_permalink().'" title="Cliquez pour en savoir plus...">Lire la suite</a>'.
'</li>';
}
echo '</ul>' .
'<div class="communiques-footer">'.
'<a class="communiques-button" href="/communiques/">Voir les anciens communiqués</a>'.
'</div><!--/.communiques-footer-->'.
'</div><!--/#communiques-->';
} else { // No post found
echo 'Aucun communiqué';
}
// Reset the original main query
wp_reset_postdata();
}
解决方案
您可以尝试将查询包装在有条件的首页,而不是运行查询并查看它是否是“家”。这样查询不会运行,除非它是主页而不是每次运行它然后检查页面。
另外,is_home()
指的是帖子页面,而不是您可能认为的“主页”页面,因此您的设置可能会错位。检查设置->阅读。
试试这个:
add_action( 'um_theme_before_content', 'extranet_header' );
function extranet_header() {
// Check for the page that is set as the "Static home page" in settings.
if ( is_front_page() ) {
// Get the posts form the "Communiques" category
$the_query = new WP_Query( 'category_name=communiques&posts_per_page=3' );
if ( $the_query->have_posts()) {
// Render posts link and title in an ordered list
echo '<div id="communiques">'.
'<h4>Les derniers communiqués</h4>'.
'<ul class="administration-notice">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>'.
'<h4><a href="'.get_permalink().'" title="Cliquez pour en savoir plus...">'.get_the_title().'</a></h4>'.
'<p>'.get_the_excerpt().'</p>'.
'<a class="admin-notice-button" href="'.get_permalink().'" title="Cliquez pour en savoir plus...">Lire la suite</a>'.
'</li>';
}
echo '</ul>' .
'<div class="communiques-footer">'.
'<a class="communiques-button" href="/communiques/">Voir les anciens communiqués</a>'.
'</div><!--/.communiques-footer-->'.
'</div><!--/#communiques-->';
} else { // No post found
echo 'Aucun communiqué';
}
// Reset the original main query
wp_reset_postdata();
}
}
推荐阅读
- java - 已连接:但没有输出
- html - 如何从菜单底部删除 iphone 阴影
- c# - 如何使用 C# 将复杂的 JSON 数组响应转换为简单的 Json
- sql-server - 尝试使用 powershell 使用服务主密钥加密数据库主密钥
- c# - 条目(数据库)已添加
- scala - 从包装的数组scala创建新的数据框
- apache-spark - 如何减少执行 SVD 的火花作业的运行时间
- c# - 在 WindowsIdentity.GetCurrent().Name 中将“\\”替换为“\”
- c# - 如何从 C# 生成进程 C++?
- c# - 来自 C# 包装器的 StyleCop 未检测到违规