Support » Fixing WordPress » the_category declaration

  • Resolved chaseman

    (@chaseman)


    In which PHP file can I find the declaration of the function called the_category?

    The problem is that I wrote my own function to shorten too long text by a defined maximum character length. This works with get_the_title inside The Loop but if I use it on the_category, the length of the category name inside The Loop will not get shortened. If I could see the declaration of the function, I perhaps could figure out what the cause for this problem is.

Viewing 15 replies - 1 through 15 (of 15 total)
  • Michael

    (@alchymyth)

    /wp-includes/category-template.php (around line 241)

    if you goto this link:

    http://phpxref.ftwr.co.uk/wordpress/nav.html?_functions/index.html

    you get an alphabetical list of (all ?) wordpress functions, linking to the respective codes;

    GRAQ

    (@graq)

    Thread Starter chaseman

    (@chaseman)

    Thanks for the many links, I’ve read into all of them the past hour.

    GRAQ, thanks for the tip with the filters I’ve read into that as well, so far I’ve quite understood how it works, there’s only one missing puzzle piece, and that is how do I now apply the filter on the specific function I want it to apply to?

    I realize I’d use add_filter, but where do I tell the add_filter function to apply that filter on for example “the_category”, so a too long category like “search engine optimization tips” becomes “search engine …”, since a too long category would break The Loop.

    GRAQ

    (@graq)

    add_filter('the_category', 'restrict_cat_length');
    function restrict_cat_length($args...$args)}
      if( <my logic that just restricts legnth when I want it to> ){
        .. do stuff ..
      }
      return $args...$args
    }

    Sorry, I’m not intimately familiar enough with the code to attempt something that actually works, so I’m doing some obvious pseudo code.

    When the core code calls do_filter(‘the_category’, $args…$args) all the add_filter() functions that have been added get invoked (like my pseudo example). So if you don’t *always* want your restrictions to apply, you will need to put the checks in your function.

    Thread Starter chaseman

    (@chaseman)

    This is the code in the PHP file that I put into the widgets folder:

    function shorten_cat ($text) {
    
    	$chars_limit = 5;
    
    	$chars_text = strlen ($text);
    
    	$text = substr ($text, 0, $chars_limit);
    
    	if ($chars_limit < $chars_text) {
    
    		$text = $text . "...";
    
    	}
    
    	return $text;
    
    }
    
    add_filter ('shorten_cat', 'shorten_cat');

    And this is the_category function that I edited:

    function the_category( $separator = '', $parents='', $post_id = false ) {
    
    	$text = get_the_category_list( $separator, $parents, $post_id );
    
    	echo apply_filters ('shorten_cat', '', $text);
    
    }

    With this, I get echo’d out an empty space, basically the shortened string does not get returned. I’m not quite sure where exactly the problem lies.

    If the string ($text) wouldn’t have been inserted correctly into the function, than I probably would have gotten an error that the function expects a string and nothing else, but that error does not occur.

    So I’m assuming that the function is not able to return the new shortened string back.

    Any ideas how I could accomplish this?

    Thread Starter chaseman

    (@chaseman)

    I just did a bit more testing, the RETURNING itself does work, the problem is that the variable $text is simply empty.

    Perhaps I’m inducing the variable wrongly into the function?

    Thread Starter chaseman

    (@chaseman)

    The $text variable simply gets induced into the function empty, and I’m not getting any “missing argument errors”.

    Thread Starter chaseman

    (@chaseman)

    I can’t even induce this into the function:

    $text = "ice";

    So it must be the apply_filters function that is passing an empty variable.

    Thread Starter chaseman

    (@chaseman)

    Ok I now got it to work with my own variables, I was able to change wit the variable $text = ice; -> cat: ice into cat: i…

    BUT, I’m not able pass the get_the_category_list function into my function, it ends up being empty again:

    This works:

    $text = get_the_category_list( $separator, $parents, $post_id );
    	$text = ice;
    	apply_filters ('shorten_cat', $text);

    And this doesn’t:

    $text = get_the_category_list( $separator, $parents, $post_id );
    
    	apply_filters ('shorten_cat', $text);
    Michael

    (@alchymyth)

    you seem to be confused about ‘filter hook’ and ‘filter function’

    please read: http://codex.wordpress.org/Plugin_API#Filters

    Thread Starter chaseman

    (@chaseman)

    I’ve read that description already, perhaps I’m misunderstanding it, note that English is my 3rd language.

    It says that filters take unmodified data and modify it.

    I see the category name as unmodified data which is in our example:
    “Search Engine Optimization Tips” (way too long) and now I want to modify it BEFORE printing it out to the BROWSER to let’s say 13 characters which is: “Search Engine…”

    Except you mean something else perhaps? As far as I can see I’ve done everything exactly how the page is describing it, I’ve created a filter function and then I’ve hooked in the filter, so it’s registered in wordpress.

    The thing I’m struggling with is how to apply the filter to the function called the_category. Since the description does not say anything about that.

    Thread Starter chaseman

    (@chaseman)

    After alchmyth’s tip I changed the filter hook name into something more unique and now I’m getting the category string into the function but I have now a whole new problem.

    This is the function:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    The category string WILL be echo’d at text2, but it will NOT be echo’d at substr, it will simply be an empty variable.
    Basically I’m not able to do a substr on the get_the_category_list function.

    Any ideas why I can’t run a substr() function on the category name?

    Michael

    (@alchymyth)

    ‘unmodified data’ in the case of ‘the_category’ is the whole output; everything you would get if you use <?php the_category(); ?> in your template.

    to shorten the category title out of this ‘unmodified data’ is, as far as i know, not an easy task, as you would need to identify the individual titles first and then replace them with its short form.

    imho, the easier way to do what you want, is to write your own function, using get_the_category() and a foreach loop.

    http://codex.wordpress.org/Function_Reference/get_the_category
    http://codex.wordpress.org/Function_Reference/get_category_link

    example:
    http://pastebin.com/DUKxQuTC

    the above example would be used instead:
    <?php the_category(', '); ?>

    Thread Starter chaseman

    (@chaseman)

    Wow that worked great, can it really be that simple?

    Thanks a lot man. The thing that was confusing me is that what I was trying worked without any problems with the_title, the post title gets shortened just like I want to, but the same thing does not work with the_category, I’m guessing it has to do with the architecture under the hood which I’m not quite understanding as of now since it all seems a bit messy at first. But I think it is just at first like that and I’ll get into it.

    Hi i am new to wordpress i am trying to hook this function in the_category filter ny one help me out plz

    add_filter(‘the_category’,’logu_cat_func’);

    function logu_cat_func(){
    $args = array(‘orderby’ => ‘term_group’, ‘order’ => ‘ASC’, ‘fields’ => ‘all’);
    $terms = wp_get_object_terms($post->ID, ‘category’, $args );
    $count = count($terms); $i=0;
    if ($count > 0) {
    $cape_list = ‘<p class=”my_term-archive”>’;
    foreach ($terms as $term) {
    $i++;
    $term_list .= ‘slug . ‘” title=”‘ . sprintf(__(‘View all post filed under %s’, ‘my_localization_domain’), $term->name) . ‘”>’ . $term->name . ‘‘;
    if ($count != $i) $term_list .= ‘ · ‘; else $term_list .= ‘</p>’;
    }
    echo $term_list;
    }
    }

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘the_category declaration’ is closed to new replies.