0

I got the first post using the loop but I can't get the second post.

I would like every post to be in a different div but the same style as the first post. To do this, I would probably need to create more loops. I want to do this so I can get a slide show with div's using jquery.

The first part of the loop selects the first image in a post and the second part of the loop removes the image from the text and shows the text using the WordPress format.

You can find an example here:

http://www.volunteeringnews.com/index2.php

Thanks you very much!

<div class="nto">
        <div class="natt">

            <?php
            $attachments = get_children(
                array(
                'post_type' => 'attachment',
                'post_mime_type' => 'image',
                'post_parent' => $post->ID
                ));
            if(count($attachments) > 0) { ?>
                <?php
                    $attachments = get_children(array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order'));
                    if ( ! is_array($attachments) ) continue;
                    $count = count($attachments);
                    $first_attachment = array_shift($attachments);
                    ?>
                    <?php echo wp_get_attachment_image($first_attachment->ID, 'medium'); ?>

                <?php } else { ?>
                    <img src="/img/body/facebook_logo.gif">
                <?php } ?>

        </div>

<!--First post text-->

    <div class ="nfh">

        <?php

            //Show first post only
        add_filter('the_content', 'narga_excerpts');
            query_posts('showposts=1');
            $ids = array();
            while (have_posts()) : the_post();
            $ids[] = get_the_ID();
            //Add remove filter first image
            function remove_first_image ($content) {
            $content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
            return $content;}
            add_filter('the_content', 'remove_first_image');
            //Get post format               
            get_template_part( 'content', get_post_format() );
            //Remove remove filter first image
            remove_filter( 'the_content', 'remove_first_image' );
            remove_filter( 'the_content', 'narga_excerpts' );


            endwhile;
        ?>

    </div>
</div>
0

Use this code for the loop. I removed your comments and added some for clarification.

<?php
if ( have_posts() ) {

    // Do first post.

    add_filter( 'the_content', 'narga_excerpts' );
    add_filter( 'the_content', 'remove_first_image' );

    the_post();
    get_template_part( 'content', get_post_format() );


    // Do remaining posts.

    remove_filter( 'the_content', 'narga_excerpts' );
    remove_filter( 'the_content', 'remove_first_image' );

    while ( have_posts() ) {
        the_post();
        get_template_part( 'content', get_post_format() );
    }
}
?>
    </div>
</div>
<?php

// Add remove filter first image.
function remove_first_image( $content ) {
    return preg_replace( '/<img[^>]+\>/i', '', $content, 1 );
}

You didn't give enough details on the <div>s you want to wrap the posts with, so I left that part off. I also removed the function definition in the loop. Place your functions at the bottom of the file.

| improve this answer | |

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.