Wordpress Custom Post category archive - wordpress

I have a custom post type set up, with a number of categories and sub categories.
What I am trying to do is create a page that shows all the posts in a specific category, with a menu that lists all the category sub categories so the posts can then be filtered.
I have tried copying the archive template and renaming it taxonomy-(my-custom-taxonomy).php which then if I go to the slug shows certain posts, and using
<?php wp_list_categories(); ?> but I just want a list of all the sub-categories of a specific category, and to filter those posts. I am struggling to show these and use one template for all categories and children.

You can use
$term_id = get_queried_object()->term_id;
and
$tax= get_query_var( 'taxonomy' )
to return the details of the current term and taxonomy being viewed in your taxonomy.php page.
You can then use that info with get_term_children to get the child terms of the current term being displayed. For examples, see the link provided
EDIT
Your code should look like this
<?php
$term_id = get_queried_object()->term_id;
$taxonomy_name = get_query_var( 'taxonomy' );
$termchildren = get_term_children( $term_id, $taxonomy_name );
foreach ( $termchildren as $child ) {
echo '<ul>';
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
$args = array(
'tax_query' => array(
array(
'taxonomy' => $taxonomy_name,
'field' => 'slug',
'terms' => $term->slug,
),
),
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<p>' . get_the_title() . '</p>';
}
wp_reset_postdata();
echo '</ul>';
}
?>

Related

WordPress Nested Loops

I've got some code (see below) from an online tutorial that displays an alphabetical list of Category names and then,
underneath each category, a list of post titles for that category.
It works, but I want the post titles to also be displayed alphabetically. At present it's only category names that are alphabetical - see image:
I've done some research online and I think I may need to set up a 'nested loop' - but I have no idea how to edit my code to do this.
Hoping someone can show me how to edit code to get both category names AND post titles to display alphabetically.
This is the code I'm using:
// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
foreach ( $categories as $category ) {
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'orderby' => 'term_order',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><?php the_title(); ?></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
} // End foreach
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
foreach ( $categories as $category ) {
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><?php the_title(); ?></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
} // End foreach
We can add the orderby argument with title.

Taxonomy list of specific post type

How to get list of custom taxonomy using custom post type name.
let say there is one post type called "products" and in that there are list of categories(ie. shirt, tshirt, jeans etc.)
So I want that categories list using post type name "products".
you can get post list by taxonomy in taxonomy-{post_type}.php In this file by default gt list of post for specified taxonomy.
REF : Custom Post Type taxonomy page still showing all posts
OR in Custom templete,
$custom_terms = get_terms('custom_taxonomy');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().'<br>';
endwhile;
}
}
You could use something like this:
$terms = get_terms( 'products' );
$count = count( $terms );
if ( $count > 0 ) {
echo '<h3>Total products: '. $count . '</h3>';
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>'.$term->name.'</li>';
}
echo '</ul>';
}
get_term_link() will give you a link to your term as well.

Display Wordpress Parent Category with siblings and current category posts

Here's an example of what I'm trying to do:
This is for a sidebar navigation. When the visitor is on the subcategory I need the subcategory to display the current posts under that category and also the sibling subcategories. This structure will also have to work at the post level as well.
When the visitor is at the category level - I have it set up so that the user is only able to see the subcategories under that category - which is what I need exactly. I'm just trying to sort out the subcategory template (which is its own template - separate from the category template).
If there’s a plugin that addresses this issue quickly, I’m all for it. Right now I’m just trying to use wp_list_categories with no luck. I appreciate all your help! Thanks!
<?php
$taxonomy_name = 'products-category';
$args = array(
'type' => 'products',
'orderby' => 'name',
'order' => 'ASC',
'taxonomy' => $taxonomy_name,
'parent' => '0',
'hide_empty' => 0
);
$categories = get_categories( $args );
foreach($categories as $category){
echo $category->name;
echo $term_id = $category->term_id.'<br>';
$termchildren = get_term_children( $term_id, $taxonomy_name );
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
// Note : Here is your Post loop using term id
}
echo '</ul>';
}
?>
Good Luck

How do I display a "category products drop down in a wordpress page" in Woocommerce 2.5.2

I would like to display a drop down menu for products in a category.
<select>
<option value="CODE HERE">Volvo</option>
</select>
So according to Wordpress coding..
<?php
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Okay so I investigated further and I am looking to do a single page template according to https://developer.wordpress.org I am using a child theme for Storefront which is called NOVA WP.
To make this "single page template" I copied page.php and renamed it to page-buildit.php
This is Mypage in which I actually editing the code. I did copy the code but it turns out blank
found this: WooCommerce: Create a shortcode to display product categories
but my undestanding is we cant do this anymore with the new wordpress version.
<?php
$args = array(
'order' => 'ASC',
'hide_empty' => $hide_empty,
'include' => $ids,
'posts_per_page' =>'-1'
);
$product_categories = get_terms( 'product_cat', $args );
echo "<select>";
foreach( $product_categories as $category ){
echo "<option value = '" . esc_attr( $category->slug ) . "'>" . esc_html( $category->name ) . "</option>";
}
echo "</select>";
?>
Check this out. This is the way to get product categories.!
You can also use the function wp_dropdown_categories to make your code simpler.
To get a dropdown of categories of products you can write like this.
$args = array('hide_empty'=> 0,
'taxonomy'=> 'product_cat',
'hierarchical'=>1);
wp_dropdown_categories($args);
Or if you want to hold the output in a variable you can use the argument 'echo'=>0 and then echo the variable to get the same output.
$args = array('hide_empty'=> 0,
'taxonomy'=> 'product_cat',
'hierarchical'=>1,
'echo'=>0);
$cats = wp_dropdown_categories($args);
echo $cats;
Okay so here is how I solved it, with the help of Hemnath mouli, I already gave you the credit for the answer but I wanted to publish the products inside a category in a dropbox.
$args = array(
'posts_per_page' => -1,
'product_cat' => 'motherboard',
'post_type' => 'product',
'orderby' => 'title',
);
$products = new WP_Query( $args );
echo "<select>";
foreach ( $products as $product ) {
$products->the_post();
?>
<option value="<?php the_permalink(); ?>"> <?php the_title(); ?>
<?php
}
echo "</select>";
?>
Now I will need to show the image of this product after selecting it.

How to display popular post by view in current category?

In case: If I want to display a popular post in a category post. So, when I open "XXX" or "YYY" category, will display a popular post first from "XXX" or "YYY" category.
The question title is a bit confusing. there is a way of getting the "popular" posts by comment count but that "by view" in the title of your question suggest you are looking for a different way to go about it?
.
if you want to check post popularity by post views...
First you need to attach a "views count" to each post.
a complete function here: catWhoCodes
Now that you got a way to check which posts are popular
you need to create a list of post related to the current
category but filtered by the post count... here is an easy
way to go about it.
<?php
$category = get_category( get_query_var( 'cat' ) );
$curCatId = $category->cat_ID;
$args = array(
'numberposts' => 10,
'cat' => $curCatId,
'meta_key' => 'views',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
$popPosts = get_posts( $args );
echo '<ul>';
foreach ( $popPosts as $popPost ) {
setup_postdata( $popPost );
echo '<li>'.get_the_title().'</li>';
}
echo '</ul>';
wp_reset_postdata();
?>
.
To get popular posts by comments count
<?php
$category = get_category( get_query_var( 'cat' ) );
$curCatId = $category->cat_ID;
$args = array(
'numberposts' => 10,
'cat' => $curCatId,
'orderby' => 'comment_count'
);
$popPosts = get_posts( $args );
echo '<ul>';
foreach ( $popPosts as $popPost ) {
setup_postdata( $popPost );
echo '<li>'.get_the_title().'</li>';
}
echo '</ul>';
wp_reset_postdata();
?>
.
Related:
How to list most popular post
.
Best of luck,
Sagive.

Resources