自定义查询-获取术语不起作用

时间:2015-02-17 作者:Benoît Vigouroux

我想在幻灯片中显示我的客户作品这是我用来显示这些作品的查询。我想获得每个自定义类型记录的分类术语“idm\\u real\\u tax”。

$query = new WP_Query($criterias);
if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        $posts[] = array(
            "id" => $query->post->ID,
            "permalink" => get_permalink($query->post->ID),
            "title" => $query->post->post_title,
            "excerpt" => $query->post->post_excerpt,
            "thumbnail" => get_the_post_thumbnail($query->post->ID , "carrousel-realisation"),
            "pager_thumbnail" => get_the_post_thumbnail($query->post->ID , "thumbnail", array( \'class\' => \'img-thumbnail img-responsive\' )), 
            // "term_single" => get_the_terms( $query->$post->id, "idm_real_tax" ),
        );
    }
问题是这一行,我评论道:

"term_single" => get_the_terms( $query->$post->id, "idm_real_tax" ),

1 个回复
SO网友:David Gard

我不知道你到底想要什么\'term_single\' 格式,但如果不完全符合您的要求,您可以根据需要修改以下内容。

我还建议不要用这个名字$posts 对于您的阵列,因为我相信WP有一个同名的全局,所以它可能会导致不受欢迎的有趣性。

最后,当您处于循环中时,您可以使用简单的函数来获取所需的所有信息(ID、名称等)-check out the Codex.

$query = new WP_Query($criterias);
if($query->have_posts()) : while ($query->have_posts()) : $query->the_post();

        /** Get all terms (as objects) from the \'idm_real_tax\' taxonomy that are associated with this Post */
        $terms = get_the_terms(get_the_ID(), \'idm_real_tax\');

        /** Check that \'$terms\' is not empty */
        if($terms && !is_wp_error($terms)) :    // It\'s not...

            $term_names = array();  // Pre-stage to avoid errors

            /** Loop through each term object and add just the name to the \'$term_names\' array */
            foreach($terms as $term) :
                $term_names[] = $term->name;
            endif

            /** Join all of the term names together to make a comman seperated string of term names */
            $term_names_string = (!empty($term_names)) ? join(", ", $term_names) : \'\';

        endif;

        $my_posts[] = array(
            \'id\'                => get_the_ID(),
            \'permalink\'         => get_permalink(),
            \'title\'             => get_the_title(),
            \'excerpt\'           => get_the_excerpt(),
            \'thumbnail\'         => get_the_post_thumbnail(get_the_ID(), \'carrousel-realisation\'),
            \'pager_thumbnail\'   => get_the_post_thumbnail(get_the_ID(), \'thumbnail\', array(\'class\' => \'img-thumbnail img-responsive\')), 
            \'term_single\'       => $term_names
        );

    endwhile;
endif;

结束

相关推荐

将选定列表值(分类)保存在wp中:wp_set_Object_Terms

在我的管理部分,我有其他字段(如价格或品牌-这是分类法)。编辑或创建新零件时,我会设置其他数据。价格节约没有任何问题,但从选择列表中保存值有些奇怪-它没有保存:wp_set_object_terms($post_id, $_POST[\'part_brand\'], \'brands\', true); 根据wp doc:此函数从分类法选择更新值。但对我来说,这不起作用。您可以在此处看到的所有代码:http://pastebin.com/N4gZL3uN如何在wp中保存选择列表(分类法)中的值?