Site logo

[Resolved] Different Header on Different Pages / Posts

Home Forums Support [Resolved] Different Header on Different Pages / Posts

Home Forums Support Different Header on Different Pages / Posts

Viewing 15 posts - 1 through 15 (of 27 total)
  • Author
    Posts
  • #2148519
    Brent Wilson

    What is the best way to have a different header on different pages or posts on a website that has a sticky header set to use the navigation as the header? Primarily I would be looking to change the header background color on certain pages/posts.

    #2148593
    Ying
    Staff
    Customer Support

    Primarily I would be looking to change the header background color on certain pages/posts.

    If just background color, CSS should do the job 🙂

    #2148614
    Brent Wilson

    Where would I apply that so that it only applies to certain pages/posts? Would I add CSS to a header element?

    #2148639
    Ying
    Staff
    Customer Support

    You can try something like this:

    .page-id-10 nav#site-navigation {
        background-color: red;
    }
    .page-id-10 nav#sticky-navigation {
        background-color: blue;
    }

    You just need to switch the page ID for different pages.

    #2148640
    Brent Wilson

    And what if it is a certain taxonomy term of posts?

    #2148683
    Ying
    Staff
    Customer Support

    You mean single post page with certain taxonomy?

    If so, CSS can’t target that as there’s no specific taxonomy class in the body tag.

    You can try this PHP snippet to add category to single post’s body class:

    add_filter('body_class','add_category_to_single');
      function add_category_to_single($classes) {
        if (is_single() ) {
          global $post;
          foreach((get_the_category($post->ID)) as $category) {
            // add category slug to the $classes array
            $classes[] = $category->category_nicename;
          }
        }
        // return the $classes array
        return $classes;
      } 

    Then you can use something like this:

    .categoryname nav#site-navigation {
        background-color: red;
    }
    .categoryname nav#sticky-navigation {
        background-color: blue;
    }
    #2148709
    Brent Wilson

    Would it be easier to create the header/navigation as block elements and then assign them to the pages/posts I want them assigned to? Or would it be challenging to get the block element headers to behave as sticky menus?

    #2148722
    Ying
    Staff
    Customer Support

    The current block element – site header replaces the site header, not the navigation.

    When you are using navigation as header, it will create a new header but won’t affect your current navigation.

    And it can’t turn to sticky navigation.

    #2148738
    Brent Wilson

    Okay, so I added the PHP to the child theme. How do I determine what the CSS category name should be for a certain taxonomy term? For example, on this post the term I want to target is taxonomy category slug camp_meeting_years and the individual taxonomy term is 2020: https://wordpress-483838-2431328.cloudwaysapps.com/events/fort-mcmurray-david-guzman/

    #2148745
    Brent Wilson

    <article id="post-247" class="dynamic-content-template post-247 events type-events status-publish hentry age-group-adults time-of-day-evening video-url-yes camp-meeting-year-12">

    I did find this line when I went to view source for that post. I think the camp-meeting-year-12 is probably the class I want to target in order to target posts with the year 2020 taxonomy. So would the .categoryname in your sample CSS just be changed to .camp-meeting-year-12. Or do I need to add more classes or ids or something?

    #2148852
    Ying
    Staff
    Customer Support

    You can’t use the class attached to the article tag, it has to come from a parent element of the header which is body.

    The link you attached is a custom post type, the previous PHP is for regular posts.

    Try this one instead:

    add_filter('body_class','add_category_to_single');
      function add_category_to_single($classes) {
        if (is_singular()  ) {
          global $post;
          foreach((get_the_category($post->ID)) as $category) {
            // add category slug to the $classes array
            $classes[] = $category->category_nicename;
          }
        }
        // return the $classes array
        return $classes;
      }

    And are you sure the taxonomy is the WP core category? Or is it a custom taxonomy?

    #2148970
    Brent Wilson

    Yes, it is a custom post type. And the taxonomy is a custom taxonomy as well.

    #2149026
    Fernando
    Customer Support

    Hi Brent,

    If that’s the case, you would need to alter the PHP code to something like this:

    add_filter('body_class','add_category_to_singlee');
      function add_category_to_singlee($classes) {
        if (is_singular()) {
          global $post;
    		
          foreach((get_field( 'customtaxonomy')) as $category) {
            // add category slug to the $classes array
            $classes[] = $category->slug;
          }
        }
        // return the $classes array
        return $classes;
      }

    For this code to work, kindly set the Return Value of the custom taxonomy to Term Object: https://share.getcloudapp.com/9ZumrKDO

    Kindly replace customtaxonomy in the code above with the field name of your custom field as well.

    Hope this clarifies. 🙂

    #2149994
    Brent Wilson

    I am kind of in over my head on this PHP stuff. But I want to get this issue figured out.

    I am getting an error when I try to save the PHP that Fernando provided above in the child theme:

    Your PHP code changes were rolled back due to an error on line 14 of file wp-content/themes/clariio general child theme/functions.php. Please fix and try saving again.

    Uncaught Error: Call to undefined function get_field() in wp-content/themes/clariio general child theme/functions.php:14
    Stack trace:
    #0 wp-includes/class-wp-hook.php(309): add_category_to_singlee()
    #1 wp-includes/plugin.php(189): WP_Hook->apply_filters()
    #2 wp-includes/post-template.php(836): apply_filters()
    #3 wp-includes/post-template.php(595): get_body_class()
    #4 wp-content/themes/generatepress/header.php(20): body_class()
    #5 wp-includes/template.php(770): require_once('/home/483838.cl...')
    #6 wp-includes/template.php(716): load_template()
    #7 /home/483838.cloudwaysapps.com/cstaqaar

    Here is the PHP I am trying to add:

    add_filter('body_class','add_category_to_singlee');
      function add_category_to_singlee($classes) {
        if (is_singular()) {
          global $post;
    		
          foreach((get_field( 'taxonomy_05r4ilvfz8h')) as $category) {
            // add category slug to the $classes array
            $classes[] = $category->slug;
          }
        }
        // return the $classes array
        return $classes;
      }

    I am not totally confident that I have the right custom taxonomy entered. I have the taxonomy set up as a custom taxonomy. But I have added it as a custom field for this custom post type. So that taxonomy_05r4ilvfz8h is the ID for the custom field that is tied back to a custom taxonomy. Should I instead be using the slug for the custom taxonomy?

    #2150173
    Fernando
    Customer Support

    To confirm, how are you adding the custom taxonomy? The code I provided may not be applicable as to how the custom taxonomy is added to your custom post type. If so, regardless if the slug placed is correct, the code still wouldn’t work.

    Hope to hear from you soon. 🙂

Viewing 15 posts - 1 through 15 (of 27 total)
  • You must be logged in to reply to this topic.