0

On my website I created a custom metabox that is being displayed in the post type "page". The metabox will display a checkbox and 4 url fields. The checkbox will control the visibility of a div (section#footerLinks) and the url fields will control the 4 links that will appear with the div links.

Ability

The checkbox once clicked (on) will display my div with the four links, once I uncheck the box (off) it will hide the div.

The Problem

When I click the checkbox on and fill in the custom fields information it works perfectly. But this gets assign to all of my pages not just the one I have assigned. If I click the checkbox "on" for another page it displays two of the same divs.

Is there a way I can have each page control the div independently and only display one div?

Example

Page 1 should display the div with its own information and hide on all other pages.

Page 2 should display the div with its own information and hide on all other pages.

Below is my code I am using the hide the div and control the meta_value of my checkbox selection on

 <?php
  $args = array(
    'post_type'     => 'page',
    'meta_key'      => '_cmb_linkCheckbox',
    'meta_value'    => 'On',
    'showposts'     =>  1,
 );
 $links = new WP_Query( $args );
 ?>
 <?php if ( $links->have_posts() ) : while ( $links->have_posts() ) : $links->the_post(); ?> 
 <section id="footerLinks">
  <article id="footerIcons">
     <ul>
        <li>
          <ul><!-- CONTACT US -->
            <li><a href="<?php echo get_post_meta($post->ID, "_cmb_contactURL", true); ?>"><img src="<?php bloginfo('template_directory'); ?>/images/contactUs.png" /></a></li>
            <li><h4>Contact Us</h4></li>
          </ul>
        </li>
        <li>
          <ul><!-- REQUEST INFO -->
            <li><a href="<?php echo get_post_meta($post->ID, "_cmb_requestURL", true); ?>"><img src="<?php bloginfo('template_directory'); ?>/images/requestInfo.png" /></a></li>
            <li><h4>Rquest Info</h4></li>
          </ul>
        </li>
        <li>
          <ul><!-- VISIT -->
            <li><a href="<?php echo get_post_meta($post->ID, "_cmb_visitURL", true); ?>"><img src="<?php bloginfo('template_directory'); ?>/images/visit.png" /></a></li>
            <li><h4>Visit</h4></li>
          </ul>
        </li>
        <li>
          <ul><!-- APPLY -->
            <li><a href="<?php echo get_post_meta($post->ID, "_cmb_applyURL", true); ?>"><img src="<?php bloginfo('template_directory'); ?>/images/apply.png" /></a></li>
            <li><h4>Apply</h4></li>
          </ul>            
        </li>
     </ul>
  </article>   
  </section>
 <?php endwhile; endif; ?>
  • if this is on a page and your intention is to show the links that belong to the current page, then you shouldn't be querying for anything or running the loop. your query is returning the first page regardless of what page you're currently on. – Milo Mar 9 '15 at 15:29
  • yeah I have been noticing this and have tired other solutions and nothing seems to be working. How would I go about this? @Milo – bigant841 Mar 9 '15 at 15:32
  • just delete the query and the loop and leave your markup and get_post_meta calls. $post->ID will then refer to the current page and not the page you queried for in the loop. – Milo Mar 9 '15 at 15:35
  • I tried that and it seems to still be calling from the first page @Milo Also if I delete the query how will I control the visibility of the div which is controlled by the checkbox? It needs to be hidden if the checkbox is not selected. – bigant841 Mar 9 '15 at 15:36
  • If there is another query elsewhere in the page, then you need wp_reset_postdata() first to restore the current page data. – Milo Mar 9 '15 at 15:39
2

If you just want to check if a meta value exists before outputting markup, then

if( get_post_meta( $post->ID, '_cmb_linkCheckbox', true ) ){
    ?>
    <section id="footerLinks">
        <?php // your markup... ?>
    </section>
    <?php
}

or if the value is "on":

if( 'on' == get_post_meta( $post->ID, '_cmb_linkCheckbox', true ) ){
    // etc..
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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