WordPress custom taxonomy templates - wordpress

I have a lot of different custom taxonomies and each one has specific template (taxonomy-customtax.php)
For now the templates are placed in the root of my theme.
Since we are able to place some template files in a subfolder (except for archive template), I would like to know if the following code is a correct way to do?
The code is in taxonomy.php in root of my theme:
<?php
$taxonomy = get_queried_object()->taxonomy;
if ($taxonomy == 'customtax')
{
get_template_part('template/customtax');
exit;
}
wp_safe_redirect(site_url('/'));
exit;
?>
Is there any problem if do that way?
Thank you

Basicaly that should work just fine. But as soon as you get my different post types or taxonomies, you would get a huge conditional script. Lets say, you have a archive page. You want to display the article teasers with different content and design for each post type or taxonomy. I would recommend you for example the following:
<?php get_template_part('templates/teaser', get_post_type()); ?>
<?php get_template_part('templates/content', get_post_type()); ?>
or for a taxonomy term (i.E. Category):
<?php $category = get_the_category(); get_template_part('templates/teaser', echo $category[0]->cat_name); ?>
<?php $category = get_the_category(); get_template_part('templates/content', echo $category[0]->cat_name); ?>
Note that this taxonomy solution only works for posts wich are assigned to a single taxonomy term. Hope you like the idea of handling it that way. I got wordpress sites up and running with 5-6 custom post types and 2-3 custom taxonomies and just one index.php as archive base.

Related

Wordpress | Design an entire category (Not category page)

I'm creating my first wordpress theme, and I've looked around on google, and the wordpress codex for an answer to my question, but I can't seem to find exactly what I'm looking for, or couldn't get it working.
What I'm trying to do, or trying to figure out, is how I can make it so a certain category has a certain design.
So if I wanted to make an index.php for any music videos in "www.domain.com/music/trash/drake-song.mp4.html"
the trash category, it'd have its own design, but songs in
"www.domain.com/music/good-music/coldplay-viva-la-vida.mp4.html"
the good-music category, I want it to look pretty much completely different. I've tried using something similar inside my header.php to this;
<?php
if( is_tag( 'good-music' ) ):
$my_classes = array( 'good-class', 'good-class-two' );
else:
$my_classes = array( 'not-good-class' );
endif;_
?>
but it seemed to simply change the category page.
"www.domain.com/categories/good-music"
Anyone know what I could be doing wrong? I know basic html/css/php/javascript, new to creating a WordPress theme, and can't seem to get this working..
Also:
Using XAMPP to host locally, using Friendly URL's, properly configured
In order to generate category-specific markup for a single post layout, you can put code like the below in your single.php file after the call of get_header().
Please note that this checks for your category based upon category slug. So if your category slug (the url version of your category) does not match the first arguments in the in_array() function call below, then you should change the argument to match the slug for your category.
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
?>
<?php
$categories = get_the_category();
$catSlugs = array();
foreach ($categories as $category){
$catSlugs[] = $category->slug;
}
if (in_array('good-music',$catSlugs)){
$post_cat = 'good-music';
} else {
$post_cat = 'not-good-music';
}
get_template_part( 'template-parts/post/content', $post_cat );
?>
<?php
endwhile; // End of the loop.
?>
It is important that this code only appear where there is a query to loop against.
Specifically, the file single.php in your theme should be the default file for displaying a single post, regardless of category. When you navigate to the url of a single post, this layout should be triggered.
As part of that triggering, a wordpress query of just that post is returned to be looped through.
Then, the code from while ( have_posts() ) : the_post(); until endwhile; will run one time, because there is a single post to be processed.
If there were more than one post, such as on a category page or on your default post listing page, then the code inside of that while loop would run as many times as there are posts in the query for that layout.
If you were to place the code in the header, it won't work because the header is prepared independently of the loop that runs on this page.
You could run a custom WP_query() in the header, but that is rarely a good way to handle site content.
In this situation it would not be appropriate, because you are customizing content of existing posts, and only differentiating based upon category.
So, just use the standard layouts files with custom layout parts.
I stripped out the divs in the loop below, because you may or may not be using bootstrap.
After this code is placed on your page, you would create files called content-good-music.php and content-not-good-music.php in YOUR_THEME_DIR/template-parts/post/ directory. The key is that you would add whatever your category slug is to the end of your
These files will contain the unique markup for these kinds of posts. You can also use a similar logic in your post listing loop to give the listed posts for each category their own unique php files.
Here's some get_template_part() documentation.
https://developer.wordpress.org/reference/functions/get_template_part/

Advice on page/slug architecture

I am looking for some sage advice on how to best create a page/slug structure for a WP site I am building. It is a portfolio site that will showcase creative work. I have currently created several custom post types for things such as the portfolio/work items and for their associated clients and have created relationships between those items using the Advanced Custom Fields Plugin. All of this works great. What I am struggling with is how to create the best URL structure.
I have the following pages already created and working well:
/work/ - index page that shows all work regardless of client
/clients/ - list of all clients
I need to create the following pages:
/clients/client/ - this page would show all work associated with a specific client. Here is where I am struggling. I need help understanding how to use the page template system to set the correct page slugs. Can I use page slugs as part of my WP query? Can I simply query based on the custom post type? What do I name the page template file for this to work?
Appreciate any advice and/or examples anyone can offer. Thank you.
WordPress offers an easy mechanism to handle pages and posts as well that are being created and rendered. It is us who has to take up the challenge to do those wonderful designs and tasks that we need. Moving on to the following topic of page and slug architecture we shall discuss in detail.
First let us look onto the following thing.
Creating Page Template for Custom Posts Type
Ex: If you are creating the post type called news the WordPress system will look for the following structures.
single-{post-type}.php
archive-{post-type}.php
single-{post_type}.php
If your custom post type were 'news', and/or query_var = "news", WordPress would look for single-news.php to display the single or permalink of the post.
archive-{post_type}.php
If your custom post type were 'news', and/or query_var = "news", WordPress would look for archive-news.php to display the archive of posts.
If these files are not available in your Theme's directory WordPress will look for archive.php and single.php, respectively. If even these files are not present it will default to index.php.
Template File will be like this.
<?php
$args = array( 'post_type' => 'news', 'posts_per_page' => 10,'post_status'=>'publish','orderby'=>'ID','order'=>'DESC' );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Hence from the above code you can display the Latest Posts from News category in the count of 10.
If you need the count to be infinite you have to change the post_per_page=10 to post_per_page=-1.
Hope so this reference and advice that i have tried to explain will be helpful for you. Still if you face any issues about my explanation comment over to my answer and i am there to help you.
I didn't go this exact route to resolve my question as I am using the Advanced Custom Relationships plug-in. My solution is somewhat similar to this: https://www.advancedcustomfields.com/resources/querying-relationship-fields/ - but your response was very helpful and got me going on the right track, thank you!

How to display wordpress post according to category in pages

I'm working on a site.The problem is i have to display the categorized posts in their respective pages. I'm using a custom theme that I'm working on. basically I'm making a site in WordPress. Links to other helpful tutorials on making will be awesome.
This is the code you need to add in your webpage. Replace 1 with the category id you want.
<?php
$posts = get_posts(array('category' => 1));
?>
You can refer this link for more detailed info.
Edit: More detailed info.
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
Now $category is you array of category. you can get the category name by
$category[$i]->cat_name So, you can search your category in array and get it's "id". and from first code you can get posts.

Wordpress - Get name of Category a post belongs to?

in Wordpress on a post how can I get the name of the category it belongs to (each post will only have one category on my website).
The version of wordpresss you're using is very much relevant for your question, but a good place to start would be the wordpress codex where you can find for example the documentation of this function.
It is also important to know whether your code will be placed within the infamous loop (which would be the case if you're editing the post template) or not (for example, if your code goes at the sidebar, header, etc).
Assuming the first case, and taking the example from the manual, you could use:
<?php
$categories = get_the_category();
// there is only one, so extract the first from the array
$category = $category[0];
?>
These can go in header.php for generating meta tags, page titles, etc.
<?php echo trim(strip_tags(category_description())); ?>
<?php echo single_cat_title(''); ?>
Also see Template Tags/single cat title « WordPress Codex

Display Posts from a category in Wordpress?

I am trying to display just post title and their links within a set category. However I am running into issues understanding the Codex. Any help or guidance would be greatly appreciated.
I use this a lot in my blogs. Helpful when you want to display featured items or such.
http://codex.wordpress.org/Template_Tags/query_posts#Category_Parameters
http://codex.wordpress.org/The_Loop#Style_Posts_From_Some_Category_Differently
You might have seen the above link. I'll explain how it works.
Posts are loaded using the loop. If you do a Query Posts just before the loop, you can choose from select category (or many categories) and also limit the number.
<?php query_posts('cat=1&showposts=5'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li><?php the_title(); ?>
<?php the_excerpt() ?>
</li>
<?php endwhile; endif; ?>
You can use the above code as many times you wish. Choose the category ID (can be found from the admin) and the number of posts you wish to show.
Comment - if you require additional help.
It seems that you are working on your templates. It basically means that you need to edit correct template and insert the right tags.
Firstly, you need to understand how the template is chosen. WP has special hierarchy for every view. Home page is usually home.php and categories are category.php or category-1.php. If any file is missing, WP simply takes next on the list. Last on the hierarchy list is index.php which is chosen if no other file is found.
[http://codex.wordpress.org/Template_Hierarchy#Category_display][1]
Secondly, look at the template tags. Displaying only title with link means you need title and permalink tag. Anything else is optional.

Resources