Grow your CSS skills. Land your dream job.

how do i write this if statement?

  • Dee
    # July 16, 2009 at 2:29 am

    Hey guys hopefully someone out there can help me with this.

    I have a bunch of pages that have custom fields within them. One of the custom fields I created is a checkbox named "featured". I’d like to grab the custom field values from all the pages that checked off the checkbox and feature them in the sidebar.

    Code:

    < ?php

    query_posts(array('showposts' => 5, ‘post_parent’ => 90, ‘post_type’ => ‘page’));

    while (have_posts()){

    the_post();

    $thumbPath= get_post_meta($post->ID, ‘feature_thumbnail’ , true);
    $featureTitle = get_post_meta($post->ID, ‘feature_title’ , true);
    $featureDescription = get_post_meta($post->ID, ‘feature_description’ , true);

    ?>

    < ?php echo $featureTitle ?>


    < ?php echo $featureDescription ?>

    < ?php

    wp_reset_query();
    }
    ?>

    Right now the code echo’s out the custom fields but the problem is i only want to echo out the custom fields from the pages with the featured checkbox clicked. Thanks :D

    # July 16, 2009 at 4:29 pm

    an if statement in PHP is very easy,

    it looks like this:

    Code:
    < ?
    if ( /*Condition*/ ) {
    //whatever you want to happen if it's true.
    }
    ?>

    For example:

    Code:
    if ($checked = true) {
    /*code to show post*/
    }

    however I don’t know how you currently grab the checkbox’s value for each post yet, but that’s how to use an if statement.

    # July 19, 2009 at 2:22 pm

    you’d probably want to do:

    Code:
    if($checked == true) {
    /* code here */
    }

    if you do $checked = true, you’re going to be setting $checked to true, not actually validating that the variable $checked already holds the value true.

    # July 23, 2009 at 4:42 pm

    How do I write this if statement? You don’t! You don’t need an If statement here, you need to go back to your query_posts statement. Since you only want to select posts that have a certain custom field (or post meta in wordpress parlance) you will need add a couple of more parameters to this statement.

    This will depend of course on what you called your custom fields and exactly what checking the box sets the field value to (since custom fields can only contain text – it might be set to ’1′, ‘true’ or ‘TRUE’ depending on what you used to add the checkbox).

    query_posts(array(‘showposts’ => 5, ‘post_parent’ => 90, ‘post_type’ => ‘page’,
    ‘post_status’ => ‘publish’, ‘meta_key’ => ‘featured’, ‘meta_value’ => ’1′));

    In this example the custom field is called ‘featured’ and a checked off checkbox sets the field to ’1′.

    Hope that is what you were trying to achieve – any problems let me know.

    PS It’s always wise to include post_status=publish, unless you want draft posts to be included the list.

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

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".