Forums

Creating custom template tags (4 posts)

  1. hbillings
    Member
    Posted 1 year ago #

    I'm trying to figure out how to add my own template tags to my theme. I've been searching for answers all morning yet I can't figure out where template tags pull from or where I'd be able to define the functions within a custom tag. (I'm a bit of a PHP n00b, so forgive me if my explanation is hard to follow.) Help?

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Template tags are just functions really, so just create your own functions and use them as you would do in any other php application.

  3. hbillings
    Member
    Posted 1 year ago #

    Oh, duh. Thanks for pointing out the obvious! :-)

    What about modifying existing tags? I've seen plenty about how to use the existing tags, but how do I find them if I want to tweak them?

  4. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    That depends upon the tag and what you want to do with it. Where possible, I'd suggest adding your modification on top of the current tag's output in functions.php and then calling your new function in place of the original tag. But just to add to the complexity, there are some tags that call actually call a series of filters - in which case you can add a new filter to the end of that queue.

    http://codex.wordpress.org/Function_Reference/add_filter

    For example, to increase the number of words displayed by the_excerpt() from 55 to 100, you could use:

    // Increase excerpt length
    function my_excerpt_length($length) {
    	return 100; // Or whatever you want the length to be.
    }
    add_filter('excerpt_length', 'my_excerpt_length');

    and then call <?php the_excerpt();?> in your template.

Topic Closed

This topic has been closed to new replies.

About this Topic