Category page in wordpress - php

I am developing a custom wordpress theme ( first time) , I have created below files and they work fine by following this tutorial.
index.php
header.php
footer.php
single.php
sidebar.php
Now issue is when I click on a category name at home page it show me 404 error, I tried to create category.php but still same issue. here is my index.php which I used for category.php
<?php get_header(); ?>
// other html stuff and loop
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php endwhile;?>
<?php endif; ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
I have two question:
Is it really require to create category.php or can we use index.php for same purpose to show posts from category or tags?
If it's require to create category.php, Do I also need to create tags.php and how will I get posts for those?

No, it is not required to have category.php file in a WordPress theme. You can see following link for finding out template hierarchy in WordPress works.
https://developer.wordpress.org/themes/basics/template-hierarchy/#category
You can see that, if there is no category.php file in theme, it ultimately falls back to index.php.

Related

WordPress not using category.php file for custom theme

I'm coding a WordPress theme on a staging site and I'm having an issue with having all of my posts display. I've set the static page for the blog to "News" in the setting > Reading option but when I go to the news page, the page is displaying using the index.php file instead of the category.php file that I have created. Template order should have index last if it can't find category or archive is my understanding.
I'm using the same coding on another site and it works correctly. I've put this new theme on that existing site and it works as it should. They are both on the same hosting company. I started a brand new WordPress instance and it's not working on that new instance either.
My template for page.php is working fine instead of defaulting to index.php
Also tried updating permalinks, checked file permissions, checked htaccess file for rewrite conditions.
So what am I missing?
category.php code
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'template-parts/content-posts', get_post_format() ); ?>
<?php endwhile; else : ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php endif; ?>
<?php echo paginate_links(); ?>

Need Help Hiding Sidebar on Custom Wordpress Php file

I am using the underscores theme to build my wordpress site. It is very barebones (which I like), but they didn't seem to include a page.php and post.php file, instead they just have page. This means if I have a sidebar, it shows on all pages and posts. The code to show the sidebar is
<?php if ( ! is_active_sidebar( 'sidebar-1' )) {
return;
}
?>
I've tried a number of if statements to exclude pages but I can't seem to get it right. How would I have it so if the person is on a page it doesn't return the sidebar?
Thanks!
Edit the page.php and REMOVE the sidebar:
<?php dynamic_sidebar( 'sidebar-1' ); ?>
or the get_sidebar() function.
get_sidebar();

Custom WordPress template loads only header & footer

I have a weird problem. I'm trying to make custom template for WordPress.
index.php works perfectly fine - everything what should load, loads with no problems.
index.php:
<?php get_header(); ?>
<?php if ( is_home() &&function_exists('get_template_part')) get_template_part( 'content', get_post_format() );?>
<?php get_footer(); ?>
But when I create a new page, like promotions.php and put custom code, It does not show up on the page. I can see only header & footer section. It looks like I didn't put any code at all.
You can make custom page templates like so
<?php
/**
* Template name: custom-name
*/
get_header();
if (is_home()) {
get_template_part('content', get_post_format());
}
get_footer(); ?>
And then inside WordPress you can edit the page. On the right side below publish you can see default template. Choose your template and hit save.
Edit:
See the image below for further instructions.
Use flush_rewrite_rules() function one time in functions.php.
All should works after that.

Clone an entire page to custom index.php

I am building a custom index.php file in my child theme overriding the default theme index.php file which loads first when the theme is first installed.
Is there a way to clone an entire page to index.php file by page name or id or slug? I will build a page by adding a new page from dashboard and add elements there. Now, when I build the page, I also want the index.php file look the same. Hope I make sense.
I have created a custom template as the index.php file. But not sure about the cloning process. Any help will be appreciative.
thanks
You can get the code from page.php of your parent theme inside your index.php - in this file you'll have all that is needed to display a page.
For example in WP default theme, this code from page.php will display the current page using content-page.php template :
while ( have_posts() ) : the_post();
get_template_part( 'content', 'page' );
endif;
endwhile;
Then, in WP settings, define your page as home page in Settings > Reading - it will tell wordpress to use that page for your index.php template.
I got it resolved this way.
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$post_format = et_pb_post_format(); ?>
<?php
$page = get_page_by_title('Default Homepage');
$content = apply_filters('the_content', $page->post_content);
echo $content;
?>

Show posts in theme development

I am new in Wordpress. I am developing my own theme. I have included index.php, functions.php, header.php, footer.php, page.php, style.css etc.. Now my home page is working perfectly. When I am going to display category menu its showing only header and footer. Not inside the content. When displaying post also its showing Header and footer.
What are the steps included for displaying posts in theme development or is there any function I want to include in function.php ?
You need to use "the loop" to display the content from the page/post.
<?php get_header(); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<article>
<header><h1><?php the_title();?></h1></header>
<?php the_content(); ?>
</article>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>
Copy this code into a new file called single.php in your theme folder.
Best way is to copy the existing theme and change the theme name ( make changes in css , images , header , footer design ) and use that , it will work correctly.
If you are working with child theme.
than all pages display under default template. and default template is page.php.
for category there is a category.php or archive.php file in which you have to change code for more functions.
And for display post you have to change single.php
you can create template for additional functionality.
that's it.
You might be missing content.php, category.php,content-single.php and many more files which in result not displaying your content part and only displaying header and footer.
Best way is download fresh word press from :
https://wordpress.org/download/
It will include all supporting files for load website.copy one of theme from fresh themes folder and replace it with your theme and customise in it.

Categories