最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Custom post type Shortcodes wordpress

programmeradmin3浏览0评论

I want to Add slider shortcodes. My all slider fields adds properly in shortcode, but the read more button and button link is not displaying. Can you please solve my problem? This my slider code:

<section id="firstsection">
    <div class="container-fluid">
        <div class="col-xs-12 firstsection-maindiv">
            <?php 
                $slide = array( 'post_type' => 'slider' ,);
                $slider_query = new WP_Query( $slide );
            ?>
            <?php 
                if( have_posts() ) : while($slider_query->have_posts() ) : $slider_query->the_post();
            ?>
            <div id="explore-section" class="owl-carousel owl-theme">
                <div class="item">
                    <div class="col-xs-12  firstsection-innerdiv">
                        <div class="col-lg-6 col-md-6 col-xs-6 col-sm-6 firstsection-inner-left-div">
                            <p class="oneplace"><?php echo get_the_title(); ?></p>
                            <p class="slider-content"><?php echo get_the_content(); ?></p>
                            <?php $buttonname = get_post_meta($post->ID, "wp_producer_name" , true) ?>
                            <?php $buttonlink = get_post_meta($post->ID, "wp_button_link" , true) ?>
                            <button class="slider-btn" type="button"><a href="<?php echo $buttonlink ; ?>"> <?php echo $buttonname ; ?></a></button>
                        </div>
                        <div class="col-lg-6 col-md-6 col-xs-6 col-sm-6   firstsection-inner-right-div">
                     <?php echo get_the_post_thumbnail();  ?>
                        </div>
                    </div>
                </div>
  </div>
            <?php endwhile; endif; ?>
        </div>
    </div>
</section>

And this is my shortcodes code:

// Add Shortcode
add_shortcode( 'valute-slider-shortcode', 'display_custom_post_type' );
function display_custom_post_type(){
    $args = array(
        'post_type' => 'Slider',
        'post_status' => 'publish'
    );

    $string = '';
    $query = new WP_Query( $args );
    if( $query->have_posts() ){
        $string .= '<section id="firstsection">';
        while( $query->have_posts() ){
            $query->the_post();


             $buttonname = get_post_meta($post->ID, "wp_producer_name" , true);
           
            if( !empty($buttonname) ): endif;  
         
            $string .= '<div class="container-fluid">'  . 
                            '<div class="col-xs-12 firstsection-maindiv">' .
                            '<div id="explore-section" class="owl-carousel owl-theme">' . 
                                '<div class="item">' . 
                                    '<div class="col-xs-12  firstsection-innerdiv">' .
                                    ' <div class="col-lg-6 col-md-6 col-xs-6 col-sm-6 firstsection-inner-left-div">' .
                                        ' <p class="oneplace">'  .get_the_title() . '</p>'.
                                        ' <p class="slider-content">'  .get_the_content()  . '</p>'.
                                           ' <button class="slider-btn" type="button">' . $buttonname . '</button>'.
                                    '</div>' . ' <div class="col-lg-6 col-md-6 col-xs-6 col-sm-6   firstsection-inner-right-div">' . get_the_post_thumbnail() . '</div>' .
                                    '</div>' . 
                                '</div>' .
                            '</div>' .
                         '</div>';
        }
        $string .= '</section>';
    }
    wp_reset_postdata();
    return $string;

I want to Add slider shortcodes. My all slider fields adds properly in shortcode, but the read more button and button link is not displaying. Can you please solve my problem? This my slider code:

<section id="firstsection">
    <div class="container-fluid">
        <div class="col-xs-12 firstsection-maindiv">
            <?php 
                $slide = array( 'post_type' => 'slider' ,);
                $slider_query = new WP_Query( $slide );
            ?>
            <?php 
                if( have_posts() ) : while($slider_query->have_posts() ) : $slider_query->the_post();
            ?>
            <div id="explore-section" class="owl-carousel owl-theme">
                <div class="item">
                    <div class="col-xs-12  firstsection-innerdiv">
                        <div class="col-lg-6 col-md-6 col-xs-6 col-sm-6 firstsection-inner-left-div">
                            <p class="oneplace"><?php echo get_the_title(); ?></p>
                            <p class="slider-content"><?php echo get_the_content(); ?></p>
                            <?php $buttonname = get_post_meta($post->ID, "wp_producer_name" , true) ?>
                            <?php $buttonlink = get_post_meta($post->ID, "wp_button_link" , true) ?>
                            <button class="slider-btn" type="button"><a href="<?php echo $buttonlink ; ?>"> <?php echo $buttonname ; ?></a></button>
                        </div>
                        <div class="col-lg-6 col-md-6 col-xs-6 col-sm-6   firstsection-inner-right-div">
                     <?php echo get_the_post_thumbnail();  ?>
                        </div>
                    </div>
                </div>
  </div>
            <?php endwhile; endif; ?>
        </div>
    </div>
</section>

And this is my shortcodes code:

// Add Shortcode
add_shortcode( 'valute-slider-shortcode', 'display_custom_post_type' );
function display_custom_post_type(){
    $args = array(
        'post_type' => 'Slider',
        'post_status' => 'publish'
    );

    $string = '';
    $query = new WP_Query( $args );
    if( $query->have_posts() ){
        $string .= '<section id="firstsection">';
        while( $query->have_posts() ){
            $query->the_post();


             $buttonname = get_post_meta($post->ID, "wp_producer_name" , true);
           
            if( !empty($buttonname) ): endif;  
         
            $string .= '<div class="container-fluid">'  . 
                            '<div class="col-xs-12 firstsection-maindiv">' .
                            '<div id="explore-section" class="owl-carousel owl-theme">' . 
                                '<div class="item">' . 
                                    '<div class="col-xs-12  firstsection-innerdiv">' .
                                    ' <div class="col-lg-6 col-md-6 col-xs-6 col-sm-6 firstsection-inner-left-div">' .
                                        ' <p class="oneplace">'  .get_the_title() . '</p>'.
                                        ' <p class="slider-content">'  .get_the_content()  . '</p>'.
                                           ' <button class="slider-btn" type="button">' . $buttonname . '</button>'.
                                    '</div>' . ' <div class="col-lg-6 col-md-6 col-xs-6 col-sm-6   firstsection-inner-right-div">' . get_the_post_thumbnail() . '</div>' .
                                    '</div>' . 
                                '</div>' .
                            '</div>' .
                         '</div>';
        }
        $string .= '</section>';
    }
    wp_reset_postdata();
    return $string;
Share Improve this question edited Aug 17, 2020 at 8:55 Tom J Nowell 61k7 gold badges79 silver badges148 bronze badges asked Aug 17, 2020 at 8:32 RubyRuby 36 bronze badges 1
  • Hi sir have you any idea o solve my problem? – Ruby Commented Aug 17, 2020 at 9:26
Add a comment  | 

2 Answers 2

Reset to default 1

Use get_the_ID() instead of $post->ID

This will fix your problem

You're using this to get the post ID:

             $buttonname = get_post_meta($post->ID, "wp_producer_name" , true);

But what is $post? PHP has never seen this before in this scope, so it resolves to an empty value, undefined/null/0. In order to use global variables, you have to declare them before using them.

So there are 2 alternatives:

  • global $post;
  • get_the_ID()
发布评论

评论列表(0)

  1. 暂无评论