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; ?>
Related
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.
i was wondering if you need or can make post templates for search results page only on wordpress. I'm developing my own child theme using underscores.me framework on localhost and have come across an issue which has lead me to ask this question. T
he issue itself is that the post content for post's (for both standard and custom post types) gets changed on the search results page to the url of the post permalink -
e.g. if i search for post titled movie3 it will return a result on the search results page, but the post movie3 will have the post body removed, including the excerpt and read more etc and instead have the title and date followed by localhost/?movies=movie3 instead of the excerpt or post body (I'm using optional excerpts), followed by tags etc.
This is the first time I'm developing with wordpress and was also wondering if a plug in like relevanssi would alleviate my issues, especially since I've read that wordpress search is supposed to be terrible?
If you are building your own theme I would not suggest using a plugin for this. The underscores theme should have an search.php page included in the wp-content/themes/yourtheme directory. This is the default template for displaying search results.
The default search.php template for underscore should have "the loop" included:
<?php while ( have_posts() ) : the_post(); ?>
You can call WordPress functions such as the_excerpt(); inside "the loop" to get the excerpt of the returned post. See http://codex.wordpress.org/Function_Reference/the_excerpt for more information on this.
I am developing a one page WordPress theme, there is no need to display any posts, it's just informative page, I was thinking to use static content, but it turned out that I'll need to use dynamic one in order for things like search to work. In "pages" section of WordPres I created a new page which contains all the content I need, I was trying to figure out a loop to include contents of that page, but failed. After doing a research I was only able to find loops used to display content of posts. So is there same loop, but to display contents of page? Also How would I than include those contents in a page e.g. <?php something();?>
Just specify the post_type inside of the query_posts() parameter. For example, if you wanted to output the content of each page, you could do this:
query_posts(array('post_type' => 'page'));
while(have_posts())
{
echo the_content();
}
For reference, you might want to check out the API Reference Docs for query_posts().
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').
I need to get archives of one author and exlude all others, Wordpress wp get archives() doesn't have that kind of filter. How can I fitler all other others except one author from archives?
http://codex.wordpress.org/Template_Tags/wp_get_archives
<?php wp_get_archives('type=monthly'); ?>
Just off the top of my head:
get_posts(array('author_name' => 'the_authors_name'))
You don't get anything preformatted like from wp_get_archives, just the list of posts, but you get the posts by author. If you need to do more fancy things or want to run a WP Loop you can do a custom WP_Query object as well.