【问题标题】:Shortcode no longer displaying custom post type简码不再显示自定义帖子类型
【发布时间】:2018-11-21 16:32:04
【问题描述】:

我创建了一个testimonial 帖子类型来显示我在我的网站上输入的评论。然后我创建了两个函数来创建一个testimonial block,它将在某些页面上使用简码显示这些评论。起初一切都很好,但随着我添加越来越多的推荐,他们停止显示,我无法弄清楚为什么。有人对这个有经验么。请参阅下面我的functions.php 中包含的两个函数:

//Testimonial widget
function sal_testimonials_widget($atts){

    //shortcode options
    extract(
        shortcode_atts(
            array(
                'section_title' => 'Testimonials',
                'custom_location_filter' => '',

            ), $atts
        )
    );

    //post data
    $filter_ids = '';
    $post_type = '';

    //initial variables
    $location_filter = '';


    $custom_filters_added = ($custom_location_filter !== '' ? true : false);

    if($custom_filters_added){
        if($custom_location_filter !== ''){
            $filter_ids = $custom_location_filter;
            $post_type = 'Locations';
        }

    }else{
        global $post;
        $post_type = $post->post_type;
        $filter_ids= $post->ID;
    }

    $output = '<div class="testimonials-block-container">';
    $output.= sal_testimonials_block($post_type, true, $filter_ids, $section_title);
    $output.= '</div>';
wp_reset_postdata();
    return $output;
}

add_shortcode('testimonials_widget', 'sal_testimonials_widget');

////Testimonials block
function sal_testimonials_block($testimonial_type, $enabled, $id, $section_title){
      $testimonial_args = array( 
        'posts_per_page' => $init_amount_to_display, 
        'offset'=> 0, 
        'post_type' => 'testimonials', 
        'post_status' => 'publish',
        'orderby' => 'date',
        'order' => 'DESC'
    );

    $testimonials_id_array = array();

    $testimonials = get_posts( $testimonial_args );
    $current_post_id = get_the_ID();
    $current_post_type=get_post_type($current_post_ID);
    $output.='<div class="testimonial-slideshow">';
    if($testimonials):
        foreach ($testimonials as $item) {

            $location = get_field('location_associated_with_testimonial', $item );  
            $city= get_field('city', $location);
            $state= get_field('state', $location);
            $author=get_field('testimonial_authors_name', $item);
            $universal=get_field('is_universal', $item);
            $date=get_field('testimonial_date', $item);
            $customer_city=get_field('customer_city', $item);

            if($location==$current_post_id){                
                $output.='<div class="testimonial-slide">';             
                $output.= '<div class="testimonial-item">';
                $output.= '<q class="testimonial">' . get_post_field('post_content', $item) . '</q>';           
                $output.= ( $author ? '<div class="testimonial-author">' . $author . '</div>' : '' );           

                $output.='<div class="testimonial-location">';
                if(strlen($customer_city)>1){$output.=$customer_city;}
                else{$output.= $city . ', ' . $state;}
                $output.='</div>';
                $output.= ( $date ? '<div class="testimonial-date">' . $date . '</div>' : '' );
                $output.= '</div>';
                $output.='</div>';                  
            }
            if($testimonial_type!='locations'&&$universal==true){               
                $output.='<div class="testimonial-slide">';             
                $output.= '<div class="testimonial-item">';
                $output.= '<q class="testimonial">' . get_post_field('post_content', $item) . '</q>';           
                $output.= ( $author ? '<div class="testimonial-author">' . $author . '</div>' : '' );
                $output.='<div class="testimonial-location">';
                if(strlen($customer_city)>1){$output.=$customer_city;}
                else{$output.= $city . ', ' . $state;}
                $output.='</div>';
                $output.= ( $date ? '<div class="testimonial-date">' . $date . '</div>' : '' );
                $output.= '</div>';
                $output.='</div>';                  
            }
            wp_reset_postdata();
        }
        $output.='</div>';   

    endif;

    return $output;



}

我还使用了一些 JavaScript (JQuery) 来制作一个滑块,以在单个页面上显示多个推荐。见下文:

/*
     * Location Single Page Testimonial Slideshow
     * Add slick slider to the testimonial section of the lcoation page
    */
    if ($(".testimonial-slideshow").length > 0) {
        $(".testimonial-slideshow").slick({
            dots: false,
            infinite: true,
            speed: 300,
            fade: false,
            cssEase: "ease",
            autoplay: true,
            autoplaySpeed: 8000,
            prevArrow:
                '<div class="slick-prev"><i class="fa fa-chevron-left"></i></div>,',
            nextArrow:
                '<div class="slick-next"><i class="fa fa-chevron-right"></i></div>',
            draggable: false,
            responsive: [
                {
                    breakpoint: 768,
                    settings: {
                        draggable: true
                    }
                }
            ]
        });
    }

我尝试在自定义帖子类型的多个区域刷新重写规则,但似乎没有效果。

【问题讨论】:

  • 它可能是其中一个推荐中的数据。检查服务器和开发者控制台上的错误消息。尝试减少推荐的数量并将其增加 1,直到找到打破它的推荐。
  • @aynber 您认为哪种类型的数据会破坏它?每个推荐都是手动单独创建的。
  • 这真的很难说。它可能是一个流氓字符或代码。如果它适用于一两个,但在一定数量或特定推荐后中断,通常是该推荐中的某些内容,您可能不得不逃避。
  • @aynber 也奇怪的是,当我添加新的推荐时。它最初会与简码一起显示,但最终会在刷新几页后从视图中消失。

标签: javascript php jquery wordpress slick.js


【解决方案1】:

我能够找到我的主要错误。在我的第二个 PHP 函数 sal_testimonials_block() 我有:

 $testimonial_args = array( 
        'posts_per_page' => $init_amount_to_display, 
        'offset'=> 0, 
        'post_type' => 'testimonials', 
        'post_status' => 'publish',
        'orderby' => 'date',
        'order' => 'DESC'
    );

我意识到我从未定义过$init_amount_to_display,我不确定它最初是如何工作的,但是我将上面的代码块更改为下面的代码,现在它似乎可以正常工作了。

$testimonial_args = $args = array(
    'posts_per_page'    => -1,
    'post_type'         => 'testimonials'
    );

【讨论】:

    猜你喜欢
    • 2017-03-12
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    相关资源
    最近更新 更多