Home > child theme > PHP loop that selects posts with a particular in-post option

PHP loop that selects posts with a particular in-post option

April 10Hits:0
Advertisement

Im trying to build a Featured post slider to show any post that i mark as a "featured" (with a in-post option) regardless the category or tag type... Im new to wordpress and although I understand a little bit php language im not a coder :(

The slider is already working. The question is, how can can i build this slider with a loop to show the last 7 post marked as "featured" with the corresponding featured image?

I already have the in-post option set up in a metabox in the post edit page, also the image size configured in my function.php ...

Im using wordpress 3.1 with the genesis framework.

Here is the slider code

<div class="slider">     <ul class="main-wapper">       <li> <img src="path to featured img" title="the post title" height="300" width="315">         <div class="main-item-desc">           <h2><a target="_parent" title="Post Title" href="post link">POST TITLE</a></h2>           <p>Post Description limited to 150 character</p>         </div>       </li>       <li> <img src="path to featured img" title="the post title" height="300" width="315">         <div class="main-item-desc">           <h2><a target="_parent" title="Post Title" href="post link">POST TITLE</a></h2>           <p>Post Description limited to 150 character</p>         </div>       </li>       <li> <img src="path to featured img" title="the post title" height="300" width="315">         <div class="main-item-desc">           <h2><a target="_parent" title="Post Title" href="post link">POST TITLE</a></h2>           <p>Post Description limited to 150 character</p>         </div>       </li>       <li> <img src="path to featured img" title="the post title" height="300" width="315">         <div class="main-item-desc">           <h2><a target="_parent" title="Post Title" href="post link">POST TITLE</a></h2>           <p>Post Description limited to 150 character</p>         </div>       </li>       </ul>   </div>   <div class="navigator-outer">     <ul class="navigator">       <li>         <div> <img src="img src" />           <h3>Content Title H3</h3>           <p><span class="date">20.01.2010</span> | <span class="category">CATEGORY NAME</span></p></div>       </li>       <li>         <div> <img src="img src" />           <h3>Content Title H3</h3>           <p><span class="date">20.01.2010</span> | <span class="category">CATEGORY NAME</span></p></div>       </li>       <li>         <div> <img src="img src" />           <h3>Content Title H3</h3>           <p><span class="date">20.01.2010</span> | <span class="category">CATEGORY NAME</span></p></div>       </li>       <li>         <div> <img src="img src" />           <h3>Content Title H3</h3>           <p><span class="date">20.01.2010</span> | <span class="category">CATEGORY NAME</span></p></div>       </li>      </ul>   </div> </div> 

here is a screenshot of the slider.. PHP loop that selects posts with a particular in-post option

UPDATE: in my functions.php files i have this..

// Add new image sizes add_image_size('Slider', 315, 300, TRUE);  require_once(CHILD_DIR . '/lib/admin/inpost-settings.php'); wich is the code below.. 

inpost-settings.php

$prefix = 'myslider_';  $meta_box = array(     'id' => 'slider-meta-box',     'title' => 'Featured Slider Options',     'page' => 'post',     'context' => 'normal',     'priority' => 'high',     'fields' => array(         array(             'name' => 'Show in featured slider',             'id' => $prefix . 'show_post_slider',             'type' => 'checkbox'         )     ) ); add_action('admin_menu', 'mytheme_add_box');  // Add meta box function mytheme_add_box() {     global $meta_box;      add_meta_box($meta_box['id'], $meta_box['title'], 'mytheme_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']); }  // Callback function to show fields in meta box function mytheme_show_box() {     global $meta_box, $post;      // Use nonce for verification     echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';      echo '<table class="form-table">';      foreach ($meta_box['fields'] as $field) {         // get current post meta data         $meta = get_post_meta($post->ID, $field['id'], true);          echo '<tr>',                 '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',                 '<td>';         switch ($field['type']) {              case 'checkbox':                 echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';                 break;         }         echo     '<td>',             '</tr>';     }      echo '</table>'; } add_action('save_post', 'mytheme_save_data');  // Save data from meta box function mytheme_save_data($post_id) {     global $meta_box;      // verify nonce     if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {         return $post_id;     }      // check autosave     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {         return $post_id;     }      // check permissions     if ('page' == $_POST['post_type']) {         if (!current_user_can('edit_page', $post_id)) {             return $post_id;         }     } elseif (!current_user_can('edit_post', $post_id)) {         return $post_id;     }      foreach ($meta_box['fields'] as $field) {         $old = get_post_meta($post_id, $field['id'], true);         $new = $_POST[$field['id']];          if ($new && $new != $old) {             update_post_meta($post_id, $field['id'], $new);         } elseif ('' == $new && $old) {             delete_post_meta($post_id, $field['id'], $old);         }     } } // check autosave if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {  return $post_id; } 

Thanks in advance.

Answers

All you need is a simple query and to iterate over that query a couple of times so you can build the two required lists. WP_Query has a convenient method for resetting the pointer in the posts array, so you can loop over it again, called rewind_posts though i believe inside custom loops you have to reference the method directly..

Anyway, here's the kind of thing you're looking for, simply make adjustments as necessary..

<?php
    $featured = new WP_Query;
    $featured->query( array(
        'meta_query'  => array(
            array(
                'key'     => 'myslider_show_post_slider',
                'value'   => array('on','1'),
                'compare' => 'IN',
                'type'    => 'CHAR',
            )
        ),
        'post_type'       => 'post',
        'post_status'     => 'publish',
        'ignore_sticky_posts' => '1',
        'posts_per_page' => '7' //The number of post in the slider.
    ) );
    if( $featured->have_posts() ) :
    ?>

    <div class="slider">
        <ul class="main-wapper">

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

        <li>
            <?php the_post_thumbnail(); ?>
            <div class="main-item-desc">
                <h2><a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <p>Where's this description suppose to be coming from?</p>
            </div>
      </li>

        <?php
        endwhile;
        $featured->rewind_posts();
        ?>

        </ul>
    </div>
    <div class="navigator-outer">
        <ul class="navigator">

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

        <li>
            <div>
                 <?php the_post_thumbnail('Slider'); ?>
                <h3><?php the_title(); ?></h3>
                <p><span class="date"><?php the_time( 'd.m.Y' ); ?></span> | <span class="category"><?php the_category(','); ?></span></p>
            </div>
        </li>

        <?php
        endwhile;
        ?>

        </ul>
    </div>

    <?php
    endif;
    ?>

Firstly, there was a stray closing DIV in the HTML you posted, so i simply removed that from the code above.

Secondly, the checkbox does not have a value specified, in such cases a checkbox isn't given a specific value, the browser assigns the checkbox one, i'm not sure whether there's a common value that all browsers will use, but Firefox produces the value on, but i accounted for the possibility that the browser may assign a value of 1, ideally the checkbox should be given a implicit value to ensure consistent behaviour, eg..

echo '<input type="checkbox" value="1" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';

Any questions, lemme know..

Related Articles

  • PHP loop that selects posts with a particular in-post option

    PHP loop that selects posts with a particular in-post optionApril 10

    Im trying to build a Featured post slider to show any post that i mark as a "featured" (with a in-post option) regardless the category or tag type... Im new to wordpress and although I understand a little bit php language im not a coder :( The s

  • How to loop through select options?September 10

    I have an app connected to a list. I have a select option in my app where I can choose between 5 sec,10 sec and 15 sec and that is a setting when the list should update.If I choose 5 sec the list should update every 5 sec etc. I am not shore how to l

  • Excel VBA looping through select worksheetsJanuary 21

    I'm trying to write a Macro that will loop through select amount of worksheets to hide empty rows on each sheet. In column "A" on each worksheet contains a 1 or a 0. If it's a 0 I want to hide the row. Here's my code that I've scrapped together

  • SQLite Loop from SELECT QUERYDecember 24

    I have this long query which works one for processing one sale. What I'm trying to do is loop it until no more B.boughtPending = 0 and S.soldPending = 0 and then Select all the ones that B.boughtPending > 0 and S.soldPending > 0 and also has to matc

  • Looping through selected cells to indent textFebruary 27

    I have written the following Google Spreadsheet script that attempts to loop through all selected cells and then update the value so that it adds an indent, by adding =CONCAT(REPT( CHAR( 160 ), 5),"' + value[row] + '") around the value. Is there

  • Loop using selection field to display content ACF

    Loop using selection field to display content ACFAugust 18

    I 'm trying to create a loop to show only the contents of a multiple choice selection I have in the ACF ! I tried to do the consultation via WP_QUERY as explained in the ACF site , more does not work! Someone would have a solution? Follow my code: <?

  • Loop a Select by Attribute on all features (ArcPy)September 2

    I have a really basic script that selects an input Polygon by an Attribute and then does a series of Buffers on that, a select by location and then appends the result into a final output. I need to run this process through each feature individually.

  • How to loop a Select by Attribute to use in a Select by Location?November 21

    I am new to python scripting and I need help. I am trying to select one row at a time on a shapefile that contains buffer rings around sites we are working on (some rings overlap), and for each site buffer I need to select by location and extract the

  • How to make a loop for select by attribute?April 24

    I am trying to pull out a sample out of a geodatabase file. I have 100800 sample and I need to select 28 records, basically every 3600, I am going to start with FID=0 and go from there. Unfortunately I haven't been able to make it work. this is what

  • JQuery Blur Class - looping and selection issueFebruary 15

    I am trying to take a dynamic background image reference from a bootstrap carousel with the following html: <div class="item active" id="i1"> <div class="fill" style="background-image:url(assets/img/temp/bgPage2

  • Selection sort algorithm with increasing/decreasing sort optionsJanuary 17

    I am running through some traditional algorithms in Go, and I was hoping to get feedback on efficiency and optimization. Below is a basic selection sort algorithm that takes a parameter to dictate increasing, or decreasing order. // SelectionSort //

  • select is not finishing after choosing an optionFebruary 10

    I am working on a script to back up my CouchPotatoServer, but I'm having problems. Here is the code I am having problems with: select OPTION in Backup Restore Finish; do echo "You choose $OPTION CouchPotatoServer settings"; case $OPTION in Backu

  • Safari forces user to select client certificate even if it is optional

    Safari forces user to select client certificate even if it is optionalDecember 20

    This question is loosely related to another one: IIS7.5 SSL Question, Safari users get a prompt of certificate to select, but since it was asked more than a year ago I thought that there may be some changes done in Safari. So, the problem I am facing

  • how to select a program as an "open with" option on mac osxFebruary 20

    I'm using mac osx 10.7.5 and I want to be able to right click a file and chose open with... and have the icon appear in that menu, without having to select "other" and go to my application folder to chose the program. I do not want this program

  • Select Form Element Not Saving from Plugin OptionsSeptember 14

    I am certain it is something silly. But I can't find it and with such simple code at a loss for anything else. Having looked at many other plugins doing settings in a lot of ways can't really see how it is different from those doing similar code. Thi

  • none of options selected in show hidden files in folder option

    none of options selected in show hidden files in folder optionJuly 7

    I want to show my hidden files in one folder but when I go to folder options and view and select the "show hidden files" option, and after OK there is no effect in my folders. Stranger is that when I go to folder options again none of options is

  • Select Items using checkbox and Multiple Submit optionsAugust 7

    A web page defines a few search criterion. When user submits the search criterion, the search results are rendered on the page. These results are email addresses to which the user might want to send an email. This is an email marketing tool. After th

  • Best way to present the user a way to select multiple entries in half a million options

    Best way to present the user a way to select multiple entries in half a million optionsSeptember 3

    We have an already very complex form that represents a contract between a dealer and multiple retailers. Until now, choosing the Retailers was done using a multiselect dropdown with a search textbox to filter results. The problem is that the list of

  • LaTeX command or environment to selectively print one out of several given optionsApril 12

    I'm currently wasting my time by trying to create a proof-of-concept class to show that writing our university's curricula with LaTeX instead of MS Word would have some benefits. Since these curricula are essentially legal documents, there already ex

  • Select default kernel to boot from "Advanced Options"April 19

    GRUB 2.02 I have advanced options entry in grub menu. How can i select the default kernel to boot if it is in advanced options menu?

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.630 s.