WordPress.org

Ready to get started?Download WordPress

Forums

Category Custom Fields
How do you call the custom fields inside the category template ? (12 posts)

  1. davidjs
    Member
    Posted 2 years ago #

    After I create a custom field for my categories, how do I get my category.php to show it ? The last person to try an answer this question said <?php $result = categoryCustomFields_GetCategoryCustomField($category->cat_ID,'slideshow');?>

    This only showed the word array where it should have show the text from my custom field "slideshow".

    http://wordpress.org/extend/plugins/categorycustomfields/

  2. migueleste
    Member
    Posted 2 years ago #

    Hi,

    For some reason the author of the plugin doesn't share the correct way to retrieve the value of the custom fields...
    Which is weird.

    So, if you want to get the value of one of those fields (at least it works for me..):

    $custom_field = categoryCustomFields_GetCategoryCustomField(cat_ID, 'yourfieldname');

    echo $custom_field[0]->field_value:

    Where 'cat_ID' is the ID of the category you want to look and 'yourfieldname' is...well your field name. :-)

    cheers,
    miguel

  3. Tite_noirode
    Member
    Posted 2 years ago #

    Hi,
    Actually, I came across the same issue. I needed an image for each category. The plugin add correctly the field image but once the image is uploaded I was unable to print it on the category page.

    So I modified the code a little bit. I removed few lines that I didn't need and renamed the function.

    You can put the following function in functions.php. (so it keeps the plugin as it is in case of an update).

    function categoryImage($taxonomy,$name,$width=10000,$height=10000){
    	$arr = categoryCustomFields_DB_GetCategoryValueById($taxonomy,$name);
    	if($arr->field_type=="image" || $arr->field_type=="file")
    	$arr->field_value=explode('@',$arr->field_value);
    	$arr->field_value = $arr->field_value[1];
    	$b = $arr->field_value;
    	return $b;
    }

    This returns the root path of my file. I had to remove the beginning of the path (everything before /wp-content) and replace it by the actual site Url.

    You can copy this code in the category.php file.

    //get the category ID
    $cat_id = the_category_ID('');
    //Call the new function	categoryImage created in functions.php
    $url = categoryImage($cat_id,'Image');
    //replace the wrong path with the site url
    $url= str_replace('/home/fvrepro/public_html', get_site_url(), $url);
    //print the image tag
    print '<img src="'.$url.'" />';

    It's probably a little bit messy and can be improved but it works!

    Hope that will help you.

    Caro.

  4. brotmaschine
    Member
    Posted 2 years ago #

    Hello World,

    another example that worked for me. I want a border-color on the left side per Post and different color by category.

    1. made a field in the admin section: text, named: Farbcode
    2. goto category and filled in the RGBcolorcode like 333333
    3. Code in Template

    <?php $my_categories = get_the_category(); $my_category_id = $my_categories[0]->term_id; ?>
    <?php $custom_field = categoryCustomFields_GetCategoryCustomField($my_category_id, 'Farbcode'); ?>
    
    <div id="postcon">
    <div id="postheader" style="border-left: 30px solid #<?php echo $custom_field[0]->field_value; ?>;">
  5. egr102
    Member
    Posted 2 years ago #

    @migueleste's code worked a treat for me!

  6. migueleste
    Member
    Posted 2 years ago #

    glad it was useful.

    Regarding images, I have to say that i didn't took the time to look into the structure the plugin uses.

    I just create a text field and store the image URL there. (previously uploaded to media library)
    In the template files, I retrieve the URL, as shown above, and build the adequate img's, thumbnails and whatever I need.

    cheers,
    miguel

  7. davidjs
    Member
    Posted 2 years ago #

    I still have not seen one person post how to make this work.. What is the exact code you put in the categories.php file to get a custom field. The name of my custom field is "options" . I dont want to insert a specific category id, i just want the category template to get the custom field for any category. So what do I put in between
    <?php and ?>

    Thanks

  8. mainstay
    Member
    Posted 2 years ago #

    some really useful information here... thanks all!

    I have a text value assigned to my categories (which is actually a full path to an image file, a different image per category).

    I am trying to pull that image in my php and can't seem to find the right coding.

    I have

    <?php $my_categories = get_the_category(); $my_category_id = $my_categories[0]->term_id; ?>
    	<?php $custom_field = categoryCustomFields_GetCategoryCustomField($my_category_id, 'top_image'); ?>
    	<h1 class="category-image"><img src="<?php echo $custom_field[0]->field_value;?>" /></h1>

    Which works to display ONE of the images for all my categories.

    I don't know how to pull a different image for each category...

    Some sort of an if statement with an array ?

    Any thoughts?

    I see others have done exactly this (migueleste).

    I would LOVE to know specifically how you do this... ANY HELP?

    Thanks!

    --Matthew

  9. adinusmail
    Member
    Posted 2 years ago #

    I need to have distinct image for every category.So this code works for me(in category page):

    $cat_ID = get_query_var('cat');
    $url = categoryCustomFields_GetCategoryCustomField($cat_ID, 'Image');
    $url = explode('@',$url[0]->field_value);
    $url = $url[0];

    echo $url;

    The $url var is link to image.
    Hope it works for you too.

  10. Jim Bob
    Member
    Posted 2 years ago #

    thanks adinusmail, that code worked. i just changed the 'image' to the my fields name.

  11. mainstay
    Member
    Posted 1 year ago #

    thanks to all of you... this is now working very well!

    I have custom fields inside my pages which allow for custom banners (top_image) + I wanted my news and other categories to have custom banners (using category custom fields).

    For anyone who may be interested, this is how my final code looks:

    <?php $Header_Image = get_post_meta($post->ID, 'top_image', true)  ?>
    
    <?php $cat_ID = get_query_var('cat');
    	$url = categoryCustomFields_GetCategoryCustomField($cat_ID, 'top_image');
    	$url = explode('@',$url[0]->field_value);
    	$url = $url[0];
    ?>
    
    <?php if(is_page() && $Header_Image !=NULL || is_single() && $Header_Image !=NULL) { ?>
    	<h1 class="category-image"><img src="<?php echo get_post_meta($post->ID, 'top_image', true);?>" /></h1>
    
    <?php } 
    
    elseif ($url!=NULL) {
    ?>
    	<h1 class="category-image"><img src="<?php echo $url; ?>" /></h1>
    <?php }
    else { ?>
    //there is nothing specified so just treat it like you normally would
  12. ederlima
    Member
    Posted 1 year ago #

    Adinusmail

    It Works! Thanks! ;)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic