0

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.

2 Answers 2

1

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.

7
  • 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?
    – M1lls
    Commented Jun 7, 2013 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
    Commented Jun 7, 2013 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>
    – M1lls
    Commented Jun 7, 2013 at 21:34
  • @Mills : Your PHP string concatenation/manipulation is badly broken. See the edit.
    – s_ha_dum
    Commented Jun 7, 2013 at 21:42
  • 1
    @Mills : The HTML is markup not markdown :) and the PHP is just syntax. :) The code in my edit should give you a link. Does that not work? If not, what is wrong?
    – s_ha_dum
    Commented Jun 10, 2013 at 18:23
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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