开发者

Including Custom Fields & static text in the_content in WordPress

I am editing a Custom Post Type template, and am using custom fields to enter info into a meta box to be included on the page, as well as include some static default text on all the pages.

I basically need to "chunk" together the post info in the_content along with the static text and some meta box info. Here's what I want:

I have plugins that add social buttons before the_content and a signature after the_content so I am trying to figure out how to get all my custom stuff sandwiched in between those.

If I just add the meta boxes i nthe template, they display outside of the_content and the plugins display in unwanted places.


I ended up figuring this out on my own. The solution: using functions.php and add_filter, I had to create a new function to create the default content, and it works great.

here's the general code for anyone interested:

function custom_post_type_default_content($content) {
global $post;
    if ($post->post_type == 'your-custom-post-type') {
    $content .= '<p> '. get_post_meta( $post->ID, "metabox-1-slug", true ).' 
    <br />
    <p> '. get_post_meta( $post->ID, "metabox-2-slug", true ).'</p>
    <p>YOUR TEXT HERE.</p>';
}
add_filter('the_content', 'custom_post_type_default_content', 0);

Note that the zero just near the end controls placement. I have a social media plugin that has a priority of "1", and to get the default content to appear above that I have to make this a priority of "0".

Also note the single apostrophes that open and close the code following $content .=

You basically add whatever you want between those apostrophes, and in this case I am pulling metabox info which have their own apostrophes containing code. It gets confusing!

In other words, your code should be $content .='YOUR CUSTOM CONTENT' and within those apostrophes, add your text, code, etc. The standalone metabox code is '. get_post_meta( $post->ID, "metabox-1-slug", true ).' which is nested inside where the YOUR CUSTOM CONTENT text is.

I am basically explaining this to myself, as these were the things that tripped me up so figured would explain them in detail to help someone else like me. Or me when I have to go look this up again!


Post your single.php here or on pastebin along with the custom field names you're using (& where you want them) and I'll try to help you figure out what you want.


Copy this code to your function.php file.

function content_function_update($content) {

    global $wp_query, $post;

    if ($post->post_type == 'your-custom-post-type') {

       $postid = $wp_query->post->ID;   
       $value1 = "your value 1";

       if($value1 !== '') {
          $content = $content . "<br>" . $value1 
       }
       else {
          $content = $content;
       }
    }

    return $content;
}

add_filter('the_content', 'content_function_update');

Add any custom content in variables and append it to $content variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜