Ask your WordPress questions! Pay money and get answers fast! (more info)

Custom Fields and Thematic WordPress

  • SOLVED

I'm using child theme of thematic and struggling to get custom fields displayed. I can create the custom field easily, in the wordpress create new post, but it doesn't display - I need to use some php and I have no idea what to use or where to put it.

Answers (5)

2012-01-16

Ivaylo Draganov answers:

Hello,

Thematic employs heavily the so called "hooks" - they allow you to insert code and change things without touching the template files. Please specify where do you want to have the custom fields displayed - on single posts, on category archives or elsewhere? And if you can tell which theme are you using (if not a custom one) it would be even easier to help you.


dv1961 comments:

Hi, it is a custom theme.

I'd like to display the custom fields on single posts and on each post summary on the blog page. I hope that makes sense


Ivaylo Draganov comments:

<blockquote>
I'd like to display the custom fields on single posts and on each post summary on the blog page.</blockquote>

Then the hook "thematic_belowpost" might be the one. Try this code:

// append content via Thematic hook
function wpq_custom_fields_content( $content ) {

global $post;

echo get_post_meta( $post->ID, 'custom_field_name', true );

}

add_action( 'thematic_belowpost', 'wpq_custom_fields_content' );


To see where action hooks in Thematic appear check out this visual guide: [[LINK href="http://visualizing.thematic4you.com/"]]http://visualizing.thematic4you.com/[[/LINK]]

Or perhaps the native WP hooks "the_content" and "the_excerpt" could work better for you. If you're not satisfied with the placement of the custom field content, then try something like that (it will append the custom field right after the post content/summary):

// append content via WordPress native hooks
function wpq_custom_fields_content_2( $content ) {

global $post;

$custom_field_content = get_post_meta( $post->ID, 'custom_field_name', true );

return $content . $custom_field_content;

}

add_filter( 'the_content', 'wpq_custom_fields_content_2' );
add_filter( 'the_excerpt', 'wpq_custom_fields_content_2' );


dv1961 comments:

The first one works great, but can you explain how to wrap a div round custom field?
Thanks


Ivaylo Draganov comments:

<blockquote>The first one works great, but can you explain how to wrap a div round custom field?</blockquote>

You can include any markup(and not just) in the function. Like this:

// append content via Thematic hook
function wpq_custom_fields_content( $content ) {

global $post;

$r = '';

$r .= '<div>';
$r .= get_post_meta( $post->ID, 'custom_field_name', true );
$r .= '</div>';

echo $r;

}

add_action( 'thematic_belowpost', 'wpq_custom_fields_content' );


<em>Using a . sign before the = means that you are appending to the existing string. </em>

2012-01-16

Francisco Javier Carazo Gil answers:

Hi dv1961,

In the loop, first take the id of a post:
$post_id = get_the_ID();

And then:
$location = get_post_meta($post_id, '_location', true);

Where "_location" is the key of meta field.


Francisco Javier Carazo Gil comments:

If you have any question, tell me.


Francisco Javier Carazo Gil comments:

Well, if you are in your single.php, out of the loop, this is the same but you get the id in another way:
$post_id = get_the_ID();


Francisco Javier Carazo Gil comments:

Hi dv1961,

To override loop in your child theme, you have to create another "index.php" with the loop inside it in the child theme.


dv1961 comments:

I don't know where to put this code. I'm not in single.php.

I thought I had to create child loop in functions.php and in that loop put the bit about the custom field, so that every post would display the custom field value if there was one to display.

But I'm not sure how to create the thematic child loop or what to put in the thematic child loop to call the custom field


Francisco Javier Carazo Gil comments:

Well, you should create a child theme and then create in your child theme a file "index.php". In this moment you have to copy&paste original loop from parent index.php.

Here you can insert the first code that I have given you.

Need more help? If you give me credentials and specifications I can do it in a moment.

2012-01-16

Hai Bui answers:

Where do you want to display the custom field? I mean on what page?


dv1961 comments:

It would be in various posts, I was planning to use a plugin to list all the posts in a category.

I think what I need is to create a child loop in my functions.php and in that loop call the custom fields. Is that correct?


Hai Bui comments:

If you want to display it in a loop, do this:


// The Query, you have to change the $args per your needs
$the_query = new WP_Query( $args );

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
//doanything here
$custom_field_value = get_post_meta(get_the_ID(), 'custom_field_name', true);
//print the custom field
echo $custom_field_value;
endwhile;

// Reset Post Data
wp_reset_postdata();

2012-01-16

Monit Jadhav answers:

Firstly, you need to decide where to use the custom field you are creating.

Then if you want to use it in the wordpress loop simply use

<strong>get_post_meta($post_id, 'custom_field_key', true);</strong>

note: Inside the wordpress loop use get_the_ID() to retriev the post ID. If you are using a custom field outside the loop you can use the

<strong>global </strong>$post;

get_post_meta(<strong>$post->ID</strong>, 'custom_field_key', true);

here <strong>$post_id</strong> means the id of the current post

<strong>custom_field_Key</strong> is the name you assigned to your custom field

if you want to return exact one value of the custom field use <strong>true</strong>, if you want to use same custom field key with different values set the third parameter to <strong>false</strong>.

If you just want to display the custom field value add <strong>echo </strong>in front of it like below

<strong>echo</strong> get_post_meta($post->ID, 'custom_field_key', true);

If you want to use the custom field value and assign it some variable you can do as below

$my_custom_field = get_post_meta($post->ID, 'custom_field_key', true);

So that's how you can output custom fields. Let me know if the answer helped & if you need anything else.

Monit



Monit Jadhav comments:

If you want to display posts in a single category, where do you intend to use custom field.

2012-01-17

Kannan C answers:

Thematic is employed with many action hooks for different locations of the layout. All content hooks are located in thematic/library/extensions/content-extensions.php
If you want to show content in index, you can do something like

//this should go in your functions.php
function loop_extra_content() {
$key = 'yourmetakey';
echo get_post_meta(get_the_ID(), $key, true);
}
add_action('thematic_above_indexloop','loop_extra_content');

to display in single post, you can make use of the hook named <strong>thematic_singlepost</strong>
This post will guide you how to use action hooks in a child theme http://themeshaper.com/2009/05/25/action-hooks-wordpress-child-themes/