1

I am new to word press, And I am working on posts, I have total 10 posts in my admin panel, I tried to get the posts by their ID's but it outputs the latest five posts.

here is my code sample. I don't know where I am going wrong here, A little hint would be highly appreciated.

<?php
            $thePostIdArray = array("11", "13", "15", "17", "19");
            $limit = 5;
            if ( have_posts() ) : while ( have_posts() ) : the_post(); $counter++; 
            if ( $counter < $limit + 1 ) : $post_id = $thePostIdArray[$counter-1]; $queried_post = get_post($post_id);
        ?>
        <a href="/applications">
        <div class="col-md-5 apps text-center">

            <?php the_post_thumbnail('full', array( 'class' => 'img-responsive' )); ?>

            <div class="caption">
                <h3><?php echo $queried_post->post_title; ?></h3>
                <button type="button" class="btn btn-default home-page-explore hvr-grow-shadow">
                    Explore
                </button>

            </div>
        </div> </a>

        <?php endif; endwhile; endif; ?>

See, I have specified the ID's in the array, but It doesn't show these ID's and shows the latest 5 posts from the amdin panel.

2

Try this:

<?php
        $thePostIdArray = array("11", "13", "15", "17", "19");
        $limit = 5;
        $query=new WP_Query(array('post__in'=>$thePostIdArray));
        if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); $counter++; 
        if ( $counter < $limit + 1 ) : $post_id = $thePostIdArray[$counter-1]; $queried_post = get_post($post_id);
    ?>
    <a href="/applications">
    <div class="col-md-5 apps text-center">

        <?php the_post_thumbnail('full', array( 'class' => 'img-responsive' )); ?>

        <div class="caption">
            <h3><?php echo $queried_post->post_title; ?></h3>
            <button type="button" class="btn btn-default home-page-explore hvr-grow-shadow">
                Explore
            </button>

        </div>
    </div> </a>

    <?php endif; endwhile; endif; ?>
  • it gives syntax error on $query. Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ',' or ')' in – Bilal Zafar Feb 8 '17 at 9:19
  • Syntax error...try now.... – Aniruddha Gawade Feb 8 '17 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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