简码不再显示自定义帖子类型

时间:2018-11-21 16:32:05

标签: javascript php jquery wordpress slick.js

我创建了一个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 个答案:

答案 0 :(得分:0)

我能够找到我的主要错误。在第二个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'
    );