Custom post type shortcode

时间:2020-11-11 作者:Ruby

我创建了一个自定义的帖子类型快捷码作为滑块。我的滑块图像在背景图像中显示为特色图像。但我的幻灯片中没有显示特色图像。

这是我的代码:

<?php
add_shortcode( \'slickslider\', \'slider_shortcodes_post_type\' );

function slider_shortcodes_post_type(){
    $args = array(
        \'post_type\' => \'slider\',
        \'post_status\' => \'publish\'
    );

    $string = \'\';
    $h_query = new WP_Query( $args );
    if( $h_query->have_posts() ){
        $string .= \'<div class="home-slides owl-carousel owl-theme">\';
        while( $h_query->have_posts() ){
            $h_query->the_post();

            // slider buttons
            // first button  text
            $slider_buttons1txt =  $sliderbtntxt1 = get_post_meta(get_the_ID(), \'slider_button1_text\', true);
            if( !empty($slider_buttons1txt) ): endif;
            
            // first button text link
            $slider_buttons1link =   $sliderbtntxtlink = get_post_meta(get_the_ID(), \'slider_button1_text_link\', true);
            if( !empty($slider_buttons1link) ): endif;
            
            // second button  text
            $slider_buttons2txt =  $sliderbtntxt2 = get_post_meta(get_the_ID(), \'slider_button2_text\', true);
            if( !empty($slider_buttons2txt) ): endif;

            // first button text link
            $slider_buttons2link = $sliderbtn2txtlink = get_post_meta(get_the_ID(), \'slider_button2_text_link\', true);
            if( !empty($slider_buttons2link) ): endif;

             // background images 
            $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'full\' );
              
            $string .=\' <div class="main-slider-item" style="background-image: url(<?php echo $backgroundImg[0]; ?>); background-size: cover; background-repeat: no-repeat;">\' . 
            \' <div class="d-table">\' . 
                \' <div class="d-table-cell">\' . 
                    \'<div class="container">\'.
                        \' <div class="main-slider-content">\' .
                        \'<h1>\' . get_the_title() . \'</h1>\' .
                        get_the_content() .
                        \' <div class="slider-btn">\'.
                        \'<a href="\'.$slider_buttons1link.\'" class="default-btn">\' .  $slider_buttons1txt . \'</a>\' .
                        \'<a href="\'.$slider_buttons2link.\'" class="default-btn-two">\' .  $slider_buttons2txt . \'</a>\' .
                        \'</div>\'.
                        \' </div>\' . 
                    \'</div>\' .
                \' </div>\'. 
            \' </div>\' . 
            \'</div>\';
            
        }
        $string .= \'</div>\';
    }
    wp_reset_postdata();
    return $string;
}

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

Its done :

 $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'full\' );
              
 $string .=\' <div class="main-slider-item" style="background-image: url(\'.$backgroundImg[0].\'); background-size: cover; background-repeat: no-repeat;">\' .

相关推荐

重写WordPress中$ATTS SHORTCODE_ATTS内的类

我有一个插件,我需要调整一个函数。特别是\'class\' => \'\'. 这是插件中的代码:function message_to( $atts, $content = null, $tag = \'\' ) { $atts = shortcode_atts( array( \'to\' => \'{current-post-author}\', \'subject\' => \'{current-po