1

I am trying to add two custom fields to my categories (which I have done thanks to this answer - Any examples of adding custom fields to the category editor?)

And now I need to call them within my template (via a Headway custom code block).

I have seen a bunch of almost there examples, but I just can't get it to work!

If you can please let me know the exact php I would need to use to call one of the fields I would be oh so grateful! Otherwise I'm resorting to a plugin (and I already have way too many!)

Here is the code

<h1 class="collapseomatic" title="Click for more" id="<?php the_ID();?>">
    <?php single_cat_title(); ?> 
</h1>
<p>FIELD ONE </p>

<div id="target-<?php the_ID(); ?>" 
    class="collapseomatic_content force_content_collapse" 
    style="background: white; min-height: 16px; padding: 5px; width: 95%;">

    <?php echo '<div class="seo_text"><p>'FIELD TWO'</p></div>'; ?>
    <?php echo '<div class="seo_text"><p>'.category_description( $category_id ).'</p> </div>'; ?>       
</div>

UPDATE

I have now tried to create a custom function

function ddgseo_title1() {
    //get the current term
    $term = get_term_by( 
        'slug', 
        get_query_var('term'), 
        get_query_var('taxonomy') 
    );

    //get the saved category custom fields
    $fields = get_option(MY_CATEGORY_FIELDS);

    if ( isset($fields[$term->term_id]) ) {

        //extract just the needed term fields
        $term_fields = $fields[$term->term_id];

        //Now $term_fields holds all of your category fields so to get a specific field:
        if ( isset($term_fields['_ce4-categoryTitle']) )
            echo $term_fields['_ce4-categoryTitle'];
    }
}

And am trying to call the title into the theme html with .ddgseo_title1() but again no luck

6
  • Show us what code you have so far. What have you tried? Jan 4, 2013 at 3:42
  • Exactly as @chrisguitarguy says... the other Q&A is just a reference, and each case is one case. Anyway, taking a look over there, isn't just a matter of doing a $field=get_option('FIELD-NAME'); and echo $field['SUB-FIELD']; ?
    – brasofilo
    Jan 4, 2013 at 3:45
  • @brasofilo - can you please expand on this a little? I am not very good with this stuff - what I have done so far has all been guess work. I try adding - get_option(MY_CATEGORY_FIELDS) and it showed "Array" I tried adding get_option(_ce4-categoryTitle) and get_option('_ce4-categoryTitle') and they both came up blankI also tried <?php $field=get_option('_ce4-categoryTitle');?> again, blank. And have tried a bunch of other combinations ... I really have no idea! I need to know the exact php that would go in between either <p> </p> or H2 tags.
    – Kate
    Jan 11, 2013 at 4:51
  • Without entering in the matter of you current issue. 1) Suggestion: give your code breathing space and a nice indentation and it will be much easier to read and debug (see edits I've done to the Q. 2) Debug: use var_dump($fields); to see the contents of the variable. Another one which prints the var with better formatting is echo '<pre>' . print_r($fields, true) . '</pre>';. The PHP Manual is always nice to consult, in the comments section you'll find lots of useful stuff
    – brasofilo
    Jan 11, 2013 at 14:44
  • @brasofilo - sorry, never used this forum before, or the whole markdown formatting, took me forever to figure out how to even get code to show (can't figure out how to do a line break)! When you say use var_dump($fields); what do I put around it, and how does it know which of the two field to show? Would you be able to give me an example using the first bit of code to show either FIELD ONE of FIELD TWO?
    – Kate
    Jan 11, 2013 at 20:49

1 Answer 1

1

If you are using the suggested method in the question you linked, then all of the category meta fields are stored in a single option in the options database table, so to get the data in your theme you need to get that option and extract the field from there, something like,

//get the current term
$term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') );
//get the saved category custom fields
$fields = get_option(MY_CATEGORY_FIELDS);
if (isset($fields[$term->term_id])){
    //extract just the needed term fields
    $term_fields = $fields[$term->term_id];
    //Now $term_fields holds all of your category fields so to get a specific field:
    if (isset($term_fields['Field-Name']))echo $term_fields['Field-Name'];
}
3
  • next time just update your question because i cant make anything out of your comment
    – Bainternet
    Jan 4, 2013 at 23:28
  • ok updated - does that help?
    – Kate
    Jan 5, 2013 at 2:15
  • I updated the question.. any ideas?
    – Kate
    Jan 11, 2013 at 4:48

Your Answer

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

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