How to call custom category field data in theme?

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

Read More

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

Related posts

Leave a Reply

1 comment

  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'];
    }