2

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??

1
  • I don't know if you ever got this solved, but I just finished writing a very extensive post navigation function for this, also check out the answer from @G.M. to my original question here. You can, stripping the code from both answers and combining it, know from which category the single post came from and then use the single_template filter to set a specific template Jan 8, 2015 at 7:13

5 Answers 5

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.

0
<?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

8
  • Thanks,for this information. But i need a category from which post has called rather than first category.
    – devjohn
    Dec 19, 2013 at 12:26
  • Sorry, maybe I didn't quite understand, what do you mean by 'which post has called'?
    – Gaffen
    Dec 19, 2013 at 12:27
  • Simply-- there is a category page for cat-1 and its display all its post i.e demo. when i open demo from this page i want to open demo post in layout-1. and if i open demo post from cat-2 category page then it must be open with layout-2.
    – devjohn
    Dec 19, 2013 at 12:41
  • I can do this using query string but i don't want to use this due to url issue.
    – devjohn
    Dec 19, 2013 at 12:45
  • I would suggest either setting the category as post data using either a form (though that's unsemantic) or javascript - though that won't work with javascript, at least it will gracefully degrade. You might also want to rephrase your question to better reflect what you want - changing a posts layout depending on what category is was visited from - as you'll be more likely to get answers.
    – Gaffen
    Dec 19, 2013 at 13:22
0

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.

0

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.

0

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
}
1
  • $cat_id = get_queried_object_id(); will retrive the post ID and NOT the category ID when you are on a post page, like the OP asked.
    – Kevin M
    Feb 15, 2016 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.