Wordpress | Design an entire category (Not category page) - wordpress

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/

Related

Wordpress - How to skip the search result page and directly jump to the first result

I am creating a shipment tracking site using wordpress.
Is there a way to jump to the first result without seeing the result page?
If you have a search.php or searchform.php file in your theme, you can just remove the while() loop so that WP only displays the first result.
if ( have_posts() ) :
the_post();
// Include your template for displaying content here
else :
// Display no post found notice
endif;
Note that this will override ALL search form results, so if you have other search forms in different places that need to return multiple results, then you will need to add some logic to handle both cases.

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.

How to change which page wordpress get_header() loads

I am trying to load a Wordpress page in another php file. Since I have integrated CodeIgniter with Wordpress.
The following works without problems and loads the wordpress site.
get_header();
get_footer();
But how do I change which page is loaded, instead of the homepage?
I only want to call the get_header() and get_footer wp functions but find a way to select the page.
I have thus far tried altering the $wp_query global object but haven't had any success.
Thanks,
The following worked:
$page_to_load = new WP_Query(array(
'page_id' => $page_id
));
global $wp_query;
$wp_query = $page_to_load;
wp();
get_header();
get_footer();
This sets the page to load in the $wp_query object, and then you have to call wp() in order to reload the page and have it route to the page.
So you want to display the header and footer only on some pages. There are 2 functions, which should solve the problem.
First one:
is_front_page()
This one just tests, whether the displayed page is your front page. You just need to use it like this:
<?php
echo is_front_page() ? "I'm a front page." : "This isn't a front page"
?>
Second one: is_page()
There are multiple ways to use this function. First one (without any parameter) just tests, whether it is page. Second one (with an ID) only returns true, if the requested page has got the specific ID. Third one (with slug or post title) is the same like the second only with those parameters.

custom taxonomy terms page not found Wordpress

I’m getting a Page Not Found message when clicking on the link to display the terms in a taxonomy
Scenario:
I have a custom post type called "glossary"
Attached is a custom taxonomy called "section" with the rewrite set to “library/glossary-start-here”
I created a page that lists the terms in the taxonomy. This is a Wordpress page with the slug of “glossary-sections” - the template for the page is set to “taxonomy-section.php”. The page is a child of “library” and so the permalink for the page is “example.com/library/glossary-starts-here/glossary-sections/"
The template “taxonomy-section.php” has the following code:
<?php // Begin header section.
$argterms=array(
'include' =>array(
117,118,115)
);
$terms = get_terms('section', $argterms);
echo '<ul>';
foreach ($terms as $term) {
echo '<h3>' . $term->name . '</h3>';
}
echo '</ul>'; ?>
<div>
<?php
When clicking on the link to the page “glossary-sections”, which is used on a number of other pages, I get a page not found message.
I know the template does its job because on some occasions as I've poked around troubleshooting I've had it working. However, I can't find a consistent condition that causes the page to work. I suspect that the problem has something to do with the rewrite but my tests have been inconclusive.
I’d appreciate any suggestions on why this isn’t working and how to correct it.
I’m particularly puzzled by the fact that I can specify a page like “glossary-section” and yet have it not be found.
Thanks.
As I understand it, WordPress has a hierarchy / parse order (so to speak) as to what it looks for when it gets a request. Technically, a taxonomy is just a tag.
I'm not sure if this will help:
http://justintadlock.com/archives/2009/06/04/using-custom-taxonomies-to-create-a-movie-database
But J.Tadlock is always a solid place to start.
The source of the problem was that I had included a rewrite for the taxonomy, while at the same time using pages with assigned templates.
Once the rewrite was removed it appears that everything worked correctly.

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

Resources