0

There seems to be something wrong with the simple function below, but I can't figure out what it is. It is displaying as the following and I can't figure out why. I know it has something to do with the functions the_ID(), the_title(), and the_permalink(), but I don't know what it is. Sorry if this sounds like a silly question.

1http://www.example.com/?p=1Hello world!<h1 class="title" id="post-"><a rel="bookmark" href=""></a></h1>


function basetheme_nodetitle($before_title="",$after_title="",$link = true) {
    /*
     * basetheme_nodetitle() can only be called inside the Loop
     * $before_title is the initial tag in which to wrap your title, usually <h2> or similar.
     * $after_title is the tag after the title, usually </h2> or similar. Only works if $before_title is NOT empty
     * $link is whether or not the title should be linked. Default is true
     */
    if(empty($before_title)){$before_title = '<h1 class="title" id="post-' . the_ID() . '">';$after_title = "</h1>";}
    if($link == false){$beforelink = '';$afterlink = '';}else{$beforelink = '<a rel="bookmark" href="' . the_permalink() . '">';$afterlink = '</a>';}
    echo $before_title . $beforelink . the_title() . $afterlink . $after_title;
}

1 Answer 1

0

The insideds of a function is separated from the global things. Therefore the variables, set by other processes are not reachable/useable inside a function until you made them so, with, for example global keyword for a var.

So

Add

the_post();

after the function's {,

or add

global $post;

and use $post->ID instead of the_ID and so on. I also recommend you to read some books on programming PHP.

2
  • Thank you! I am still quite the noob in php and didn't even know about the $post variable in WordPress. Commented Jan 13, 2012 at 9:40
  • In this case I seriously recommend to read through codex.wordpress.org/Developer_Documentation Commented Jan 16, 2012 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.