Custom Category Template in wordpress ? - php

I made a file as category-videos.php, since i read that either use category-[slug].php or category-[ID].php . But, i don't know the next steps. How to link it with wordpress ? How to link all posts with this template ?
Mainly my requirement is that I want to create a new template for my specific category "VIDEOS" so that all posts under this category show in new style. Basically, i want to show posts with videos in a new way as 3 posts in one row with featured images and a featured slider on top of that page showing 4 new posts of that category ?
But i am confused... Can i do this in wordpress ? Because my home page also has a featured slider. Can i create a new slider for that page template but one thing is sure. I need to work with only one category for that custom template i.e. videos.........
Any help will be appreciated. Thanks in advance ....

create a file category-[slug].php or category-[id].php - Put category's slug for which you want a different template in [slug] or its [id]. Wordpress provides this functionality by default.
See http://codex.wordpress.org/User:Lorelle/Custom_Category_Template for more information on this.
Another, not good approach is to use decision statements based on (is_category('id'))
like
<?php if (is_category('ID')) : ?>
// Code For Category with defined ID
<?php else : ?>
// Code for else
<?php endif; ?>
but again this is not a good approach to use too much if else - also affects your server as well as each time server side decision will be taken to display content for if or else

Create category-Id.php file (for example category-1.php). You can found Id number when hover on category in admin.
You can do with this category what do you want.
For example, in my blog i have with thumbnail and without.
Another way via Is_category tag
<?php if (is_category('1')) : ?>
Code For Category One
<?php else : ?>
Code for other categories
<?php endif; ?>

My custom template works just fine by adding slug or id ( category-slug.php ).. Wordpress will automatically find the right template according the template hierarchy. If you need the child of the category use the parent template too, use add_action('template_redirect','your_function').

Related

Showing taxonomies in Wordpress

I have a Wordpress site with three custom taxonomies. They're configured and working, with some posts marked with each, and want to run code related to them. As per the documentation, if the custom taxonomies are cats, dogs and trees, and I want to run php about cats, after lots of trying and going up and down over the documentation, I think I should create a template and call it "taxonomy-cats.php":
taxonomy-{taxonomy}.php: For example, if the taxonomy is named “sometax,” WordPress would look for a file named taxonomy-sometax.php
But I still don't get something: the docs say "wordpres would look for". Why would WP look for it and not other? I suppose I at least have to tell it what or when to search for it. How or where do I tell WP to look for it?
<?php /* Template Name: Taxonomy cats */
get_header(); ?>
<h1>Hello world</h1>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
As a template, I've found I can add it in the administration as an attribute to a post, but then when I enter that page if I try to get the id of the taxonomy, I get the id of the post.
Wordpress uses a loop to display posts. Wherever you are, most of time, the loop is being used.
The Loop is PHP code used by WordPress to display posts. Using The
Loop, WordPress processes each post to be displayed on the current
page, and formats it according to how it matches specified criteria
within The Loop tags. Any HTML or PHP code in the Loop will be
processed on each post.
Source # https://codex.wordpress.org/The_Loop
Wordpress is also based on a template hierarchy system. With the exception of the basic index.php template file, you can choose whether you want to implement a particular template file or not.
WordPress uses the query string to decide which template or set of
templates should be used to display the page. The query string is
information that is contained in the link to each part of your
website.
You can take a look at this image to have a visual understanding of the Wordpress templates hierarchy.
Source # https://developer.wordpress.org/themes/basics/template-hierarchy/
In your case when displaying the dogs taxonomy, Wordpress look for the following files in that order: taxonomy-pets-dogs.phpif this template doesn't exist it move on to look for pets taxonomy-pets.php, if it doesn't exist it moves on to taxonomy.php... etc
(Where pets is the name of your taxonomy and dogs is a term inside that taxonomy)
Specifying taxonomy-pets-dogs.php enables you to create a custom template for that specific term. cats wont have the same template as dogs exept if both are using taxonomy.php or taxonomy-pets.php and if no specific templates exist.
But in any case taxonomy-{taxonomy}.php or taxonomy-{taxonomy}-{term}.php will always use the loop to display posts and terms. Like any other template.
<?php if ( have_posts() ) :
while ( have_posts() ) : the_post();
echo the_title().'<br/>';
endwhile; else :
echo 'No posts found!';
endif; ?>

Use a single wordpress category.php file for every category registered

As the title suggests, I'm trying to use one category.php file for every category registered by a user in the back-end. I've always created category-[specific category].php files in the past but this feels very messy as the design usually remains the same.
Without being specific in the query I return all posts in all categories however, I want to be able to show the custom posts for only the category selected from the nav. The problem is, I can't seem to find a way to pass the category the user selected into the query in order to only return posts attributed to it.
Thanks in advance for any help.
You don't need to do anything specific to show a list of posts from a specific category. Take a look at this article on the WordPress Codex. Here is an excerpt from that page:
In the case of categories, the hierarchy is fairly simple. For
instance, suppose the slug of the Category in question is news and the
Category ID is 6. The Template Hierarchy specifies that WordPress will
use the first Template file it finds in your current Theme's directory
from the following list:
category-slug.php
category-ID.php
category.php
archive.php
index.php
So all you need to do is create category.php in your theme and then view any category and it'll use that template. You can then use is_category() to check which category is currently being used if you want to do custom things depending on the category.

Wordpress - Display Post from categories

I need to display a limited post from a specific category but with a page title , little description of page and also a read more link mention it, whats the procedure to i get this or guide me some details about the wordpress coding because i am very new in it.
I dont need to paste code in page.php i need to display this only on one specific page.
Basically i want to make a custom news column on page and i need to get post from category news but limit of five and when the new post added in category automatic display on home page i try lots of plugins but i still didn't get the exact solution all the time differs that's why i need to make a proper and permanent coding.
Creating a custom page template and then adding wp_query on it should work. After creating the template, you can create a page then set the template to the new custom page template you created. For example: create a News Page Template. In your theme, create news-template.php then add this code:
<?php
/**
* Template Name: News Page Template
*
*/
// add your markup here and wp_query
?>

how to show different post of different category on different pages in wordpress?

I am working with wordpress theme , I want to show posts on different pages according to category .
for example page name is "Blog" and post category is postBlog, can you tell me how I can use post according to category .
Thanks
What are you trying to do can be achieved with WordPress Category Template.
From the Codex:
For instance, when a viewer clicks on a link to one of the Categories on your site, he or she is taken to a page listing the Posts from that particular Category in chronological order, from newest Posts at the top to oldest at the bottom.
You can create a file in your theme, called: category.php, put a loop inside of it and then WordPress will automatically do that for you.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
And then fill your needs with the page. You can check the documentation for The Loop here in this link.
create a template category-catname.php or category-catID.php in your theme dir.
See http://codex.wordpress.org/Category_Templates
I think people get confused trying to use Pages when they are actually better off using Categories. If you want to have blog posts show up on different URLs or 'pages' then all you have to do is set up your categories and put those categories into you Menu. Then when you post make sure to select which categories you want that post to show on. You don't have to use pages at all. Pages are better for static content.
Good luck, I hope this helps.

Make a Custom template for a specific category in wordpress

I am having issue with a custom category template. I want to create a new template for my specific category "VIDEOS" so that all posts under this category show in new style. Basically, i want to show posts with videos in a new way as 3 posts in one row with featured images and a featured slider on top of that page showing 4 new posts of that category ?
But i am confused... Can i do this in wordpress ? Because my home page also has a featured slider. Can i create a new slider for that page template but one thing is sure. I need to work with only one category for that custom template i.e. videos.........
Just create category-[id].php & you will be ready to use that. Wordpress will automatically load that file for that ID of category.
You can also do same task via slug like category-files.php etc as well
Yes, certainly possible with WordPress.
Find the slug or the category id. Your slug for the category may be videos. So create a custom template named category-videos.php. This template will be used for the category archive VIDEOS.
If you know the category id, you can create template named category-<category_id>.php.
For other people landing on this page and wondering what to do when they go to their theme's folder and don't find a file called "category.php", what you need is to find the next file in the hierarchy, copy it, and rename it category-[slug or id].php (this should be archive.php, followed by index.php).
For a listing of the category hierarchy, see the official documentation: http://codex.wordpress.org/Category_Templates

Categories