[英]Custom Post Query WordPress and a better way to display posts
This seems a little noob, but I didn't found a better option. 这似乎有点菜鸟,但我没有找到更好的选择。 I created a custom loop to display only the title of custom post type I created. 我创建了一个自定义循环,以仅显示我创建的自定义帖子类型的标题。
Example: 例:
Custom Post Type: Atuação 自定义帖子类型:Atuação
The problem is: I can't "validate" at the menu if the post is active or only a link. 问题是:如果帖子处于活动状态或仅是链接,则无法在菜单上“验证”。 Example: My visitor is visiting the Direito Penal Empresarial page. 示例:我的访客正在访问Direito Penal Empresarial页面。 But the menu don't display any class so I can customize it. 但是菜单不显示任何类,因此我可以对其进行自定义。 It just shows the <a href> link
. 它仅显示<a href> link
。
See the code of the custom loop below. 请参阅下面的自定义循环的代码。
<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>
There is any better solution for this? 有更好的解决方案吗? Or if not, how can I add the "active" class to the href? 否则,如何将“ active”类添加到href中?
UPDATE: You can check out the website live. 更新:您可以实时查看网站 。
You need to store current post ID in a variable then you need to compare current Post ID with list item Post ID if both are same then apply active class. 您需要将当前帖子ID存储在变量中,然后需要将当前帖子ID与列表项“帖子ID”进行比较(如果两者相同),则应用活动类。 So your code will be something like this- 所以您的代码将是这样的-
<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>
use this 用这个
// 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; ?>
for WP_Query consider this link 对于WP_Query,请考虑此链接
it's great article... 很棒的文章...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.