Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Count Correct Terms for Display Pagination WordPress

  • SOLVED

Hi, all. I have the code below in a page template for a child term. The code displays a list (in grid view) of the terms associated to it's parent (ID 16). The parent's name is Basic Materials and it's slug is "basic-materials".

<strong>The problem</strong> I have is that the pagination is counting the number of terms in the taxonomy instead of the specific parent term. So in this case what should only show two page links is showing over 18 because that is how many pages get shown when viewing ALL terms. Can someone help me figure out how to make it count the # of terms in the parent and have the right number of page links display?

<?php
/*
Template Name: 100 List Page Testing 2
*/
?>

<?php get_header(); ?>

<div class="container">
<div class="row">
<div class="col-md-12">

<div class="row">
<div class="col-md-9">
<h2>Index Post Type Archive </h2>
<hr/>
<!-- Begin LG Screen View-->
<span class="visible-lg">
<article>

<div class="row">
<?php

if ( get_query_var( 'paged' ) ) {
$paged = get_query_var('paged');
}elseif( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
}else{
$paged = 1;
}

$per_page = 12;
$number_of_terms = wp_count_terms( '100list' ); // This counts the total number terms in the taxonomy with a function)
$paged_offset = ($paged - 1) * $per_page;

$libargs = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'exclude' => array(), //* Enter ID's of parent categories to exclude from list
'number' => $per_page,
'offset' => $paged_offset,
'parent' => '16',
);

$libcats = get_terms( '100list', $libargs);

$i = 0;

foreach($libcats as $lc){
if( $i % 4 == 0 ) { ?>

<div class="clearfix"></div>

<?php }

$i++; ?>

<div class="col-lg-3">

<?php $termlink = get_term_link( $lc->slug, '100list' ); ?>

<div class="panel panel-default">
<div class="panel-image">


<div class="thumbnail">
<div class="caption">
<br/><br/>
<h1><span class="label label-info"><?php echo $lc->count ?></span></h1>
<p> Symbols </p>

<h4> <a class="label label-default" href="<?php echo $termlink; ?>"> View Group</a> </h4>



</div>

<!-- Get Image by Attachment ID Start-->
<?php

$attachment_id = get_field('taximage', '100list_'.$lc->term_id);

if ($attachment_id) {
$image = wp_get_attachment_image_src($attachment_id, 'industrygroup-img');

if ($image) {
?>
<img class="img-responsive" src="<?php echo $image[0]; ?>" />
<?php
}
}

else { ?>

<img class="img-responsive" src="http://www.runningalpha.com/wp-content/uploads/2014/08/RA_logo_300px_groups.jpg" alt="<?php the_title(); ?>" />

<?php } ?>
</div>

<!-- Get Image by Attachment ID End-->

</div>
<div class="panel-footer text-center"><a href="<?php echo $termlink; ?>"><?php echo $lc->name; ?></a>
</div>
</div>
</div>

<?php }

$big = 999999999; // need an unlikely integer
echo paginate_links(
array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?page=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => ceil( $number_of_terms / $per_page ), // 20 items per page
'type' => 'list',
'end_size' => 5,
'mid_size' => $mid_size,
)
);
?>
</div>
</article>
</span>
</div> <!-- End col-md-9 -->

<div class="col-md-3">
<h2 class="pagetitle">Sectors</h2><hr/>
<?php
wp_nav_menu( array(
'theme_location' => '100listmenu',
'container' => false,
'menu_class' => 'nav nav-pills nav-stacked',
'fallback_cb' => 'wp_page_menu',
//Process nav menu using our custom nav walker
'walker' => new wp_bootstrap_navwalker())
);
?>
<?php include("sidebar100list.php"); ?>
</div> <!-- End col-md-3 -->

</div><!-- End row -->
</div><!-- End Container -->
<?php get_footer(); ?>

Answers (4)

2014-08-22

Martin Pham answers:

hi,
...something is not logical


$number_of_terms = wp_count_terms( '100list' ); // hide_empty = true

$libargs = array(
....
'hide_empty' => 0,
....
);


change to


$number_of_terms = wp_count_terms( '100list', array('hide_empty' => false) );


streetfire comments:

Hi, can you explain what this is doing?


Martin Pham comments:

try this

<?php

/*

Template Name: 100 List Page Testing 2

*/
?>
<?php get_header(); ?>

<div class="container">

<div class="row">

<div class="col-md-12">



<div class="row">

<div class="col-md-9">

<h2>Index Post Type Archive </h2>

<hr/>

<!-- Begin LG Screen View-->

<span class="visible-lg">

<article>



<div class="row">

<?php



if ( get_query_var( 'paged' ) ) {

$paged = get_query_var('paged');

}elseif( get_query_var( 'page' ) ) {

$paged = get_query_var( 'page' );

}else{

$paged = 1;

}



$per_page = 12;

#fix
$term_args = array(
'hide_empty' => 0,
'parent' => 16,
);

$number_of_terms = wp_count_terms( '100list' , $term_args); // This counts the total number terms in the taxonomy with a function)

$paged_offset = ($paged - 1) * $per_page;

$libargs = array(
'orderby' => 'name',
'order' => 'ASC',
'exclude' => array(), //* Enter ID's of parent categories to exclude from list
'number' => $per_page,
'offset' => $paged_offset,
);

$_libargs = wp_parse_args($term_args, $libargs);

$libcats = get_terms( '100list', $_libargs);

#fix

$i = 0;



foreach($libcats as $lc){

if( $i % 4 == 0 ) { ?>



<div class="clearfix"></div>



<?php }



$i++; ?>



<div class="col-lg-3">



<?php $termlink = get_term_link( $lc->slug, '100list' ); ?>



<div class="panel panel-default">

<div class="panel-image">





<div class="thumbnail">

<div class="caption">

<br/><br/>

<h1><span class="label label-info"><?php echo $lc->count ?></span></h1>

<p> Symbols </p>



<h4> <a class="label label-default" href="<?php echo $termlink; ?>"> View Group</a> </h4>







</div>



<!-- Get Image by Attachment ID Start-->

<?php



$attachment_id = get_field('taximage', '100list_'.$lc->term_id);



if ($attachment_id) {

$image = wp_get_attachment_image_src($attachment_id, 'industrygroup-img');



if ($image) {

?>

<img class="img-responsive" src="<?php echo $image[0]; ?>" />

<?php

}

}



else { ?>



<img class="img-responsive" src="http://www.runningalpha.com/wp-content/uploads/2014/08/RA_logo_300px_groups.jpg" alt="<?php the_title(); ?>" />



<?php } ?>

</div>



<!-- Get Image by Attachment ID End-->



</div>

<div class="panel-footer text-center"><a href="<?php echo $termlink; ?>"><?php echo $lc->name; ?></a>

</div>

</div>

</div>



<?php }



$big = 999999999; // need an unlikely integer

echo paginate_links(

array(

'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),

'format' => '?page=%#%',

'current' => max( 1, get_query_var('paged') ),

'total' => ceil( $number_of_terms / $per_page ), // 20 items per page

'type' => 'list',

'end_size' => 5,

'mid_size' => $mid_size,

)

);

?>

</div>

</article>

</span>

</div> <!-- End col-md-9 -->



<div class="col-md-3">

<h2 class="pagetitle">Sectors</h2><hr/>

<?php

wp_nav_menu( array(

'theme_location' => '100listmenu',

'container' => false,

'menu_class' => 'nav nav-pills nav-stacked',

'fallback_cb' => 'wp_page_menu',

//Process nav menu using our custom nav walker

'walker' => new wp_bootstrap_navwalker())

);

?>

<?php include("sidebar100list.php"); ?>

</div> <!-- End col-md-3 -->



</div><!-- End row -->

</div><!-- End Container -->

<?php get_footer(); ?>


streetfire comments:

Excellent! Martin Pham, this is working beautifully! Thank you!!!


Martin Pham comments:

Because

$number_of_terms = wp_count_terms( '100list' );


===> return an incorrect result, it includes the results are not a child of 16.

you can see more in codex:

http://codex.wordpress.org/Function_Reference/wp_count_terms
http://codex.wordpress.org/Function_Reference/get_terms

2014-08-22

John Cotton answers:

Surely this:

$number_of_terms = wp_count_terms( '100list' );

should be this:

$number_of_terms = wp_count_terms( '100list', array( 'parent' => $parent_id ) );

where $parent_id is the id of basic-materials.


streetfire comments:

John, that actually worked quite well too! :D

I wonder which "working" method is best, lol


John Cotton comments:

<blockquote>I wonder which "working" method is best, lol</blockquote>
I think they are essentially the same.

Your problem was that you were filtering your term count, hence getting the count for all terms.

You need to look at the args param for get_terms (it's the same as wp_count_terms).

http://codex.wordpress.org/Function_Reference/get_terms


streetfire comments:

John, Thank you. In all honesty I was studying that link before I posted, but I need to understand better how everything all works together I believe. I'm still learning php functions the core wordpress functionality...

2014-08-22

Fahad Murtaza answers:

If I understood the question correctly, something along these lines should work


$parent = get_queried_object()->parent; // Get parent of the current term



The above should give you the parent which you can use within

wp_count_terms as parameter.

Let me know if that helps.


streetfire comments:

Hm, like this?

$number_of_terms = wp_count_terms( '100list', array ($parent = get_queried_object()->parent) );

That doesn't work. If I didn't insert it right please provide more information. Thanks


Fahad Murtaza comments:

$parent = get_queried_object()->parent; // Get parent of the current term
$number_of_terms = wp_count_terms( '100list', array( 'parent' => $parent ) ); // You can make it more dynamic by inserting the dynamic variable for '100list'


Fahad Murtaza comments:

I see, you got it solved already!


streetfire comments:

Hi, yes I think we do! Seems a couple of ways to do it :) You ALL were most helpful. I will be visiting here more often ;)

2014-08-22

timDesain Nanang answers:

change this part:

$per_page = 12;
$number_of_terms = wp_count_terms( '100list' ); // This counts the total number terms in the taxonomy with a function)
$paged_offset = ($paged - 1) * $per_page;
$libargs = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'exclude' => array(), //* Enter ID's of parent categories to exclude from list
'number' => $per_page,
'offset' => $paged_offset,
'parent' => '16',
);
$libcats = get_terms( '100list', $libargs);
$i = 0;

with:

$per_page = 12;
//removed
$paged_offset = ($paged - 1) * $per_page;

$libargs = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'exclude' => array(), //* Enter ID's of parent categories to exclude from list
'number' => $per_page,
'offset' => $paged_offset,
'parent' => '16',
);
$libcats = get_terms( '100list', $libargs);
$number_of_terms = wp_count_terms( '100list', $libargs); //addition
$i = 0;