WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I created a custom loop for a particular category. Can I create a list of those posts -- kind of like a table of contents -- and have it link to that service in that loop? Pagination is not used.

My current loop is created with this function (I'm sure my function could be more efficient and cleaner -- I'm still learning):

function add_services() {

    echo '<div style="clear:both"></div>';

    $custom_query = new WP_Query('cat=5'); 
        while($custom_query->have_posts()) : $custom_query->the_post();

            echo '<hr />';
            echo '<h2>';
            echo the_title();
            echo the_content();


share|improve this question
possible duplicate of Pagination not working with custom loop – Chip Bennett Feb 4 '13 at 21:00
Please search for related questions before posting. This one has been asked/answered several times. :) – Chip Bennett Feb 4 '13 at 21:01
I'm not using pagnation by design. I'm just wanting to list the post titles at the top and then link them to the posts in the loop below – NW Tech Feb 4 '13 at 21:04
seems to be about using anchor tags in one list which then point to the posts in a different list. question: does the list need to be in a different location from 'add_services' output? – Michael Feb 4 '13 at 23:00
Looks up at question title... Ah, I see. I misread navigation as pagination. Mea culpa. :) But I'm not completely sure I understand the question. Is your custom loop supposed to correspond to the same posts in the main query loop? – Chip Bennett Feb 5 '13 at 1:11
up vote 0 down vote accepted

I ended up creating two loops and utilizing anchors. I'm not sure if it's the most efficient way of doing things, however, it works.

add_action( 'genesis_post_content', 'child_do_content' ); // Adds your custom page code/content
function child_do_content() {?>

    <ul class="multicol">
        $custom_query = new WP_Query(array('orderby' => 'title', 'order' => 'ASC','cat=5')); 
            while($custom_query->have_posts()) : $custom_query->the_post();?>

                <li><a href="#<?php the_title(); ?>"><?php the_title(); ?></a></li>

            <?php endwhile;

<?php }

function add_services() {?>

    <div style="clear:both"></div>

    $custom_query = new WP_Query(array('orderby' => 'title', 'order' => 'ASC','cat=5')); 
        while($custom_query->have_posts()) : $custom_query->the_post();?>

            <hr />
            <a name="<?php the_title(); ?>"></a>
            <h2><?php the_title(); ?></h2>
            <?php the_content(); ?>         

        <?php endwhile;
    wp_reset_postdata(); ?>

<?php }
add_action( 'genesis_after_content', 'add_services' );

As seen in action here

share|improve this answer

Your Answer


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.