Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress Woocommerce自定义商店页面,带infinte加载_Wordpress_Woocommerce_Hook Woocommerce_Woothemes - Fatal编程技术网

Wordpress Woocommerce自定义商店页面,带infinte加载

Wordpress Woocommerce自定义商店页面,带infinte加载,wordpress,woocommerce,hook-woocommerce,woothemes,Wordpress,Woocommerce,Hook Woocommerce,Woothemes,您好,目前我正在创建一个woocommerce自定义商店页面模板 为此,我在我的主题页面中创建了一个名为custom shop的新模板&我创建了一个名为my shop的新页面,并指定了custom shop模板 我有三大类,汽车、公共汽车、船。现在,我将显示每个类别及其说明和产品详细信息 因此,我在我的cutom-shop模板(custom-shop.php)中编写了以下代码 custom-shop.php /* 模板名称:定制商店 */ 代码块 汽车 车辆说明 事实上,如果你想自己做,我建议你

您好,目前我正在创建一个woocommerce自定义商店页面模板

为此,我在我的主题页面中创建了一个名为custom shop的新模板&我创建了一个名为my shop的新页面,并指定了custom shop模板

我有三大类,汽车、公共汽车、船。现在,我将显示每个类别及其说明和产品详细信息

因此,我在我的
cutom-shop
模板(
custom-shop.php
)中编写了以下代码

custom-shop.php
/*
模板名称:定制商店
*/
代码块
汽车

车辆说明


事实上,如果你想自己做,我建议你遵循->这将比任何巨大的答复在这里更有帮助。 这家伙在你需要使用的所有ajax过程中都会用到。你也可以解析他的一段代码,然后把它全部完成很快,您只需要ajax。更明确->您可以在本视频中看到

我将在这里粘贴的代码结果的指南绕过所有的小程序,以便更好地理解

首先,他创建了一个模板来输出如下所示的内容

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header text-center">

    <?php the_title( '<h1 class="entry-title"><a href="'. esc_url( get_permalink() ) .'" rel="bookmark">', '</a></h1>'); ?>

    <div class="entry-meta">
        <?php echo sunset_posted_meta(); ?>
    </div>

</header>

<div class="entry-content">

    <?php if( sunset_get_attachment() ): ?>

        <a class="standard-featured-link" href="<?php the_permalink(); ?>">
            <div class="standard-featured background-image" style="background-image: url(<?php echo sunset_get_attachment(); ?>);"></div>
        </a>

    <?php endif; ?>

    <div class="entry-excerpt">
        <?php the_excerpt(); ?>
    </div>

    <div class="button-container text-center">
        <a href="<?php the_permalink(); ?>" class="btn btn-sunset"><?php _e( 'Read More' ); ?></a>
    </div>

</div><!-- .entry-content -->

<footer class="entry-footer">
    <?php echo sunset_posted_footer(); ?>
</footer>

</article>
”);?>
下一步是在wp中验证ajax函数

add_action( 'wp_ajax_nopriv_sunset_load_more', 'sunset_load_more' );
add_action( 'wp_ajax_sunset_load_more', 'sunset_load_more' );
function sunset_load_more() {

  $paged = $_POST["page"]+1;
  $prev = $_POST["prev"];
  $archive = $_POST["archive"];

  if( $prev == 1 && $_POST["page"] != 1 ){
    $paged = $_POST["page"]-1;
}

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'paged' => $paged
);

if( $archive != '0' ){

    $archVal = explode( '/', $archive );

    $type = ( $archVal[1] == "category" ? "category_name" : $archVal[1] );

    $args[ $type ] = $archVal[2];

    $page_trail = '/' . $archVal[1] . '/' . $archVal[2] . '/';

} else {
    $page_trail = '/';
}

$query = new WP_Query( $args );

if( $query->have_posts() ):

    echo '<div class="page-limit" data-page="' . $page_trail . 'page/' . $paged . '">';

    while( $query->have_posts() ): $query->the_post();

        get_template_part( 'template-parts/content', get_post_format() );

    endwhile;

    echo '</div>';

else:

    echo 0;

endif;

wp_reset_postdata();

die();

}
function sunset_check_paged( $num = null ){

$output = '';

if( is_paged() ){ $output = 'page/' . get_query_var( 'paged' ); }

if( $num == 1 ){
    $paged = ( get_query_var( 'paged' ) == 0 ? 1 : get_query_var( 'paged' ) );
    return $paged;
} else {
    return $output;
}

}
add_action('wp_ajax_nopriv_sunset_load_more'、'sunset_load_more');
添加动作('wp\u ajax\u sunset\u load\u more','sunset\u load\u more');
函数sunset\u load\u more(){
$paged=$_POST[“page”]+1;
$prev=$_POST[“prev”];
$archive=$_POST[“archive”];
如果($prev==1&&$_POST[“page”]!=1){
$paged=$_POST[“page”]-1;
}
$args=数组(
“post_type”=>“post”,
“发布状态”=>“发布”,
“paged”=>paged美元
);
如果($archive!=“0”){
$archVal=explode(“/”,$archive);
$type=($archVal[1]=“类别”?“类别名称”:$archVal[1]);
$args[$type]=$archVal[2];
$page_trail='/'.$archVal[1]./'.$archVal[2]./';
}否则{
$page_trail='/';
}
$query=新的WP\u查询($args);
如果($query->have_posts()):
回声';
而($query->have_posts()):$query->the_post();
获取模板部分(“模板部分/内容”,获取发布格式());
结束时;
回声';
其他:
回声0;
endif;
wp_reset_postdata();
模具();
}
函数日落检查分页($num=null){
$output='';
if(is_paged()){$output='page/'.get_query_var('paged');}
如果($num==1){
$paged=(get_query_var('paged')==0?1:get_query_var('paged');
返回$paged;
}否则{
返回$output;
}
}
最后一步是AJAX函数本身

/* Ajax functions */
$(document).on('click','.sunset-load-more:not(.loading)', function(){

    var that = $(this);
    var page = $(this).data('page');
    var newPage = page+1;
    var ajaxurl = that.data('url');
    var prev = that.data('prev');
    var archive = that.data('archive');

    if( typeof prev === 'undefined' ){
        prev = 0;
    }

    if( typeof archive === 'undefined' ){
        archive = 0;
    }

    that.addClass('loading').find('.text').slideUp(320);
    that.find('.sunset-icon').addClass('spin');

    $.ajax({

        url : ajaxurl,
        type : 'post',
        data : {

            page : page,
            prev : prev,
            archive : archive,
            action: 'sunset_load_more'

        },
        error : function( response ){
            console.log(response);
        },
        success : function( response ){

            if( response == 0 ){

                $('.sunset-posts-container').append( '<div class="text-center"><h3>You reached the end of the line!</h3><p>No more posts to load.</p></div>' );
                that.slideUp(320);

            } else {

                setTimeout(function(){

                    if( prev == 1 ){
                        $('.sunset-posts-container').prepend( response );
                        newPage = page-1;
                    } else {
                        $('.sunset-posts-container').append( response );
                    }

                    if( newPage == 1 ){

                        that.slideUp(320);

                    } else {

                        that.data('page', newPage);

                        that.removeClass('loading').find('.text').slideDown(320);
                        that.find('.sunset-icon').removeClass('spin');

                    }

                    revealPosts();

                }, 1000);

            }


        }

    });

});

/* scroll function */
$(window).scroll( function(){

    var scroll = $(window).scrollTop();

    if( Math.abs( scroll - last_scroll ) > $(window).height()*0.1 ) {
        last_scroll = scroll;

        $('.page-limit').each(function( index ){

            if( isVisible( $(this) ) ){

                history.replaceState( null, null, $(this).attr("data-page") );
                return(false);

            }

        });

    }

});

/* helper functions */
function revealPosts(){

    var posts = $('article:not(.reveal)');
    var i = 0;

    setInterval(function(){

        if( i >= posts.length ) return false;

        var el = posts[i];
        $(el).addClass('reveal').find('.sunset-carousel-thumb').carousel();

        i++

    }, 200);

}

function isVisible( element ){

    var scroll_pos = $(window).scrollTop();
    var window_height = $(window).height();
    var el_top = $(element).offset().top;
    var el_height = $(element).height();
    var el_bottom = el_top + el_height;
    return ( ( el_bottom - el_height*0.25 > scroll_pos ) && ( el_top < ( scroll_pos+0.5*window_height ) ) );

}
});
/*Ajax函数*/
$(文档).on('click',')。日落加载更多:不是(.loading'),函数(){
var,该值=$(此值);
var page=$(this.data('page');
var newPage=page+1;
var ajaxurl=that.data('url');
var prev=该.data('prev');
var archive=that.data('archive');
if(typeof prev==‘未定义’){
prev=0;
}
如果(存档类型==='undefined'){
存档=0;
}
addClass('loading').find('text').slideUp(320);
find('.sunset icon').addClass('spin');
$.ajax({
url:ajaxurl,
键入:“post”,
数据:{
第页:第页,
prev:prev,
档案馆:档案馆,
行动:“日落\装载\更多”
},
错误:函数(响应){
控制台日志(响应);
},
成功:功能(响应){
如果(响应==0){
$('.sunset posts container').append('您到达了行的末尾!没有更多要加载的帖子。

'); 那.slideUp(320),; }否则{ setTimeout(函数(){ 如果(上一个==1){ $('.sunset posts container')。前置(响应); 新页=第1页; }否则{ $('.sunset posts container')。追加(响应); } if(newPage==1){ 那.slideUp(320),; }否则{ 数据('第页',新页); .removeClass('loading').find('.text').slideDown(320); .find('.sunset icon').removeClass('spin'); } 重新登顶(); }, 1000); } } }); }); /*滚动功能*/ $(窗口)。滚动(函数(){ var scroll=$(窗口).scrollTop(); if(Math.abs(滚动-最后一次滚动)>$(window.height()*0.1){ 最后滚动=滚动; $('.page limit')。每个(函数(索引){ 如果(可见($(此))){ replaceState(null,null,$(this.attr(“数据页”)); 返回(假); } }); } }); /*辅助函数*/ 功能性前哨(){ var posts=$('article:not(.leaver'); var i=0; setInterval(函数(){ 如果(i>=posts.length)返回false; var el=员额[i]; $(el).addClass('reveal').find('sunset carousel thumb').carousel(); 我++ }, 200); } 函数是可见的(元素){ var scroll_pos=$(窗口).scrollTop(); var window_height=$(window.height(); var el_top=$(元素).offset().top; var el_height=$(元素).height(); 变量el_底部=el_顶部+el_高度; 返回((el_底部-el_高度*0.25>滚动_位置)和&(el_顶部<(滚动_位置+0.5*窗口高度)); } });
我建议您遵循alecadd创建的指南,因为它将更容易理解流程并适合您的目的。希望现在足够了


干杯。

你不能合并一些无限滚动插件,比如说YITH无限滚动插件之类的吗?我想你可以根据你的要求定制。
add_action( 'wp_ajax_nopriv_sunset_load_more', 'sunset_load_more' );
add_action( 'wp_ajax_sunset_load_more', 'sunset_load_more' );
function sunset_load_more() {

  $paged = $_POST["page"]+1;
  $prev = $_POST["prev"];
  $archive = $_POST["archive"];

  if( $prev == 1 && $_POST["page"] != 1 ){
    $paged = $_POST["page"]-1;
}

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'paged' => $paged
);

if( $archive != '0' ){

    $archVal = explode( '/', $archive );

    $type = ( $archVal[1] == "category" ? "category_name" : $archVal[1] );

    $args[ $type ] = $archVal[2];

    $page_trail = '/' . $archVal[1] . '/' . $archVal[2] . '/';

} else {
    $page_trail = '/';
}

$query = new WP_Query( $args );

if( $query->have_posts() ):

    echo '<div class="page-limit" data-page="' . $page_trail . 'page/' . $paged . '">';

    while( $query->have_posts() ): $query->the_post();

        get_template_part( 'template-parts/content', get_post_format() );

    endwhile;

    echo '</div>';

else:

    echo 0;

endif;

wp_reset_postdata();

die();

}
function sunset_check_paged( $num = null ){

$output = '';

if( is_paged() ){ $output = 'page/' . get_query_var( 'paged' ); }

if( $num == 1 ){
    $paged = ( get_query_var( 'paged' ) == 0 ? 1 : get_query_var( 'paged' ) );
    return $paged;
} else {
    return $output;
}

}
/* Ajax functions */
$(document).on('click','.sunset-load-more:not(.loading)', function(){

    var that = $(this);
    var page = $(this).data('page');
    var newPage = page+1;
    var ajaxurl = that.data('url');
    var prev = that.data('prev');
    var archive = that.data('archive');

    if( typeof prev === 'undefined' ){
        prev = 0;
    }

    if( typeof archive === 'undefined' ){
        archive = 0;
    }

    that.addClass('loading').find('.text').slideUp(320);
    that.find('.sunset-icon').addClass('spin');

    $.ajax({

        url : ajaxurl,
        type : 'post',
        data : {

            page : page,
            prev : prev,
            archive : archive,
            action: 'sunset_load_more'

        },
        error : function( response ){
            console.log(response);
        },
        success : function( response ){

            if( response == 0 ){

                $('.sunset-posts-container').append( '<div class="text-center"><h3>You reached the end of the line!</h3><p>No more posts to load.</p></div>' );
                that.slideUp(320);

            } else {

                setTimeout(function(){

                    if( prev == 1 ){
                        $('.sunset-posts-container').prepend( response );
                        newPage = page-1;
                    } else {
                        $('.sunset-posts-container').append( response );
                    }

                    if( newPage == 1 ){

                        that.slideUp(320);

                    } else {

                        that.data('page', newPage);

                        that.removeClass('loading').find('.text').slideDown(320);
                        that.find('.sunset-icon').removeClass('spin');

                    }

                    revealPosts();

                }, 1000);

            }


        }

    });

});

/* scroll function */
$(window).scroll( function(){

    var scroll = $(window).scrollTop();

    if( Math.abs( scroll - last_scroll ) > $(window).height()*0.1 ) {
        last_scroll = scroll;

        $('.page-limit').each(function( index ){

            if( isVisible( $(this) ) ){

                history.replaceState( null, null, $(this).attr("data-page") );
                return(false);

            }

        });

    }

});

/* helper functions */
function revealPosts(){

    var posts = $('article:not(.reveal)');
    var i = 0;

    setInterval(function(){

        if( i >= posts.length ) return false;

        var el = posts[i];
        $(el).addClass('reveal').find('.sunset-carousel-thumb').carousel();

        i++

    }, 200);

}

function isVisible( element ){

    var scroll_pos = $(window).scrollTop();
    var window_height = $(window).height();
    var el_top = $(element).offset().top;
    var el_height = $(element).height();
    var el_bottom = el_top + el_height;
    return ( ( el_bottom - el_height*0.25 > scroll_pos ) && ( el_top < ( scroll_pos+0.5*window_height ) ) );

}
});