• Hi guys so im having some trouble was hoping another pair of eyes could help find out whats wrong here..

    I have a loop that gives each post a unique div, everythings working great but for some reason the first post seems to come up as the website.. heres a screenshot of what i mean..

    https://prntscr.com/bhjgib

    As you can see, for some reason its always a blank post in the loop that links to the homepage.. very annoying, here is my loop.

    <div id="featured-topics">
    <?php $count = 0; ?>
    <?php
        $args = array(
            'posts_per_page' => 4,
            'category_name' => 'news'
        );
        $posts = get_posts( $args ); ?>
    
    <?php foreach (array_chunk($posts, 1, true) as $posts) : $count++; ?>
    
        <div class="column-<?php echo $count; ?>" style="background-image: url('<?php the_post_thumbnail_url( $size ); ?>')">
        <a class="featured-<?php echo $count; ?>" href="<?php the_permalink(); ?>">
    
            <?php foreach( $posts as $post ) : setup_postdata($post); ?>
    
            <?php endforeach; ?>
    
        </a></div> <!-- column/featured -->
    
    <?php endforeach; ?>
    
    </div>

    Thanks in advance

Viewing 1 replies (of 1 total)
  • Maybe this will work for you or help you get started. First I would retrieve the ID of the front page post and skip it if you come across it in the loop. That would eliminate your home page from the display.

    <div id="featured-topics">
    <?php $count = 0; ?>
    <?php
        $args = array(
            'posts_per_page' => 4,
            'category_name' => 'news'
        );
        $posts = get_posts( $args ); ?>
    
    <?php foreach (array_chunk($posts, 1, true) as $posts) : $count++; ?>
    
        <div class="column-<?php echo $count; ?>" style="background-image: url('<?php the_post_thumbnail_url( $size ); ?>')">
        <a class="featured-<?php echo $count; ?>" href="<?php the_permalink(); ?>">
    
            <?php
                //get the post id of the home page
                $frontpage_id = get_option( 'page_on_front' );
    
                //Loop through the posts
                foreach($posts as $post){
    
                    //test if this is the home page, if so then skip it.
                    if(frontpage_id == $post->ID){
                            //do nothing
                    } else {
                        //if not the home page, display your information
                        setup_postdata($post);
                    }
    
                }
    
            ?>
    
        </a></div> <!-- column/featured -->
    
    <?php endforeach; ?>
    
    </div>
Viewing 1 replies (of 1 total)
  • The topic ‘Please inspect my loop’ is closed to new replies.