Shortcode won’t take into account custom post ID put in parameter

Question

I wrote a little custom shortcode and when I try to use it in a custom post it won’t take into account the parameter I set up.

What it’s supposed to do

The shortcode is supposed to take a custom post ID into parameter and display a div that use the thumbnail of this post as a background if there’s one, and then display the title of the post inside that DIV. Pretty simple stuff.

What it does

In front the shortcode appears to work as intended but don’t take ID parameter into account and use current post ID to execute the function instead. The weird thing is that when I put a default value “4075” for $ID in the array it works fine. It just won’t take the parameter into account when I set it with [lire-aussi ID="4075"]

The code

function shortcode_lire_aussi( $atts ){
        extract(
          shortcode_atts( array(
          'ID' => '',
        ), $atts));
    
        
          $title= get_the_title($ID);
          $linkURL= get_permalink($ID);
    
          if(has_post_thumbnail($ID)){
            $background = get_the_post_thumbnail_url( $ID );
            $output="
            <div style = "background-image : url(".$background.');">
              <p>
                <a href="'.$linkURL.'">'.$title.'</a>
              </p>
          </div>';
          }
          else{
            $output="
          <div style = "background-color : black;">
            <p>
              <a href="".$linkURL.'">'.$title.'</a>
            </p>
          </div>';
          }
    
    
            return $output;
        
       
     
}
    
add_shortcode('lire-aussi', 'shortcode_lire_aussi');

I’m not really familiar with PHP and my knowledge of that language is very limited, I’m clearly missing something but can’t figure what.

0
Tom J Nowell 5 months 2022-03-15T07:19:40-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse