shutupchigo
shutupchigo

Reputation: 709

How to get post url in shortcode

I've created shortcode in the function to display promotions on page/post using shortcode in the editor.

    //Testimonial shortcode
    add_shortcode('promotions', 'adenPromotions');   
    function adenPromotions($attr, $content)
   {        
   ob_start();  
    get_template_part('./templates/promotions-loop');  
    $ret = ob_get_contents();  
    ob_end_clean();  
    return $ret;    
   }

Here's the code in the template file I have.

<?php
if ($_SESSION['selectLocation'] != '') {
$args = array( 'post_type' => 'promotion', 
          'meta_key' => 'builder',
          'posts_per_page' => 3,
          'meta_value' => $_SESSION['selectLocation']);
} else {
$args = array( 'post_type' => 'promotion',
          'posts_per_page' => 3,
          'meta_value' => 0); 
};
if ($_SESSION['selectLocation'] != ''):
?>
<div class="container-fluid px-0">
<div class="row">
    <div class="col-md-12">
        <div class="offer-section-page-ah">
            <?php
                $the_query = new WP_Query( $args );?>              
                <?php if ( $the_query->have_posts() ) : $i = 0; ?>
                    <?php while ( $the_query->have_posts() ) : 
$the_query->the_post(); $i++ ?>
                        <div class="offer-loop-page  block-<?php echo $i 
?>">
                            <div class="offer-banner-ah" 
style="background-image:url('<?php echo the_post_thumbnail_url('full') ;?>');"></div>
                            <div class="offer-right">
                                <a href="<?php echo get_permalink() ;?>"><h2 class="heading"><?php the_title() ;?></h2></a>
                                <div class="post-expirator-ah"><p><?php echo do_shortcode('[postexpirator]') ;?></p></div>
                                <div class="sub-heading"><p><?php the_excerpt() ;?></p></div>
                                <div class="more-btn-ah"><a href="<?php echo get_permalink() ;?>" class="pink">Find out more</a></div>
                            </div>
                        </div>
                    <?php endwhile; ?>
                    <?php wp_reset_postdata(); ?>        
                <?php else : ?>
                    <h2 class="heading text-center mb-0 m-0 py-4"> Sorry, no promotion available in your region.</h2>
            <?php endif; ?>
        </div>
    </div>
</div>

get_permalink() should return the url of the post in the loop however it is returning the URL of the post where the shortcode is added, if that makes sense.

Hope the code makes sense. Here's the pastebin version if code here doesn't make sense. -> https://pastebin.com/MSNn6rKQ

Edit: Apparently the_excerpt() function was playing up, not sure why, removed that and it fixed the problem. Created ACF field for description instead.

Upvotes: 1

Views: 2957

Answers (3)

Peter HvD
Peter HvD

Reputation: 1661

To get information about the current post that your shortcode is used in, use:

global $post;

You can then

get_the_permalink($post->ID)

Upvotes: -1

Yogesh Garg
Yogesh Garg

Reputation: 333

In Post loop you can use get_the_ID() function..

With this function you can get ID of Post with in Loop..

And Use this ID to get Permalink of post in loop

$post_id = get_the_ID();
get_the_permalink($post_id);

Upvotes: 1

wsoil
wsoil

Reputation: 63

May you should try this:

get_permalink(get_the_ID()) instead of get_the_permalink

Upvotes: 2

Related Questions