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;
}
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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