Display taxonomy terms for current post only (Drupal) - php

I have many snippets to show all taxonomy terms. However, I want to be able to list all taxonomy terms for the current post that you're readying (with links behind them to see more posts tagged with that same term).
Any ideas?

Most drupal stock templates show the nodes terms in the node display by default, if you're not seeing them perhaps your template doesn't expose them in your node.tpl.php like so:
<?php if ($taxonomy): ?>
<div class="terms"><?php print $terms ?></div>
<?php endif;?>
OR...
You may be using views with field display/row style and need to add the taxonomy terms as a field

Related

Wordpress - get tags from specific category

I want to get wordpress tags from specific category. For example, I have a subpage with posts from a specific category and I would like to display the tags that are available on that page. Is it possible?
I think this code might be useful:
<?php
query_posts('category_name=Anime cartoon');
if (have_posts()) : while (have_posts()) : the_post();
if( get_the_tag_list() ){
echo get_the_tag_list('</li><li>');
}
endwhile; endif;
wp_reset_query();
?>
Only that this code shows all tags from each post separately, and I would like them not to be repeated
Display tags avaiable at specific page
Yes, what you are asking is possible!
However, the pages themselves aren't connected to anything in terms of tags. Like the comment above states, the tags are taxonomies for post only, whether that is a custom post type or not.
If you are wanting tags from a specific category that is on a specific page, one way to do it is to customize your permalinks so that the post category page urls are something like "https://yourwebsite.com/post-category/category/". From there you can get the current slug(the category), then loop through the posts within that category and get the tags that are associated.

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; ?>

wordpress next_post_link() only from current child category

I've created a custom loop that only displays one post at a time, the navigation between posts is done using next_post_link() and previous_post_link().
The problem is that my post hierarchy includes a parent category which is common to many posts, and I want to navigate only to posts in that child category.
The links are enabling navigations between all the posts from the parent category as well.
I've done some research and I can't find a fix for this, any idea?
Code:
<div class="setas-next">
<?php previous_post_link('%link', '»', TRUE); ?>
</div>
<div class="setas-prev">
<?php next_post_link('%link', '«', TRUE); ?>
</div>
I'm creating this navigation in both loop.php and loop-single.php.
Thanks,
Ricardo
It's been a while since I was working in Wordpress, but I have found that for more complicated pagination needs like this you may be best served working with a WP_Query ( http://codex.wordpress.org/Class_Reference/WP_Query ) to use the exact ids of the Child category you want and then use the post IDs to find the next and previous to the one currently being used.
Mind your edge cases with this, your most recent post in particular is not going to have one more recent ... at least not right away ;)

Custom Category Template in wordpress ?

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').

How can I get Wordpress archives by author username?

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.

Categories