Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create Query-based template files?

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 94
    Comment on it

    Welcome to wordpress discussion. Today we are going to discuss the database related functions in wordpress. We can simply create query to display the different types of posts. We start the discussion with hook named pre_get_posts which calls the function before getting the posts. it can change the queries for different conditions. We are going to take an example which is useful to modify the query.

    <?php
    function modify_query() {
    
     if ( !is_front_page() && $myquery->is&#95;main&#95;query() ) {
    
     $myquery->set( 'post_type', array( 'post', 'custom_post_type') );
     return $myquery;
    
     }
    
    }
    add_action( 'pre_get_posts', 'modify_query');
    ?> 
    

    We need to pass the array of arguments in WP_Query function and with the help of have_posts function you can check the posts and to display the posts details write the function $myquery->the_post() . You can get the post details with different posts functions.

    <?php
    $args = array(
     'post_type' => 'post',
     'posts_per_page' => '4',
     'post_not_in' => array( $post->ID )
    );
    // the query
    $myquery = new WP_Query( $args );
    
    // The Loop
    if ( $myquery->have_posts() ) { ?>
    
     <section class="recent-posts clear">
    
     <?php while ( $myquery->have_posts() ) : $myquery->the_post() ; ?>
    
     <article id="post-<?php the_ID(); ?>" <?php post_class( 'left' ); ?>>
    
     <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
     <?php post_thumbnail( 'thumbnail' );?>
     </a>
    
     <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
     <?php the_title(); ?>
     </a>
    
     </article>
    
     <?php endwhile; ?>
    
     </section>
    
    <?php }
    wp_reset_postdata();
    ?>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: