Site logo

Archived topic

Display content depending on the url parameter

7 replies · Started by Stefan on December 30, 2022

Viewing posts 1–8 of 8

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 ;-)

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"?

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' );

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;

}

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 );
}

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

You're welcome :)

This archived topic is closed to new replies.