How to pull sticky post permalink inside php?

Question

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
, , , Summer Pratt 1 year 2020-07-14T16:10:26-05:00 0 Answers 55 views 0

Leave an answer

Browse
Browse