Site logo

[Resolved] Display content depending on the url parameter

Home Forums Support [Resolved] Display content depending on the url parameter

Home Forums Support Display content depending on the url parameter

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2477835
    Stefan

    Hey,

    I have a question that is not part of the Generatepress core … but maybe you have a solution.

    I would like to display different content on a page, depending on the url parameter.

    Example

    test.com/site/?example-1
    Content: XYZ

    test.com/site/?example-2
    Content: ABC

    In the best case it would still be ACF Fields that are displayed.

    Do you have any ideas how this could be implemented?

    I have tried a few examples and found them on the search / Google, but nothing that has brought me completely further.

    Thank you very much for your help!
    If it is too far away from the “GP-Support” – feel free to delete the post 😉

    #2477967
    David
    Staff
    Customer Support

    Hi there,

    does this doc on ACF help?

    https://support.advancedcustomfields.com/forums/topic/acf-field-value-from-query-string/

    The basics here is its using: $_GET['product_type'] to get the product_type query param from the URL which it then uses to return the relevant field.

    #2478117
    Stefan

    Hey David,

    thanks a lot for your help!
    I have now built a version that works:

    add_filter( ‘the_content’, ‘my_quiz_filter’ );

    function my_quiz_filter( $content ) {
    if ( isset($_REQUEST[‘quiz-2’]) && is_single() && ‘quiz’ == get_post_type() )
    {
    $content = ‘<p>’ . get_field(‘quiz_losungs-story’) . ‘</p>’ . $content;
    }

    if ( isset($_REQUEST[‘quiz-3’]) && is_single() && ‘quiz’ == get_post_type() )
    {
    $content = ‘<p>’ . get_field(‘quiz-story-longread’) . ‘</p>’ . $content;
    }

    return $content;

    }

    But maybe you can help me with one last thing.

    In this case the dynamic content is placed in “$content”.
    Is it possible to you use one of the GP hooks like “generate_after_main_content”?

    #2478129
    David
    Staff
    Customer Support

    For sure, instead of using the the_content filter hook. You can use an action hook eg.

    add_action( 'generate_after_main_content', 'my_quiz_filter' );

    #2478139
    Stefan

    Okay, thanks … something I do wrong … or think wrong – in the variant it does not work unfortunately.
    Do you have an idea?

    add_action ( 'generate_after_main_content', 'my_quiz_action' );
    
    function my_quiz_action( $content ) {
      if ( isset($_REQUEST['quiz-2']) && is_single() && 'quiz' == get_post_type() ) 
      {
        $generate_after_main_content = '<p>' . get_field('quiz_losungs-story') . '</p>' . $generate_after_main_content;
    }
    
      if ( isset($_REQUEST['quiz-3']) && is_single() && 'quiz' == get_post_type() ) 
      {
        $generate_after_main_content = '<p>' . get_field('quiz-story-longread') . '</p>' . $generate_after_main_content;
    }
    
      return $generate_after_main_content;
    
    }
    #2478147
    David
    Staff
    Customer Support

    Just print the content to the hook eg.

    
    add_action ( 'generate_after_main_content', 'my_quiz_action' );
    
    function my_quiz_action() {
        $field_value = '';
        if ( !is_single() && 'quiz' != get_post_type() )) {
            return; // quit out if condition is not met
        }
        if ( isset($_REQUEST['quiz-2']) ) {
            $field_value = get_field('quiz_losungs-story');
        }
        if ( isset($_REQUEST['quiz-3']) ) {
            $field_value = get_field('quiz-story-longread');
        }
        printf('<p>%1$s</p>', $field_value );
    }
    #2478151
    Stefan

    Wow! This is fantastic! It works! Thank you very much. I’ll send the new year drink directly to you!

    #2478154
    David
    Staff
    Customer Support

    You’re welcome 🙂

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