• Resolved dig.ga

    (@wwwdigga)


    So there are 4 categories on my blog. To make things simple let’s just call them category 1, 2, 3 and 4. The problem is with posts being in two categories at the same time. In that case posts from other categories won’t show up anymore.

    Example:
    Post A is in the cateogries 1AND 2.
    Post B is in category 2 only.
    Now, when I click on category 2 all posts from category 2 only are gone. The only thing I see is post A and all the other posts which belong to category 1 AND 2. So post A has somehow suppressed post B.

    Here is the code being used for every category:

    <!-- Loop-->
    <?php
    $current_cat = get_the_category();
    $cat_ID = $current_cat[0]->cat_ID;
    $loop = new WP_Query(array('offset' => 1,'cat' => $cat_ID));
    if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div id="loopcontent"><a href="<?php esc_url( the_permalink() ); ?>">
    
    	<div id="content" class="article" <?php //Thumbnail-Hintergrund
        if ( $thumbnail_id = get_post_thumbnail_id() ) {
            if ( $image_src = wp_get_attachment_image_src( $thumbnail_id) )
                printf( ' style="background-image: url(%s);"', $image_src[0] );
        }
    ?>>
    	<div id="backgroundlayer">
    	<h1><?php the_title(); ?></h1>
    	<div class="date"><php echo get_the_date(); ?><p></div>
    
    	<div class="clear"></div>
    	</div></a>
    </div>
    
    	<?php endwhile; ?> <div class="clear"></div>
    	<?php endif; ?>
    <!-- End Loop -->

    I am googling for hours and days now and couldn’t figure out what the problem is. So any help is very much appreciated. Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • BryanWalters

    (@bryanwalters)

    1. Make sure your theme supports WP version you are using.

    2. Make sure your WP version is updated.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    The logic is wrong.
    It is assumed that this function will return the current category:

    $current_cat = get_the_category();

    But it does not return the current category, it returns all categories.
    https://developer.www.ads-software.com/reference/functions/get_the_category/

    The developer had a half-way-house understanding of this, because the developer knew that an array was returned. Arrays have potential to hold multiple objects, which in this case are categories.

    $cat_ID = $current_cat[0]->cat_ID;

    Here, the developer is extracting the first result from the “get_the_category” function that was called above. This code will only return the first category that the post is assigned to, regardless of any other categories.

    Thread Starter dig.ga

    (@wwwdigga)

    1. Make sure your theme supports WP version you are using.

    It does.

    2. Make sure your WP version is updated.

    It is.

    $cat_ID = $current_cat[0]->cat_ID;
    Here, the developer is extracting the first result from the “get_the_category” function that was called above. This code will only return the first category that the post is assigned to, regardless of any other categories.

    Thanks for pointing at the right direction. Now I know what’s going on.

    I changed the code to

    <?php
    $category = get_category_by_path();
    $cat_ID = $category[0]->cat_ID;
    $loop = new WP_Query(array('offset' => 1,'cat' => $cat_ID));
    if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
    ?>

    So now I am trying to get the cat_ID based on the url. Unfortunately this doesn’t do the job. Every category now shows me every single post ??

    Thread Starter dig.ga

    (@wwwdigga)

    Solved it:

    <?php
    $current_cat = end(get_the_category());
    $cat_ID = $current_cat->cat_ID;
    $loop = new WP_Query(array('offset' => 1,'cat' => $cat_ID));
    if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Not all posts show up under category’ is closed to new replies.