Support

Account

Home Forums General Issues Selection option used in header

Solving

Selection option used in header

  • I am not sure if I am missing a step. I am using this code in my header.php:

     <?php if (get_field('options_posts') == "para_1") {
    	      echo '<link rel="stylesheet" id="parallax-css"  href="#" type="text/css" media="all" />' ;
          } else if (get_field('options_posts') == "para_2") {
            echo '<link rel="stylesheet" id="parallax2-css"  href="#" type="text/css" media="all" />' ;
          } ?>

    I can not get it to echo out the content when the choice in the selection compontent is selected. Do I need to do something else? I was wondering if I need to do a step in the functions.php.

  • Hi @justawebbie,

    If this code is in your header, I’m guessing you’re trying to pull this field in from an options page?

    If that’s the case, you just need to add in another parameter to get_field().

    So it would become get_field('options_posts','option').

    If you’re just trying to pull in the field from the page you’re on, I’m assuming your header is being called before the WP loop starts. In that case, you’ll need to pass the page/post ID to get_field() instead. Let me know if you need more clarification.

  • Hi @dkeeling,

    It is an option someone can set on a post by post basis and should only be needed for the single view of that individual post.

    I am not quite sure how to pass the post id dynamically to the get_field() would you mind showing me how to do this.

    Thank you in advance.

  • Sure, no problem! Ok, since we’re outside the loop, first you just want to grab the global query that is being passed to your template files and assign the post data to a variable.

    Then you just add the post ID as a second parameter to get_field(). Here’s what it should look like:

    
    global $wp_query;  
    $post = $wp_query->post;
    
    <?php if (get_field('options_posts', $post->ID) == "para_1") {
        echo '<link rel="stylesheet" id="parallax-css"  href="#" type="text/css" media="all" />' ;
    } else if (get_field('options_posts', $post->ID) == "para_2") {
        echo '<link rel="stylesheet" id="parallax2-css"  href="#" type="text/css" media="all" />' ;
          } ?>
    

    That should take care of it for you, but if you have any trouble, let me know.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Selection option used in header’ is closed to new replies.