• anatoly14

    (@anatoly14)


    Question, I’m trying to display page title/name in the widget, but for some reason it’s not working. I’m trying to use <?php the_title(); ?>
    Can someone point me in the right direction on how I can display the page title/name in the widget?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    both the_title and get_the_title use the global $post object, so in your code try adding global $post.

    https://developer.www.ads-software.com/reference/functions/get_the_title/

    Thread Starter anatoly14

    (@anatoly14)

    @sterndata can you explain a little more in detail?

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Are you writing a widget? If so, please post your code on pastebin.com or github.com.

    Moderator bcworkz

    (@bcworkz)

    the_title() and get_the_title() are specifically for post titles. Since most widgets can appear on any page of the site, many of which may be a list of posts instead of a single post, such as posts with a certain category term, I suggest you use wp_title(). This is often the same function themes use in header files for content between <title> tags. This function may return or output more content than you like. You can alter this with the ‘wp_title’ filter.

    Thread Starter anatoly14

    (@anatoly14)

    @bcworkz tried using <?php wp_title(); ?> still no go.

    Thread Starter anatoly14

    (@anatoly14)

    Got it to work. Didn’t realize that text widgets don’t recognize php, so I just added the following code to the functions.php
    add_filter(‘widget_text’,’execute_php’,100);
    function execute_php($html){
    if(strpos($html,”<“.”?php”)!==false){
    ob_start();
    eval(“?”.”>”.$html);
    $html=ob_get_contents();
    ob_end_clean();
    }
    return $html;
    }

    Works fine now

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    You’ve just created an incredible security hole in your site. Anyone who can add a widget can now execute arbitrary PHP code!

    Moderator bcworkz

    (@bcworkz)

    Agreed, eval() on external input is a great toehold for hackers. Because your initial example included PHP, I assumed you were writing a custom widget and not including PHP in content.

    Alter your filter callback to place the title where you want it without putting PHP in the text content and evaluating it. If need be you could include a unique tag in content that your callback replaces with the current page’s title.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Display Page Title / Name’ is closed to new replies.