自定义POST类型快捷代码WordPress

时间:2020-08-17 作者:Ruby

我想添加滑块短代码。“我的所有滑块”字段正确地添加到快捷码中,但未显示“阅读更多”按钮和按钮链接。你能解决我的问题吗?这是我的滑块代码:

<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>
这是我的短代码:

// 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;

2 个回复
最合适的回答,由SO网友:Jagruti Rakholiya 整理而成

使用get_the_ID() 而不是$post->ID

这将解决您的问题

SO网友:Tom J Nowell

您正在使用此获取帖子ID:

             $buttonname = get_post_meta($post->ID, "wp_producer_name" , true);
但是什么是$post? PHP以前从未在这个范围内看到过这一点,因此它解析为空值,未定义/空/0。为了使用全局变量,您必须在使用它们之前声明它们。

因此,有两种选择:

  • global $post;
  • get_the_ID()

相关推荐

What does this shortcode do?

function my_shortcode($atts, $content = null){ extract(shortcode_atts(array(\"type\" => \"warrning\"), $atts)); return \".$content.\"; } add_shortcode(\"warrning_dialog\", \"my_shortcode\"); 我知道它会创建短代码[warning\\u dialo