How to use theme options with in_category
Hi I'm tring to add a theme option in wordpress to automatically assign the appropriate template to certain categories and single image templates.
I have my theme option built where a user can enter category numbers separated with a coma and I have modified the single.php template to check if the single post is in that category but the template is not being picked up.
<?php
$catArr = get_option('scp_gallery_cats');
if (in_category( array($catArr)))
{
include (TEMPLATEPATH . '/single-开发者_如何学运维image.php');
}
else {
include (TEMPLATEPATH . '/single-other.php');
}
?>
Any help would be appreciated
<?php
global $options;
global $post;
ob_start();
$catArr = print_r(get_option('scp_gallery_cats'),false);
ob_end_clean();
if (in_category('1')) {
include(TEMPLATEPATH.'/single-image.php.php');
} elseif (in_category (array(($catArr))){
include(TEMPLATEPATH.'/single_other.php.php');
} else {
include(TEMPLATEPATH.'/single_default.php');
}
?>
Try doing a print_r($catArr); is the comma-separate list of categories one string, or is each category a separate value in the array?
If $catArr is actually a string and not an array, you'll have to do something like:
$catArr = explode(",", $catArr);
Given the below comment, I think you have to break the value into an array first; try the following code:
<?php
$catArr = get_option('scp_gallery_cats');
$catArr = explode(",", $catArr); //break the string into array keys!
if (in_category($catArr))
{
include (TEMPLATEPATH . '/single-image.php');
}
else {
include (TEMPLATEPATH . '/single-other.php');
}
?>
Hope this helps!
精彩评论