wordpress获取带有特色图片网址的自定义帖子类型类别

时间:2016-01-11 05:58:57

标签: wordpress

我的代码是

$args='job_listing_category';
$recent_posts = get_terms ( $args );
print_r($recent_posts);
if ( $recent_posts ) {
    $recent_listing = array();
    foreach ( $recent_posts as $value ) {
       echo $post_thumbnail_id = get_post_thumbnail_id( $value['ID'], 'medium' );
        $feat_image = wp_get_attachment_image_src( $post_thumbnail_id );
        $feat_ext = explode(".", (string) $feat_image[0]);
        $feat_name = explode("-150x150", (string) $feat_image[0]);
        $feat_ext = end($feat_ext);
        $feat_image = $feat_name[0] . '.' . $feat_ext;

        $value['image'] = $feat_image;

           $result[] = $value;

          //$recent_listing[] = $category;
    }
} 
print_r($result);

但没有工作,我没有获得自定义帖子类型类别的图片。

1 个答案:

答案 0 :(得分:0)

更改您的代码,如下所示。您必须在参数中传递自定义帖子类型。

$taxonomies = array( 
    'your_custom_post_type',
    'job_listing_category',
);
$args = array(
  'orderby'                => 'name',
  'order'                  => 'ASC',
);
$recent_posts = get_terms($taxonomies, $args);
print_r($recent_posts);
if ( $recent_posts ) {
    $recent_listing = array();
    foreach ( $recent_posts as $value ) {
       echo $post_thumbnail_id = get_post_thumbnail_id( $value['ID'], 'medium' );
        $feat_image = wp_get_attachment_image_src( $post_thumbnail_id );
        $feat_ext = explode(".", (string) $feat_image[0]);
        $feat_name = explode("-150x150", (string) $feat_image[0]);
        $feat_ext = end($feat_ext);
        $feat_image = $feat_name[0] . '.' . $feat_ext;

        $value['image'] = $feat_image;

        $result[] = $value;
    }
} 
print_r($result);

有关详情:https://codex.wordpress.org/Function_Reference/get_terms