0

I would like to display the last 3 articles of category 'camping-camping' with a shortcode but the function does not seem valid, an idea ?

function derniers_articles_camping() {

// the query
$the_query = new WP_Query( array(
  'category_name' => 'location-camping-var',
  'posts_per_page' => 3,
)); 


if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();

$contenu = '

<div class="effect-jazz">'.the_post_thumbnail();.'<div class="jazz-try">
        <div class="h3"><a href="'.the_permalink();.'">'.the_title();.'</a></div>
        <p>'the_resume(60);.'</p>
    </div>          
</div>

 ';

endwhile;
wp_reset_postdata();

return $contenu;

endif;

} 

add_shortcode('camping', 'derniers_articles_camping');

Thank you in advance for your help !

2 Answers 2

0

The problem is your $contenu variable.

Try:

$contenu .= '<div class="effect-jazz">'.get_the_post_thumbnail().'<div class="jazz-try">
    <div class="h3"><a href="'.get_permalink().'">'.get_the_title().'</a></div>
    <p>'.get_the_resume(60).'</p><!-- THIS FUNCTION NEEDS TO BE CREATED -->
</div>
</div>';

Do not use ; trailers when using inline php, ex <a href="'.get_permalink().'">.

Use the appropriate return functions:

Do use the correct operator .= when you wish to append to a variable, rather than overwrite it with each loop iteration.

Do not line break after $variable .= '.

0

My solution :

ob_start();

// the query
$the_query = new WP_Query( array(
 'category_name' => 'location-camping-var',
  'posts_per_page' => 3,
)); 

if ( $the_query->have_posts() ){

    while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

        <div class="col-md-4">

        <div class="grid">

            <div class="effect-jazz">
                <?php the_post_thumbnail('child-thumb'); ?>
                <div class="jazz-try">
                    <div class="h3"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
                    <p><?php the_excerpt(); ?></p>
                </div>          
            </div>

        </div>

        </div>

        <?php

    endwhile;

    wp_reset_postdata();

    $laboucle = ob_get_clean();
    return $laboucle;

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.