在定制帖子上按定制分类获取帖子

时间:2018-02-09 作者:Lizajean

我一直在努力根据分类法slug“peers”引入自定义帖子,我不知道我做错了什么!

   \'coaches_post_type\' => [
        \'slug\'                      => \'coaches\',
        \'name_plural\'               => \'Coaches\',
        \'name_singular\'             => \'Coach\',
        \'taxonomy_slug\'             => \'coaches_category\',
        \'taxonomy_rewrite\'          => \'coaches-category\',
        \'taxonomy_name_plural\'      => \'Coach Categories\',
        \'taxonomy_name_singular\'    => \'Coach Category\'
    ],


public static function WhoHTML() {

    $args = array(
        \'post_type\' => \'coaches\',
        \'posts_per_page\' => \'4\',
        \'order\' => \'DESC\',
        \'orderby\' => \'date\',
        \'tax_query\' => array(
            array(
                  \'taxonomy\' => \'coaches_category\',
                  \'field\' => \'slug\',
                  \'terms\' => \'peers\'
            )
         )
    );

    $coaches = new WP_Query($args);

    foreach ($coaches->posts as $coach) {

        $image = get_the_post_thumbnail_url($coach->ID);

        $html .= "<div class=\'single-coach\'>
            <img class=\'coach-image img-responsive\' src=\'$pluginDirectory/assets/images/square.png\' />
            <p class=\'coach-name\'>{$coach->post_title}</p>
        </div>";
    }

    echo $html;
    return;
}

1 个回复
SO网友:Max Yudin
<?php
// get the WP_Query object
$query = new WP_Query($args);

// Missed essential point:
// loop through the WP_Query object
// to get the array of distinct posts objects
$coaches = $query->posts;

// each $coach is distinct post object now
foreach( $coaches as $coach ) {
    // echo $coach->ID;
}
结束

相关推荐

WordPress如何以正确的方式覆盖link-template.php中的邻近_POSTS_REL_LINK_WP_HEAD()函数

我对WordPress开发很陌生。我正在使用WordPress 4.9.2。我目前正在研究以下问题。我有几个博客类别,它们彼此没有关系。例如“我们的服务”和“新闻”。现在我想确保每个帖子页面上的上一页/下一页链接只指向同一类别中的上一页/下一页。这应该适用于页面上的链接和head部分中的rel链接。我已经想好了,我只需要将标志$in\\u same\\u term设置为true。对于文章末尾的链接,我通过添加正确的参数解决了主题中已经存在的问题。只有head部分中的rel链接不是这样工作的。Needed