如何不为未登录的用户显示特定类别的帖子缩略图

时间:2013-03-24 作者:Robert Ramsey

你们能帮我举一些例子吗?这样我就可以从一个特定的类别(示例)中整理出拇指贴子,不显示给未登录的用户,而是显示一个图像,告诉他们必须登录才能看到该贴子。

登录的用户可以查看所有拇指贴子。我尽量简短地回答这个问题,如果还需要什么,我会做的。谢谢

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

您可以将筛选器挂接到post_thumbnail_html 并检查其是否属于该类别的帖子,以及用户是否未登录,例如:

add_filter( \'post_thumbnail_html\', \'my_post_image_html_wpa92119\', 10, 3 );

function my_post_image_html_wpa92119( $html, $post_id, $post_image_id ) {

    $category_to_exclude = "CHANGE_WITH_CATEGORY ID";
    $logIn_Img_path = "CHANGE THIS WITH PATH/TO/NON-LOGGED/USERS/IMAGE";

    //check if the post have that category nad if the user is not logged in
    if (has_category( $category_to_exclude ) && !is_user_logged_in())
        return \'<a href="\'.wp_login_url( get_permalink($post_id) ).\'"><img src="\'.$logIn_Img_path.\'" alt="please log in"></a>\';

    //if you got here then he is either logged in or its not the specified category
    return $html;
}

结束

相关推荐