Loop through pages with specific template

WordPress saves page templates that is assigned to a specific page in the db in the wp_postmeta table. These are saved as follows: ‘meta_key’ => ‘_wp_page_template’, ‘meta_value’ => ‘NAME_ OF_TEMPLATE’ With this in mind, you can loop through pages which shares a specific page template, using get_pages (You can also make use of WP_Query) Example: … Read more

Loop through all posts in a certain year

You will have to run a custom query for that. Here is the query for displaying posts from a particular year. I have used year 2012 as an example. $args = array( ‘post_type’ => ‘post’, ‘ignore_sticky_posts’ => 1, ‘year’ => ‘2012’, ); $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) : while ( … Read more

Style first 3 posts differently with a container, then the rest of the posts [closed]

As Milo noted, the final result might look something like the following if ($wp_query->have_posts()) { while ($wp_query->have_posts()) { $wp_query->the_post(); $post_title = get_the_title(); # On the first pass, write the start of your single container if ( 0 == $wp_query->current_post ) { echo ‘<div class=”container”>’; } echo <<<HTML <div class=”different-post-style”> {$the_title} </div> HTML; # On the … Read more

Filtering The Loop For Single Page and Blog Page

As an example (adjust to suit your needs): add_action(‘loop_start’, function() { if ( is_single() || is_home() ) echo ‘<div>’; }); add_action(‘loop_end’, function() { if ( is_single() || is_home() ) echo ‘</div>’; }); You may need to adjust the conditional statements further if you wish to target archive pages and the like — learn more about … Read more

Get Pagination (WP-PageNavi) not to work

Couple of issues here; Don’t use get_posts for paginated queries. It legally breaks/ignore pagination as it passes no_found_rows=true to WP_Query. Further more, get_posts only returns the $posts property from the query object, and pagination needs other properties from the query object to calculate pagination. You can paginate get_posts, but it is a really messy affair, … Read more

Loop repeating design pattern

You could use 3 separate loops, and break out of each loop early once you’ve got enough posts. For example, here is a 2 column grid with a single query: $q = new WP_Query( … ); if ( $q->have_posts() ) { ?> <div class=”columns”> <div> <?php $counter = 0; while( $q->have_posts() ) { $q->the_post(); the_title(); … Read more

Adding shortcode closing tag after a loop

Collect all of the loop’s output in a variable, wrap it in the accordion shortcode, then pass that through do_shortcode: $output=””; if ( have_posts() ) : while ( have_posts() ) : the_post(); $output .= ‘<div class=”col-lg-12 col-md-12 col-sm-12 col-xs-12″>’; $output .= do_shortcode(‘[su_spoiler title=”‘.get_the_title().'” open=”no” style=”default” icon=”plus” anchor=”” class=””]’.get_the_content().'[/su_spoiler]’); $output .= ‘</div>’; endwhile; else : $output … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)