WordPress.org

Ready to get started?Download WordPress

Forums

How to make single posts display something depending on category? (2 posts)

  1. digitalsky
    Member
    Posted 1 year ago #

    What I am trying to do sounds simple in my head but it isn't turning out to be simple once I try and code it.

    Basically I have a website that is using WordPress and I want the pages to display something at the top depending on which category a post is in.

    In my category.php file I wrote this code...

    <?php if(is_category('cisco')) { echo 'Cisco Banners'; }
    				elseif(is_category('comptia')) { echo 'CompTIA Banners'; }
    				elseif(is_category('developer')) { echo 'Developer Banners'; }
    				elseif(is_category('exchange-server')) { echo 'Exchange Server Banners'; }
    				elseif(is_category('itil-cobit')) { echo 'ITIL/COBIT Banners'; }
    				elseif(is_category('office-365')) { echo 'Office 365 Banners'; }
    				elseif(is_category('pmp')) { echo 'PMP Banners'; }
    				elseif(is_category('powershell')) { echo 'PowerShell Banners'; }
    				elseif(is_category('sharepoint')) { echo 'Sharepoint Banners'; }
    				elseif(is_category('sql-server')) { echo 'SQL Server Banners'; }
    				elseif(is_category('system-center')) { echo 'System Center Banners'; }
    				elseif(is_category('web-development')) { echo 'Web Development Banners'; }
    				elseif(is_category('windows-7')) { echo 'Windows 7 Banners'; }
    				elseif(is_category('windows-8')) { echo 'Windows 8 Banners'; }
    				elseif(is_category('windows-server')) { echo 'Windows Server Banners'; }
    				else { echo 'Global Banners'; }
    				?>

    That code basically determines which category page you're on, and consequently echoes something different for each category page.

    Now, I tried to use this code in my single.php template but it doesn't work. I assume this is because when someone is browsing a single post WordPress doesn't assume you're looking at that single post from a category standpoint.

    So, I'm wondering if anyone can help me out here. Basically all I want to do is use code similar to that code I wrote for the category.php template but make it work for the single.php template.

    That way if someone clicks on a single blog post it will determine what category that blog post is in (I only use one category per blog post) and then echo something different for each category. So, if someone is viewing a single blog post that is in the 'Cisco' category it would echo 'Cisco Ad Banners'. If they were viewing a post that was in the 'Windows 7' category it would echo 'Windows 7 Ad Banners', and so forth.

    I'm more of a designer than I am a PHP programmer so anyone with more knowledge on the subject would be of much help! Thank you so much!

  2. vtxyzzy
    Member
    Posted 1 year ago #

    Try changing is_category to in_category everywhere in the code above when used in single.php.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags