0

I am wondering on the best way to approach a particular (common) scenario.

I have a standard wordpress website, with various pages.

I have one page currently designated to be the "blog" - this is currently powered by a template in the format page-81.php - where 81 is the ID of this page.

I want to loop all of the posts in the posts - nothing special here, I want all the posts to be listed on the blog page with pagination of 10 posts per page.

I also want the categories and tags to filter the loop by the corresponding categories and tags.

At present, Wordpress has been told that the Blog page (page-81.php) - is the blog page. and (if it matters) the homepage has also been set to a static page.

I also have an index.php file, which contains the same layout details as page-81.php - I have no idea if this is correct or not, because at present links from wp_list_categories() seem to point to it - even though I have specified in the backend of Wordpress that my blog is "page-81.php" under reading settings.

Ideally, I would like to know:

1) Do I need these two templates, or is there some way to marry them together and just have one template (page-81.php and index.php). As a side note, I'm using it in a Wordpress menu item, and there doesn't seem to be an option to show "Blog Archive" out of the box, rather than having Page-81.php.

2) What should the loop look like? At present I'm using this:

query_posts( array ( 'posts_per_page' => 10 ) );
if (have_posts()): while (have_posts()) : the_post(); ?>

Placed inside loop.php which seems to work. However everywhere I've read tells me I should be using WP_Query. My issue with that is what does WP_Query need to look like to cater for each of the scenarios of filtering by tag and / or category and / or search parameters? i.e. the default parameters that seem to already get handled in various templates.

Oh yeah, and the_posts_pagination doesn't seem to work either when using query posts, so I'm hoping it will play ball if / when I get WP_query with standard wp variables passed in.

EDIT: modified to use WP_Query this is what I have so far: which seems to be working ok, albeit a shed ton of code for what I thought should be simple?

if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
    $paged = get_query_var('page');
} else {
    $paged = 1;
}

$custom_query_args = array(
    'post_type' => 'post',
    'posts_per_page' => get_option('posts_per_page'),
    'paged' => $paged,
    'post_status' => 'publish',
    'order' => 'DESC', // 'ASC'
    'orderby' => 'date' // modified | title | name | ID | rand
);

if ( get_query_var('cat') ) {
    $cat_id = get_query_var('cat');
    $custom_query_args['cat'] = $cat_id;
}


$custom_query = new WP_Query( $custom_query_args );

if ($custom_query->have_posts()): while ($custom_query->have_posts()) : $custom_query->the_post(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.