您知道一些简单的方法吗?如何通过ajax (使用回调或类似的方法)向Woocommerce展示最新产品?现在我有了一个短代码:
[recent_products per_page="12" columns="3"]我想创建一个按钮,这将允许我显示下12个产品在同一页。
发布于 2015-07-16 19:52:23
一个简单的例子是,返回recent_products短代码输出的是基本的wordpress ajax调用。将带有#wc_recent_products id的链接放在页面的某个位置,ajax响应将替换div#main的内容(如果您想将内容放到其他地方,请更改该行)。
<?php
add_action( 'wp_ajax_wc_recent_products', function()
{
if ( isset( $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'wc_recent_products' ) )
{
echo do_shortcode( '[recent_products per_page="12" columns="3"]' );
}
exit();
});
add_action( 'wp_head', function() {
?>
<script type="text/javascript" >
jQuery(function ($) {
$( '#wc_recent_products' ).on( 'click', function() {
$.post(
'<?php echo admin_url( 'admin-ajax.php' ); ?>',
{ action: 'wc_recent_products', nonce: '<?php echo wp_create_nonce( 'wc_recent_products' ); ?>' },
function( response ) {
$( '#main' ).html( response );
}
);
return false;
});
});
</script>
<?php
});
?>https://stackoverflow.com/questions/31460566
复制相似问题