Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* DEFINING THE METABOX
- * note I have used FUNCTIONS_PATH- this is a constant i've set in my theme, so you'd need to adjust accordingly
- */
- $query_metabox = new WPAlchemy_MetaBox(array
- (
- 'id' => '_query_meta', // underscore prefix hides fields from the custom fields area
- 'title' => _('Custom Query Arguments'),
- 'template' => FUNCTIONS_PATH .'WPAlchemy/query_meta.php',
- 'prefix' => '_query_meta_',
- 'include_template' => array('template-page-blog.php','template-page-home.php'),
- 'priority' => 'high',
- ));
- /* QUERY_META.PHP
- * contents of my metaboxes- multi group that asks for query parameters and their respective value
- * I use a little jquery to hide the normal post box if I don't want any content shown
- */
- <script type='text/javascript'>
- /* <![CDATA[ */
- //avoid jquery flicker
- document.write('<style type="text/css">body{display:none}</style>');
- jQuery(document).ready(function($) {
- //hide normal text editor when editing page running blog template
- var template = $('select#page_template').val();
- if( template == "template-page-blog.php" ) {
- $('#postdivrich').hide();
- }
- //show page after loading
- $('body').css('display','block');
- });
- /* ]]> */
- </script>
- <div class="my_meta_control">
- <p><?php _e('This page is running a blog-style template and will display all your posts unless you modify the query posts arguments below. ');?></p>
- <p><?php _e('See the ');?><a href="http://codex.wordpress.org/Function_Reference/query_posts"><?php _e('Wordpress Codex Function Reference');?></a> <?php _e('for more details on helpful parameters.')?><?php _e('Please be careful and leave this blank if you are not comfortable with query parameters.')?></p>
- <h4><?php _e('Query Arguments');?></h4>
- <a style="float:right; margin:0 10px;" href="#" class="dodelete-args button"><?php _e('Remove All Arguments');?></a>
- <p><?php _e('Add arguments to the custom query by entering in the parameter name and the corresponsding parameter value. Add new arguments by using the "Add Argument" button.');?></p>
- <?php while($mb->have_fields_and_multi('args')): ?>
- <?php $mb->the_group_open(); ?>
- <table>
- <tr>
- <td><p style="margin-bottom:0"><span><?php _e('Parameter');?></span></p></td>
- <td><p style="margin-bottom:0"><span><?php _e('Value');?></span></p></td>
- </tr>
- <tr>
- <td>
- <?php $mb->the_field('param_key'); ?>
- <p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>
- </td>
- <td>
- <?php $mb->the_field('param_value'); ?>
- <p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>
- </td>
- <td>
- <p><a href="#" class="dodelete button">Remove</a></p>
- </td>
- </tr>
- </table>
- <?php $mb->the_group_close(); ?>
- <?php endwhile; ?>
- <p style="margin-bottom:15px; padding-top:5px;"><a href="#" class="docopy-args button">Add Argument</a></p>
- </div>
- /* MODIFYING THE QUERY BASED ON THE RESULTS
- * essentially I loop the values I got from the metaboxes to create an array
- * then i merge this array with some default to create a new array that will serve
- * as the query parameters */
- $user_args = array();
- // loop a set of field groups to create user query
- while($query_metabox->have_fields('args')) {
- $user_args[$query_metabox->get_the_value('param_key')] = $query_metabox->get_the_value('param_value') ;
- }
- /*solves the problem with pagination on a static page*/
- if ( get_query_var('paged') ) {
- $paged = get_query_var('paged');
- } elseif ( get_query_var('page') ) {
- $paged = get_query_var('page');
- } else {
- $paged = 1;
- }
- $default_args = array('posts_per_page' => 5, 'paged' => $paged );
- $query_args = array_merge($default_args, $user_args);
- $wp_query = new WP_Query();
- $wp_query->query( $query_args );
- $more = 0;
- /* just a note that you can retrieve the current, default WP query by accessing the global object variable $wp_query
- $default_args = $wp_query->query_vars;
- $my_args = array('posts_per_page'=>6);
- $new_args = array_merge($default_args, $my_args);
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement