hide some posts; my code does not work
-
Hi
My them has some featured posts and blog loop.
I want to hide featured posts from blog loop.here is the code I use:
<?php while ( have_posts() ) : the_post(); $post_Id= get_the_ID(); if ( $post_Id == ($welcome_post_id || $featured_post1 || $featured_post2 || $featured_post3)){} else {the_content();} endwhile; ?>
And these are my featured posts IDs:
$welcome_post_id $featured_post1 $featured_post2 $featured_post3
but it does not work!
what’s the problem?Thanks
-
You’ve got the wrong syntax for your “if” statement. Try:
if ( ! $post_Id == $welcome_post_id ) || ! $post_Id == $featured_post1 || ! $post_Id == $featured_post2 || ! $post_Id == $featured_post3 ) { the_content(); }
Or for even cleaner code:
if ( ! in_array( $post_Id, array( $welcome_post_id, $featured_post1, $featured_post2, $featured_post3 ) ) { the_content(); }
thanks for your reply
but both codes do not work!I use this code and it’s work
if ( ($post_Id == $accesspresslite_welcome_post_id) || ($post_Id ==$featured_post1) || ($post_Id ==$featured_post2) || ($post_Id ==$featured_post3) ){} else {the_content());}
but still there is a problem.
I want to show 5 post in this blog loop. 2 of them are featured. and now it shows 3 post! (there enough posts to show 5 posts)now what?!
Instead of hiding the posts after they’re fetched, try excluding the posts from the query entirely:
<?php $new_query_args = array( 'post__not_in' => array( $accesspresslite_welcome_post_id, $featured_post1, $featured_post2, $featured_post3 ); ); $new_query = new WP_Query( $new_query_args ); if ( $new_query->have_posts() ) : while ( $new_query->have_posts() ) : $new_query->the_post(); ... do something ... endwhile; endif;
That way, you’ll still get the correct number of posts per page.
sorry
but does not work again ??please post the full code of your template.
Here is this section’s code:
<section id="ak-blog"> <section class="ak-container" id="ak-blog-post"> <div id="primary2" class="content-area"> <main id="main" class="site-main" role="main"> <?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); get_template_part( 'content' ); endwhile; ?> <div class="clear"></div> <?php accesspresslite_paging_nav(); ?> <?php else : ?> <?php get_template_part( 'content', 'none' ); ?> <?php endif; ?> <?php wp_reset_query(); ?> </main><!-- #main --> </div><!-- #primary --> <div id="secondary-right" class="widget-area right-sidebar sidebar"> <?php if ( is_active_sidebar( 'blog-sidebar' ) ) : ?> <?php dynamic_sidebar( 'blog-sidebar' ); ?> <?php endif; ?> </div> </section> </section>
and this is content.php code where related to above code:
<article id="post-<?php the_ID(); ?>" class="blogposts"> <header class="entry-header"> <h1 class="entry-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h1> </header><!-- .entry-header --> <?php if ( is_search() ) : // Only display Excerpts for Search ?> <div class="entry-summary"> <?php the_excerpt(); ?> </div><!-- .entry-summary --> <?php else : ?> <div class="entry-content"> <?php if(has_post_thumbnail()){?> <div class="entry-thumbnail"> <?php the_post_thumbnail('thumbnail'); ?> </div> <?php } ?> <div class="short-content"> <p><?php echo limitword( get_the_excerpt() , 65 ) ?>...</p> </div> <a href="<?php the_permalink(); ?>" class="bttn"><?php _e('More','accesspresslite')?></a> <?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'accesspresslite' ), 'after' => '</div>', ) ); ?> </div><!-- .entry-content --> <?php endif; ?> <footer class="entry-footer"> <?php if ( 'post' == get_post_type() ) : // Hide category and tag text for pages on Search ?> <?php /* translators: used between list items, there is a space after the comma */ $categories_list = get_the_category_list( __( ', ', 'accesspresslite' ) ); if ( $categories_list && accesspresslite_categorized_blog() ) : ?> <span class="cat-links"> <?php printf( __( 'Posted in %1$s', 'accesspresslite' ), $categories_list ); ?> </span> <?php endif; // End if categories ?> <?php endif; // End if 'post' == get_post_type() ?> </footer><!-- .entry-footer --> </article><!-- #post-## -->
Where is the code that you’re currently using to exclude the featured posts from the main loop? I don’t see any of the code snippets that you or I posted earlier in the thread.
it’s the main code and currently I do not use exclude codes you give me.
I change this part of the above code with codes you got me at line 6:<?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); get_template_part( 'content' ); endwhile; ?>
Which code of mine did you use? Can you put that code back and then post the full contents of the template?
I used all of them. for example the last one is this:
<section id=”ak-blog”>
<section class=”ak-container” id=”ak-blog-post”>
<div id=”primary2″ class=”content-area”>
<main id=”main” class=”site-main” role=”main”><?php if ( have_posts() ) : ?>
<?php
$new_query_args = array(
‘post__not_in’ => array( $accesspresslite_welcome_post_id, $featured_post1, $featured_post2, $featured_post3 );
);$new_query = new WP_Query( $new_query_args );
if ( $new_query->have_posts() ) :
while ( $new_query->have_posts() ) : $new_query->the_post();
get_template_part( ‘content’ );
endwhile;
?><div class=”clear”></div>
<?php accesspresslite_paging_nav(); ?><?php else : ?>
<?php get_template_part( ‘content’, ‘none’ ); ?>
<?php endif; ?>
<?php wp_reset_query();
?></main><!– #main –>
</div><!– #primary –><div id=”secondary-right” class=”widget-area right-sidebar sidebar”>
<?php if ( is_active_sidebar( ‘blog-sidebar’ ) ) : ?>
<?php dynamic_sidebar( ‘blog-sidebar’ ); ?>
<?php endif; ?>
</div>
</section>
</section>I used all of them. for example the last one is this:
<section id="ak-blog"> <section class="ak-container" id="ak-blog-post"> <div id="primary2" class="content-area"> <main id="main" class="site-main" role="main"> <?php if ( have_posts() ) : ?> <?php $new_query_args = array( 'post__not_in' => array( $accesspresslite_welcome_post_id, $featured_post1, $featured_post2, $featured_post3 ); ); $new_query = new WP_Query( $new_query_args ); if ( $new_query->have_posts() ) : while ( $new_query->have_posts() ) : $new_query->the_post(); get_template_part( 'content' ); endwhile; ?> <div class="clear"></div> <?php accesspresslite_paging_nav(); ?> <?php else : ?> <?php get_template_part( 'content', 'none' ); ?> <?php endif; ?> <?php wp_reset_query(); ?> </main><!-- #main --> </div><!-- #primary --> <div id="secondary-right" class="widget-area right-sidebar sidebar"> <?php if ( is_active_sidebar( 'blog-sidebar' ) ) : ?> <?php dynamic_sidebar( 'blog-sidebar' ); ?> <?php endif; ?> </div> </section> </section>
sorry, that’s it:
<section id="ak-blog"> <section class="ak-container" id="ak-blog-post"> <div id="primary2" class="content-area"> <main id="main" class="site-main" role="main"> <?php $new_query_args = array( 'post__not_in' => array( $accesspresslite_welcome_post_id, $featured_post1, $featured_post2, $featured_post3 ); ); $new_query = new WP_Query( $new_query_args ); if ( $new_query->have_posts() ) : while ( $new_query->have_posts() ) : $new_query->the_post(); get_template_part( 'content' ); endwhile; ?> <div class="clear"></div> <?php accesspresslite_paging_nav(); ?> <?php else : ?> <?php get_template_part( 'content', 'none' ); ?> <?php endif; ?> <?php wp_reset_query(); ?> </main><!-- #main --> </div><!-- #primary --> <div id="secondary-right" class="widget-area right-sidebar sidebar"> <?php if ( is_active_sidebar( 'blog-sidebar' ) ) : ?> <?php dynamic_sidebar( 'blog-sidebar' ); ?> <?php endif; ?> </div> </section> </section>
Argh, sorry about that. That was a copy-paste error on my part. The code should be this:
$new_query_args = array( 'post__not_in' => array( $accesspresslite_welcome_post_id, $featured_post1, $featured_post2, $featured_post3 ) );
No semicolon at the end of the
post__not_in
line.wow!
thanks a lot my friend ??
it’s working very nice :))
- The topic ‘hide some posts; my code does not work’ is closed to new replies.