• I was wondering if there was a way to list all posts in a certain category in alphabetical prder on a page with just the title and url to open, possibly in a table?

    post1 post2 post3
    post4 post5 post6

    I’ve tried a few different plugins, one I found that will (without tables) but displays the first bit of content for each post, the others I tried aren’t even worth mentioning.

    • This topic was modified 7 years, 1 month ago by altere987. Reason: correction
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hey,

    yes, you can do this easily with a WP Query, just create a page template and paste the following code:
    Just replace ‘category_slug’ with the desired category slug.

    <?php
    		// Custom WP query
    		$args_query = array(
    			'post_type' => array('post'),
    			'posts_per_page' => -1,
    			'order' => 'ASC',
    			'orderby' => 'title',
    			'tax_query' => array(
    				array(
    					'taxonomy' => 'category_slug',
    					'field' => 'slug',
    				),
    			),
    		);
    
    		$query = new WP_Query( $args_query );
    
    		if ( $query->have_posts() ) {
    			echo '<table>';
    			while ( $query->have_posts() ) {
    				$query->the_post();
    
    				echo '<tr>';
    					echo '<td>';
    						echo '<a href="'.get_the_permalink().'">'.get_the_title().'</a>';
    					echo '</td>';
    				echo '</tr>';
    
    			}
    			echo '</table>';
    		} else {
    			echo 'No posts found.';
    		}
    
    		wp_reset_postdata();
    
    		?>
    Thread Starter altere987

    (@altere987)

    @michaeljames1 , that’s close to what I’m looking for, there’s bound to be a way to go a step further into what I was thinking but I’m not sure with wp query on how to get 3 per table row? I also took out the tax_query and just used category__in after a little searching.

    <table>
    <tr>
       <td> </td>
       <td> </td>
       <td> </td>
    </tr>
    <tr>
       <td> </td>
       <td> </td>
       <td> </td>
    </tr>
    </table>

    I’m sure I can clean it up a bit once I get it working to be in line with the rest of the theme but I’m not even worried about that right now. Added get_header(), http://www.masakiclan.com/testing

    • This reply was modified 7 years, 1 month ago by altere987. Reason: added header and url to view
    Thread Starter altere987

    (@altere987)

    Alright, I managed to remember incrementing records and was able to manipulate it to do what I wanted, hopefully I’ve got everything closed. My question on this, is formatting so the table isn’t as wide as the whole page as other pages posted without a template fit in the center and don’t run out left or right too much. I’m not sure if this is CSS or something else I’m missing, here’s what I have so far.

    <?php
    /**
     * Template Name: Testing
     */
    get_header();
                    // Custom WP query
                    $args_query = array(
                            'post_type' => array('post'),
                            'category__in' => array('4'),
                            'posts_per_page' => -1,
                            'order' => 'ASC',
                            'orderby' => 'title',
                    );
                    // counter for cells
                    $i = 1;
    
                    $query = new WP_Query( $args_query );
    
                    if ( $query->have_posts() ) {
                            echo '<table border="1">';
                            while ( $query->have_posts() ) {
                                    $query->the_post();
    
                                    // for 1st, 3rd, 6th, etc record insert new row
                                    if (($i == 1) or (($i - 1) % 3) == 0) {
                                    echo '<tr>' . "\n";
                                    }
                                            // insert cell
                                            echo '<td>';
                                                    echo '<a href="'.get_the_permalink().'">'.get_the_title().'</a>';
                                            echo '</td>';
                                    // for 3rd, 6th, 9th, etc record insert end of row
                                    if ((($i) % 3) == 0) {
                                    echo '</tr>';
                                    }
                                    // increment $i
                                    $i++;
                            }
    
                            // if no records left and row isn't filled, fill with empty cells
                            while(($i - 1) % 3 != 0) {
                                    echo '<td>&nbsp;</td>';
                                    $i++;
                            // for 3rd, 6th, 9th etc record insert a tag for end of the row
                            if (($i - 1) % 3 == 0) {
    
                                    echo '</tr>';
                                    }
                            }
                            echo '</table>';
                    } else {
                            echo 'No posts found.';
                    }
    
                    wp_reset_postdata();
    
                    ?>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘display posts’ is closed to new replies.