dream2891 2016-12-29 07:24
浏览 23
已采纳

在wordpress中显示不同颜色的自定义帖子用户

I am using ACF(advance custom field) and custom post type plugin to create my own custom post type.

I have successfully created custom post type with custom field. [no problem here]

Then i have display all custom posts to users(front end user) after login. [no problem here].

if front end user(after login) like particular custom post, he/she will fill-up free subscription form against particular custom post and submit it.

Now i want to display all custom posts to users but if user already subscribed for particular post then i want to show it with different color. any solution for this?

  • 写回答

1条回答 默认 最新

  • dongxing8009 2016-12-29 09:13
    关注

    When user submits the subscription form, save the current post id in users meta data using some custom meta key(subscribed_posts)

    <?php 
    add_user_meta( $user_id, 'subscribed_posts', $post_id);
    //$post_id is the id of post user subscribes to.
    ?>
    

    Now while displaying the post in frontend, just check for the post id and use some specific class to display in different color(write css for that class in your style.css, i've used the class green for subscribed and grey for not subscribed posts.)

    <?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();
    

    ?>

    Would have been more specific if you had showed the specific codes and html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法