用短码中的帖子缩略图替换背景图像

时间:2020-12-06 作者:ladyc

所以,我正试图通过一个快捷码来获取帖子的特色图片。但我想替换将其设置为背景图像的内联样式,并将其作为img标记返回。

<?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 个回复
SO网友:Muhammet DÜLGER

替换(&Q);获取\\u post\\u thumbnail\\u url();包含以下内容

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

get_the_post_thumbnail_url(get_the_ID(),"thumbnail");

相关推荐

使用admin-ajax.php发出的AJAX POST请求的状态400

从昨天开始,我就一直在处理这个问题,编写并重写代码来修复这个400 XRH错误。我找不到办法来修理它。在向您展示代码之前,下面是一些上下文:我正在尝试将可变产品添加到归档循环中。我已经成功地将变体选择表单带到了那里,我可以使用AJAX发送正确的数据。问题似乎与请求有关。这是我的代码:// The main php function function variation_add_to_cart_product(){ $product_id = apply_filt