在我的网站上,我有几种不同的自定义帖子类型,我已经启用了它们来显示在搜索结果中。我想知道如何才能以不同的方式查询每个自定义post类型的结果。
例如,如果我要做一个搜索,从我的博客文章和我的自定义帖子类型称为卡片,这是一个卡片的列表,我想显示我的文章的标题和摘录,但对于我的卡片的结果,我希望贝尔显示标题和卡片图像,我将调用从元数据字段。
具体如何做到这一点?
你会有单独的WP_Query,还是会在默认的搜索循环中使用if语句?如果是这样的话,我该如何判断该帖子是否来自自定义帖子类型。
这是我对WordPress搜索结果的默认循环。
<?php while ( have_posts() ) : the_post(); ?>
<li>
<article>
<h2><a href="<?php esc_url( the_permalink() ); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<time datetime="<?php the_time( 'Y-m-d' ); ?>" pubdate><?php the_date(); ?> <?php the_time(); ?></time> <?php comments_popup_link('Leave a Comment', '1 Comment', '% Comments'); ?>
<?php the_content(); ?>
</article>
</li>发布于 2013-12-16 17:02:28
您可以通过$post->post_type::
you can go through below code, just edit with your post types::
<?php while ( have_posts() ) : the_post(); ?>
<li>
<?php
if ($post->post_type == "cards") {
//do fancy things
}
if ($post->post_type == "mycards") {
//do fancy things
}
else {
//otherwise just sit
}
<article>
<h2><a href="<?php esc_url( the_permalink() ); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<time datetime="<?php the_time( 'Y-m-d' ); ?>" pubdate><?php the_date(); ?> <?php the_time(); ?></time> <?php comments_popup_link('Leave a Comment', '1 Comment', '% Comments'); ?>
<?php the_content(); ?>
</article>
?>
</li>谢谢
发布于 2013-12-16 07:02:46
是的,您可以在while循环中使用if条件。
<?php while ( have_posts() ) : the_post(); ?>
<li>
<?php
if ($post->post_type == "card") {
//do fancy things
}
else {
//otherwise just sit
}
?>
<article>
<h2><a href="<?php esc_url( the_permalink() ); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<time datetime="<?php the_time( 'Y-m-d' ); ?>" pubdate><?php the_date(); ?> <?php the_time(); ?></time> <?php comments_popup_link('Leave a Comment', '1 Comment', '% Comments'); ?>
<?php the_content(); ?>
</article>
</li>https://stackoverflow.com/questions/20600924
复制相似问题