【问题标题】:Wordpress query to include posts AND pagesWordpress 查询以包含帖子和页面
【发布时间】:2014-10-08 22:57:53
【问题描述】:

我正在一个 wordpress 站点中实现同位素脚本,并且当前有以下查询来填充网格。我希望将 Wordpress 页面与帖子一样对待,这可以实现吗?

提前感谢您的帮助。

<?php $the_query = new WP_Query( 'posts_per_page=10' ); //Check the WP_Query docs to see how you can limit which posts to display ?>

<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-grid">
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); 
    $termsArray = get_the_terms( $post->ID, "category" );  //Get the terms for this particular item
    $termsString = ""; //initialize the string that will contain the terms
    foreach ( $termsArray as $term ) { // for each term 
    $termsString .= $term->slug.' '; //create a string that has all the slugs 
    }
    ?> 

<article class="<?php echo $termsString; ?> isotope-brick">

        <?php if ( has_post_thumbnail() ) { 
                  the_post_thumbnail();
            } ?>
</article><!-- end article -->

<?php endwhile;  ?>
</div> <!-- end isotope-grid -->

【问题讨论】:

    标签: wordpress jquery-isotope posts


    【解决方案1】:

    默认情况下,您的查询将显示帖子。添加“post_type”参数以包含页面。

    变化:

    <?php $the_query = new WP_Query( 'posts_per_page=10' ); //Check the WP_Query docs to see how you can limit which posts to display ?>
    

    收件人:

    <?php $the_query = new WP_Query( array(
        'post_type'      => array( 'post', 'page', ),
        'posts_per_page' => 10,
    ) ); ?>
    

    延伸阅读:http://codex.wordpress.org/Class_Reference/WP_Query

    【讨论】:

    • 除此之外,如何将类添加到特定的帖子类型?例如,我希望能够在同位素网格中区分页面和帖子,允许我分配不同的宽度?干杯
    【解决方案2】:

    在回复评论:将一个类分配给不同的帖子类型时,我找到了一个解决方案。只需将以下内容添加到相关的

    <?php echo get_post_type( $post ) ?>
    

    【讨论】:

      猜你喜欢
      • 2013-05-26
      • 2012-04-29
      • 1970-01-01
      • 2017-06-03
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      相关资源
      最近更新 更多