Get current category id from post page wordpress

I have a problem to get category id of current post. We are using <?php get_the_category( $id ) ?> for finding out category of current post.
If a post like ‘demo’ has 2 category like cat1 and cat2. When i open a cat1 page and then go to demo page, i want different layout.. and when i open a cat2 page and then go to demo page, i want to open this demo page with other layout. It can be only done when i will identify.. from which post has called.. but how??

Related posts

5 comments

  1. WordPress has a function wp_get_referer, which gets the referring url from the http-header. So, if you go from a category archive to a single post, the name of the category will normally be included in that url. This means you can start your single.php like this:

    $refer = wp_get_referer();
    if (strpos($refer, 'category/cat1') != false) {... do something ...}
    elseif (strpos($refer, 'category/cat2') != false) {... do something else ...}
    else {... do a default thing ...}
    

    Depending on your setup this solution may need some tweaking, but the general idea is clear, I hope.

  2. <?php $categories = get_the_category( $post->ID ); ?>
    

    This will return an array of categories, which you can get the id from, like so:

    $categories[0]-> term_id;
    

    This will be for the first category in the array if there is more than one.

    Information on usage can be found in the codex

  3. As mentioned by Gaffen : “changing a posts layout depending on what category is was visited from”

    First on the category template page in your theme (i.e. category.php)

    get the category id and store it in the WordPress Transient option.

    global $wp_query;
    
    //get category id (or name, slug) and store to the transient api
    $categoryId = $wp_query->queried_object->cat_ID;
    set_transient( 'category_id_visited', $categoryId, 1 * HOUR_IN_SECONDS );
    

    now on single.php or the single post display template page.

    get the value from Transient and change display acccordingly

    if ( false === ( $category_id_visited = get_transient( 'category_id_visited' ) ) ) {
        // this code runs when there is no valid transient set
        //do nothing for now
    } else {
        echo 'You came by visiting category: ' . $category_id_visited;
        //delete transient if required, otherwise it will be expired automatically
        delete_transient( 'category_id_visited' );
    }
    

    Hope it helps.

  4. In your single.php you could use for example

    if(has_category('layout-1')) {
      // do layout-1-category stuff here
    } elseif (has_category('layout-2')) {
      // layout-2 stuff here
    } else {
      // stuff for all the other styles/regular stuff
    }
    

    Better yet, use get_template_part so you don’t just flood your single.php with all kinds of silly code.

  5. First get category ID:

    $cat_id = get_queried_object_id();
    

    Then get category name:

    $cat_name = get_cat_name($cat_id)
    

    Then you can decide depending on category name what to do:

    if ($cat_name == 'Cat 1') {
       // ... url = layout-1
    } elseif ($cat_name == 'Cat 2') {
       // ... url = layout-2
    }
    

Comments are closed.