最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get widget Title from widget id

programmeradmin3浏览0评论

Is there anyway at all to get the title of a widget from the widget id? The post title can be used by using

get_the_title()

Is there a way to get the title for a widget using the same kind or function?

Is there anyway at all to get the title of a widget from the widget id? The post title can be used by using

get_the_title()

Is there a way to get the title for a widget using the same kind or function?

Share Improve this question asked Jun 19, 2017 at 14:27 Zach TackettZach Tackett 1931 gold badge2 silver badges6 bronze badges 3
  • 2 retrive widget title/data – Den Isahac Commented Jun 19, 2017 at 15:47
  • Not even close to what I'm wanting – Zach Tackett Commented Jun 19, 2017 at 18:03
  • I don't know how are we suppose to help you this time around. Seems like the above link contains the necessary logic of getting the meta data of the widget. – Den Isahac Commented Jun 19, 2017 at 18:08
Add a comment  | 

2 Answers 2

Reset to default 2

You can get the widget name from the widget id with this:

<?php
    global $wp_registered_widgets;
    $id = 'recent-comments-1';  // example
    if ( isset($wp_registered_widgets[$id]['name']) ) {
        echo $wp_registered_widgets[$id]['name'];
    }
?>

This worked for me in the dynamic_sidebar_params:

/**
 *  Get Widget instance title or widget name as a fallback.
 *  Returns empty widget titles too.
 *
 *  @param string $widget_id
 *  @return string Widget title or widget name
 */
function get_widget_title( $widget_id ) {

    global $wp_registered_widgets;

    // get instance
    $instance = $wp_registered_widgets[$widget_id]['callback'][0];
    // @see https://developer.wordpress/reference/classes/wp_widget/get_settings/
    $settings = $instance->get_settings();

    // fallback 1 – widget number can't be found
    if ( isset( $wp_registered_widgets[$widget_id]['params'][0]['number'] ) ) {
        $num = $wp_registered_widgets[$widget_id]['params'][0]['number'];
    } else {
        return $instance->name;
    }

    // nasty nesting
    if ( isset( $settings[ $num ]['title'] ) ) {
        return $settings[ $num ]['title'];
    }
    return $instance->name;

}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论