Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've been able to figure out how to include the custom field i'm referencing within an inline style within a div that wraps my header.

The problem is that it's in my header and so when a particular Post is called, to display in the body, that has the custom field of color picker and a certain color (say #567263) has been entered from the admin for that post it won't appear because it has no reference to the permalink that was clicked to get to that page.

Here in lies a new lesson for me to learn in WP. And i'm not sure if it's conditions, functions, etc.

I can call a custom post type from within my header like i did here: (this is my custom header for neighborhood pages)

<?php

  $args = array(
    'post_type' => 'neighborhood',
    'posts_per_page' => 1,
    );

  $the_query = new WP_Query( $args );

  ?>


  <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <div class="container-hdr-neighborhood" style="background-color: <?php the_field( 'bg_clr' ); ?>"><!--This is where i added the_field(); i'm referencing in the question-->

    <header class="col-hdr-internal">

      <nav role="navigation">

        <?php

          $args = array(
            'menu' => 'main-menu'
            );

          wp_nav_menu( $args );

        ?>

      </nav>

But it only calls on the "bg_clr" custom field of the last post i created. So i have 27 neighborhoods i've created each with a custom color for their background but the_field(); only captures the hex/color value of the last neighborhood post created.

What could be a good solution to this? I thought about adding this div into the single-neighborhood.php file but it wraps the header so that wouldn't be possible.

I could also include the header php code itself within this particular template, that could be a solution but i want to keep my header separate and push myself to learn something new so i'd rather learn another approach.

Any takers? Thanks.

share|improve this question
    
What do you mean by "but it wraps the header"? What wraps the header and what are you calling "the header"? –  Charles Clarkson Sep 27 '13 at 2:20
    
@CharlesClarkson the container that sits at the top of the page wraps the "header" tag which includes my logo, navs, etc in my particular design. So at the moment i have this header-neighborhood.php file that includes a specific header for single-neighborhood.php pages. The the_field(); that calls on the background value input only takes the value from the last neighborhood post added (I have 27 neighborhoods each requiring a specific bg color) but since the header is not part of the page it doesn't detect the permalink that lead the user to a specific page and hence won't render that color. –  MARS Sep 27 '13 at 13:39
    
@CharlesClarkson So because this top container wraps my header nave i assume it has to be within the header-neighborhood.php file and cannot be inside of the single-neighborhood template and therefore does not render the color value of that specific neighborhood. That's my analysis of the situation though, i could very well be wrong. Thank you. –  MARS Sep 27 '13 at 13:40
add comment

1 Answer 1

Because my neighborhoods were the only pages that required custom header colors i decided to just include its custom header within the single-neighborhood.php page so that the top header container, that was wrapping the navigation, logo, etc., would reflect the custom color chosen from the custom fields in the admin. It was the only way for the header calling the custom field to acknowledge which post was being called within the loop and render the accurate color for the background.

Here was my code to accomplish that. Note that i didn't call this page a template since it's working off of the neighborhood custom post type.

<!DOCTYPE html>

  <html>

    <head>

      <title>

        <?php

          wp_title( '_', true, 'right' );

          bloginfo( 'name' );

        ?>

      </title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="index, nofollow" />
      <?php wp_head(); ?>
    </head>

    <body>

      <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <div class="container-hdr-neighborhood" style="background-color: <?php the_field( 'bg_clr' ); ?>">

        <header class="col-hdr-internal">

          <a class="logo" href="<?php ( 'home' ) ?>" alt="Return Home" title="Return Home"></a>

          <nav role="navigation">

            <?php

              $args = array(
                'menu' => 'main-menu'
                );

              wp_nav_menu( $args );

            ?>

          </nav>

          <h1><?php wp_title($echo); ?></h1>

          <nav role="navigation">

            <?php

              $args = array(
                'menu' => 'neighborhood-menu'
                );

              wp_nav_menu( $args );

            ?>

        <?php endwhile; else: ?>

        <?php endif; ?>

        <?php wp_reset_query(); ?>

          </nav>

  </header>

</div>
<!--/container-hdr-home-->

<!--END HEADER-->

    <?php

    $bg = get_field( 'bg_img' );

    ?>

  <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <div class="bg-img-default" style="background-image: url(<?=$bg?>); "></div>

    <div class="container-main">

      <!--COL 1-->

      <div class="col-block">

        <h2><span class="bold">latest</span> <span class="font-condensed">news</span></h2>

        <?php the_field( 'latest_news' ); ?>

      </div>

      <!--COL 2-->

      <div class="col-block">

        <h2><span class="bold">local</span> <span class="font-condensed">events</span></h2>

        <a><?php the_field( 'local_events' ); ?></a>

      </div>

      <!--COL 3-->

      <div class="col-block-last">

        <h2><span class="bold">listings</span></h2>

        <?php the_field( 'listings' ); ?>

        <a href="/search-listings/">Search Listings</a>

      </div>

  <?php endwhile; endif; ?>

  <?php get_template_part( 'content', 'slider' ); ?>

    </div>

<?php get_footer(); ?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.