WordPress.org

Ready to get started?Download WordPress

Forums

I need HELP re-arranging the content of my posts (4 posts)

  1. entasis
    Member
    Posted 2 years ago #

    Hi there,

    I'm trying to get my posts to show up with the first image of the post at the top, then the title, then the post text, then the rest of the images.

    |img |
    |title|
    |text |
    |img |
    |img |
    |img |
    |.....|

    I'm somewhat succeful in breaking down the posts into parts and rearranging them.

    <?php get_header();?>
    		<?php if (have_posts()) : ?>
    		<?php while (have_posts()) : the_post(); ?>
    		<div class="post_single" id="post-<?php the_ID(); ?>">
    		<?php
               $beforeEachImage = "<div>";
               $afterEachImage = "</div>";
               preg_match_all("/(<img [^>]*>)/",get_the_content(),$matches,PREG_PATTERN_ORDER);
               for( $i=0; isset($matches[1]) && $i < count($matches[1]); $i++ ) {
                     echo $beforeEachImage . $matches[1][$i] . $afterEachImage;
               }
    ?>
    <div id="post">
    <h2 class="small"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a> <?php edit_post_link('- Edit Post'); ?></h2>
    			<br><div class="postmetadata-top"><small><?php the_date('F d, Y'); ?> </small></div>
    </div>
    <div class="content"><?php
       ob_start();
       the_content('Read the full post',true);
       $postOutput = preg_replace('/<img[^>]+./','', ob_get_contents());
       ob_end_clean();
       echo $postOutput;
    ?>
    </div>
    		</div>
    		<?php endwhile; ?>
    		<?php endif; ?>
    <?php get_footer();?>

    Using the above code I get this:

    |img |
    |img |
    |img |
    |img |
    |.....|
    |title|
    |text |

    I tried using

    <?php echo catch_first_image() ?>

    but that just gives me a link to the image and doesn't display the image.
    Also how would I adress the remaining images to be shown at the bottom of the post?

    Best regards,

    thf

  2. vtxyzzy
    Member
    Posted 2 years ago #

    I have no way of testing this, but try replacing this:

    for( $i=0; isset($matches[1]) && $i < count($matches[1]); $i++ ) {
       echo $beforeEachImage . $matches[1][$i] . $afterEachImage;
    }

    with this:

    $first_image = $matches[1][0];
         if ( $first_image ) echo $beforeEachImage . $first_image . $afterEachImage;

    and this:

    $postOutput = preg_replace('/<img[^>]+./','', ob_get_contents());
    ob_end_clean();

    with this:

    $postOutput = ob_get_clean();
    if ( $first_image ) $postOutput = preg_replace('#' . $first_image . '#','', $postOutput);
  3. entasis
    Member
    Posted 2 years ago #

    Muahaha! It works! You are my coding God! I worship you!

    (an additional question: The first image isn't clickable - which is fine - but the rest are. Can that be disabled?

    Thank you so much!

    Sincerely,

    thf

  4. vtxyzzy
    Member
    Posted 2 years ago #

    You might need to create a custom template for that. The exact instructions depend on your theme.

    If you are using a free theme, post a link to your site here and someone may be able to help.

    If you are using a purchased theme, we cannot examine its code or options. You should contact the theme supplier for help.

Topic Closed

This topic has been closed to new replies.

About this Topic