0

Puzzled to see posts in category are showing on local XAMPP server, however they do not show on the live server. Here is the code to display the category posts.

It is a really simple one pager with links to each of the sections. front-page.php holds the template-parts to each of the sections. index.php decides if there is a static page or a category page and serves the corresponding template. In the folder template-parts I have category-photos.php that displays the photos posts.

Like I said, local version runs just fine. On the front-page.php all the posts from all the sections display fine. In the photos section the posts also show, on the front-page.php but also on the individual category page, meaning example.com/photos does show the photos posts locally just fine.

I also comment each of the php files with <!-- category-photos.php --> at the top so that I can see in the source if the file is loaded. Locally it shows up in the source, on the live server the category-photos.php is not even included in the source on the category page, but indeed on the front page.

I use WordPress Duplicator to backup and copy the WordPress installation, the theme files and the database exactly as it is to the live host. The photos posts also show on the front-page.php of the live page, however not on the category page, example.com/photos. Must say I tried with theme twentyseventeen and there the category photos posts display fine of course. What am I doing wrong please?

Have no idea where to start looking for errors. Had a chat with support of the hosting company and they said I might have forgotten to include the category in the menu, but I did include it, .. and they have no clue either apart from that. If it helps I will try and make a public repo of the theme as well as upload to a live dummy server.

index.php

<?php
get_header();

global $wp_query;
$pagename = $wp_query->queried_object->post_name;

// checking if it is a static page or category page
// if the $pagename variable is empty get the category
if(empty($pagename)):
  $current_cat = single_cat_title("", false);
  // get the template for photos, category-photos.php
  get_template_part('template-parts/category', $current_cat);
else:
// else it is a static page
$args = array(
  'pagename' => $pagename
);
  $query = new WP_Query($args);
    if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
      // get the template with corresponding page
      get_template_part( 'template-parts/page', $pagename );
      endwhile;
    endif;
  wp_reset_postdata();
endif;
get_footer();
?>

front-page.php

<?php
get_header();
  // the the template for the photos category, so category-photos.php
  get_template_part( 'template-parts/category', 'photos');  
get_footer();
?>

category-photos.php

<?php
$args = array(
    'category_name' => 'photos',
    'post_type' => 'post'
);
// loop over the category photos posts
$query = new WP_Query($args);

echo '<div class="photos-wrap">';
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
  the_title();
  the_content();     
endwhile;
endif;
wp_reset_postdata();
?>
</div>
  • Where does it fail? Does category-photos.php never get included? Does the query not return any results? I assume the database is identical on live & dev? – janh Oct 22 '17 at 19:24
  • Yes the db is identical, category-photos.php is not included on the live server on the category page, example.com/photos but indeed on the front page, where all the photos posts appear fine. So this means the photos posts are there in the db but for some reason are not showing on the category page. – lowtechsun Oct 22 '17 at 19:42
  • Can you add the code for the categories page where it should get included? – janh Oct 22 '17 at 20:13
  • It is there in index.php starting with if(empty($pagename)): then get the current category and then get_template_part('template-parts/category', $current_cat);, this pulls in the file category-photos.php and there it loops through the photos posts with a simple WP_Query using $args for photos and post as post_type. This is what is so puzzling to me, I think I am including the template files right and calling the loops OK. Locally the photos posts show on the front page and photos category page fine. So for me this means I am including the category-photos template fine. – lowtechsun Oct 22 '17 at 20:33
  • Are you sure there is no other template that gets preferred over index.php, say archive-photos.php, archive.php etc pp? Does it get into the block following if(empty($pagename)):? – janh Oct 22 '17 at 21:08
0

After having a really good chat that taught me a lot with janh2 we have come up with this solution that got rid of the issue. Read trough the chat to learn from my mistake(s).

Instead of going about it the way I did use this approach, much less code and it will work on the live server also.

Since WP automatically looks for a category-$slug.php file if you are on an archive (category) page create a file named category.php in the theme root folder. In that category.php get the current category.

Since I have my category-photos.php in the template-parts folder I get the current category of the archive page and then use that to get the template for it from the template-parts folder.

category.php

$current_category = sanitize_post( $GLOBALS['wp_the_query']->get_queried_object() );
$category_slug = $current_category->slug;  
get_template_part('template-parts/category', $category_slug);

category-photos.php

$args = array(
    'category_name' => 'photos',
    'post_type' => 'post'
);
$query = new WP_Query($args);
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
      the_title();
      the_content();
    endwhile;
endif;
wp_reset_postdata();

Since static and other pages use index.php if there are no other template files I can write this code below in there. I understand that once there is a category-$slug.php or page-$slug.php WordPress will automatically use that. Again since I have my page templates in folder template-parts I only need this in index.php and be done with it.

index.php

 get_header();
 $current_page = sanitize_post( $GLOBALS['wp_the_query']->get_queried_object() );
 $pagename = $current_page->post_name;
   get_template_part( 'template-parts/page', $pagename );
 get_footer();

For reference, I use this answer for getting the queried object with the correct data.

Your Answer

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

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