0

I'm using a child theme based on 'Twenty Seventeen' and have created a custom post type ('review').

I want to make two Pages, one which lists 'reviews' and the other which lists a mixture of 'posts' and 'reviews' (ordered by recency).

For the first of those I've made a copy of Twenty Seventeen's index.php, as page-reviews.php, and added code like this at the start:

$args = array(
    'post_type' => 'review',
    'orderby' => 'date',
    'order' => 'DESC',
    'posts_per_page' => 10,
);
$the_query = new WP_Query( $args );

Then, further down, replaced have_posts() and the_post() with $the_query->have_posts() and $the_query->the_post() respectively.

If I choose to use that template for a Page then it works in that the 'reviews' are listed, but the layout is all messed up. There are a lot of classes in the <body> which mess up the layout.

For example, the standard blog front page, using index.php, has these classes in its <body> and looks fine:

blog logged-in admin-bar hfeed has-header-image has-sidebar colors-light customize-support

While my custom page has these:

page-template page-template-page-postslist page-template-page-postslist-php page page-id-15071 logged-in admin-bar has-header-image page-two-column colors-light customize-support

How can I make a custom template that acts more like index.php in terms of the classes it adds?

4
  • I think you should check which classname is the one being used in CSS, then update the CSS to work with your template. a template acting as another template doesnt soung right, you can just use the original template.
    – David Lee
    Feb 16, 2017 at 18:03
  • Thanks, but that seems impractical - there are a lot of elements on the page that are rendered differently due to the differing classes. If there's a simpler way to make a Page list a Custom Post Type, or a mixture of Post Types, I'd be happy with that too! Feb 16, 2017 at 19:03
  • For the reviews, you could give the custom post type an archive, and then customize archive.php. That might get the default styling closer to index.php's.
    – WebElaine
    Feb 16, 2017 at 22:33
  • Ah, thanks WebElaine! This helped the solution click... I had to set the has_archive (and rewrite) settings when making my custom post type. Then WP automatically uses archive.php and I don't need that particular Page I'd made! Feb 18, 2017 at 13:58

2 Answers 2

0

You can use the Body Class Filter for this to adjust the classes being added to the body tag.

add_filter('body_class','review_pages_classes');
function review_pages_classes($classes) {
    $classes[] = 'blog';
    return $classes;
}

Which classes you need to add or remove is probably a matter of trial and error, or looking at the stylesheets in more detail.

0

In this case, I will create a shortcode which you can put on any page you want. Paste the following code to the functions.php file

add_shortcode('review-list', 'show_review_list');

function show_review_list($attr){
    $args = array(
        'post_type' => 'review',
        'orderby' => 'date',
        'order' => 'DESC',
        'posts_per_page' => 10,
    );
    $the_query = new WP_Query( $args );

    ob_start();

    //...
    //Put code from index.php here to get the correct HTML structure
    //just like what you did before
    //

    return ob_get_clean();
}

To use, put [review-list] anywhere on the page/post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.