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
Code being used
<?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
<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/>";
?>
You simply have to pass
false
instead oftrue
when usingget_post_meta()
:With the
var_dump
you’ll be able to see the raw contents of the variable. Anyway, you will receive an array, so you can simply do:get_post_custom
, on the other hand, grabs ALL custom fields of a post and produces the same result at the end, it just takes an extra command for you to the the values.Note that you should not be using
query_posts
.