• Hi all, still got a way to go to learn wordpress but one little thing ive been trying to figure out for a while is this problem:

    at the minute i use this code in the loop

    <h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    <?php the_content("read more..."); ?>

    this display the title, then the image, then the post info. I am trying to get the Image, then the Title, and then the post info.

    how do i go about this?

    thanks
    Dan

Viewing 6 replies - 1 through 6 (of 6 total)
  • What image? nothing in your code displays an image…. an image in the content? Part of your theme?

    Thread Starter meshdesign

    (@meshdesign)

    sorry, yes the image is in the post, so i presume it is in the content.

    my site this is what i am currently working on so you can have a better idea.

    thanks

    Thread Starter meshdesign

    (@meshdesign)

    still not managed to sort this one out.

    if anybody can help me would be much appreciated

    thanks
    dan

    Because the image is in the content, the only way to get the title below it is to code the title in the content between the image and the rest of the text (and remove it from the php code).

    You could code something like this into the content of each post:

    <h3><a href="https://mydomain.com/mylink" title="My Post Title">My Post Title</a></h3>

    but, you will have to do this for every post.

    Another alternative would be to write a content filter to look for an image at the first of the content and insert the title. This could be tricky if any posts have images that are not at the front of the post.

    A third possibility would be to use shortcodes to insert the title. This is probably the best solution.

    Adding a shortcode is one way to to it. Add the code below to your theme’s functions.php. Then, place the shortcode [mm-insert-title] in your post where you want the title to appear. You can use the before and after parameters to put code before and after the title. For example, [mm-insert-title before='<h2>' after='</h2>'] will place the title inside h2 tags.

    function mm_insert_title_func($atts) {
       // Insert the post title, with optional before and after strings.
       // [mm-insert-title before='<h3>' after='</h3>']
       global $post;
       extract(shortcode_atts(array('before' => '', 'after' => '' ), $atts));
       $output = "{$before}{$post->post_title}{$after}";
    
       return $output;
    }
    add_shortcode('mm-insert-title', 'mm_insert_title_func');
    ?>

    Hey vtxyzzy, Thanks for that shortcode, it works great. Just a quick correction to the code though, you’ll want to remove the ?> at the end as it’s the closing PHP code (a simple copy/paste slip).
    On another note, would you know how one might maintain the hyperlink in the title? Is there maybe a lovely little line of code that we could park inside the function to do that? You’ve already been a great help, thanks in advance for any more you can offer.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘trying to get post Title below Image and above Content’ is closed to new replies.