phpwordpresspostcategoriesblogs

Get specific category and all post within the category


I created a blog with, latest post at the top section, newsletter on the second section and the third section is the report section, where I need the report category and all post within the report category.

the post in the report category should have thumbnail and the title looking like a card.

Below the result is listing a list of post in the report category. But to style it with string it's redundant.

 <?php 
                $post_args = array(
                    'post_type' => 'post', //get post by 'post' (default post type).
                    'posts_per_page' => -1, //show all posts.
                    'tax_query' => array( 
                        array( 
                            'taxonomy' => 'category', 
                            'field' => 'slug', 
                            'terms' => array('Report') 
                        )
                    ) 
                );
                $the_query = new WP_Query($post_args);
                if ( $the_query->have_posts() ) {
                    while ( $the_query->have_posts() ) {
                        $the_query->the_post();

                        //show all list the post.
                        echo '<li>
                                <a href="'.get_permalink().'">'.get_the_title().'</a>
                            </li>';
                    }
                    wp_reset_postdata();
                }
            ?>

So this is what i have so far in term of listing the post, now I need a "foreach" loop to properly style it. So this is what I have, but it doesn't display anything.

<?php
    $post_args = array(
        'post_type' => 'post',
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'category',
                'feild' => 'slug',
                'terms' => array('Report')
            )
        )
    );
    $the_query = new WP_Query($post_args);

if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
    foreach($the_query as $query) : ?>
        <a href="<?php echo get_permalink() ;?>">
            <ul>
                <?php get_template_part('includes/section', 'blogsmall') ;?> 
                <?php get_the_title() ;?>
            </ul>
        </a>

    
        
    <?php endforeach ;?>
<?php endwhile; else: endif;?>

Feels like I did something wrong. No error and doesn't display anything at all. The difference here is that I can put proper code rather than getting wordpress code into strings.

I prefer the second adaptation code which I implemented, but doesn't display anything. If you can help out would be much appreciate.

Thanks in advance!


Solution

  • Thanks to @ChrisHaas for taking the time to actually response and let me do the thinking rather than silver spooning me. I actually have to sleep over it and start over again with a fresh mind and set of eyes and mood.

    So below are the end result of my question.

    <?php 
        $post_args = array(
            'post_type' => 'post', //get post by 'post' (default post type).
            'posts_per_page' => -1, //show all posts.
            'tax_query' => array( 
                array( 
                    'taxonomy' => 'category', 
                    'field' => 'slug', 
                    'terms' => array('Report') 
                )
            ) 
        );
    $the_query = new WP_Query($post_args);
        // wp_reset_postdata() This has a long standing issue for 12 years which causes some problem.
    
        if ( $the_query -> have_posts() ) : while ( $the_query -> have_posts() ) : $the_query -> the_post(); ?>
    
            <a href="<?php the_permalink() ;?>">
                <li>
                    <div class="featured-images">
                        <?php get_template_part('includes/section', 'blogsmall') ;?>
                    </div>
                    <div class="post-content">
                        <?php the_title() ;?>
                    </div>
                </li>
            </a>
    
    <?php endwhile; else : endif ;?>
    

    Above is the end result, Thank you @ChrisHaas. Turns out the problem is the wp_reset_postdata() And also scraping of the idea of having foreach loop which mention by @ChrisHaas Thank to you mate!

    I just started using PHP and Wordpress for a week now. So everything new to me. Excuse my logic @ChrisHaas