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.

I want to get the right link format for the second link, i tried many time to move the a open tag but no way the result is as the image below.

foreach( $terms as $term ) : 

    echo '<div class="one_half last">';

    $customposts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=2" );

    echo '<ul>';
    if( $customposts->have_posts() ): while( $customposts->have_posts() ) : $customposts->the_post();
        echo '<li>';
        echo '<a href="'.the_permalink().'">'.the_title_attribute(); // The problem
        endwhile;  endif;
        echo '</a></li>';
    echo '</ul>';
    echo '</div>';
endforeach;

Thanksenter image description here

share|improve this question

closed as off topic by Chip Bennett, s_ha_dum, toscho Jan 7 '13 at 16:11

Questions on WordPress Development Stack Exchange are expected to relate to WordPress within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
This appears to be a PHP syntax question, rather than a WordPress-specific question. –  Chip Bennett Jan 7 '13 at 15:30
    
This is a Wordpress for loop –  Ahmad Ajmi Jan 7 '13 at 15:38
    
"This is a Wordpress for loop." - Please refer to the WPSE FAQ. Just because a question takes place in the context of WordPress does not make the question inherently in-scope for WPSE. –  Chip Bennett Jan 7 '13 at 16:06

1 Answer 1

up vote 0 down vote accepted

First of all you have the PHP syntax all messed up. Second, you misused some WordPress template tags - you already echo the entire link like so you need to return post permalink and title, but right now the_permalink and the_title_attribute functions echo the code.

Here's your code fixed:

foreach( $terms as $term ) :

    $customposts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=2" );

    if( $customposts->have_posts() ):

        echo '<div class="one_half last">';
        echo '<ul>';

        while( $customposts->have_posts() ) : $customposts->the_post();
            echo '<li>';
            echo '<a href="' . get_permalink() . '">' . get_the_title(); // The problem
            echo '</a></li>';
        endwhile;

        echo '</ul>';
        echo '</div>';

    endif;

endforeach;
share|improve this answer
    
Thanks so much, this is perfect and well organized –  Ahmad Ajmi Jan 7 '13 at 15:49

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