• Hello, I’m new in wordpress and I have simple question.

    the_content() – this display whole post with first image, if image is added. How can I put title between first image and text?

    I set something like this:

    <?php
    				if( has_post_thumbnail() ) :
    					$content = get_the_content();
    					$content = preg_replace("/<img[^>]+\>/i", "", $content);
    					$content = apply_filters('the_content', $content);
    					$content = str_replace(']]>', ']]>', $content);
    					echo $content;
    				else :
    					the_content();
    				endif;
    			?>

    So when there is thumbnail I set only text – and here there is no problem with title. But when thumbnail isn’t setted, I just can’t put title between first image from content and text.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi There,

    Is there a particular reason you need to put the title between the first image in the content and the rest of the content? This IS possible with regular expressions like you’re exploring in your code above, but it is more usual to use a featured image. I would recommend uploading a featured image to your post and using this code:

    if ( has_post_thumbnail() ) {
        the_post_thumbnail();
    }
    
    the_title( '<h2>', '</h2>' );
    
    echo '<div class="post-content">';
    the_content();
    echo '</div>';

    If you must use the first image I recommend using get_the_post_thumbnail() to store the thumbnail if there is a post thumbnail and using your regular expression method if there isn’t:

    if ( has_post_thumbnail() ) {
        $thumbnail = get_the_post_thumbnail();
    }
    else {
        $thumbnail = // figure it out with regexp
    }
    
    echo $thumbnail;
    
    the_title( '<h2>', '</h2>' );
    
    echo '<div class="post-content">';
    the_content();
    echo '</div>';
    Thread Starter quomodo

    (@quomodo)

    This doesn’t change anything – the_content() will also put image if there is any and $thumbnail will put image … so I will have two images …

    Is there a particular reason – Yes, I want to have image on top, then title and then text … ??

    When somebody won’t set thumbnail (forget, do not know about it) but put image in post – I want to display it like : first image -> title -> text.

    Ah, sorry, I wasn’t thinking ??

    What you can do is use regular expressions to split the content into three parts:

    – text before image
    – image
    – text after image

    If you create the correct regexp and then concatenate the text from before and after the image you should end up with two variables, for example: $thumbnail and $content. The content will the n contain only the text and not the image.

    I would advise against doing this though, simply because of all the different situations you need to take into account. What if there are no images, what if there is an image but the first one is a 16×16 icon? Will this all work if the first image is 800×140 and if it is 250×250?

    It may be better to set a fallback image which is used if no featured image is set. This can be set globally, or perhaps for each category if you would like more variation.

    Daniel

    Thread Starter quomodo

    (@quomodo)

    ok You give good logic earlier – I think I get it …

    <?php if( has_post_thumbnail() ) : ?>
    			<figure class="post-thumbnail">
    				<a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_post_thumbnail(); ?></a>
    			</figure>
    
    			<?php else : ?>
    			<figure class="post-thumbnail">
    				<a href="<?php the_permalink(); ?>" rel="bookmark"><img src='<?php echo catch_that_image() ;?>'></a>
    			</figure>
    
    			<?php endif; ?>
    
    			<h2 class='entry-title'>
    				<a href='<?php the_permalink(); ?>' title='<?php the_title_attribute(); ?>' rel='bookmark'>
    					<?php the_title(); ?>
    				</a>
    			</h2>
    <?php
    
    	$content = get_the_content();
            $content = preg_replace("/<img[^>]+\>/i", "", $content);
    	$content = apply_filters('the_content', $content);
    	$content = str_replace(']]>', ']]>', $content);
    	echo $content;
    
    ?>

    Thanks for Your time ??

    Thread Starter quomodo

    (@quomodo)

    But now I got title up to for example iframe youtube ?? … eh … learning is always hard ??

    Yes, there will be many problems with that kind solution … better to use thumbnail and fallback image if there isn’t any .

    Agreed ?? That’s also why I didn’t give you specific regexp help, I’m not that good at it, there are just SO many variables ??

    Good luck

    Daniel

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Put Title between content and image’ is closed to new replies.