WordPress.org

Support

Support » How-To and Troubleshooting » place the_title inside the_content

place the_title inside the_content

  • Hello,

    I am trying to place the ‘the_title()’ inside the ‘the_content()’ but I can’t figure it out for the life of me.

    I currently have:

    <h2>Post Title</h2>
    <div class=”entry”>
    <p><img src=”example” />The post text</p>
    </div>

    I would like to have:

    <div class=”entry”>
    <p><img src=”example” /><h2>Post Title</h2>The post text</p>
    </div>

    any help would be greatly appreciated

Viewing 15 replies - 1 through 15 (of 21 total)
  • I think what I’m really trying to do is put the image before the title and text, for instance:

    <div class=”entry”>
    <img src=”example” />
    <p><h2>Post Title</h2>The post text</p>
    </div>

    I’m currently looking into writing an add_filter, but I don’t see how it will help.

    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');
    ?>

    thanks vtxyzzy.

    but i dont think that will solve my problem. The <img> tag is inserted inside of the <p> tag. i.e. <p><img>some text</p>

    is it possible to define the ‘some text’ part. to make myself clear, i would like to place my post title next to / below my picture just like on playgrounder.com and uncrate.com. if this is possible with only the use of css, that would be great,but i dont think that is possible either.

    please help!

    The code above will allow you to place the title within the post content (which is what you asked for), please read the reply again.

    If that’s not exactly what you mean, please clarify in more detail so vtxyzzy is able to understand better what it is you mean.

    With vtxyzzy’s code, you’d write your post as normal but then add this inside the content of the post where you want the title..

    [mm-insert-title before='<h2>' after='</h2>']

    What t31os_ said.

    Following your example, you would code:

    <p><img>[mm-insert-title]some text</p>

    No, t310s_ thats not what I asked for, please reread MY post. I am trying to rearrange the tags, not find some hack.

    Thanks vtxyzzy for your answer. I find it completely ridiculous that one cannot cleanly edit the layout of the post (one of the biggest parts of the blog itself).

    My guess is that you CANNOT cleanly edit the post and instead of saying something like “not possible, you have to find a workaround”, you get little a-hole moderators.

    Example:
    http://wordpress.org/support/topic/307410?replies=2

    Did I hear a 9er in there?

    Does the <img src=”example” /> is an image uploaded with the edit post in wordpress?

    S.

    Simon,

    When you insert an image in your post, it places the <img src=”example” /> tag inside you post.

    Regular Post:
    <p>The post text</p>

    Post with Image:
    <p><img src=”example” />The post text</p>

    Yes yes yes… Ok… So basically you want to put the image before the title and the text…. (the title of this thread is not clear about that :-))

    To do this, you should use a function to make a custom template tag and edit the loop in the template files (index.php/single.php/etc.)

    [1] Edit / create a function.php in your theme directory.

    [2] paste this function in function.php :

    function my_image_tag() {
    global $wpdb;
    global $post;
    $my_post_image = $wpdb->get_var("SELECT ID FROM $wpdb->posts where post_parent= $post->ID and post_type = 'attachment'");
    if ($my_post_image == 0) {
    echo "";
    }
    else {
    echo wp_get_attachment_image()($my_post_image, $size='large', $icon = false);
    }
    }

    This function retreive the first image attached to a post. Look at the last line and change $size='large' for the format you want to use (large/medium/thumbnail).

    [3] Save your function.php and upload it in your theme directory.

    [4] Now, you have a custom template tag you can use in your loop : <?php my_image_tag(); ?>

    [5] When you upload your first image in your post, DO NOT insert it in the post itself. It is “attached” to the post in the database. Juste upload the image and that’s all…

    [6] Then, in you loop, you go that way :

    <div class=”entry”>
    <?php my_image_tag(); ?>
    <h2><?php the_title(); ?></h2>
    <?php the_content(”); ?>
    </div>

    Of course, you can wrap my_image_tag in a div if you like to do so…

    It should do the trick. Edit the loop in all the template file where you want the image to be displayed : single.php, index.php, category.php…

    You can even make multiple functions (my_thumbnail_tag, my_medium_tag, etc) with different format… All you have to do is to edit the size in the function as I explained…

    Hope this help.

    @+

    S.

    Oh… And I forgot…

    This function will display nothing if there is no image attached to a post…

    And if you want to wrap the picture in a div, it’s better to do it in the function itself to prevent an empty div if there is no image…

    Like this

    else {
    echo '<div class="my_picture_div">';
    echo wp_get_attachment_image()($photo, $size='large', $icon = false);
    echo '</div>';
    }

    @+

    S.

    I am trying to place the ‘the_title()’ inside the ‘the_content()’ but I can’t figure it out for the life of me.

    I can’t see how that can be read any other way.. that’s exactly what the example code would have done..

    No, t310s_ thats not what I asked for, please reread MY post. I am trying to rearrange the tags, not find some hack.

    I can’t see how tags are related, so i can only assume when you say tags you mean.. the_title() and the_content() , which although could be called tags, are in PHP terms, function calls. It’s not essential you use those particular function calls(tags) if you have needs that require a custom layout …

    Sorry if i misunderstood, Simon knew what you meant though so i’ll leave you to it … 😉

    He he… You did’nt misunderstood t31os_… 🙂 As I said, the title of the thread is really not clear.

    But in his second message : “I think what I’m really trying to do is put the image before the title and text”

    So I guess this is what he want to do… 🙂

    S.

    yes “place the_title inside the_content” is not clear at all (Gay Smiley Face). Functions and tags are the same thing when the functions PRODUCE those tags, but thanks for unwittingly pointing that out.

    Am I supposed to tell a client to insert [mm-insert-title before='<h2>’ after='</h2>’] everytime they post??

    A custom layout? Because a BLOG cms will not let you change the layout of the BLOG’s post. Thats a joke. I’m just going to do what I should have done in the first place, use CI.

    Thanks WordPress.

    I was actually trying to be polite before, obviously my posts failed to come across as friendly…. i even apologised in my second response, yet you seemed to take it personally.

    We misunderstood each other … so what, let’s move on and solve your problem, yes? … or have you had enough?

    🙂

    mtokoly… We explain how to do what you want…

    If you’re not able to do it, don’t blame wordpress, blame you skills.

    With the solution I gave you, you should manage to do what you want.

    And of course, you can change the layout of a blog post… Just edit the template files… (!?)

    I really don’t understad your point.

    Did you try my solution?

    @

    S.

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘place the_title inside the_content’ is closed to new replies.