WP_GET_ATTACH_URL未获取URL?

时间:2021-02-26 作者:Dean

下面的代码似乎根本没有返回图像url,chrome inspector表示源代码未知

  foreach ($subcategories as $subcategory) {
            $link = get_term_link( $subcategory->slug, $subcategory->taxonomy );
            $thumbnail_id = get_term_meta( $subcategory->term_id, \'thumbnail_id\', true );
            $image= wp_get_attachment_url( $thumbnail_id );
            echo \'<img src="\' . $image . \'" width="50" height="50" />\';
完整代码

// Adding Child Category List to Main Category Display Grid

add_action(\'woocommerce_after_subcategory\', \'woocommerce_subcats_from_parentcat_by_ID\', 20);

function woocommerce_subcats_from_parentcat_by_ID($category) {
    $parent_category_ID = $category->term_id;
    $args = array(
        \'hierarchical\' => 1,
        \'show_option_none\' => \'\',
        \'hide_empty\' => 0, // Set to 0 to show empty categories and 1 to hide them
        \'parent\' => $parent_category_ID,
        \'taxonomy\' => \'product_cat\'
    );
    $subcategories = get_categories($args);
    echo \'<ul class="woo_subcategory_list">\';
    foreach ($subcategories as $subcategory) {
        $link = get_term_link( $subcategory->slug, $subcategory->taxonomy );
        $thumbnail_id = get_term_meta( $subcategory->term_id, \'thumbnail_id\', true );
        $image= wp_get_attachment_url( $thumbnail_id );
        echo \'<img src="\' . $image . \'" width="50" height="50" />\';
    }
    echo \'</ul>\';
}

1 个回复
最合适的回答,由SO网友:Jignesh Patel 整理而成

然后检查如下:

$thumb_id = get_woocommerce_term_meta( $subcategory->term_id, \'thumbnail_id\', true );
$term_img = wp_get_attachment_url(  $thumb_id );
像这样检查

$meta = get_term_meta( $subcategory->term_id );
var_dump($meta);
$thumb_id = $meta[\'thumbnail_id\'][0];
$term_img = wp_get_attachment_url(  $thumb_id );
你也会这样

$thumbnail_id = get_term_meta( $subcategory->term_id, \'thumbnail_id\', true );
$image = wp_get_attachment_url( $thumbnail_id );

相关推荐

Bootstrap grid while loop

我用bootstrap制作了一个网格,我想在其中显示5个不同的帖子,但出于某种原因,它复制了网格,并且每个网格显示一个帖子。第一幅图显示了它如何开始将网格复制5次,而不是仅在这些列中显示5篇文章。第二张图片显示了我想如何工作。 <?php get_header(); ?> <main> <div class=\"container\"> <div class=\"row\"> <?php &