0

I would like to exclude the current "page" (not post) from a categroy loop :

files :

archive.php, content.php, content-top-page.php (page template)

I have created 2 categories (archive.php) and releated 2 posts to each category. I have included "content-top-page.php" at the top of the category. Those are "pages" and I don't want them to change (a title, a post thumbnail and a specific text, that's all).

The problem :

The template pages are being displayed within the loop with the other posts. The current template page of each category is being included in. I am using specific ACF variables in the template pages.

Thanks if you have some ideas.

Archive.php

$exclude_ids = array( 45, 36 );

$my_query = new WP_Query(array(
    'exclude' => $exclude_ids,
    'post_type' => 'post',
    'post_status' => 'publish',

));

<section style="padding:10px!important;">

    if ( have_posts() ) : ?>

        <section>
            <?php
                get_template_part('content','top-page');

            ?>
        </section><!-- .section -->


        <div class="row">

            <?php while ( $my_query->have_posts() ) : $my_query->the_post(); 

                <div class="col-lg-3">

                    <?php get_template_part( 'template-parts/content', 
  get_post_format() ); 
                    ?>

                </div>

            <?php endwhile; ?>

        </div>  


    <?php endif;

    wp_reset_postdata(); ?>

    </section>

content-top-page.php (template page)

<?php if ( has_category('my_category_1') ): ?>

<div class="row">

    <div class="col-lg-12">
        <h1><strong><?php the_title(); ?></strong></h1>
    </div>

    <div class="col-sm-12 col-md-6 col-lg-6 col-xl-6" style="background-color:red;">
        <img src="<?php get_field('top_profil_featured_image'); ?>" />
    </div>

    <div class="col-lg-6">
        <?php echo get_field('top_profil_content'); ?>
    </div> 

</div>

<?php endif ?>




<?php if ( has_category('my_category_2') ): ?>

<div class="row">

    <div class="col-lg-12">
        <h1><strong><?php the_title(); ?></strong></h1>
    </div>

    <div class="col-sm-12 col-md-6 col-lg-6 col-xl-6" style="background-color:green;">
        <img src="<?php get_field('top_profil_featured_image'); ?>" />
    </div>

    <div class="col-lg-6">
        <?php echo get_field('top_profil_content'); ?>
    </div> 

</div>

<?php endif ?>

content.php

<article id="post-<?php the_ID(); ?>" class="" <?php post_class(); ?> style="border:1px solid #6f3fa5;">

<?php 
    if ( !is_single() && has_post_thumbnail() ) { ?>
        <div class="entry-img">
            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('blog-way-common'); ?></a>
        </div>   
    <?php
} 
?>

    <div class="entry-content">

        <?php

            if( is_single() ){

                the_content( sprintf(
                    /* translators: %s: Name of current post. */
                    wp_kses( __( 'Continue reading %s <span class="meta-nav">&rarr;</span>', 'blog-way' ), array( 'span' => array( 'class' => array() ) ) ),
                    the_title( '<span class="screen-reader-text">"', '"
 </span>', false )) );

                wp_link_pages( array(
                    'before' => '<div class="page-links">' . esc_html__( 'Pages :', 'blog-way' ),
                    'after'  => '</div>',
                ) );

            } else {

                echo excerpt_custom_length(20);

            }

        ?>

    </div><!-- .entry-content -->
 </article><!-- #post-## -->
0

Use pre_get_posts to exclude the categories you don't want to display in the loop.

function exclude_posts_from_specific_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-2' );
    }
}
add_action( 'pre_get_posts', 'exclude_posts_from_specific_category' );

Or create a new WP_Query and use the Category Parameters.

$args = array( 

'category__not_in' => 2 ,

'category__in' => 4 

);

$the_query = new WP_Query( $args );


if ( $the_query->have_posts() ) {
        echo '<ul>';
        while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
        echo '</ul>';
} else {

}

wp_reset_postdata();

If you only want to display posts from one category, you would use the category archive.

  • Helps but not solves the problem entirely (but that's a start for me ;) Thanks. – Nick83 Oct 26 '17 at 8:53

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.