Make the home page template my archive/category page template without code duplication
Exploring WordPress and bumped into this issue: In my theme root page-templates/home.php
which is a common page template used in multiple pages. Now I want to include the same template (with some minor query parameters changed) in my archive/category page. I know I can just copy and paste the whole home.php template to a category.php. But It feels like there is a better way to do it.
[my home page showing the latest article at the top, 3 popular articles (by view count) in right side and at the bottom all the recent articles]
So my question is how can use the same template as the home and other pages in the category pages without code duplication??
The only code difference between home and category template is at the top of the category.php
$archive_category = get_queried_object();
$popular_posts = new WP_Query(
array(
'cat' = $archive_category-cat_ID, /* home page doesn't have this parameter (only difference) */
'meta_key' = 'wpb_post_views_count',
'posts_per_page' = _POPULAR_POST_LIMIT,
'orderby' = array(
'meta_value_num' = 'DESC',
'date' = 'DESC'
)
)
);
Topic archive-template categories customization Wordpress
Category Web