WordPress.org

Ready to get started?Download WordPress

Forums

Category Icons
How to apply icon to blank descendant categories (10 posts)

  1. Franklyn Monk
    Member
    Posted 1 year ago #

    I am trying to figure out how apply a generic icon to any imageless subcategory of a specified parent.

    I've been hacking away at a function all week and I just keep confusing myself more... so much so I'm not even sure if I'm stating the question clearly. If I'm not here's my logic tree.

    Ask post: do you have a category that descends from Poems?
      No: well, don't let me bother you, continue along.
      Yes: does it already have an icon?
        Yes: sorry, go on
        No: use this icon, link it to the subcat with priority high (optionally also displaying linked icon to parent) , and continue to process other categories.

    http://wordpress.org/extend/plugins/category-icons/

  2. submarine
    Member
    Plugin Author

    Posted 1 year ago #

  3. Franklyn Monk
    Member
    Posted 1 year ago #

    I managed to kludge something together with that example.
    $content doesn't have exactly what I want but I made due. The code below appends an icon to the list if it descends from Poems. Next step is to see if that subcategory has an image; I'm digging through the plug-in code to see if I can find something to grab on to (if not I'm playing with querying $content for partial url to "poems/" to tell me if it's linked to something under poems/ which is ugly even to me).

    So...here's some code that does kinda what I want but not really.

    // Generic drafts category icon
    function bm_noicon($content) {
    	if (post_is_in_descendant_category(19)) {
    	$content =$content.'<a title="Poems" href="/index.php?cat=19"><img class="caticon" src="http://asoulinprogress.quasigentsia.com/wp-content/themes/styleless2/images/caticons/ensolrg.png" width="32" height="32" alt="Drafts"></a>';
    	}
    	return $content;
    }
    add_filter('category_icons', 'bm_noicon');

    If your curious about the post_is_in_descendant_category function, it's from here.

  4. submarine
    Member
    Plugin Author

    Posted 1 year ago #

    If there is no image assigned to the category, $content will be empty. So what do you think of that :

    function bm_noicon($content) {
    	if (post_is_in_descendant_category(19) AND empty($content)) {
    	  // no image assigned to this category
    	  $content = '<a title="Poems" href="/index.php?cat=19"><img class="caticon" src="http://asoulinprogress.quasigentsia.com/wp-content/themes/styleless2/images/caticons/ensolrg.png" width="32" height="32" alt="Drafts"></a>';
    	}
    	return $content;
    }
    add_filter('category_icons', 'bm_noicon');
  5. Franklyn Monk
    Member
    Posted 1 year ago #

    $content won't be empty if the post has any other categories assigned to it.

  6. submarine
    Member
    Plugin Author

    Posted 1 year ago #

    Ouch ! You're right. And if you keep in an array the categories id which have an icon and test if the current category id is in this array ?

    Put the result of :
    SELECT cat_id FROM wp_ig_caticons
    in an array, say $cat_id_assigned, for example.

    Then :

    function bm_noicon($content) {
    	global $cat_id_icons;
    
    	if (post_is_in_descendant_category(19) && !in_array($cat_id,$cat_id_icons)) {
    	  	$content = '<a title="Poems" href="/index.php?cat=19"><img class="caticon" src="http://asoulinprogress.quasigentsia.com/wp-content/themes/styleless2/images/caticons/ensolrg.png" width="32" height="32" alt="Drafts"></a>';
    	}
    	return $content;
    }
    add_filter('category_icons', 'bm_noicon');

    I didn't test it.

    Update : it's not a good idea, as there are several cat_id to process...

  7. submarine
    Member
    Plugin Author

    Posted 1 year ago #

    Maybe if you computes the differences between the 2 arrays : get_term_children(19) and $cat_id_assigned ? Like this :

    $myarray = array_diff(get_term_children(19),$cat_id_assigned);

    If count($myarray) > 0, you can then add your image "ensolrg.png" to the content.

  8. Franklyn Monk
    Member
    Posted 1 year ago #

    I struggle to stay above water in the submarine's wake. Its violent dive caught me off guard and I smashed my head on the bow of my fishing troller. Struggling for breath and hold, blood stains my vision and I can no longer tell which way is up. :)

    I do not understand building the array $cat_id_assigned. SELECT is some kinda crazy SQL command, right? So I'll need to find how to select sql objects in php and add them to an array? I can do that, but before I start is that the basic idea?

  9. submarine
    Member
    Plugin Author

    Posted 1 year ago #

    :-D

    Never mind. The basic idea is to compare the children of category id 19 (array A) and the category ids that have an icon (array B) assigned to. If there is a category in array A that is not in array B, it is a category that has no icon.

  10. Franklyn Monk
    Member
    Posted 1 year ago #

    Right on!

    I'm much further along than I would have been without your input. I've been looking for an excuse to learn more about arrays so I'm gonna take this opportunity to hit the books for a while and see if I can't wrangle something up over the weekend.

    If anyone knows of a good php resources I'd love to add them to my own, I generally hit php.net and stackoverflow, are there any other good ones?

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic