在 wordpress 中以不同颜色显示自定义 post 用户明智

Showing custom post user wise with different color in wordpress

我正在使用 ACF(高级自定义字段)和 自定义 post 类型 插件来创建我自己的自定义 post类型。

我已成功创建带有自定义字段的自定义 post 类型。 [这里没问题]

然后我在登录后向用户(前端用户)显示所有自定义 post。 [这里没问题]。

如果前端用户(登录后)喜欢特定自定义 post,he/she 将针对特定自定义 post 填写免费订阅表格并提交。

现在我想向用户显示所有自定义 post,但如果用户已经订阅了特定 post,那么我想用不同的颜色显示它。有什么解决办法吗?

当用户提交订阅表单时,使用一些自定义元键(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 并使用一些特定的 class 以不同的颜色显示(为此写 css class 在你的 style.css 中,我使用 class 绿色表示订阅,灰色表示未订阅 post。)

<?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

会更具体