Forums

[resolved] Class for Current blog post (5 posts)

  1. Thomas Misund
    Member
    Posted 2 years ago #

    wp_list_pages() produces a class .current_page_item for the page you are currently viewing. This makes it possible to style the current page item different from the other page items in the list.

    But there are several other places I would ike to use the same functionality. Are there equivalent ways to style the current blog post item in the Recent Posts widget? Or the current category in wp_list_categories()? Or the current tag in wp_tag_cloud('format=list')?

  2. Mark / t31os
    Moderator
    Posted 2 years ago #

    Plonk them in a theme file, save, view the page and view source...

  3. Thomas Misund
    Member
    Posted 2 years ago #

    Added ticket #10432

  4. Mark / t31os
    Moderator
    Posted 2 years ago #

    wp_list_categories does add classes already, you can remove that from the ticket...

    Not sure on tag_cloud, going to go dig in the files now and check..

    EDIT: The tag cloud does add a class name to the list opening tag..
    class="wp-tag-cloud"
    or in CSS..
    ul.wp-tag-cloud

    However you can pass the tag_cloud into PHP as an array using 'format=array&echo=0'

    What you then do with it in PHP is up to you, so it's possible to add your own classes using that..

  5. Mark / t31os
    Moderator
    Posted 2 years ago #

    I gave it a shot, and after a few searches i've realised the wp_tag_cloud function is not really helpful in determining the active tag...

    However!.. i wrote this for you to add a tag class to LI elements, and an active tag class if a tag matches the current tag queried...

    It may have been done before, or done better, but this works and is all yours to use if you want... ;)

    <?php
    // Grab tag cloud as array
    $tag_cloud = wp_tag_cloud('format=array&echo=0');
    // Set queried tag as variable (if one exists)
    $active_tag = get_query_var('tag');
    
    // Start a list
    $showtags = '<ul>';
    // Loop over each tag cloud array item
    foreach($tag_cloud as $tag) {
    	// Check if there's a queried tag and if it matches the current array item
    	// Strip tags required because each item is inside linked elements
    	if($active_tag && ($active_tag == strip_tags($tag))) {
    		// Match, so create current tag item
    		$showtags .= '<li class="tag-current">'. $tag .'</li>';
    	}
    	else {
    		// No match, create regular item
    		$showtags .= '<li class="tag">'. $tag .'</li>';
    	}
    }
    // Close the list
    $showtags .= '</ul>';
    // Echo the end result
    echo $showtags;
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic