Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

single.php - show content if in_category WordPress

  • SOLVED

<strong>Problem</strong>
On single.php I need to show different post content depending on the category which the post is being <em>viewed in</em>. (All posts all in multiple categories).

Custom fields would be easy except it's long, multi-paragraph text that needs formatting. (can custom fields be formatted (easily by non WP people))?

<strong>Example</strong>
postname = I'm Not That Smart
post slug = im-not-that-smart
post categories = category A, B & C

<strong>What I want</strong> (but not necessarily the method to achieving it)
if viewing at URL/category-a/im-not-that-smart, then show 'content 1'
else if viewing at URL/category-b/im-not-that-smart, then show 'content 2'
else if viewing at URL/category-c/im-not-that-smart, then show 'content 3'

I've got it working where posts are in a single category. I've installed the Secondary Content plugin which is excellent (http://www.cmurrayconsulting.com/software/wordpress-secondary-html-content) and this works perfectly via if in_category when each post is in one category ONLY. Obviously when multiple categories are selected for a post, in_category is true for all and therefore all secondary_content displays.

This works when post is in single category .....


<?php the_content(); ?>
<?php the_secondary_content(); ?>
<?php if(post_is_in_descendant_category(4)) : ?><?php the_secondary_content(2); ?><?php endif;?>
<?php if(post_is_in_descendant_category(5)) : ?><?php the_secondary_content(3); ?><?php endif;?>
<?php if(post_is_in_descendant_category(6)) : ?><?php the_secondary_content(4); ?><?php endif;?>


I don't know how/if it's possible to make this work for posts in multiple categories.

Is it possible to call/detect the category the category slug or something? I asked Andrew Nacin about detecting category slug of viewed post.

I asked .... "In single.php can you determine the category which the post is being viewed 'from' (category/post.php slug?)."

He replied ... "Not without checking the referer... Because the post URLs will look the same either way." (link here: http://twitter.com/nacin/status/23952336070)

Still a bit beyond me.


* This is super urgent
**I posted this in the WordPress forums earlier but have not had any replies.: http://wordpress.org/support/topic/multpi-category-conflict-how-to-use-in_category-when-1-category-exists?replies=1

Answers (1)

2010-09-09

Denzel Chia answers:

Hi,

<blockquote>Custom fields would be easy except it's long, multi-paragraph text that needs formatting. (can custom fields be formatted (easily by non WP people))?</blockquote>

You can use custom meta box, to update custom fields.
Please see my attached image.
You can use jQuery to initialize tinyMCE on any custom meta box textarea, thus enabling formatting of post.

But, the trick is to use do_shortcode() and wpautop() on the custom field data when it is retrieved for use in theme. Or your custom field data will appear not formatted, and any shortcode put in custom field data will not be activated.

Please reply if you are interested to use this approach, so that I can show you some example codes.

Thanks.


Cracks comments:

Definitely interested.

Are there any negatives to doing it this way? Does it limit what I can do with the custom fields?

Now that I think about it though, is it any easier to achieve my goal with custom fields (as opposed to the secondary_content plugin)? Or does it leave me in the same dilemma? How would I isolate the custom field according to the category being viewed?

if viewing at URL/category-a/im-not-that-smart, then show 'custom_field'
else if viewing at URL/category-b/im-not-that-smart, then show 'custom_field'
else if viewing at URL/category-c/im-not-that-smart, then show 'custom_field'


Denzel Chia comments:

Hi

I downloaded secondary content plugin and read the codes, it is the same as what I have for my theme. The secondary content plugin is actually custom meta boxes, using custom fields.

I have an idea from the above code you post, since you say secondary content plugin works for you.


<?php the_content(); ?>

<?php the_secondary_content(); ?>

<?php

if(is_category('4')){
the_secondary_content(2);
}elseif(is_category('5')){
the_secondary_content(3);
}elseif(is_category('6')){
the_secondary_content(4);
}else{
//do nothing
}

?>


You can refer to http://codex.wordpress.org/Function_Reference/is_category for other category check parameters.

I am not sure if
is_category('4') with the quotes are correct or
is_category(4) without the quotes are correct.

You may have to try them out.

Thanks.


Denzel Chia comments:

Hi,

You can also use the category slug to determine which category you are in,

for example;


<?php the_content(); ?>

<?php the_secondary_content(); ?>


<?php

if(is_category('category-a')){

the_secondary_content(2);

}elseif(is_category('category-b')){

the_secondary_content(3);

}elseif(is_category('category-c')){

the_secondary_content(4);

}else{

//do nothing

}

?>


Thanks


Cracks comments:

* Remembering that the post is in <em>more-than-one category</em>,
* Remembering that this is in <em>single.php</em>
* secondary_category <em>only works</em> when posts are in one category (not mulitple categories) .....

Won't the above code result in the same problem?
The post is in category-a AND category-b AND category-c. So all conditional statements are true (always. Irrespective of which category the post is being viewed in), and therefore all secondary_contents are displayed.




Cracks comments:

Just to clarify .....

* post is 'hello world'
* post is in 3 categories (category-a, category-b, category-c)
* 3 <em>different</em> contents will be declared - content for category-a, content for category-b, content for category-c
* I'm totally open to how the content is declared - either custom field (where wysiwyg-able) or a plugin like secondary_content

The end result just needs to be ...
* if viewing at - URL/category-a/hello-world, show content declared for category-a
* if viewing at - URL/category-b/hello-world, show content declared for category-b
* if viewing at - URL/category-c/hello-world, show content declared for category-c


Denzel Chia comments:

Hi,

Got the solution. Please put the below in your functions.php


<?php
//function to get current url of webpage
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}

//find match of category slug in current url
function cat_match($cat_name){
$subject = curPageURL();
$pattern = "/$cat_name/";
preg_match($pattern, $subject, $matches);
echo $matches[0];
}

?>


Put the below code in single.php



<?php the_content(); ?>

<?php the_secondary_content(); ?>


<?php
if(cat_match('category-a')){
the_secondary_content(2);
}elseif(cat_match('category-b')){
the_secondary_content(3);
}elseif(cat_match('category-c')){
the_secondary_content(4);
}else{
//none
}
?>



Thanks


Denzel Chia comments:

Hi,

Sorry make a mistake, for function cat_match($cat_name)
it should be return not echo

function cat_match($cat_name){

$subject = curPageURL();

$pattern = "/$cat_name/";

preg_match($pattern, $subject, $matches);

return $matches[0];

}


Thanks.