我尝试了很多插件,但没有一个插件对我来说是完美的。我终于找到了一个解决方案,如何在没有插件的情况下将帖子添加到收藏夹中,它适用于帖子类型"张贴">,但我也需要woommerce产品。
我试图将$args从'post_type'=>'更改为post'到'post_type'=>'post,product'或'post_type'=>'product,但它不会将产品添加到收藏夹页面。仍然只有添加的帖子。
https://wptrick.ru/kak-sdelat-izbrannye-zapisi/这是一个来源,但有些单词是西里尔字母,所以我会翻译。
1(向函数添加3个函数。php
//favorite posts array
function favorite_id_array() {
if (!empty( $_COOKIE['favorite_post_ids'])) {
return explode(',', $_COOKIE['favorite_post_ids']);
}
else {
return array();
}
}
//add to favorite function
function add_favorite() {
$post_id = (int)$_POST['post_id'];
if (!empty($post_id)) {
$new_post_id = array(
$post_id
);
$post_ids = array_merge($new_post_id, favorite_id_array());
$post_ids = array_diff($post_ids, array(
''
));
$post_ids = array_unique($post_ids);
setcookie('favorite_post_ids', implode(',', $post_ids) , time() + 3600 * 24 * 365, '/');
echo count($post_ids);
}
die();
}
add_action('wp_ajax_favorite', 'add_favorite');
add_action('wp_ajax_nopriv_favorite', 'add_favorite');
//delete from favorite function
function delete_favorite() {
$post_id = (int)$_POST['post_id'];
if (!empty($post_id)) {
$favorite_id_array = favorite_id_array();
if (($delete_post_id = array_search($post_id, $favorite_id_array)) !== false) {
unset($favorite_id_array[$delete_post_id]);
}
setcookie('favorite_post_ids', implode(',', $favorite_id_array) , time() + 3600 * 24 * 30, '/');
echo count($favorite_id_array);
}
die();
}
add_action('wp_ajax_delfavorite', 'delete_favorite');
add_action('wp_ajax_nopriv_delfavorite', 'delete_favorite');
2(添加";添加到收藏夹";链接到您的模板。对于帖子,它是单独的.php
<?php if(in_array($post->ID, favorite_id_array())){ ?>
<div class="fv_<?php echo $post->ID; ?>" title="Already in favorite" ><img src="http://yoursite.com/path-to-your-icon.svg" ><a href="http://yoursite.com/favorite/">In favorite</a></div>
<?php } else { ?>
<div class="fv_<?php echo $post->ID; ?>" >
<div class="add-favorite" title="Add to favorite" data-post_id="<?php echo $post->ID; ?>">
<img src="http://yoursite.com/path-to-your-icon.svg">Add to favorite
</div>
</div>
<?php } ?>
3(JS
jQuery(function($) {
//adding to favorite
$('body').on('click', '.add-favorite', function() {
var post_id = $(this).data('post_id');
$.ajax({
url: "/wp-admin/admin-ajax.php",
type: 'POST',
data: {
'action': 'favorite',
'post_id': post_id,
},
success: function(data) {
$('.fv_' + post_id).html('<img src="http://yoursite.com/path-to-your-icon.svg" ><a href="http://yoursite.com/favorite/">In favorite</a>');
$('.num-favorite').html(data);
},
});
});
//deleting from favorite
$('body').on('click', '.delete-favorite', function() {
var post_id = $(this).data('post_id');
$.ajax({
url: "/wp-admin/admin-ajax.php",
type: 'POST',
data: {
'action': 'delfavorite',
'post_id': post_id,
},
success: function(data) {
$('.fv_' + post_id).html('Deleted');
$('.num-favorite').html(data);
},
});
});
});
4(将链接添加到您的收藏夹,无论您想在哪里,例如在标题中
<a href="http://yoursite.com/favorite/">Favorite <div class="num-favorite"><?php echo count(favorite_id_array()); ?></div> </a>
5.在你的主题中创建新的页面模板,favorite.php,然后在管理面板中创建一个带有slug收藏夹的新页面,并为其选择该模板。
<?php
/*
Template Name: favorite
*/
?>
<?php
$favorite_id_array = favorite_id_array();
global $wp_query;
$save_wpq = $wp_query;
$args = array(
'paged' => get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1,
'post_type' => 'post',
'post__in' => !empty($favorite_id_array) ? $favorite_id_array : array(0),
);
$wp_query = new WP_Query( $args );
?>
<?php if ($wp_query->have_posts()) : ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="fv_<?php echo $post->ID; ?>">
<div class="delete-favorite" data-post_id="<?php echo $post->ID; ?>" title="Delete from favorite">
<img src="http://yoursite.com/link-to-your-icon.svg" >Delete
</div>
</div>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<?php endwhile; ?>
<?php else : ?>
<p>No posts in your favorite</p>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
your pagination code there.
它对你有用吗?请任何人尝试一下,我需要它,也许有人也需要同样的东西。
它对我有效,但在Favorite Template页面上我看不到任何帖子返回。但是如果我点击";添加到收藏夹";在产品内部,然后我刷新它,它说";在收藏夹中";这意味着产品保存在cookie中。
更新:我将查询从'post_type' => 'post',
更改为'post_type' => 'product',
,它成功了!