简体   繁体   中英

Wordpress display custom posts based on category_name

I'm trying to create a thumbnail approach with products from a custom post type (lawnmowers, actually) based on the category written in an input field using ACF.

However, I'm currently unable to present any posts at all even when hardcoding my category.

$loop = new WP_Query( array( 'category_name' => 'frontmower', 'posts_per_page' => 10 ) ); 

Lawnmowers is a subcategory found 2 steps under "Products" & "Machines".

<div class="thumbContainer">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <?php echo '<a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">'; ?>
    <div class="productThumbnail">
        <img src="<?php the_field( "productImage" ); ?>" alt="image"/>
        <?php the_title( '<h2 class="entry-title">', '</h2>' ); ?>

            <div class="entry-content">
                <?php the_content(); ?>
            </div>
    </div>
    <?php echo '</a>'; ?>
<?php endwhile; ?>
</div>

For whatever reason I cannot understand why it isn't working. Trying different echo's to find out if my script enter the while-loop, but it doesn't!

However, this works just fine:

$loop = new WP_Query( array( 'post_type' => 'ridinglawnmowers', 'posts_per_page' => 10 ) ); 

But I cannot use post_type, as I would need to create zillions different post types. Having them all count as lawnmowers and presenting them with categories is the way to go.

To access all posts based on particular category use following code -

<?php 
 $catPost = get_posts(get_cat_ID("NameOfTheCategory")); //change this
   foreach ($catPost as $post) : setup_postdata($post); ?>
       <div>
             <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
             <p><?php the_content(); ?></p>
       </div>
<?php  endforeach;?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM