Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using WordPress 3.5.1 / Twenty Twelve Theme. I would like to display a custom field in my Category Archives. I have the following custom field working in my single.php , it displays a custom url / title

<a href="http://<?php echo get_post_meta($post->ID, "user_submit_url", true); ?>" target="_blank">  <h1 class="entry-title"><?php the_title(); ?> </a>

I would like to put the following code underneath the "entry-title" in Category Archives. I am confused of were to put the following php code, either in category.php or archive.php? Since I need to put this code inside the loop, I want the code to register on every post just like it registers in the single.php in the example I provided above. Something like the following, I presume?

get_post_meta($post->ID, "user_submit_url", true);

Let me know if I need to clarify anything.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

If your Loop is constructed correctly $post will be set for each post in the loop in turn. You could also consider using get_the_ID.

get_post_meta($post->ID, "user_submit_url", true); by itself won't do much. You will have to format and echo the information but that should retrieve your post meta data.

For example, to generate a link (assuming your meta value is an URL and that you've save a string and not an array):

$meta = get_post_meta($post->ID,'user_submit_url',true); 
echo '<a class="meta" href="'.$meta.'" target="_blank">'.$meta.'</a>';

If your theme has a category.php and you want this only on category archives put that code in category.php. archive.php is used by all archives that do not have a specialized template-- tag and author archives, for example. The template hierarchy is explained in the Codex.

share|improve this answer
    
Thanks, so it seems I have to format and echo the "get_post_meta" code that I am using and put that in category.php , what is the proper was to format the code so I can display it in the Category.php loop? –  Mills Jun 7 '13 at 21:18
    
The "proper" way to format it depends entirely on what you want the result to be. I can't answer that. get_post_meta doesn't echo anything. You need to grab the data-- $meta = get_post_meta($post->ID, "user_submit_url", true);-- and do something with it. What you do with it is up to you. –  s_ha_dum Jun 7 '13 at 21:24
    
I would just like to display the user_submit_url inside the category loop. Something like <code> $meta = get_post_meta($post->ID, user_submit_url, true); <a class=meta href= $meta; target=_blank> $meta; </a> </code> –  Mills Jun 7 '13 at 21:34
    
@Mills : Your PHP string concatenation/manipulation is badly broken. See the edit. –  s_ha_dum Jun 7 '13 at 21:42
    
Hey thanks, I'm still trying to learn the markdown, basically I want to display (echo) the custom link in a hyperlink, the same way I do in my single.php example –  Mills Jun 10 '13 at 18:12
show 2 more comments

I would like to display a custom field in my Category Archives.

So, you have to put the code into archive.php.

I want the code to register on every post just like it registers in the single.php

If I'm not wrong, you need the custom field to be shown in every pages where the post is echoed. If so, you have to follow the following steps specific to TwentyTwelve:

In T12, the post-format is generally called by content.php with this code:

get_template_part( 'content', get_post_format() );

So to add the custom field, you have to edit the content.php. Where the visibility of certain things are moderated with conditions like is_home(), is_search() etc. You have to add the custom field code into the perfect block of code specific to the archive page.

And remember: Custom field is Case-sensitive. So user_submit_url is not equal to User_submit_url.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.