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

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
Thematic and custom fields in post meta

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

This question has been answered.

dv1961 | 01/19/12 at 5:57am Edit


(7) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    01/19/12
    6:01am
    Francisco Javier Carazo Gil says:

    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.
    }

    • 01/19/12 6:02am

      Francisco Javier Carazo Gil says:

      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.

    • 01/19/12 6:03am

      Francisco Javier Carazo Gil says:

      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)
      }
      ?>

  • avatar
    Last edited:
    01/19/12
    6:01am
    Arnav Joy says:

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

    • 01/19/12 6:03am

      Arnav Joy says:

      for single page display use

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

      }

      and for blog page

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

      }

  • avatar
    Last edited:
    01/19/12
    6:02am
    Fahd Murtaza says:

    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/

  • avatar
    Last edited:
    01/19/12
    6:06am
    rizaljohn says:

    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.

  • avatar
    Last edited:
    01/19/12
    6:07am
    Ivaylo Draganov says:

    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);

    }

  • avatar
    Last edited:
    01/19/12
    6:19am
    Hai Bui says:

    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');

    Previous versions of this answer: 01/19/12 at 6:18am | 01/19/12 at 6:19am

  • avatar
    Last edited:
    01/19/12
    6:22am
    Kannan C says:

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

This question has expired.



Julio Potier, dv1961 voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.