Results 1 to 7 of 7

Thread: Reformatting how categories and tags display on my posts / PHP help

  1. #1
    Foomandoonian's Avatar
    Foomandoonian is offline Hello World
    Join Date
    Aug 2009
    Location
    Cardiff, UK
    Posts
    10

    Default Reformatting how categories and tags display on my posts / PHP help

    I've recently been working on my own WP theme (based on Starkers HTML5). It's been pretty straightforward, and I've learned a lot about how WordPress themes work, but I'm stuck on one detail.

    The theme displays tags per post like this:
    Tagged: tag1, tag2, tag3

    I've been attempting to rework this so the tags (and categories) are instead displayed in HTML unordered lists (with CSS classes attached to each UL). Eg:

    HTML Code:
    <ul class="tags">
    <li>tag1</li>
    <li>tag2</li>
    <li>tag3</li>
    </ul>
    As simple as this seems, I can't get my head around what the PHP in the theme is doing enough to rework it. Can anyone help?

    You can see my (unfinished) site here.

    Also, I'm happy to take any constructive feedback on the design of the blog. This is only phase 1, but it is nearly ready to launch. :)

    Thanks in advance!
    Last edited by Foomandoonian; 03-25-2011 at 04:22 AM.

  2. #2
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,997

    Default

    You need to use get_the_tags(), which returns an array of tag objects, rather than the_tags(), which only outputs the list of tags.

    For example (untested):
    PHP Code:
    $my_post_tags get_the_tags();
    echo 
    '<ul class="taglist">';
    foreach ( 
    $my_post_tags as $tag ) {
         
    $tagurl home_url() . '/tag/' $tag->slug;
         
    $tagname $tag->name;
         
    $listitem '';
         
    $listitem '<li>';
         
    $listitem .= '<a href="' $tagurl '">' $tagname '</a>';
         
    $listitem .= '</li>';
         echo 
    $listitem;
    }
    echo 
    '</ul>'
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  3. #3
    Foomandoonian's Avatar
    Foomandoonian is offline Hello World
    Join Date
    Aug 2009
    Location
    Cardiff, UK
    Posts
    10

    Default

    Thanks Chip, that seems to have done the trick, for the tags that display on the main page anyway. Tags that appear on individual posts seem to be controlled from functions.php:

    PHP Code:
    if ( ! function_exists'starkers_posted_in' ) ) :
    /**
     * Prints HTML with meta information for the current post (category, tags and permalink).
     *
     * @since Starkers HTML5 3.0
     */
    function starkers_posted_in() {
        
    // Retrieves tag list of current post, separated by commas.
        
    $tag_list get_the_tag_list'''<br />' );
        if ( 
    $tag_list ) {
            
    $posted_in __'This entry was posted1 in %1$s %2$s''starkers' );
        } elseif ( 
    is_object_in_taxonomyget_post_type(), 'category' ) ) {
            
    $posted_in __'This entry was posted2 in %1$s.''starkers' );
        } else {
            
    $posted_in __'Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.''starkers' );    
            
        } 
    I don't really understand what's going on here. Bear in mind, this is code I've already messed around with.

  4. #4
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,997

    Default

    Quote Originally Posted by Foomandoonian View Post
    Thanks Chip, that seems to have done the trick, for the tags that display on the main page anyway. Tags that appear on individual posts seem to be controlled from functions.php:

    PHP Code:
    if ( ! function_exists'starkers_posted_in' ) ) :
    /**
     * Prints HTML with meta information for the current post (category, tags and permalink).
     *
     * @since Starkers HTML5 3.0
     */
    function starkers_posted_in() {
        
    // Retrieves tag list of current post, separated by commas.
        
    $tag_list get_the_tag_list'''<br />' );
        if ( 
    $tag_list ) {
            
    $posted_in __'This entry was posted1 in %1$s %2$s''starkers' );
        } elseif ( 
    is_object_in_taxonomyget_post_type(), 'category' ) ) {
            
    $posted_in __'This entry was posted2 in %1$s.''starkers' );
        } else {
            
    $posted_in __'Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.''starkers' );    
            
        } 
    I don't really understand what's going on here. Bear in mind, this is code I've already messed around with.
    Would it be advantageous maybe just to replace the call to starkers_posted_in() with get_the_tags(), in your loop.php, or single.php, or wherever?
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  5. #5
    Foomandoonian's Avatar
    Foomandoonian is offline Hello World
    Join Date
    Aug 2009
    Location
    Cardiff, UK
    Posts
    10

    Default

    In other words, that whole chunk I posted above? I tried replacing in, and components of it, with either limited success or outright server error.

    I really have only the most basic grasp of what all the code there is doing, but it looks to me like my categories are also handled here. I'm striving to get my categories listed, followed by the tags. (I don't care if the edit button or bookmark permalink vanishes).

  6. #6
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,997

    Default

    Quote Originally Posted by Foomandoonian View Post
    In other words, that whole chunk I posted above? I tried replacing in, and components of it, with either limited success or outright server error.

    I really have only the most basic grasp of what all the code there is doing, but it looks to me like my categories are also handled here. I'm striving to get my categories listed, followed by the tags. (I don't care if the edit button or bookmark permalink vanishes).
    The whole chunk you posted above is in functions.php, right?

    Look for where it gets *called* in your template files, i.e. single.php, and replace with appropriate calls to core WordPress functions, like the_category() and the_tags().
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  7. #7
    Foomandoonian's Avatar
    Foomandoonian is offline Hello World
    Join Date
    Aug 2009
    Location
    Cardiff, UK
    Posts
    10

    Default

    Ah, lightbulb moment! I'll try that tomorrow, when it's not 1.30am.

    Thanks again! :)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •