Display custom post categories and their posts - wordpress

I am trying to display the category titles and their posts from a custom post type. I can get the category to display but it currently lists all the posts within the custom post area instead of separating them in to the
<?php
$taxonomy = 'staff';
$cat_args = array(
'taxonomy' => $taxonomy,
'tax_input' =>$tax_input,
'orderby' => 'name',
'order' => 'ASC',
'child_of' => 0
);
$tax_terms = get_terms($taxonomy);
foreach ($tax_terms as $tax_term) {
echo '<div class="categorybox">';
echo '<h4> <a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></h4>';
$args = array(
'post_type' => 'staff',
"singular_label" => "Department",
'numberposts' => 5,
'taxonomy' => $taxonomy->$tax_term,
);
$posts = get_posts($args);
?>
<ul><?php
foreach($posts as $post) {
?>
<li><?php the_title(); ?></li>
<?php
} ?>
</ul><?php
echo '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>View all articles in ' . $tax_term->name.' ยป</a>';
echo '</div>';
}
?>

There no option taxonomy in get_posts(). Try this one
'tax_query' => array(
array(
'taxonomy' => 'staff',
'field' => 'slug',
'terms' => $tax_term->slug
)
)

<?php
$query = new WP_Query( array( 'post_type' => 'testimonials','posts_per_page' => 10 ) ); //testimonials custom post type
while($query->have_posts()) : $query->the_post();
?>
<ul>
<li>
Category:<?php the_category(', '); ?>
<h3><?php the_title(); ?></h3>
</li>
</ul>
<?php endwhile; ?>

Related

Limit posts to current category in shortcode

I have a custom shortcode to be used on archive pages and list the first 3 posts. But how do I limit the posts displayed to the current category without hardcoding categories?
This is working, but shows posts from all categories.
function archive_loop_shortcode() {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
);
$my_query = null;
$my_query = new WP_query($args);
if($my_query->have_posts()):
while($my_query->have_posts()) : $my_query->the_post();
$custom = get_post_custom( get_the_ID() );
echo "<div>".get_the_post_thumbnail()."</div>";
echo "<h3>".get_the_title()."</h3>";
endwhile;
wp_reset_postdata();
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
}
add_shortcode( 'archive_loop', 'archive_loop_shortcode' );
Hope this code helps,
function archive_loop_shortcode() {
$current_cat = get_queried_object();
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
'tax_query' => array(
array(
'taxonomy' => $current_cat->taxonomy,
'field' => 'term_id',
'terms' => $current_cat->term_id,
),
),
);
$my_query = null;
$my_query = new WP_query( $args );
if ( $my_query->have_posts() ):
while ( $my_query->have_posts() ) : $my_query->the_post();
$custom = get_post_custom( get_the_ID() );
echo "<div>" . get_the_post_thumbnail() . "</div>";
echo "<h3>" . get_the_title() . "</h3>";
endwhile;
wp_reset_postdata();
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
}
add_shortcode( 'archive_loop', 'archive_loop_shortcode' );

How to show custom taxonomy in tab not working when using with wp post query?

I am new Wordpress and I want to use custom post type categories as based on pagination, Below is my code in which Tab's name is working fine but the content it does not show anything so as for pagination.
<?php
$currentPage = get_query_var('paged');
echo '<ul class="nav nav-tabs" role="tablist">';
$args = array(
'taxonomy' => 'blogposts_categories',
'hide_empty'=> 1,
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) {
echo
'<li>
<a href="#'.$category->slug.'" role="tab" data-toggle="tab">
'.$category->name.'
</a>
</li>';
}
echo '</ul>';
echo '<div class="tab-content">';
foreach($categories as $category) {
echo '<div class="tab-pane" id="' . $category->slug.'">';
$the_query = new WP_Query(array(
'post_type' => 'freight_blogposts',
'posts_per_page' => 1,
'paged' => $currentPage,
'taxonomy' => 'blogposts_categories',
'category_name' => $category->slug,
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<h1>';
the_title();
echo '</h1>';
endwhile;
}
echo '</div>';
echo "<div class='page-nav-container'>" . paginate_links(array(
'total' => $posts->max_num_pages,
'prev_text' => __('<'),
'next_text' => __('>')
)) . "</div>";
?>

Looping through product of a specific category

I am trying to get all products with a specific category. It shows the data if I remove the product category code from the code else doesn't show anything. I am sure I am using the right product category.
$args = array(
'post_type' => 'product_variation',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'men'
),
),
);
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
while($post_query->have_posts() ) {
$post_query->the_post();
$tre = get_post_meta(get_the_ID(), '_xoo-wl-users', true );
$data = json_decode($tre);
foreach($data as $key => $value)
{
echo $key;
echo "<br>";
}
}
}
Use below code in $args so it will display products only of men category
<?php $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => 'men', 'orderby' => 'date' ); ?>
Try this :
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'men', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
The reference here: https://wordpress.stackexchange.com/questions/67247/how-to-display-product-specific-to-a-category-with-woocommerce-plugin

How to get categories product in archive.php in WordPress?

I would like to display categories (from taxonomy-product_category) in archive-product.php.
Get categories:
Fruit
Herbs
Salad
Vegetables
See screenshot what I mean:
When I added coding in archive-product.php:
<?php
$args = array(
'post_type' => 'product',
'taxonomy' => 'product_category',
'hierarchical' => 1,
'nopaging' => false,
'posts_per_page' => '2',
'posts_per_archive_page' => '10',
'ignore_sticky_posts' => true,
'order' => 'rand',
);
echo '<div class="container">';
echo '<div class="row">';
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<div class="col-lg-4">';
echo '<a href="'.get_the_permalink().'">';
if ( has_post_thumbnail() ) {
the_post_thumbnail(array(486,226));
}
the_title();
the_content();
echo '</a>';
echo '</div>';
}
} else {
// no posts found
echo wpautop( 'Sorry, no posts were found' );
}
echo '</div>';
echo '</div>';
// Previous/next post navigation.
previous_post_link( '%link', 'Prev post in category', true
);
next_post_link( '%link', 'Next post in category', true );
// Restore original Post Data
wp_reset_postdata();
?>
But not display categories (Fruit, Herbs, Salad, Vegetables)
Would anyone know about this?
Thanks,
Shaun.
Please try below code:
$args = array(
'taxonomy'=> 'product_category',
'order' => 'DESC',
);
$categories = get_categories($args);
print_r($categories);
You can get the list of all product categories using Below code
$orderby = 'name';
$order = 'asc';
$hide_empty = false ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
);
$product_categories = get_terms( 'product_cat', $cat_args );
if( !empty($product_categories) ){
echo '
<ul>';
foreach ($product_categories as $key => $category) {
echo '
<li>';
echo '<a href="'.get_term_link($category).'" >';
echo $category->name;
echo '</a>';
echo '</li>';
}
echo '</ul>
';
}

Add WordPress custom taxonomy terms as css class

On a custom post type archive I'm trying to add the custom taxonomy terms slug as a css class to the tag. I've managed to get it to output the page->ID but struggling to get the $term->slug to work. Feel like I'm missing something really simple. Here's the full code, thanks for any help:
<?php
$parent_pages = get_pages( array( 'parent' => 0, 'post_type'=> 'archive', 'orderby' => 'menu_order' , 'order' => 'ASC', 'sort_column' => 'menu_order' ) );
foreach ( $parent_pages as $parent_page ) {
echo '<h1 class="page-heading" id="';
echo $parent_page->post_name;
echo '">';
echo $parent_page->post_title;
echo '</h1>';
echo '<div class="wrapper grid4">';
$all_pages = get_pages(array( 'post_type'=> 'archive', 'orderby' => 'menu_order' , 'order' => 'ASC', 'sort_column' => 'menu_order' ) );
$child_pages = get_page_children($parent_page->ID, $all_pages );
foreach ( $child_pages as $child_page ) {
echo '<article class="post col ' . $child_page->ID, $term->slug .'">';
echo '<a class="fancybox" data-fancybox-type="iframe" href="http://www.iofpi.co.uk/civicworks.net/wp-content/plugins/pdfjs-viewer-shortcode/web/viewer.php?file=http://www.iofpi.co.uk/civicworks.net/wp-content/uploads/2014/05/Citizen_Manchester.pdf" title="' . the_title_attribute('echo=0') . '" >';
echo get_the_post_thumbnail( $child_page->ID, 'medium');
echo '</a>';
echo '<h1>';
echo $child_page->post_title;
echo '</h1>';
echo '</article>';
}
echo '</div>';
}
?>
Use this function get_the_terms( $id, $taxonomy ) just pass it the post id in your case '$parent_page->ID' and the Name of taxonomy to retrieve terms from. For example: 'category', 'post_tag', 'taxonomy slug'
It will return an object and you can then access the slug with, say $result->slug.

Resources