previous_post_link in same taxonomy not working - wordpress

I have my cutom post taxonomy which is term_taxonomy_id (it's a reference to the ID of custom category of posts) and want to put prev/next post link but
<?php while ( have_posts() ) : the_post();
next_post_link('%link','Next post',true,'','id');
endwhile; ?>
simply doesn't work (it doesn't show any code in result page).
After removing three last arguments:
next_post_link('%link','Next post');
there is no problem with showing the link though (but it's not in the same taxonomy of course which I'm not interested in).
Wordpress version is 3.8 of course.
Do you have any idea where the problem is?

Related

Cant get a basic "If Has Tag" Statement to work - Wordpress

I'm simply placing a small snippet of code on a woocommerce product page of my Wordpress website, to display an image "IF" the product has a specific TAG in place.
The code itself seems like it would be simple enough, and ive googled the heck out of this and tried many variations, with no luck on any of them.
<?php if( has_tag( 'tagnamehere') ) : ?> <div>my content</div> <?php endif; ?>
Also tried this:
<?php if( has_tag( 'tagnamehere' ) ){ echo '<div>my content</div>'; } ?>
Thats didnt work either.
basically, i just want it to look at the TAG of the product, and if the TAG exists, simply show the image (div). Nothing seems to work.
WordPress actually uses what's called Taxonomies and Terms.
Taxonomies are basically a grouping of Posts or Custom Post Types (like 'Category' or 'Post Tag', and Terms are basically the individual grouping names 'Featured Posts', etc.
WooCommerce basically registers a Custom Post Type called product, and also a Taxonomy called product_tag. Note, this is different than the default Tags on Posts.
Effectively this means you'll need to check if the term 'tagnamehere' exists in the product_tag taxonomy, the easiest way would be with the has_term() function. This is basically like the "Custom Post Type with Custom Category (aka Taxonomy)" version of has_tag()
if( has_term( 'tagnamehere', 'product_tag' ) ){
echo '<div>my content</div>';
}
Also to address your original "two versions" of code - The curly brace or alternative-syntax if statements work identically, and are mostly up to style/preference.

WordPress custom taxonomy templates

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.

Getting a list of all ID's on the site

Sounds inane, but I think it would be handy to have an id list of the name of every element on my site. By every element I mean Posts, Pages, Comments, Users, the works.
Id, Title
That's it.
I do not know how to loop through PHP code for this. My PHP skills are weak. Is this too ridiculous to answer? Maybe, but I'd still find it handy. I've looked through every Plug-in name and description that was close to fitting this task and found nothing.
Thanks,
Mike
The Loop is used by WordPress to display each of your posts. Using The Loop, WordPress processes each of the posts to be displayed on the current page and formats them according to how they match specified criteria within The Loop tags. Any HTML or PHP code placed in the Loop will be repeated on each post. When WordPress documentation states "This tag must be within The Loop", such as for specific Template Tag or plugins, the tag will be repeated for each post.
For example, among the information The Loop displays by default: the Title (the_title()), Time (the_time()), and Categories (the_category()) for each post. Other information about each post can be displayed with the appropriate Template Tags or (for advanced users) by accessing the $post variable, which is set with the current post's information while The Loop is running.
Simple Example:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
What you need to use is the_id() function. It displays the numeric ID of the current post. This tag must be within The Loop.
Put something like this in your loop
<p>Post Number: <?php the_ID(); ?></p>
References:
http://codex.wordpress.org/Function_Reference/the_ID
http://codex.wordpress.org/The_Loop

Wordpress: Removing posts in "The Loop" using filters

After reading the Wordpress documentation, I realized you can remove posts from the index using filters inside "The Loop", e.g.:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- The following tests if the current post is in category 3. -->
<?php if (in_category('3')) continue; ?>
<!-- display normal post -->
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
What I'm wondering is if there is a filter/hook to filter posts in have_posts() without modifying the template. So far, I found options to change the results, but not remove results form the result set.
You can hook into 'pre_get_posts'.More info here: http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts. As the article describes:
This hook is called after the query variable object is created, but before the actual query is run.
Using query_posts(), you can override the query variables and exclude any posts. The link has some decent examples on how to do this.
So you want to remove it from displaying but still have it 'there' incase you decide to show it later? Not really clear on what you want to do.
As one example, in the past I've used Query Posts to keep categories off of my homepage: http://codex.wordpress.org/Template_Tags/query_posts#Exclude_Categories_From_Your_Home_Page

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