我使用(高级自定义字段)和自定义post类型插件来创建自己的自定义post类型。
我已经成功地用自定义字段创建了自定义post类型。这里没问题
然后,我已经显示所有自定义的帖子给用户(前端用户)在登录后。这里没问题。
如果前端用户(登录后)喜欢特定的自定义帖子,他/她将填写针对特定定制帖子的免费订阅表单并提交。
现在我想向用户显示所有的自定义帖子,但是如果用户已经订阅了特定的帖子,那么我想用不同的颜色显示它。有什么解决办法吗?
发布于 2016-12-29 09:13:27
当用户提交订阅表单时,使用自定义元键(Subscribed_posts)将当前的post id保存在用户元数据中。
<?php
add_user_meta( $user_id, 'subscribed_posts', $post_id);
//$post_id is the id of post user subscribes to.
?>现在,在前面显示post时,只需检查post id并使用一些特定的类以不同的颜色显示(在您的style.css中为该类编写css,我已经将类绿色用于订阅,灰色用于未订阅的帖子)。
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'fields' =>'ids'
);
$the_query = new WP_Query( $args );//Query for all post that you want to display
if ( $the_query->have_posts() ) :
$subscribed_posts = get_user_meta($user_id);//Get current user id and fetch all subscribed posts.
while ( $the_query->have_posts() ) : $the_query->the_post();?>
<div class="default <?php echo in_array(get_the_ID(),$subscribed_posts)?'green':'gray'?>">
/*Your post content for every post goes here*/
</div>
<?php
endwhile;
endif;
wp_reset_postdata();?>
如果您已经显示了特定的代码和html,则会更加具体。
https://stackoverflow.com/questions/41375006
复制相似问题