0

If I go to: domain.example/category/skydancers-en-blikvangers/ I want to get all the posts in the current category for skydancers-en-blikvangers, I know I can hard code the category but this needs to be a dynamic template. So if I go to category/other-category that needs to work as well.
Category.php:

<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="jumbotron">
                <h2><?php _e( 'Posts for' ); single_cat_title(); ?></h2>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
        <?php get_template_part('loop'); ?>

        <?php get_template_part('pagination'); ?>
        </div>
    </div>

</div>

Loop.php:

<!-- article -->
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

    <!-- post thumbnail -->
    <?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
            <?php the_post_thumbnail(array(120,120)); // Declare pixel size you need inside the array ?>
        </a>
    <?php endif; ?>
    <!-- /post thumbnail -->

    <!-- post title -->
    <h2>
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    </h2>
    <!-- /post title -->

    <!-- post details -->
    <span class="date"><?php the_time('F j, Y'); ?> <?php the_time('g:i a'); ?></span>
    <span class="author"><?php _e( 'Published by', 'product' ); ?> <?php the_author_posts_link(); ?></span>
    <span class="comments"><?php if (comments_open( get_the_ID() ) ) comments_popup_link( __( 'Leave your thoughts', 'html5blank' ), __( '1 Comment', 'html5blank' ), __( '% Comments', 'html5blank' )); ?></span>
    <!-- /post details -->

    <?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>

    <?php edit_post_link(); ?>

</article>
<!-- /article -->
<!-- article -->
<article>
    <h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
</article>
<!-- /article -->
1

The good basic technique to go from specifics to more general context is to look at the calls you already have.

You already call single_cat_title(), which somehow knows current category, right? If you look inside you will see it passes call to single_term_title(). And how does that one in turn knows?

$term = get_queried_object();

So there is your way to get to the full category object when on category archive page.

Still it's a little unclear from your code example what do you want to do with "all" posts. If you want to display all posts, in a context that already does appropriate query, you should be properly modifying main query via pre_get_post or such. It shouldn't be done in template for performance and compatibility reasons.

  • Thanks, I'm gonna try it in a few hours. The idea is that i want to show al the posts that are inside that category. Like a catelogus. I have a custom post type product, with product categories. So if i go to /category/pc i want to display al the pc's, then if you click on a pc. I want to go to the product page :) – Thijs Jun 2 '17 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.