0

So, I have the following code written inside my function.php. I use it on my blog post to display the sticky post, but my only issue is I'm not sure how to get the permalink inside my HTML.

Also, how can I assign a post as sticky, but only show it in my shortcode? Right now this shortcode displays above all my posts, but then the sticky post is posted again at the top (I understand this is the correct functionality, but I only want the sticky post display via shortcode).

Thank you in advance!

functions.php

function wpb_latest_sticky() { 
 
$sticky = get_option( 'sticky_posts' );
 
$sticky = array_slice( $sticky, 0, 1 );

$the_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) );

// The Loop
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
            echo "<div class='w-100 w-50-m w-third-ns sticky-blog-column fl'>";
                echo "<div class='pv5'>";
                    echo "<a href = 'get_permalink()' >";           
                    echo "<div class='featured-img'>"
                        .get_the_post_thumbnail();
                    echo "</div>";
                    echo "<h5 class='pt4 pb3'>"
                        .get_the_date();
                    echo "</h5>";
                    echo "<h3>"
                        .get_the_title();
                    echo "</h3>"; 
                    echo "<p>"
                        .get_the_excerpt();
                    echo "</p>";
                echo "</a>";
            echo "</div>";
        echo "</div>";    
    }
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
 
return $return; 
 
} 
add_shortcode('latest_stickies', 'wpb_latest_sticky');

blog.php

<?php echo do_shortcode("[latest_stickies]"); ?>
    <?php if (have_posts()) : while(have_posts()) : the_post();?>
      <!-- start standard column -->
        <div class="w-100 w-50-m w-third-ns blog-column fl">
          <div class="pv5">
            <a href="<?php the_permalink();?>">
            <div class="featured-img"><?php the_post_thumbnail();?></div>
            <h5 class="pt4 pb3"><?php echo get_the_date();?></h5>
            <h3><?php the_title();?></h3>
            <?php the_excerpt();?>
            </a>
          </div>
        </div>
      <!-- end standard column -->
      <?php endwhile; 
    endif;?>  
  </div>
0

In your functions.php probably you want:

echo '<a href="' . get_permalink()  .'">'; 

There's a function is_sticky( int $post_id ) which will let you see when a post is sticky. On the page you want to hide the sticky, inserting this line in the PHP at the top of the loop while tell it to skip to to the next post if this one is a sticky:

if (is_sticky()) continue;

If you get stuck with that, post the PHP of the start of the loop on the page that you want to hide the sticky in.

| improve this answer | |
  • You're my savior!! Thank you :D – Summer Pratt Jul 14 at 22:07
  • @SummerPratt welcome :) upvotes and accepted answers appreciated if it works for you – mozboz Jul 14 at 22:37

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.