How to determe if viewing a specific category (is active) (4 posts)

  1. Zentode
    Posted 6 years ago #

    Well I gave up on these two after several hours of attempts.

    I am trying to return true if the post being displayed is in a specific category and only that category.

    Also create a "dynamic" link to the latest post if it is in a specific category and only that category.

    Basically, I have navigation graphics on my site.
    Two of the graphics are (temporarily) hard coded links in the header template file that each link to their specific post.

    1- I would like a link to go to the latest post in a specific category.(Must be only one category assigned to it and cat must exist)

    I know there is a way to click on a category and get/view the latest post in that category.

      Thing is, I want some sterilization/filters so:

    • If the post belongs to more than one category, it (result) is not included.
    • Must be assigned to the one specific category only!
    • Would like the link only to appear if there is a post in that category!

    2- I desire to change the links style when they are are clicked on (active). Hence, how do I detect if the post is being viewed is an a specific category and only that category.

    I tried several things including using post in pages but, I want all the features and control of a post like selecting how may per page and all the other core functs...

    No plugins but, all in the template and/or template functions.
    (And world peace)

    I was attempting:

    <?php query_posts('category_name=Wordpress&post_status=publish,future');?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();

    So: (if viewing a post in category WordPress (and only that category))

    <li class="<?php $activestyle ?>">WordPress

    I realize I need some elses for sterilization but aaahhh! Help!

    I came close a couple times but, ultimately failed. I know this will be useful to others when I get it all nailed down. With everyones help here...

    Thanks in advance..

  2. Zentode
    Posted 6 years ago #

    Well, my downfall, well one of many, is not being familiar with the wp codex.

    I made a function that allows me to display specific things and assign vars based on the a single category and only that category, It is really crude but, I am grabbing at straws now and getting frustrated.

    function pet_my_cats() {
    foreach((get_the_category()) as $category) {
    if ($category->cat_ID == "" || $category->cat_name == "") {
    elseif ($category->cat_ID == "1"){
    elseif ($category->cat_name == "Plans" || $category->category_nicename == "plans") {
    elseif ($category->cat_name == "Newsletter" || $category->category_nicename == "newsletter") {
    if ($it == "0" ||  $it == "86") {
    $thisis = "error";
    $response =  "Error, no category selected!<br />";
    if ($it == "2") {
    $thisis = "plans";
    $response =  "Plans <br />";
    if ($it == "3") {
    $thisis = "newsletter";
    $response =  "Newsletter <br />";
    if ($it == "4") {
    $thisis = "default";
    $response =  "Default <br />";
    if ($it >= "5" &&  $it < "86") {
    $thisis = "multiple";
    $response =  "Please choose only one category! <br />";
    if ($it > "86" ||  $it == "0" || !$it) {
    $thisis = "error";
    $response = "There was an error!<br />";

    It allows me to set specific templates based on the category.

    It works in my page template either as a standalone or calling the function but, does not work in the header template. Perhaps I am outside of the "loop"? I was also trying to use the above to create switches for creating the two nav links that will link to the latest post in that cat.

    I really know there is an easy solution, the wp codex is confusing me more.

  3. Zentode
    Posted 6 years ago #

    Sorry all,

    Looks like keeping the functions in the post template files and out of the header was the solution. (and deleting the above code!)

    I always seem to make things too complicated.

  4. Zentode
    Posted 6 years ago #

    This solved almost all my issues.


    I was able to set variables, pass them to other scripts and use them in remotely hosted files too pulled in via curl. I used is_category to detect which was being viewed and set it in wp_cache. (After testing if empty and some other items specific to my application.)

    My index.php file then determines which template file to use.
    VERY basically:

    <?php if (is_archive() || is_single()) {
    if (wp_cache_get('category') == "Newsletter") {
    include(TEMPLATEPATH . '/newsletter.php');
    if (wp_cache_get('category') == "Plans") {
    include(TEMPLATEPATH . '/plans.php');

    I have achieved so much with just a couple lines of code. I am learning. Hopefully, I will be able to help someone figure out their coding query here someday!

Topic Closed

This topic has been closed to new replies.

About this Topic