I have this piece of code
<ul>
<?php
$categories = get_categories('include=9,4,5,6,7,8,3&hide_empty=0');
foreach ($categories as $category) {
$option = '<li class="mainop"><a href="#">'.$category->name.'</a>';
$args = array( 'category' => $category->cat_ID );
$myposts = get_posts( $args );
if ( count($myposts) > 0) {
$option .= '<ul>';
foreach( $myposts as $post ) {
setup_postdata($post);
$option .= '<li><a href=" '.the_permalink().' "> '.the_title().' </a></li>';
}
$option .= '</ul>';
}
$option .= '</li>';
echo $option;
}
?>
</ul>
It is supposed to draw all categories in a WordPress blog and for each category the titles of the child posts.
However when running it is returning the following (html code):
<ul>
<li class="mainop"><a href="#">Category</a></li>
http://URL/?p=13Page13http://URL/?p=11Page 11http://URL/?p=9Page 9
<li class="mainop"><a href="#">Category</a>
<ul>
<li><a href=" "> </a></li>
<li><a href=" "> </a></li>
<li><a href=" "> </a></li>
</ul>
</li>
<li class="mainop"><a href="#">Category</a></li>
<li class="mainop"><a href="#">Category</a></li>
</ul>
Do you have any idea why the titlea and the URLs are put in first place at the output and not where they are supposed to be?
Thank you
the_permalink() and the_title() print out the link and title – they don’t return the values as strings. So when you call the functions, you are printing out the values into the page, and concatenating empty strings in their place to $option.