0

So, I'm trying to pull the featured image of posts in via a shortcode. But I want to replace the inline style of setting it as a background image and have it returned as an img tag instead.

<?php

// get the more like this posts
add_shortcode('more-like-this', 'more_like_this');
function more_like_this($atts)
{
    extract(shortcode_atts(array(
        'count' => 3
    ), $atts));

    // create a variable to store our content
    $content = '';

    // if we have no trending article fall back to latest
    $args = array(
        'posts_per_page' => $count,
        'post_status' => 'publish',
        'orderby' => 'publish_date',
        'order' => 'DESC'
    );

    // get our posts
    $posts = new WP_Query($args);

    // if we have posts
    if ($posts->have_posts()) {
        // while we have posts
        while ($posts->have_posts()) {
            $posts->the_post();

            $content .= '<article class="more-like-this-article clearfix">';

            $content .= '<div class="left-side" style="background-image:url(\'' . get_the_post_thumbnail_url() . '\')">';
            $content .= '<a href="' . get_permalink() . '"></a>';
            $content .= '</div>';

            $content .= '<div class="right-side">';

            $content .= '<div class="article-meta">';

            $categories = get_the_category();
            $category_content = '';

            if (!empty($categories)) {
                foreach ($categories as $category) {
                    $category_content .= '<a href="' . esc_url(get_category_link($category->term_id)) . '">';

                    if (get_field('icon_black', $category)) {
                        $category_content .= '<img class="category-icon" src="' . get_field('icon_black', $category) . '">';
                    }

                    $category_content .= esc_html($category->name);
                    $category_content .= '</a>, ';
                }
            }

            $content .= substr($category_content, 0, -2);
            $content .= ' | ' . reading_time(get_the_ID());

            $content .= '</div>';

            $content .= '<a href="' . get_permalink() . '"><h2>' . get_the_title() . '</h2></a>';

            $content .= '</div>';

            $content .= '</article>';
        }

    }

    // return our content
    return $content;
}

1 Answer 1

0

Replace "get_the_post_thumbnail_url()" with below

wp_get_attachment_image_src( get_post_thumbnail_id( $posts->ID ),"thumbnail")[0];

OR

get_the_post_thumbnail_url(get_the_ID(),"thumbnail");
1
  • So, I made the change, removing the "style='background-image..' and putting the new line a little further down and it's still not working. As I said, I'm trying to not have it as a background image. This is what I have now: $content .= '<div class="left-side">'; $content .= '<a href="' . get_permalink() . '"></a>'; $content .= wp_get_attachment_image_src( get_post_thumbnail_id( $posts->ID ),"thumbnail")[0]; $content .= '</div>';
    – ladyc
    Commented Dec 6, 2020 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.