0

I'm using a wordpress theme which uses custom post types for portfolio items and "sets" to categorize them. When I hover over a "set" to edit, it reveals this at the bottom of the browser: taxonomy=portfoliosets, &tag-id=37 &post_type=portfolios.

I am trying to make it so the header image will change depending on the category or tag. We different sets such as "videos" and "magazines" etc.

So far I've tried adding the following into header.php:

<?php  is_single( 'videos' ) ?>
<style type="text/css">
.page_header {
background: url('mysite/image.jpg') no-repeat scroll center center / cover #F9F9F9 ;
</style>

Which just seems to change the image for everything and:

<?php if ( has_tag('videos') && (in_category('portfolios')) ) 
{ echo '    <style type="text/css">
.page_header {
background: url('mysite/image.jpg') no-repeat scroll center center / cover #F9F9F9 ;
</style>' ; } else { echo '' ; } ; ?>

Which just seems to give errors - any help much appreciated - thanks

1

Two things are important here, your taxonomy is portfoliosets, which mean this is not a built-in category. videos and magazines are terms of your taxonomy. From what I can make from your question, you need to show different header image for different single post page view depending on the term a post belongs to.

Based on this, you can use the has_term( $term, $taxonomy, $post ) conditional tag to check if a post belongs to a certain term. I just want to point out one little mistake here, is_single( $post ) doesn't take the term as parameter, only the post's ID, name or slug.

I would suggest that you make use of is_singular( $post_types ) to check for your single post page.

After all this, your query should be something like this

if(is_singular( 'portfolios')){
    if(has_term( 'videos', 'portfoliosets' )){
        <---DO SOMETHING FOR TERM video--->
    }elseif(has_term( 'magazine', 'portfoliosets' )){
        <---DO SOMETHING FOR TERM magazine--->
    }else{
        <---DO SOMETHING ELSE FOR OTHER POSTS--->
    }
}
  • Thank you so much Pieter - works perfectly and will come in very useful. And thanks Sabarnix for your help too. – speedypancake Jul 14 '14 at 16:44
  • Glad it worked, enjoy :-) – Pieter Goosen Jul 14 '14 at 16:46
0

I think portfoliosets is custom taxonomy and videos and magazines are terms.

So your code should be like

$post_terms = wp_get_post_terms( get_the_ID(), 'portfoliosets', array( 'fields' => 'slug' ) );
if( in_array( 'videos', $post_terms ) ) {
    echo '    <style type="text/css">
        .page_header {
        background: url("mysite/image.jpg") no-repeat scroll center center / cover #F9F9F9 ;
    </style>';
} else {
    echo '';
};

Check out wp_get_post_terms for allowed parameters

  • Thanks - tried that. Added opening and closing <?php brackets to header.php but get a parse error: Parse error: syntax error, unexpected '{' Which is also what I was getting before... – speedypancake Jul 14 '14 at 12:22
  • Missed a bracket there. I've updated the code, hopefully it will work now. – sabarnix Jul 14 '14 at 12:52
  • Thanks for trying sabarnix - that sorted the syntax error, but unfortunately no css changes have any effect :( – speedypancake Jul 14 '14 at 14:22
  • Debug the script a bit. View source the page and see if the css is printed properly and also check if the slug is correct. – sabarnix Jul 14 '14 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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