Cant get a basic "If Has Tag" Statement to work - Wordpress - 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.

Related

Combine Wordpress site search results with product search results on one page

On my Wordpress website, a site search that is limited to only posts renders a perfect results display of posts. A product search also renders a perfect display of products, although displayed differently. But if I attempt to search for both posts and products, the display is terrible. I think the products are probably forced into the posts display template. Since each of my products includes an explanation post inside the product description, those posts are repeated after every product -- mighty ugly. I don't need any product descriptions to be shown the results and prefer all products to be displayed like they are in the only product search.
This is all visible if you search for 'Grace' using the three different search inputs at this testing post: https://www.hebrewwordpics.com/dummypost/
I want to use only one search input form that will do both searches and display them together sequentially on one page.
Using WooCommerce and StoreFront child theme. Currently limiting searches with Ivory Search plugin, but the results are similar with other plugins or none at all.
You can use get_post_type() to identify which post type is being queried. Then display a specific template per post type.
get_post_type( int|WP_Post|null $post = null )
Retrieves the post type of the current post or of a given post.
Parameter
Description
$post
(int/WP_Post/null) (Optional) Post ID or post object. Default is global $post. Default value: null
You can use the following on your search page.
<?php
if ( 'post' === get_post_type() ):
// ... post template
elseif ( 'product' === get_post_type() ):
// ... product template
else:
// ... else
endif; ?>

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/

previous_post_link in same taxonomy not working

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?

Wordpress: Different look for different posts

I've looked everywhere to work out how to do this, but I'm having no luck. I read up on Post Formats, but I don't think that's what I'm looking for.
All the posts on the frontpage are the same, but say, for features or reviews you can have an entirely different look compared to the "standard" single.php post.
Examples:
Homepage //
http:// ausdroid.net (All posts look the same)
Standard single post //
http://ausdroid.net/2013/02/03/samsung-galaxy-nexus-from-vodafone-receives-android-4-1-2-update
Different single post (for a review in this case) //
http://ausdroid.net/2013/02/02/huawei-ascend-d1-quad-review
Thanks!
Sample on using category to present different Singles:
single.php
<?php
if ( in_category( $foo ) )
require( 'single-foo.php' );
else
require( 'single-default.php' );
So you'll have the above simple code in the usual single.php, put your previous single.php to single-default.php, and create your "other" Single layout in single-foo.php.
$foo can be category ID or Slug.
in_category()
EDIT
You can modify this to work with Post Formats:
In functions.php:
add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );
... then, change the if statement in my sample above to use has_post_format() instead.
has_post_format().
http://www.wpbeginner.com/wp-themes/create-custom-single-post-templates-for-specific-posts-or-sections-in-wordpress/
this explains it pretty well using " Single Post Template" plugin, if thats an option
You can try adding custom fields to the posts and check them in the single.php to show different layouts. See this video for and example of a plugin doing just that - youtube.com/watch?v=L3VXnryN9iY

Wordpress - Option to hide posts from front page

I'm using Wordpress page templates to pull posts with certain categories into their own page. My problem is that they are also showing on the home page. I know how to hide categories from the home page entirely, but I'd like a toggle so that if it's checked, it will post to home page and sub-page and if it's not checked, it will display only on the sub-page. Does that make sense?
This code is on the "Awards" template and pulls in posts containing the category slug "awards".
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args= array(
'category_name' => 'awards', // Change these category SLUGS to suit your use.
'paged' => $paged
);
query_posts($args);?>
The "awards" category is then blocked from the home page using a query_posts tag:
<?php if ( is_home() ){
query_posts( 'cat=-5' ); //-5 is the category id for 'awards'
}?>
I tried to get around this by also tagging the post with a category that isn't blocked from the home page, but it apparently doesn't care that it's tagged with something else.
So my conundrum is that I would like a simple option (hah) that allows me to pick and choose if it belongs on the home page or not, while still having them all show on this other page.
Well.. in the middle of writing this, I was inspired and sure enough I found the simple solution. 'Uncategorized' is the default category and has the id of 1. On the index page, I simply added the 'query_posts( 'cat=1' );' underneath the bit of code that blocks the other categories. This way, I can tag it for the category I want on one of the sub pages, but also for ones that I want to show on the homepage. Here is the code as it is on my index.php:
<?php if ( is_home() ){
query_posts( 'cat=-5' );
query_posts( 'cat=1' );
}?>
So if I want it on the home page AND the subpage, I'll categorize it as 'awards' AND 'Uncategorized' (or whatever you choose to be the homepage category). If I want it only on the subpage, I just choose that category. This works because the 'query_posts( 'cat=1' );' is written below the 'query_posts( 'cat=-5' );' part, so it's happening after the block which tells the server to display it after all.
Is it an option to use a Wordpress Plugin?
If so, you can use WP Hide Post, which enables a feature to hide certain posts to show on the frontpage.
Here is the link to the resource:
http://wordpress.org/plugins/wp-hide-post/

Resources