how do remove the "Stick to the top of the blog" post from my recent blog post?

  blogs, php, wordpress
  • When one of my blog post is "Stick to the top of the blog", it shouldn’t show on my latest post carousel. Because it will show up 2 times, one in "Stick to the top of the blog" and 1 in "latest posts". So if it’s "Stick to the top of the blog" remove it from latest post. How do I do that? But when the blog post is not "Stick to the top of the blog", it should show up on the latest post 🙂

  • Also is it possible to do this in WordPress backend? When I have a blog post that is "Stick to the top of the blog" already and I make another blog post (checked) ""Stick to the top of the blog" for another blog post, it should pop up message and said " you have one "Stick to the top of the blog" already, do you want to remove the old one and make this one the new "Stick to the top of the blog"? So it will let you know that you have more than 1 "Stick to the top of the blog" checked. Something like that.

<!-- Stick to the top of the blog -->
    <?php
    $sticky = get_option( 'sticky_posts' );
        $args = array(
        'posts_per_page' => 1, /* how many post you need to display */
        'offset' => 0,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'post_type' => 'post', /* your post type name */
        'post_status' => 'publish',
        'post__in'  => $sticky,
        'ignore_sticky_posts' => 1
    );

    $query = new WP_Query($args);
    if ($query->have_posts()) :
        while ($query->have_posts()) : $query->the_post();
            
            get_template_part( 'template-parts/content', get_post_format() );
            
        endwhile;
    endif;
    ?>
    <!-- /Stick to the top of the blog -->
    
    
    <!-- Latest Post -->
    <div class="posts-items latest-posts">
        <section class="row">
            <h2 class="title slider-title">
                <a href="<?php echo get_permalink( get_page_by_path( 'recipes' ) ) ?>">
                <?php _e('Latest Recipes ', 'wpcrumbs'); ?>
                    <img src="<?php bloginfo('template_url')?>/icon/right-arrow.svg">
                </a>
            </h2>

            <div class="owl-carousel latest-posts owl-theme">

            <?php 
                $args = array(
                    'posts_per_page' => 5,
                    'ignore_sticky_posts' => 1
                );

                // Define our WP Query Parameters
                $the_query = new WP_Query( $args ); ?>
                  
                <?php 
                // Start our WP Query
                while ($the_query -> have_posts()) : $the_query -> the_post(); 
                // Display the Post Title with Hyperlink
            ?>



            <article class="posts-content">
                <a href="<?php the_permalink() ?>">
                    <div class="thumb">
                    <?php if (has_post_thumbnail()) { ?>
                        <?php the_post_thumbnail('post-thumbnail', array('alt' => the_title_attribute(array('echo' => false)))); ?>
                        
                    <?php } else { 
                        waisiechef_thumbnail_default();
                    } ?>
                    </div>

                    <?php the_category(); ?>
                   
                    <h2><?php the_title(); ?></h2>
                </a>
            </article>
        
            <?php 
            // Repeat the process and reset once it hits the limit
            endwhile;
            wp_reset_postdata();
            ?>

            </div>

        </section>
    </div>
    <!-- /Latest Post -->

Source: Ask PHP

LEAVE A COMMENT