1

I am trying to make this a Wordpress shortcode, i mean the below code, but it doesn't seem to work , I included this in my function.php file, but it gave back multiple query result, as seen in the image below, however when i load this in single.php without the shortcode functionality i.e removing the // Add Shortcode function post_list() { at the beginning and the } add_shortcode( 'post-list', 'post_list' ); at the end,replacing both with '' respectively it loads, normally displaying an array of post 1-5 .

please i need help on this one here is the two image i got.

Image 1

Image 2 - How my code looks like in function.php

enter image description here

// Add Shortcode
function post_list() {

if (have_posts()) :
$count = 0; $paged = ( get_query_var('paged') > 1 ) ? get_query_var('paged') : 1;
while (have_posts()) : the_post();
    $count++;
    if ($count <= 1 && $paged === 1) :
        if ($count === 1) echo '<div class="break"><h2>first</h2></div>'; ?>
        <div class="first-three">
            <?php the_title() ?>
        </div>
    <?php elseif (1 < $count && $count <= 2 && $paged === 1) :
        if ($count === 2) echo '<div class="break"><h2>secound</h2></div>'; ?>
        <div class="next-four">
            <?php the_title() ?>
        </div>
    <?php elseif (2 < $count && $count <= 3 && $paged === 1) :
        if ($count === 3) echo '<div class="break"><h2>third</h2></div>'; ?>
        <div class="next-six">
            <?php the_title() ?>
        </div>
    <?php elseif (3 < $count && $count <= 4 && $paged === 1) :
        if ($count === 4) echo '<div class="break"><h2>forrth</h2></div>'; ?>
        <div class="next-other-six">
            <?php the_title() ?>
        </div>
    <?php elseif (4 < $count && $count <= 5 && $paged === 1) :
        if ($count === 5) echo '<div class="break"><h2>fifth one</h2></div>'; ?>
        <div class="next-four">
            <?php the_title() ?>
        </div>

        <?php elseif (5 < $count && $count <= 6 && $paged === 1) :
        if ($count === 6) echo '<div class="break"><h2>sixth one</h2></div>'; ?>
        <div class="next-four">
            <?php the_title() ?>
        </div>
    <?php endif;
endwhile; ?>
<div class="nav-previous alignleft"><?php next_posts_link('Older posts'); ?></div>
<div class="nav-next alignright"><?php previous_posts_link('Newer posts'); ?></div><?php
endif;


}
add_shortcode( 'post-list', 'post_list' );
3
  • Start with returning a string instead of echoing output.
    – fuxia
    Commented Feb 17, 2017 at 19:12
  • I'll guess that running the loop inside the loop is causing the outer while(have_posts()) to never terminate, because it's always reset to the first post.
    – Milo
    Commented Feb 17, 2017 at 19:17
  • @Milo thanks for you comment, do you have any ideal on how i could sort this ? Commented Feb 17, 2017 at 19:30

0

Your Answer

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