WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to add Meta-Field in Categories ? (26 posts)

  1. cooljaz124
    Member
    Posted 3 years ago #

    Hi,

    Does anyone know how to add custom meta field in Categories ? Like, When I make a new category, there should be 2 additional fields

    - Icon , where we add url of icon image of that category.
    - Check Box to set that particular category "POPULAR"

    See the screenshot of the functionality I have asked help for.

    Thanks in advance.
    Jaz

  2. Joseph
    Member
    Posted 3 years ago #

    I think you'll need to create a new table. Custom fields are for posts only.

  3. cooljaz124
    Member
    Posted 3 years ago #

    Thanks Joseph, yes it will be playing with tables. Any more specific solutions with codes ?

    Functionality achieved from this plugin is what I am looking for. But, this plugin just have text,text-area and image as the additional meta fields. I need check box as well and a way to fetch the meta values in the loop.

    Thanks.
    Jaz

  4. Joseph
    Member
    Posted 3 years ago #

    You can hack that plugin to include checkbox.

    Find the following in /views/options.php

    <option value="text"><?php _e('Text','wp-category-meta'); ?></option>
    <option value="textarea"><?php _e('Text Area','wp-category-meta'); ?></option>
    <option value="image"><?php _e('Image','wp-category-meta'); ?></option>

    and add

    <option value="checkbox"><?php _e('Check Box','wp-category-meta'); ?></option>

    Insert the following after line 578 in wp-category-meta.php

    <?php } elseif($inputType == 'checkbox') { ?>
    <tr>
    	<th scope="row" valign="top"><label for="category_nicename"><?php echo $inputName;?></label></th>
    </tr>
    <tr>
    	<td><input value="checked" type="checkbox" <?php echo $inputValue ? 'checked="checked" ' : ''; ?>
    		name="<?php echo 'wptm_'.$inputName;?>" /><br />
    		<?php _e('This additionnal data is attached to the current term', 'wp-category-meta');?></td>
    </tr>

    The checked category will have a meta_value of "checked"

  5. jocken
    Member
    Posted 3 years ago #

    Big thanks! This was exactly what I have been looking for!

  6. cooljaz124
    Member
    Posted 3 years ago #

    Thanks a lot Joseph.

    @Joseph @Jocken But, is the plugin working in WordPress 3.0.1 ?? I can't see it working in 3.0.1, any lucks for you guys ?

  7. Joseph
    Member
    Posted 3 years ago #

    I've tested it on 3.0.1 and it works. However, there is one minor problem with the plugin itself. The list doesn't refresh automatically when a new category is created. Everything else seems fine.

  8. cooljaz124
    Member
    Posted 3 years ago #

    Oh ! Can you copy here the function you used to display the meta info here ?? Is it the same that as they described in the plugin page ? I'm having some hard luck !

  9. Joseph
    Member
    Posted 3 years ago #

    Yep, I used get_terms_meta and get_all_terms_meta.

    Note that the functions return a numeric and 2D array respectively.

  10. cooljaz124
    Member
    Posted 3 years ago #

    Thanks I will try it again.

    The thing is I need to fetch all categories, style certain category a bit different. So I'm trying to put the "certain" categories with a meta value so tht I can style them differently. Can you help getting me the right code ?

  11. Joseph
    Member
    Posted 3 years ago #

    Just to clarify before I help you with the code. You want a list of all categories and have different styling for ones with the popular meta?

  12. cooljaz124
    Member
    Posted 3 years ago #

    Exactly !

    I need to be able to change the popular categories often. Thats why thought of this plugin. Thanks for your help, Joseph.

  13. Joseph
    Member
    Posted 3 years ago #

    A couple of things I forgot to ask. It was my sleeping time :P

    How are you planning to style it differently?
    If it's only different in between the anchor tags then we could just use a filter to change the output of wp_list_categories.

    If the filter is not enough, do you want it flat or multilevel?
    If it's flat then we just need to run a simple loop. If it's miltilevel, we'll need to extend the WordPress walker class.

  14. cooljaz124
    Member
    Posted 3 years ago #

    Hi,

    The markup goes like this

    <span class="category-name popular">Category Name
         <div class="left"></div><!-- *left round corner -->
         <div class="right"></div><!-- *right round corner -->
     </span>

    <span> have to get an additional class "popular", its not :(

    * Some dumb IE compatibility, can't help :(

  15. Joseph
    Member
    Posted 3 years ago #

    I made a mistake about the filter. It also filters the title and alt attributes so it's no good.

    Are you already using a custom function to add the markup? If so I'll just modify that to include the popular class.

  16. cooljaz124
    Member
    Posted 3 years ago #

    Mistake ? Is it about the hack on plugin to add the checkbox ? Oops.

    Nop, I have'nt added any custom functions.

  17. Joseph
    Member
    Posted 3 years ago #

    I'm referring to the filter I mentioned about changing the display of wp_list_categories, not the plugin hack.

    Since the filter method won't work, do you want the list to be flat or multilevel? Do you want it to have options like wp_list_categories?

    We can do a simple loop or extend wp_list_categories depending on what you want.

  18. cooljaz124
    Member
    Posted 3 years ago #

    Oops. Got you.

    May be we can go the simple loop way.

  19. Joseph
    Member
    Posted 3 years ago #

    Alright, so it'll be flat, ie not multileveled for child categories?

  20. cooljaz124
    Member
    Posted 3 years ago #

    You should have correct view if you have a glimpse on the website.

    Take a look at -- LINK REMOVED -- , and place the mouse over "POSTS" nav which will give you the dropdown.

    The drop down shows - specific categories listed under posts. You can see how each category is styled. For the category checked "popular", I may need to change the style, may be change the color to red.

    PS : This is all part of drag and drop of categories, as user can control which all posts they need to see. Made based on cookies.

  21. Joseph
    Member
    Posted 3 years ago #

    Ok, this should do the trick:

    function custom_list_categories() {
    	global $wpdb;
    	$popular = $wpdb->get_results("SELECT terms_id FROM wp_termsmeta WHERE meta_key = 'popular'", OBJECT_K);
    
    	$categories = get_categories();
    	foreach ($categories as $category) {
    		if ( isset( $popular[$category->term_id] ) )
    			$class = ' popular';
    		else
    			$class = '';
    		?>
    		<li id="category-<?php echo $category->slug; ?>">
    			<span class="category-name<?php echo $class; ?>"><?php echo $category->name; ?>
    				<div class="left"></div>
    				<div class="right"></div>
    			</span>
    		</li>
    		<?php
    	}
    }

    Call the function inside <ul></ul>.

  22. cooljaz124
    Member
    Posted 3 years ago #

    @Joseph,

    Thanks a lot. I will be trying to implement this once I am back at my work PC.

    BTW, your site looks great ! May I know which Photo Gallery plugin you used there ? Love the way its organised into folders and subfolders.

    Also, I am not able to find your contact info anywhere. Let me know. Thanks again.

  23. Joseph
    Member
    Posted 3 years ago #

    Thanks. I'm using Nextgen Gallery with a fair bit of customisation, implementing categories and year by linking it with custom post type.

    I've been redesigning my site so I haven't put up any sort of contact yet. It's just a personal site so contact info isn't of high priority.

  24. cooljaz124
    Member
    Posted 3 years ago #

    Thanks again. I will be updating the reply once I playing around with the codes you gave. :)

  25. cooljaz124
    Member
    Posted 3 years ago #

    Thank you so much Joseph for the help. It worked, very well :) Glad that you helped.

  26. elebail
    Member
    Posted 3 years ago #

    Hello,

    I made a new version (1.2.3) of the "category meta" plugin to include you "check-box" type.

    Regards,
    Eric

Topic Closed

This topic has been closed to new replies.

About this Topic