从作者自己的个人资料页面隐藏部件的小部件 - WordPress

时间:2018-03-11 21:30:33

标签: wordpress

我正在使用名为Widget Logic的插件。

在每个作者的页面上,我添加了一个联系我表单,您可以在其中向作者发送消息,但如果作者正在访问他自己的个人资料页面,我想隐藏它。 我看到像!is_author()这样的例子,但我不知道如何进行比较。

https://wordpress.org/plugins/widget-logic/

1 个答案:

答案 0 :(得分:0)

您无法使用is_author()功能进行此检查,因为:

  

此条件标记检查是否正在显示作者存档页面。这是一个布尔函数,意味着它返回TRUE或FALSE。

当然,您可以使用is_author()在主题模板文件author.php以外的更高级别进行检查。例如,当使用钩子时。然后你可以使用它。

但我认为你正在模板文件中做事,所以你要找的是这样的:

$current_author = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
var_dump($current_author->ID); // < just for you to confirm what value it returns for the current profile you are on
var_dump(get_current_user_id()); // < just for you so you can see what the current user ID is of the user who is logged in at this moment
if( $current_author->ID!=get_current_user_id() ) {
    // Display the form...
}else{
    // Maybe do something else or do nothing...  
}

更新(当作者ID与登录的用户ID相同时取消注册小部件)

在此示例中,我们通过将文本小部件WP_Widget_Text传递给unregister_widget()函数来取消注册文本小部件。

有关WordPress窗口小部件类名称的完整列表,请参阅:https://codex.wordpress.org/Function_Reference/unregister_widget

将其放在主题functions.php文件中。

function riseagainst_remove_text_widget() {
    // Because `widget_init` is called early, we can not use `get_permalink()` 
    // neither can we use `home_url( $wp->request )`, neither can we get the
    // current author ID at this level when we are on profile page. And we
    // need to deregister the widget at this hook, and not at a later stage
    // otherwise it will simply override our deregister call to wordpress.
    // That's why we will use some of the `$_SERVER` array to obtain the current
    // URL (which is provided by the server upon requesting the page).
    // We only need the last part of the URL to check if we are on an author page.
    $page_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

    // The last part of the URL is the author slug in case we are on the author
    // profile page. To get the last part of an URL we use `basename()`,
    // this will return the slug of the author
    $author_name = basename($page_url);

    // Now we have the author slug, we can retrieve all author information
    // We only need the author ID but this can also be used for other data
    // When plain permalink is enabled, we can not search based on slug,
    // Instead we will be provided with the author ID via the URL parameter
    // named `author`. So do a check on that first
    $current_author = ( isset($_GET['author']) ? get_user_by('id', absint($_GET['author'])) : get_user_by('slug', $author_name) );

    // This check is important to only execute the code whenever we actually
    // are on an author page, when no author was found, we are probably not
    // on an author page. (note that if you have problems with it, you can
    // also do a check on if the URL path contains `/author/`)
    if( $current_author ) {
        // Only display the widget whenever current author ID
        // is not equal to current logged in user ID
        if( $current_author->ID!=get_current_user_id() ) {
            // Display the widget...
        }else{
            // Do not display the widget...
            unregister_widget('WP_Widget_Text');
        }
    }
}
add_action( 'widgets_init', 'riseagainst_remove_text_widget' );
相关问题