Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress PHP for WordPress PHP Basics for WordPress Custom Loops

I'm trying to get a list of blogs with every blog containing a link to the blog. I just can't get it working any help?

Any help will be much appreciated!

Christian Moran
Christian Moran
6,514 Points

Do you have an example of what you have so far?

Chris Ellinger
Chris Ellinger
26,494 Points

I think what you were trying to ask is how to add links to your list items being outputted by the loop. You would want to wrap your echo statement (echo '<li>' . get_the_title() . '</li>';) inside the while loop with your anchor tag. The code loop would then look like the code below. Hope this helps.

        $args = array(
            'post_type' => 'post'
        // The Query
        $the_query = new WP_Query( $args );

        // The Loop
        if ( $the_query->have_posts() ) {
            echo '<ul>';
            while ( $the_query->have_posts() ) {
                echo '<a href=' . get_permalink() . '>' . '<li>' . get_the_title() . '</li>' . '</a>';
            echo '</ul>';
        } else {
            // no posts found
        /* Restore original Post Data */
        wp_reset_postdata(); ```