• I have two “Custom Fields” assigned to a post I have. Both of these “Custom Fields” have the same name, but different “Value”. At the moment, my code below only presents one of the links. I am trying to get it to display both. So anytime I add another “Custom Field” with the name of “Featured-Blog”, it will continue display all of them.

    Custom Field’s
    1) Name: Featured-Blog and Value: 704 (704 is the postID)
    2) Name: Featured-Blog and Value: 699 (699 is the postID)

    Code being used to display a link to each of the posts. (can only get one of the custom fields to display)

    Screenshot of output
    Screenshot

    <?php $related = get_post_meta($post->ID, "Featured-Blog", $single=true);
    
    		$related=explode(',',$related);
    		$args = array_merge( array('post__in'  => $related, $wp_query->query ) );
    		query_posts($args);
    		if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    		<div id="<?php the_ID(); ?>">
    			<a href="<?php the_permalink();?>"><p class="caption"><?php the_title(); ?></p></a>
    		</div>
    
    	<?php endwhile; else: ?>
     	<p>no related</p>
    	<?php endif; wp_reset_query();?>

    Now below is an older code that I was originally trying to use, but didn’t end up using. This one actually does pull both of my “Custom-Fields”. You can see it’s obviously coded different because you can see it says “Title” instead of the posts title. But I am just using this code as an example to show you that more than one “Custom Field” can be displayed, unless there is an easy fix for the code below?. Maybe some of the code form that can be incorporated into my working script above. Both the above code, and this bottom one are very close to what I’m trying to do. It seems like one, has something the other one needs.

    Screenshot of output
    Screenshot

    <div id="related-posts">
    <?php
      $custom_fields = get_post_custom($post_id); //Current post id
      $my_custom_field = $custom_fields['Featured-Blog']; //key name
      foreach ( $my_custom_field as $key => $url )
     echo $key ="<a href='".$url."'>TEST</a><br /><br /><br/>";
    ?>
Viewing 1 replies (of 1 total)
  • I think you need to change this:

    <?php $related = get_post_meta($post->ID, "Featured-Blog", $single=true);
    
    		$related=explode(',',$related);
    		$args = array_merge( array('post__in'  => $related, $wp_query->query ) );

    to this:

    <?php $related = get_post_meta($post->ID, "Featured-Blog");
    
    		$args = array_merge( array('post__in'  => $related, $wp_query->query ) );
Viewing 1 replies (of 1 total)
  • The topic ‘Displaying more than one Custom Field for a post’ is closed to new replies.