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.

Basically i have a custom post with a custom metabox, with a checkbox inside it.

If you check the box i want something (styled box) to appear within the post loop.

If not checked the box doesn't appear...

I have the following in my template page..

<?php get_header(); ?>

<?php $loop = new WP_Query( array( 'post_type' => 'books','posts_per_page' => '-1') ); ?>

<?php while (have_posts()) : the_post() ; $meta = get_post_meta( get_the_ID(), 'rw_title_block', true ); ?>

<?php if( checked( $meta, 1, false ) ) : ?>

  check box is checked to display stuff like... <?php the_title(); ?>

<?php endif ?>     

not checked ignore this stuff...

<?php endwhile; ?>

<?php wp_reset_query() ?>

<?php get_footer(); ?>

The metabox functions code (this seems to display fine in the admin.)

$meta_boxes[] = array(
'id'         => 'test_metabox',
'title'      => 'Test Metabox',
'pages'      => array( 'books', ),
'context'    => 'normal',
'priority'   => 'high',
'show_names' => false, 
'fields'     => array(

    array(
        'name' => 'Test Checkbox',
        'desc' => 'field description (optional)',
        'id'   => $prefix . 'title_block',
        'type' => 'checkbox',
    ),

),
);

Thank you for any help, it would be a life saver.

Regards.

share|improve this question

1 Answer 1

First make the custom post query with the WP_Query, and then we get the metadata by get_post_meta. Checkboxses are saved as boolean, that means either true or false. So make a check in the loop if checked or not (1=true, 0=false).

<?php get_header(); ?>

    <?php

    // The Query
    $the_query = new WP_Query( array(
       'post_type'      => 'books',
       'posts_per_page' => -1,
    ));

    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post(); 

    $checke_meta = get_post_meta( $post->ID, 'rw_title_block', true );
    ?>

    <?php if( $checked_meta ) {
       echo 'Do stuff here if checked';
    } else {
       echo the_title();
    }
    ?>

   <?php 
   endwhile;
   // Reset Post Data
   wp_reset_postdata();

   ?>

<?php get_footer(); ?>
share|improve this answer
    
That doesn't seem to be working, in fact its only showing the content from the page its on. Rather than the custom post type, this is a template attached to a page and its only showing the page content... any ideas, never usually have this problem displaying custom posts on a page..? –  user4630 Jun 7 '12 at 12:45
1  
the problem with your code is that you don't check if the custom post have post, you only check if "post (have_posts)" instead of your own loop ( $loop->have_posts) so thats why you get the page content. look at my updated answer on the $the_query –  Pontus Abrahamsson Jun 7 '12 at 13:17
    
I found this worked...pastebin.com/TX21G3iM –  user4630 Jun 26 '12 at 9:31

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.