• Hi!

    I’ve almost got what I want working from cobbling bits but I need a bit of help finishing it up. What I’m trying to do is output the following (for a custom post type) within a page template (not a separate archive page):

    <ul class="accordion">
        <li>
            <h3>2014</h3>
            <div><a href="the_permalink>"Post title 1</a></div>
            <div><a href="the_permalink>Post title 2</a></div>
            <div><a href="the_permalink>Post title 3</a></div>
            <div><a href="the_permalink>Post title 4</a></div>
        </li>
        <li>
            <h3>2013</h3>
            <div><a href="the_permalink>Post title 5</a></div>
            <div><a href="the_permalink>Post title 6</a></div>
            <div><a href="the_permalink>Post title 7</a></div>
            <div><a href="the_permalink>Post title 8</a></div>
    </ul>

    The code I’ve got so far looks like this:

    <?php
    $years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type = 'press-releases' AND post_status = 'publish' GROUP BY year DESC" );
    
    foreach ( $years as $year ) {
        $posts_this_year = $wpdb->get_results( "SELECT post_title FROM wp_posts WHERE post_type = 'press-releases' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "'" );
    
        echo '<ul class="accordion">';
        echo '<li><h3>' . $year->year . '</h3>';
        foreach ( $posts_this_year as $post ) {
        	echo '<div>' . $post->post_title . '</div>';
        }
        echo '</li>';
        echo '</ul>';
    }
    ?>

    Which is the basics of what I want. However, wrap the post title in the permalink, it only outputs the titles themselves. I’m sure it’s a simple fix to output the title but I don’t know how to do it. Any ideas or is there a better way to do what I’m trying to achieve?

    Thanks!

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Output yearly archive withinin a page’ is closed to new replies.