I managed to make this shortcode work and it shows everything as I want. The only issue I am facing right now is that if I use posts_per_page => 1, it keeps showing all the posts. What can I do to be able to show only the latest post of the category? I tried: posts_per_page => 1 without '', but nothing it's still not working.

Here's the code I am using:

function Arte($atts, $content = null) {
                "pagination" => 'false',
                "query" => '',
                "category" => 'arte',
                "posts_per_page"    => '1',

    global $wp_query,$paged,$post;

    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();

    if ( $pagination == 'true' ) {
        $query .= '&paged='.$paged;

    if (!empty($category)) {
        $query .= '&category_name='.$category;

    if (!empty($query)) {
        $query .= $query;


    <ul class="loop">
        <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
            <li><a href="<?php the_permalink() ?>" rel="bookmark">
                    <div class="cont-overlay"><?php the_post_thumbnail(
                            'ilRaccoglitore_single', array(
                            'class' => 'img-thumb-sidebar',
                            'alt'   => get_the_title(), )); ?>
                        <div class="overlay"></div>
                    <h5 class="title-sidebar"><?php echo the_title(); ?></h5>
            <li class="excerpt-sidebar"><?php the_excerpt(); ?></li>
        <?php endwhile; ?>

    <?php if ($pagination == 'true') { ?>
        <div class="navigation">
            <div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
            <div class="alignright"><?php next_posts_link('More »') ?></div>
    <?php } ?>

    $wp_query = null; $wp_query = $temp;

    $content = ob_get_contents();


    return $content;

add_shortcode("arte", "Arte");

Thank you in advance!

  • You’re not using the posts per page attribute anywhere in your code... Mar 8, 2018 at 1:59
  • Line 8... maybe I am missing something.
    – Marya
    Mar 8, 2018 at 2:03
  • 1
    But you’re not putting it in the query Mar 8, 2018 at 2:03
  • And can you help me out? I am trying to learn, this is totally new to me.
    – Marya
    Mar 8, 2018 at 2:10
  • remove single quote of number in 'posts_per_page' and replace double quote with single quote. some time this is create problem. just try it i'm not sure about that. Mar 8, 2018 at 4:05

2 Answers 2


You have the posts_per_page attribute in your shortcode, but you're not actually calling it in your WP_Query. Your're doing a few unnecessary things in your code, but for now, the issue at hand is that you need to add:

if( !empty( $posts_per_page )) {
    $query .= '&posts_per_page='.$posts_per_page;

Before you call:


As it is currently written, your query doesn't take into the posts_per_page parameter into account


For Example try this:

function my_shortcode($atts,$content=null)

        $query = new WP_Query(array(
        'posts_per_page'=> $posts


and add shortcode [latest_post posts=1]

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.