woocommerce:通过 Ajax 显示最近的产品
woocommerce: show recent products throught Ajax
有没有人知道如何通过 ajax 显示 Woocommerce 最新产品的简单方法(使用一些回调或类似的东西)?现在我有了这个简码:
[recent_products per_page="12" columns="3"]
我想创建一个按钮,允许我在同一页面上显示接下来的 12 个产品。
一个简单的例子,它是基本的 wordpress ajax 调用 returns recent_products
简码的输出。在页面某处放置一个带有 #wc_recent_products
id 的 link,ajax 响应将替换 div#main
的内容($( '#main' ).html( response );
,如果您想放置,请更改该行其他地方的内容)。
<?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
});
?>
有没有人知道如何通过 ajax 显示 Woocommerce 最新产品的简单方法(使用一些回调或类似的东西)?现在我有了这个简码:
[recent_products per_page="12" columns="3"]
我想创建一个按钮,允许我在同一页面上显示接下来的 12 个产品。
一个简单的例子,它是基本的 wordpress ajax 调用 returns recent_products
简码的输出。在页面某处放置一个带有 #wc_recent_products
id 的 link,ajax 响应将替换 div#main
的内容($( '#main' ).html( response );
,如果您想放置,请更改该行其他地方的内容)。
<?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
});
?>