Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
Thank you! I am still quite the noob in php and didn't even know about the $post variable in WordPress. –  Seth Jan 13 '12 at 9:40
    
In this case I seriously recommend to read through codex.wordpress.org/Developer_Documentation –  petermolnar Jan 16 '12 at 8:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.