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

Thematic and custom fields in post meta

  • SOLVED

I'm a beginner with php so I'm sure even how to ask this question! I'm using a child theme of Thematic

I want to display a custom field in the post meta, after the author and publication date.

I'd like it to be in a span, as with the rest of the post meta, and I'd like it to only be displayed if the post is in a certain category. And I'd like it to be displayed on the blog page and on the sinlgle post. Any ideas?
Dv

Answers (7)

2012-01-19

Ivaylo Draganov answers:

Hello,

you can do that by overriding the Thematic function that prints the postmeta. It's easy to do and it is one of the intended ways to do such things in Thematic. Try this in your functions.php:

<?php

function childtheme_override_postheader_postmeta() {

global $post;

$postmeta = '<div class="entry-meta">';
$postmeta .= thematic_postmeta_authorlink();
$postmeta .= '<span class="meta-sep meta-sep-entry-date"> | </span>';
$postmeta .= thematic_postmeta_entrydate();

$postmeta .= thematic_postmeta_editlink();

// custom field wrapped in <span>; only if post is in certaint category (1 is the category ID)
if( in_category( 1, $post->ID ) ) {
$postmeta .= '<span class="meta-sep"> | </span>';
$postmeta .= '<span>';
$postmeta .= get_post_meta( $post->ID, 'custom_field_name', true );
$postmeta .= '</span>';
}

$postmeta .= "</div><!-- .entry-meta -->\n";

return apply_filters('thematic_postheader_postmeta',$postmeta);

}

2012-01-19

Hai Bui answers:

It's simple. Put this in functions.php

function my_new_header($postheader) {
global $post;
if ( in_category( 'category-name-here' )) {
$postheader.= '<span>'.get_post_meta($post->ID, 'custom_field_key', true).'</span>';
}
return $postheader;
}

add_filter('thematic_postheader','my_new_header');

2012-01-19

Francisco Javier Carazo Gil answers:

Hi dv1961,

If you want to show for only some categories, use this:

if ( in_category( 'pachyderms' )) {
// They have long trunks...
} elseif ( in_category( array( 'Tropical Birds', 'small-mammals' ) )) {
// They are warm-blooded...
} else {
// & c.
}


Francisco Javier Carazo Gil comments:

To show the meta:

<?php $key_1_values = get_post_meta(76, 'key_1'); ?>


Where 76 is the post_id and the other value is the key of meta.


Francisco Javier Carazo Gil comments:

To set a meta box in dashboard, in your functions.php:

<?php
/* Define the custom box */

add_action( 'add_meta_boxes', 'myplugin_add_custom_box' );

// backwards compatible (before WP 3.0)
// add_action( 'admin_init', 'myplugin_add_custom_box', 1 );

/* Do something with the data entered */
add_action( 'save_post', 'myplugin_save_postdata' );

/* Adds a box to the main column on the Post and Page edit screens */
function myplugin_add_custom_box() {
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_inner_custom_box',
'post'
);
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_inner_custom_box',
'page'
);
}

/* Prints the box content */
function myplugin_inner_custom_box( $post ) {

// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );

// The actual fields for data entry
echo '<label for="myplugin_new_field">';
_e("Description for this field", 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="whatever" size="25" />';
}

/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times

if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
return;


// Check permissions
if ( 'page' == $_POST['post_type'] )
{
if ( !current_user_can( 'edit_page', $post_id ) )
return;
}
else
{
if ( !current_user_can( 'edit_post', $post_id ) )
return;
}

// OK, we're authenticated: we need to find and save the data

$mydata = $_POST['myplugin_new_field'];

// Do something with $mydata
// probably using add_post_meta(), update_post_meta(), or
// a custom table (see Further Reading section below)
}
?>

2012-01-19

Arnav Joy answers:

ok tell me exactly what do you want?
I am here .


Arnav Joy comments:

for single page display use

if(is_single()){
//your custom field

}

and for blog page

if(is_page(PAGEIDOGBLOGPAGE)){
//your custom field
// content

}

2012-01-19

Fahad Murtaza answers:

A good start is this, the first step is creating the meta box. This tutorial should help.

http://www.farinspace.com/how-to-create-custom-wordpress-meta-box/

2012-01-19

rizaljohn answers:

Here is the code exactly what you want:
if( in_category('category_name') ) {
echo '<span>'. get_post_meta($post->ID, 'your_post_meta', true) .'</span>';
}


Add this to your index.php and single.php

Hope it helps.

2012-01-19

Kannan C answers:

You can try the above solutions or if you need an expert to look in to it, i can help you. please pm me.