Custom Field on Post – Pull Post Title and URL
-
I have two Custom Fields being used below, with the same name
Name and Value of Custom Post
Featured-Blog – http://www.google.com
Featured-Blog – httP;//www.ebay.comThe code I am using to display these Custom Fields is below
<?php get_post_meta( $post->ID, 'Featured-Blog', true); ?> <a href=""> <?php the_title(); ?> </a>
I got it to pull the URL that I have in the Custom Field, and I was able to use the_title to grab the title of the URL. The problem is that It is only displaying one of the Custom Fields, and not them both. I was under the impression that you can have Custom Fields with the same name, so you can display more than one item(or link title in my case)
I used this quick guideline about Custom Fields and how to display them, and even multiple valued custom fields but I just can’t seem to get it.
http://css-tricks.com/snippets/wordpress/using-custom-fields/
-
looks like you forgot the foreach from the example on the css-tricks site. Hope this helps 🙂
<?php $featured_blogs = get_post_meta( $post->ID, 'Featured-Blog', true); ?> <?php foreach($featured_blogs as $featured_blog) { ?> // do something with $featured_blog such as echo $featured_blog <a href=""> <!-- use PHP code in between quotes to display URL --> <?php the_title(); echo $featured_blog; ?> </a> }
Hey thanks for your fast response. I did try out your code, had a little issue at first but I removed the { and }
It does pull the first custom field url and gives a clickable link with the title of the page which is indeed what I want, but the second custom field didnt seem to show up
The code is like this now. same as yours, but { and } removed
<?php $featured_blogs = get_post_meta( $post->ID, 'Featured-Blog', false); ?> <?php foreach($featured_blogs as $featured_blog) ?> // do something with $featured_blog such as echo $featured_blog <a href=""> <!-- use PHP code in between quotes to display URL --> <?php the_title(); echo $featured_blog;?> </a>
If removing braces worked you must already be in the ‘loop’. I didn’t post the entire code because I didn’t know the second variable 🙂
To simplify things, you could just use the exact same code twice in a row and just change the variables:
$featured_blogs to $name_of_second_field_array
$featured_blog to $name_of_second_field_valueYou can see here in the screenshot I have 3 different ways to try out what I’m trying to do. And you can see the results.
The very top one, I cannot figure out how to get it to just have Title of the URL. And the bottom ones that you came up with, do have it. Except yours shows the same url and post title. The one on the very top pulls both of the custom field urls, but it doesn’t have the title of the page URL.
I looked at the image. Now I have a better idea of what you are trying to do so in the morning I will try to post a solution 🙂
thanks for your effort. Im going to continue looking on the net for some other ways to do this.
You won’t be able to extract the title of URL because it is not in the database. You would need to have another custom field called featured_blog_post_title or something like that and assign it the title of the post.
/
Okay, I have the code pulling the custom fields URL and title of the url. Now I can’t seem to get it to show the second featured blog. Here is the working code.
Here is a screenshot in Actions:
<?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();?>
This code example here produces two results, which is almost what I want. Which is caused by the foreach I believe. I do not want to use the code below, but I need to find a way to add the foreach I think to get it to list all of the featured-blogs if I have more than one.
Screenshot of this one in action:
<?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/>"; ?>
- The topic ‘Custom Field on Post – Pull Post Title and URL’ is closed to new replies.