首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据自定义帖子类型结果设置WordPress搜索结果的样式

问根据自定义帖子类型结果设置WordPress搜索结果的样式
EN

Stack Overflow用户
提问于 2013-12-16 06:53:06
回答 2查看 1.4K关注 0票数 0

在我的网站上,我有几种不同的自定义帖子类型,我已经启用了它们来显示在搜索结果中。我想知道如何才能以不同的方式查询每个自定义post类型的结果。

例如,如果我要做一个搜索,从我的博客文章和我的自定义帖子类型称为卡片,这是一个卡片的列表,我想显示我的文章的标题和摘录,但对于我的卡片的结果,我希望贝尔显示标题和卡片图像,我将调用从元数据字段。

具体如何做到这一点?

你会有单独的WP_Query,还是会在默认的搜索循环中使用if语句?如果是这样的话,我该如何判断该帖子是否来自自定义帖子类型。

这是我对WordPress搜索结果的默认循环。

代码语言:javascript
复制
<?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>
EN

回答 2

Stack Overflow用户

发布于 2013-12-16 17:02:28

您可以通过$post->post_type::

代码语言:javascript
复制
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>

谢谢

票数 1
EN

Stack Overflow用户

发布于 2013-12-16 07:02:46

是的,您可以在while循环中使用if条件。

代码语言:javascript
复制
<?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>
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20600924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档