Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Element location

  • Hello.

    For example, the categories are organized like this.

    travel ( parents )
    └ unitestates (child, subcategory)
    └ japan (child, subcategory)
    └ france (child, subcategory)

    For example, when writing, check only ‘japan’. (The category is shown as ‘japan’, but systematically, japan belongs to a subcategory of ‘travel’)

    I wanted to print out ‘After_content Element’ to all categories belonging to the travel, so I made ‘After_content’ Element.

    I have designated both location as Post category Archive ‘Travel’ and Post category ‘Travel’, but the ‘After_Content’ element is not output on the paper.

    If I want to print ‘After_content’ in units, Japan, France, etc. belonging to ‘Travel’, do I have to select them one by one? In other words, should I add all subcategories belonging to ‘Travel’ as well as select ‘Travel’? If I select the upper category, doesn’t the lower category automatically apply?

    I’ve given an example as ‘World’, but the actual site is ‘Tip’ and can be found in Priatab.

  • Hi James,

    Yes, you need to add the Sub Categories in the Display Rule settings if you want your Element to display for them.

    If you want it to be automatic that the Element displays for all sub categories of the category, you’ll need a custom PHP snippet. Here’s what you may refer to: https://docs.generatepress.com/article/generate_element_display/#display-an-element-to-a-parent-page-and-all-child-pages

  • Where can I change 1180 and 415?

  • Hi there,

    that won’t work, Fernando linked you to an example that is for Parent Pages not categories.

    If you want an element to display on child categories you need to include them in the Element Display Rules.

  • So, if there are 20 subcategories, do you mean I have to add all 20 of them one by one?

  • Yes, that is correct.

    Or give this snippet a try, replace 1000 with the actual element ID which can be found in the element editor’s URL, and replace travel with the actual parent category slug.

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        global $post;
        $travel_category = get_term_by( 'slug', 'travel', 'category' );
        
        if ( $travel_category ) {
            $travel_category_id = $travel_category->term_id;
            $subcategories = get_term_children( $travel_category_id, 'category' );
    
            // Check if the post is in the "travel" category or its subcategories
            if ( 1000 === $element_id && ( is_single() &&  has_term( $subcategories, 'category', $post ) ) ) {
                $display = true;
            }
        }
    
        return $display;
    }, 10, 2 );
  • https://postimg.cc/d7Fjy07z

    You mean change 1,000 to 2205 in the Archive Element (tip) url as in the image, and change travel to the Slug in the top category that you want to apply?

  • Hi James,

    Yes, that’s correct.

  • 1. I modified the PHP code to element ID 2205, and changed the travel to tip.

    2. I’ve done the location with the tip, but it still seems to work only with the tip.

    Can you check it through the link, please?

  • Alright. Once you’re done editing the Element, let us know so we can view it.

  • Yes, it’s available now.

    And I pressed the information menu, but the categories on the tip are displayed, what should I do with this?

  • I’m checking the Loop Template you have for Tip. Could you remove and Display rule setting you’ve set for this.

    Also, could you exclude Cartip for the “Archieve” Loop Template for testing purposes?

  • I’m sorry, I didn’t understand. You can use it as much as you want for the test.

  • Could you try this snippet instead?:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        $current_category = get_queried_object();
        // Get the ID of the "tip" category
        $tip_category_id = get_cat_ID('tip');
        
        if ( $tip_category_id ) {
            // Check if the post is in the "tipl" category or its subcategories
            if ( 2205 === $element_id && is_category() && ( is_category( 'tip' ) || cat_is_ancestor_of( $tip_category_id, $current_category->term_id ) ) ) {
                $display = true;
            }
        }
    
        return $display;
    }, 10, 2 );
  • I think it’s working well. Can you check the code snippet link about the tip subcategories coming out even though I pressed info?

  • And, we need to generate code snippet the upper categories that we want to include all the subcategories such as Tip, Info, etc., right?

    ex)

    tip _ codesnippet_2205_tip
    info_ codesnippet_3102_info

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