• Resolved mentalward

    (@mentalward)


    Hello–

    I have the following structure:
    Blog
    |-> Kids
    | ‘-> Post #1
    |-> Students
    | ‘-> Post #2
    |-> Teens
    ‘-> Post #3

    I am trying to get the Category to display with an icon to the rss feed. I can get each of them to work except on the posts. The posts returns a null value for the Category.

    For example:
    Blog => Blog & /blog/feed/
    |-> Kids => Kids & /blog/kids/feed/
    | ‘-> Post #1 => (null) & (null)
    |-> Students => Students & /blog/students/feed/
    | ‘-> Post #2 => (null) & (null)
    |-> Teens => Teens & /blog/teens/feed/
    ‘-> Post #3 => (null) & (null)

    I am trying not to code custom headings and feeds if possible, but I am getting a little frustrated. Here is the code:

    <?php
    	$imgurl = get_bloginfo('stylesheet_directory');
    	$rssurl = get_bloginfo('rss2_url');
    	$category = get_category( get_query_var('cat') );
    if ( is_category()) {
    	echo '<div id="blogheader" class="group"><h2>'. $category->name .'</h2><a rel="nofollow" title="Subscribe to the "'. $category->name .'" category" '. $rssurl .'href="http://localhost/bsfl/category/blog/feed/"><img src="' . $imgurl . '/images/rss_32.png"></a></div>';
    }
    ?>

    I don’t have a link to the site because it’s on a test server during redesign.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The is_category() checks if you are on an archive page for a category.

    And on a post page use…

    $category = get_the_category();

    More info on WP Codex – get_the_category

    Thread Starter mentalward

    (@mentalward)

    This is what I finally came up with to make it work. I am still trying to find a cleaner (and dynamic) way to display the information for the post, but it works for now and does what I need.

    <?php
    if ( is_category()) {
    	$imgurl = get_bloginfo('stylesheet_directory') . '/images/'; 			// Sets the image path
    	$thisurl = 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];	// Current URL
    	$rssurl = get_category_feed_link( get_query_var('cat') );				// Generates the RSS Blog URL
    	$current_cat = single_cat_title("", false); 							// Displays the child cat name
    
    	echo '<div id="blogheader" class="group"><h2><a href="' . $thisurl . '">'. $current_cat .'</a></h2><div id="rss"><a rel="nofollow" title="Subscribe to the ' . $current_cat .' category" href="'. $rssurl . '"></a></div></div>';
    }
    elseif ( is_single()) {
    	$imgurl = get_bloginfo('stylesheet_directory') . '/images/'; 	// Sets the image path
    	$thisurl = 'http://' . $_SERVER["HTTP_HOST"];					// Current URL
    	$category = get_the_category();									// Gets the Categories for the Post
    		foreach ( $category as $cat ) {
    			if ($cat->cat_ID != "15") {		// Won't Display "Blog"
    				$thisurl = $thisurl . '/category/blog/' . $cat->slug . '/'; 	// Category Blog URL
    				$rssurl = $thisurl . 'feed/';										// Category Blog RSS Feed
    					echo '<div id="blogheader" class="group"><h2><a href="' . $thisurl . '">'. $cat->cat_name .'</a></h2><div id="rss"><a rel="nofollow" title="Subscribe to the ' . $cat->cat_name .' category" href="'. $rssurl . '"></a></div></div>';
    
    			}
    		}
    }
    ?>

    No matter how I sorted/retrieved the categories the “Blog” was normally the first one returned. So, I couldn’t rely on calling the second item in the array. This way I have it loop only if it isn’t the “Blog” category.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display Category Name on Single Post’ is closed to new replies.