0
votes

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.

1
What is the custom category you created called? Plus I would recommend adding your post_type to the query parameters and use codex.wordpress.org/Template_Tags/get_posts rather than messing with WP_Query - its safer :) - Simon Pollard
I'm using a custom post_type called "lawnmower", with normal wordpress post categories called "frontmower" and "backmower". Calling these categories give nothing currently, and I'm unsure why. I'll edit the question with the category names to make it clear. - Effect Erik

1 Answers

1
votes

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;?>