• Resolved Jim Bob

    (@usmanmalikx)


    I’m trying to insert a custom field into the_content.. I got these codes from around the web, can someone please help me combine the two.

    Code for the custom field

    <?php $source = get_post_custom_values("cf_Source");if ( !empty($source) ) {echo '' . $source[0].'';}else { echo '';}?>

    Code to add something after the_content in my template’s funtions.php

    function my_content($content) {
        $content .= 'My Content';
        return $content;
    }
    add_filter('the_content', 'my_content');

    I’m guessing you need to put the first code goes in where it says “My Content” but I don’t know exactly how to do that.

Viewing 11 replies - 1 through 11 (of 11 total)
  • I use the below code to add a copyright notice to all my posts and pages:

    function add_post_content($content) {
    	if(!is_feed() && !is_home() && !is_search()) {
    		$content .= '<p id=postCopyright><strong>Copyright &copy; <a href="'.get_bloginfo('url').'">'.get_bloginfo('name').'</a>, '.date('Y').'. All Rights Reserved.</strong></p>';
    	}
    	return $content;
    }
    add_filter('the_content', 'add_post_content');
    Thread Starter Jim Bob

    (@usmanmalikx)

    Thanks, that similar to what I’m looking for.. Do you know how to output

    <?php $source = get_post_custom_values(“cf_Source”);if ( !empty($source) ) {echo ” . $source[0].”;}else { echo ”;}?>

    via the $content ??

    use the below:

    <?php
    function add_post_content($content) {
    	if(!is_feed() && !is_home() && !is_search()) {
            $source = get_post_custom_values("cf_Source");
            if ( !empty($source) ) {
                $content .= '' . $source[0].'';
            } else {
                $content .= '';
            }
    	}
    	return $content;
    }
    add_filter('the_content', 'add_post_content');
    ?>
    Thread Starter Jim Bob

    (@usmanmalikx)

    THANK YOUU! I think that’s a very good code for anyone who is trying to load custom fields through a function…

    here is the final code that I’m using after slightly modifying yours to show the $source as a link.

    function add_post_content($content) {
    	if(!is_feed() && !is_home() && !is_search()) {
            $source = get_post_custom_values("cf_Source");if ( !empty($source) ) {
            $content .= '<a href="'.$source[0].'">Click Here to read more</a>';} else {
            $content .= '';}}
    	return $content;
    }
    add_filter('the_content', 'add_post_content');

    Thanks again tiaanswart.

    cool

    Thread Starter Jim Bob

    (@usmanmalikx)

    I’m running into one small problem… I modified the code so that the .$source[0]. shows up as a link but then it shows up on posts that dont the custom field of cf_source. If I use the code that you have me it works fine its just that when I make the changes it gives that problem. Can you please so me how I can have the code you gave me come up as a href if it exists if not, it should be empty. Thanks.

    like so:

    <?php
    function add_post_content($content) {
    	if(!is_feed() && !is_home() && !is_search()) {
            if ( get_post_meta($post->ID, 'cf_Source', true) ) {
                $source = get_post_custom_values("cf_Source");
                if ( !empty($source) ) {
                    $content .= '' . $source[0].'';
                } else {
                    $content .= '';
                }
            }
    	}
    	return $content;
    }
    add_filter('the_content', 'add_post_content');
    ?>
    Thread Starter Jim Bob

    (@usmanmalikx)

    That didn’t work.. The original one you sent me worked fine, the only thing I want to change is for it to be a link with href tag.

    try this:

    <?php
    function add_post_content($content) {
    	if(!is_feed() && !is_home() && !is_search()) {
            $postid = get_the_ID();
            if ( get_post_meta($postid, 'cf_Source', true) ) {
                $source = get_post_custom_values('cf_Source');if ( !empty($source) ) {
                $content .= '<a href="'.$source[0].'">Click Here to read more</a>';} else {
                $content .= '';}}
            }
    	return $content;
    }
    add_filter('the_content', 'add_post_content');
    ?>
    Thread Starter Jim Bob

    (@usmanmalikx)

    Thank you for the code. Really that was very helpful thanks a lot.

    — and anyone else who might use this code… you can adjust where to show the custom field by editing this 000 at the end. Use the “1” if you want it immediately after the text of the content.

    add_filter('the_content', 'add_post_content', 000);

    pleasure

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Combine these two codes for me please’ is closed to new replies.