自定义帖子查询WordPress和更好的方式来显示帖子
问题描述:
这似乎有点noob,但我没有找到一个更好的选择。我创建了一个自定义循环来仅显示我创建的自定义帖子类型的标题。自定义帖子查询WordPress和更好的方式来显示帖子
实施例:
自定义文章类型:Atuação
- Contratos(CívelËSocietário)
- 迪雷托刑法EMPRESARIAL
的问题是:不能 “验证”在菜单中如果帖子处于活动状态或只有链接。示例:我的访问者正在访问Direito Penal Empresarial页面。但菜单不显示任何类,所以我可以自定义它。它只显示了<a href> link
。
请参阅下面的自定义循环的代码。
<ul class="menu-advogados">
<?php
// WP_Query arguments
$args = array (
'post_type' => 'atuacao_posts',
'pagination' => false,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$exibir_atuacao_posts = new WP_Query($args);
// The Loop
if ($exibir_atuacao_posts->have_posts()) {
while ($exibir_atuacao_posts->have_posts()) {
$exibir_atuacao_posts->the_post();
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
}
} else {
echo "Nenhum post encontrado";
}
// Restore original Post Data
wp_reset_postdata();
?>
</ul>
有没有更好的解决方案呢?或者,如果没有,我该如何将“主动”类添加到href?
更新:你可以check out the website住。
答
您需要将当前帖子ID存储在变量中,那么您需要将当前帖子ID与列表项目帖子ID进行比较,如果两者相同则应用活动类。所以,你的代码会像这个 -
<ul class="menu-advogados">
<?php
global $post;
$post_id = $post->ID; // Store current page ID in a variable.
// WP_Query arguments
$args = array (
'post_type' => 'atuacao_posts',
'pagination' => false,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$exibir_atuacao_posts = new WP_Query($args);
// The Loop
if ($exibir_atuacao_posts->have_posts()) {
while ($exibir_atuacao_posts->have_posts()) {
$exibir_atuacao_posts->the_post();
?>
<li><a href="<?php the_permalink(); ?>" <?php echo ($post_id==$post->ID)?'class="active"':''; ?> ><?php the_title(); ?></a></li>
<?php
}
} else {
echo "Nenhum post encontrado";
}
// Restore original Post Data
wp_reset_postdata();
?>
</ul>
答
使用本
// WP_Query arguments
$args = array (
'post_type' => 'atuacao_posts',
'post_status' => 'publish',
'pagination' => false,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$query = new WP_Query($args);
<?php if ($query ->have_posts()) : ?>
<!-- the loop -->
<?php while ($query ->have_posts()) : $query ->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
为WP_Query考虑这个link
这是伟大的文章...
+0
我认为你不清楚我需要什么:( - 我想使用名为active的类进行链接。请参见[response](http://*.com/a/27110295/2356834)。 – starkbr 2014-11-24 20:28:19
谢谢! :D解决了问题。 – starkbr 2014-11-24 20:26:57
欢迎您:D @starkbr – user2839914 2014-11-26 17:44:58