WordPress.org

Ready to get started?Download WordPress

Forums

Custom content above title outside the_content (4 posts)

  1. stevenknight
    Member
    Posted 1 year ago #

    Hello, I have been trying to figure this out for days and I can't.

    I would like to add a custom field when editing a post similar to how the title and content have separate fields to add custom content inside of a div above the title and outside of the_content.

    This is so I can have custom content depending on the post such as an image or YouTube video, and style it like I have for the featured posts on my homepage: http://stevenknight.info/blog.

    I have been able to do it on the homepage because it relies on the post thumbnail output, something I can't do for single posts because not all will have images (but they all will have a featured image on the homepage) I want above the title, sometimes it will be text or a YouTube video for example.

    Essentially I want a single post to be "split" up like so:

    Post:
    - Custom div content
    - Title
    - Content

    Crucially, only the last 10 or so posts will need the custom div as it's a new thing I'm doing, older posts will ignore the content div because it will be empty (or I can use some PHP to only display it on certain post IDs and above).

    I have been trying for *days* and cannot create a field to publish custom content outside of the the_content tag.

    Any help would be wonderful,

    Steven.

  2. bcworkz
    Member
    Posted 1 year ago #

    Oddly enough, what you want to use are Custom Fields. You then would modify your templates, using the functions and tags referenced in the above article to display the custom content where ever you wish.

    If you don't see the custom fields box in your edit screen, pull down the screen options tab and checkmark the appropriate option.

  3. stevenknight
    Member
    Posted 1 year ago #

    Thank you, I tried to work with Custom Fields for about two hours a few days ago but could not good it to format how I wanted, and when I added the meta tags into my template, all custom fields were published such as Disqus ID, and the Custom Field section in Edit Post would not work properly. I'll try again.

  4. stevenknight
    Member
    Posted 1 year ago #

    I worked it out, if anyone else comes across this issue I worked it out by creating the custom field then using php if else to determine if there's a value, and what it should output if there is or isn't.

    <header>
         <span class="blog-head"></span>
              <?php $values = get_post_custom_values("top-content");
    	        if (isset($values[0])) {
    	   ?>
    	   <div id="top-content">
    	        <?php $values = get_post_custom_values("top-content"); echo $values[0]; ?>
    	   </div>
    	    <h2 class="top">
                     <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
               </h2>
    	   <?php }
    	         else { ?>
    		     <h2>
                              <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                        </h2>
    	   <?php } ?>
    </header><!-- header ends -->

Topic Closed

This topic has been closed to new replies.

About this Topic