【问题标题】:Get Wordpress posts from two categories从两个类别中获取 Wordpress 帖子
【发布时间】:2014-11-18 20:51:57
【问题描述】:

我正在尝试在一页(6 和 7)上显示来自两个类别的帖子。它应该包括来自任何一个类别的帖子以及与这两者都匹配的帖子。不幸的是,到目前为止,我只找到了后者的解决方案,这就是我现在向您寻求帮助的原因。这是我当前的代码:

<div class="sponsoren_container_wrapper">
<?php $args = array ( 'posts_per_page' => -1, 'category' => 6 );
    $myposts = get_posts ( $args );
    foreach ($myposts as $post) : setup_postdata( $post ); ?>
    <div class="gallery_image_container">
        <a href="<?php
$content = get_the_content();
print $content;
?>" title="<?php the_title_attribute(); ?>">
            <div class="gallery_image_thumb">
                <?php the_post_thumbnail('thumbnail'); ?>
            </div>
            <div class="gallery_title">
                <h2>
                    <?php the_title(); ?>
                </h2>
            </div>
        </a>
    </div>
<?php endforeach; wp_reset_postdata(); ?>

</div> 

我将如何将类别 ID“7”添加到该代码以使一切正常,如上所述? 提前致谢!

【问题讨论】:

    标签: php wordpress posts


    【解决方案1】:

    使用逗号分隔的字符串:

    $args = array ( 'posts_per_page' => -1, 'category' => '6,7' );
    

    http://codex.wordpress.org/Template_Tags/get_posts

    【讨论】:

    • 我是个白痴,我试过了,但忘了在它周围加上''...谢谢!
    • @NrGBar 没问题。通常很容易忽略这些类型的事情
    【解决方案2】:
    $args = array ( 'posts_per_page' => -1, 'category' => '6,7' );
    $myposts = get_posts ( $args );
    

    【讨论】:

      【解决方案3】:
      $args = array ( 'posts_per_page' => -1, 'category' => '6,7' );
      $myposts = get_posts ( $args );
      

      对我来说,“类别”不会通过,但“猫”可以。我希望这对某人有所帮助。

      【讨论】:

        猜你喜欢
        • 2011-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-22
        • 2017-04-23
        相关资源
        最近更新 更多