I've a custom loop which should display 3 projects per category in a random order.
The problem is, that the loop always shows all projects in that category.
I've tested the the code on a single page and it works how it should and only showed 3 projects.
So I guess it has something to do with the archive page?!
Here's ist the loop itself:
<?php
$args_thema = array (
'post_type' => array( 'project' ),
'orderby' => 'rand',
'posts_per_page' => 3,
'paged' => $paged,
//'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'project-category',
'field' => 'slug',
'terms' => $category->slug,
),
),
);
$query_thema = new WP_Query( $args_thema ); if ( $query_thema->have_posts() ) : ?>
<ul class="">
<?php $i = 0; while ( $query_thema->have_posts() ) : $query_thema->the_post(); $i++; // echo $i; ?>
<li class="">
<a class="url uid" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<?php endif; wp_reset_postdata(); ?>
And here's the full code (I guess not that relevant):
<?php
$args = array('hide_empty' => '0', 'taxonomy' => 'project-category', 'orderby' => 'name', 'order' => 'ASC', 'parent' => 0 );
$categories = get_categories($args);
foreach($categories as $category) {
?>
<div class="row">
<div class="col-lg-4">
<?php
$term_child = get_term_by('slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args_child = array('hide_empty' => '0', 'taxonomy' => 'project-category', 'orderby' => 'name', 'order' => 'ASC', 'parent' => $category->term_id );
?>
<h4><?php echo $category->name; ?></h4>
</div>
<div class="col-lg-8">
<?php
$args_thema = array (
'post_type' => array( 'project' ),
'orderby' => 'rand',
'posts_per_page' => 3,
'paged' => $paged,
//'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'project-category',
'field' => 'slug',
'terms' => $category->slug,
),
),
);
$query_thema = new WP_Query( $args_thema ); if ( $query_thema->have_posts() ) : ?>
<ul class="">
<?php $i = 0; while ( $query_thema->have_posts() ) : $query_thema->the_post(); $i++; // echo $i; ?>
<li class="">
<a class="url uid" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<?php endif; wp_reset_postdata(); ?>
</div>
</div>
<?php } ?>
Sorry, it was an extra function which sets the archive posts to 99.
Istead of is_post_type_archive I used is_archive
Related
I have made this code and the variable $link is not returning the product url, instead is returning home url. Please some Help! What I want is to get the product link selecting it by category. The display is like a menu by category but with the link going directly to one product I selected by category.
<ul>
<?php
$categories= get_terms( 'product_cat', array( 'parent' => 15, 'hide_empty' => false ) );
foreach($categories as $category): ?>
<li>
<div class="grid-subheader">
<?php
$metaArray = get_option('taxonomy_' . $category->term_id);
$productCatMetaTitle = $metaArray['wh_meta_title'];
$type = get_term_by( 'id', $category->parent, 'product_cat' );
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $category->term_id,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
$products = new WP_Query($args);
foreach ($products as $prod){
$link = get_the_permalink($prod->ID);
}
?>
<h3><?php echo $productCatMetaTitle; ?></h3>
<p><?php echo $category->description; ?></p>
<a href="<?php echo $link; ?>">
<div class="button-menu">
<?php //echo $type->name; ?> <?php echo $category->name; ?>
</div>
</a>
</div>
</li>
<?php endforeach; ?>
</ul>
Try the following revisited code:
<ul>
<?php
$taxonomy = 'product_cat';
$category_terms = get_terms( $taxonomy, array( 'parent' => 15, 'hide_empty' => false ) );
foreach( $category_terms as $term ) { ?>
<li>
<div class="grid-subheader">
<?php
$meta = get_option('taxonomy_' . $term->term_id);
$title = isset($meta['wh_meta_title']) ? $meta['wh_meta_title'] : '';
$type = get_term_by( 'id', $term->parent, $taxonomy );
$query = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => 1,
'fields' => 'ids',
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => array( $term->term_id ),
) ),
) );
$product_id = reset($query);
?>
<h3><?php echo $title; ?></h3>
<?php if ( ! empty($term->description) ) ?>
<p><?php echo $term->description; ?></p>
<a href="<?php echo get_permalink($product_id); ?>">
<div class="button-menu"><?php echo $term->name; ?></div>
</a>
</div>
</li>
<?php
}
?>
</ul>
Pagination doesn't work on a single page of WP.
In the code below, everything looks great except the pagination shows the same contents on the next page.
I tried every possible solution I found on the internet, but none of them worked. I'm relatively new to WP and php, so if you could pinpoint the code that might be wrong, that'd be really helpful.
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged'
) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'book',
'offset' => 1,
'tax_query' => array(
array(
'taxonomy' => 'news_cat',
'field' => 'slug',
'terms' => array( 'disney' )
),
),
);?>
<?php $the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts()) ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo ('/test/'.get_the_ID()); ?> "ontouchstart="" >
<li>
<div>
<figure><img src="<?php the_field('thumb'); ?>" alt="">
</figure>
</div>
<span><em><?php the_field('category'); ?></em><?php the_field('date'); ?></span>
<p><?php the_title(); ?></p>
</li>
</a>
<?php endwhile; ?>
<?php $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
$args = array (
'prev_text' => '',
'next_text' => '',
'show_all' => false,
'mid_size' => 1,
'type' => 'list'
);
the_posts_pagination($args);?>
I expect the pagination to work correctly instead of showing the same contents on all pages.
As you see, I want to make the pagination work only on the posts with "disney" slug.
You can replace the code instead of your old code
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged'
) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'book',
'offset' => 1
),
);?>
<?php $the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts()) ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo ('/test/'.get_the_ID()); ?> "ontouchstart="" >
<li>
<div>
<figure><img src="<?php the_field('thumb'); ?>" alt="">
</figure>
</div>
<span><em><?php the_field('category'); ?></em><?php the_field('date'); ?></span>
<p><?php the_title(); ?></p>
</li>
</a>
<?php endwhile; ?>
<?php $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
$args = array (
'prev_text' => '',
'next_text' => '',
'show_all' => false,
'mid_size' => 1,
'type' => 'list'
);
the_posts_pagination($args);?>
Now, you can check it that pagination is working or not.
i want to get archives of multipal custom post types can u help me guys here is my code which is not working
<?php $args = array(
'type' => 'yearly',
'limit' => '',
'format' => 'custom ',
'before' => '',
'after' => '',
'show_post_count' => false,
'echo' => 1,
'order' => 'DESC',
'post_type' => array('news','update')
);
wp_get_archives( $args );
Try This:
<?php
$args = array(
'post_type' => array('news','update'),
'post_status' => 'archive',
'type' => 'monthly',
'echo' => 0,
'order' => 'DESC'
);
$query = new WP_Query( $args );
// Check that we have query results.
if ( $query->have_posts() ) {
// Start looping over the query results.
while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" >
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php post_thumbnail( 'thumbnail' );?>
</a>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
<?php the_excerpt(); ?>
</article>
<?php
}
}
// Restore original post data.
wp_reset_postdata();
I list my categories in a page using the following code:
<?php
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => true,
) );
$count = count($terms);
$categories = array();
if ($count > 0) :
foreach ($terms as $term) {
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'show_count' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'categorys',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$post_from_category = new WP_Query( $args );
if( $post_from_category->have_posts() ){
$post_from_category->the_post();
}else{}
$term->slug;
$term->name; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('thumb-block'); ?>>
<a href="<?php echo bloginfo('url'); ?>?categories=<?php echo $term->slug; ?>" title="<?php echo $term->name; ?>">
<header class="entry-header">
<span class="category-title"><?php echo $term->name; ?></span>
</header><!-- .entry-header -->
</a>
</article><!-- #post-## -->
<?php }
endif; ?>
My question is: How can I list the categories which contains minimum 5 posts only?
Thank you very much!
Inside your foreach loop you can just check to make sure $term->count is greater than or equal to 5. There's also a few other things I noticed:
You define a $categories array but don't appear to use it again.
Try not to mix if: endif; and if{} statements - stick to a syntax for ease-of-maintenance.
You can just check to see if $terms gets set to a truthy value instead of checking its count.
Are you sure your Tax Query is set properly? It's checking the taxonomy "categorys" which is a typo of "categories"
You don't need an empty else{} loop after your ->have_posts() check
You don't need to instantiate the $term object variables before the article.
You should consider using home_url() instead of bloginfo('url')
All that said, this should get you started:
<?php
$term_args = array(
'taxonomy' => 'category',
'hide_empty' => true,
);
if( $terms = get_terms( $term_args ) ){
foreach( $terms as $term ){
if( $term->count >= 5 ){
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'show_count' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'categorys',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$post_from_category = new WP_Query( $args );
if( $post_from_category->have_posts() ){ $post_from_category->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'thumb-block' ); ?>>
<a href="<?= home_url( "?categories={$term->slug}" ); ?>" title="<?= $term->name; ?>">
<header class="entry-header">
<span class="category-title"><?= $term->name; ?></span>
</header>
</a>
</article>
<?php }
}
}
}
?>
IN my code I am fetching data behalf of category id .but problem is that <div id="<?php echo $term_id = $term->term_id;?>" class="tabcontent">. $term_id does not change in this div.Here is my code .
<?php
//loop the names of the slugs for the portfolio_categories
$terms = get_terms( array ( 'taxonomy' => 'portfolio_categories', 'hide_empty' => false, 'parent' => 0, 'orderby' => 'date', 'order' => 'DESC' ));
foreach ($terms as $term) {
$slug= $term->slug;
$term_id = $term->term_id;
$args = array(
'post_type' => 'Portfolio',
'tax_query' => array(
array(
'taxonomy' => 'portfolio_categories',
'terms' => $slug,
'field' => 'slug',
)
),
'orderby' => 'ID',
'order' => 'DESC',
'posts_per_page' => -1
);
}
?>
<?php
$posts_query = new WP_Query( $args );
if (have_posts()) :?>
<div id="<?php echo $term_id = $term->term_id;?>" class="tabcontent">
<?php
while ( $posts_query->have_posts() ) : $posts_query->the_post();?>
<?php echo '<div class="col-1-3">';?>
<?php echo $term_id = $term->term_id;?>
<?php $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "size" );?>
<div class="wrap-col" >
<a class="fancybox" href="<?php echo $thumbnail[0]; ?>" data-fancybox-group= gallery>
<img src="<?php echo $thumbnail[0];?>"></a> </div>
</div>
<?php echo '</div>';?>
<?phpendwhile;endif;?>
</div>
Hi every one I have solved this issue .problem was only .div was not manage in under for loop. so this is answer if you trying to get custom post by custom category.
<?php
$taxonomy = 'portfolio_categories';
$args = array(
'hide_empty' => false
);
global $terms;
global $terms;
$terms = get_terms( 'portfolio_categories', $args );
?>
<div class="tab">
<?php $i = 0; ?>
<?php foreach ( $terms as $term ) { ?>
<?php $i++; ?>
<button class="tablinks" onclick="openCity(event, '<?php echo $term->term_id;?>')"
<?php if ($i == 1) { echo 'id="defaultOpen"'; } ?> ><?php echo $term->name;?></button>
<?php } ?>
</div>
<?php
//loop the names of the slugs for the portfolio_categories
$terms = get_terms(array('taxonomy' => 'portfolio_categories', 'hide_empty' => false, 'parent' => 0, 'orderby' => 'date', 'order' => 'DESC'));
foreach ($terms as $term) {
$slug = $term->slug;
$term_id = $term->term_id;
$args = array(
'post_type' => 'Portfolio',
'tax_query' => array(
array(
'taxonomy' => 'portfolio_categories',
'terms' => $slug,
'field' => 'slug',
)
),
'orderby' => 'ID',
'order' => 'DESC',
'posts_per_page' => -1
);
//}//Instead closing of here
?>
<?php
$posts_query = new WP_Query($args);
if (have_posts()) :
?>
<div id="<?php echo $term_id = $term->term_id; ?>" class="tabcontent">
<?php while ($posts_query->have_posts()) : $posts_query->the_post(); ?>
<?php echo '<div class="col-1-3">'; ?>
<?php echo $term_id = $term->term_id; ?>
<?php $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "size"); ?>
<div class="wrap-col" >
<a class="fancybox" href="<?php echo $thumbnail[0]; ?>" data-fancybox-group= gallery>
<img src="<?php echo $thumbnail[0]; ?>"></a> </div>
<?php echo '</div>'; ?>
<?php
endwhile;
endif;?>
</div>
<?php }//Close foreach here
?>
Inorder to get every term->term_id close the foreach at the end
//loop the names of the slugs for the portfolio_categories
$terms = get_terms(array('taxonomy' => 'portfolio_categories', 'hide_empty' => false, 'parent' => 0, 'orderby' => 'date', 'order' => 'DESC'));
foreach ($terms as $term) {
$slug = $term->slug;
$term_id = $term->term_id;
$args = array(
'post_type' => 'Portfolio',
'tax_query' => array(
array(
'taxonomy' => 'portfolio_categories',
'terms' => $slug,
'field' => 'slug',
)
),
'orderby' => 'ID',
'order' => 'DESC',
'posts_per_page' => -1
);
//}//Instead closing of here
?>
<?php
$posts_query = new WP_Query($args);
if (have_posts()) :
?>
<div id="<?php echo $term_id = $term->term_id; ?>" class="tabcontent">
<?php while ($posts_query->have_posts()) : $posts_query->the_post(); ?>
<?php echo '<div class="col-1-3">'; ?>
<?php echo $term_id = $term->term_id; ?>
<?php $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "size"); ?>
<div class="wrap-col" >
<a class="fancybox" href="<?php echo $thumbnail[0]; ?>" data-fancybox-group= gallery>
<img src="<?php echo $thumbnail[0]; ?>"></a> </div>
</div>
<?php echo '</div>'; ?>
<?php
endwhile;
endif;
}//Close foreach here
?>
</div>