Ask your WordPress questions! Pay money and get answers fast! (more info)

How to get current previous next for navigation WordPress

  • REFUNDED

<?php
echo'<ul class="subNav-container">';

$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc){
echo $wsc->name;

//How to do below:
//IF Current page is equal to Array and Array is NOT-First-or-Last-Array echo 'Current-Array' and 'PREV-Array' and 'PREV-Array-URL' and 'NEXT-Array' and 'NEXT-Array-URL';
//IF Current page is equal to Array and Array is the First-Array echo 'Current-Array' and 'NEXT-Array' and 'NEXT-Array-URL';
//IF Current page is equal to Array and Array is the Last-Array echo 'Current-Array' and 'PREV-Array' and 'PREV-Array-URL';

}
echo '</ul>';

Answers (5)

2016-11-28

Bob answers:

do you want to implement next previous links for category pages?


email889 comments:

I think you are correct. This is for product categories.
- Category Name only.


email889 comments:

this is for product category loop


email889 comments:

no response?

2016-11-28

Reigel Gallarde answers:

what is this Array? what might be it's value?


email889 comments:

This is product categories.
- Category Name


email889 comments:

Name only.


email889 comments:

this is for product category loop


email889 comments:

I need to do below:
//IF Current page is equal to Array and Array is NOT-First-or-Last-Array echo 'Current-Array' and 'PREV-Array' and 'PREV-Array-URL' and 'NEXT-Array' and 'NEXT-Array-URL';
//IF Current page is equal to Array and Array is the First-Array echo 'Current-Array' and 'NEXT-Array' and 'NEXT-Array-URL';
//IF Current page is equal to Array and Array is the Last-Array echo 'Current-Array' and 'PREV-Array' and 'PREV-Array-URL';


Here is a working code that echo category names. Will be used on a blank custom page.

<?php
$taxonomy = 'product_cat';
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$show_option_none = '';

$wsubargs = array(
'taxonomy' => $taxonomy,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty,
'show_option_none' => $show_option_none,
);

echo'<ul class="subNav-container">';

$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc){
echo $wsc->name. '<br>';
}
echo '</ul>';
?>

2016-11-28

Arnav Joy answers:

please explain more about your requirement.

2016-11-28

mod mi answers:

Assuming you are in a category page please try this:

<?php echo'<ul class="subNav-container">';

$this_wsc = get_queried_object(); //current

echo $this_wsc->name; //current name

$wsubargs = array(
'taxonomy' => 'product_cat'
);

$wsubcats = get_categories($wsubargs);
foreach( $wsubcats as $position => $wsc ) :
if( $this_wsc->term_id == $wsc->term_id ) :
$next_cat = $position + 1; // Check next
$prev_cat = $position - 1; // Check previous
break;
endif;
endforeach;

$next_cat = $next_cat == count($wsubcats) ? 0 : $next_cat;

if ($prev_cat != -1):
echo '<li>previous: ' . get_term_link( $wsubcats[$prev_cat] ) . '</li>';
endif;
if ($next_cat != 0):
echo '<li>next: ' . get_term_link( $wsubcats[$next_cat] ) . '</li>';
endif;
echo '</ul>';?>


email889 comments:

this is for product category loop


mod mi comments:

Ok, this should work. Did you try it?


email889 comments:

Only blank


mod mi comments:

So I guess you are using this inside archive-product.php right?
What are the arguments in $wsubargs?


email889 comments:

$wsubargs = array(
'taxonomy' => 'product_cat',
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty,
'show_option_none' => $show_option_none,
);


mod mi comments:

I don't have the variables $hierarchical etc. contained in your args so I've update my code with just the taxonomy in the args and made some changes for the conditional display of the links. It works! Try it like this first to check and then add the rest of your args. Please notice on your args there is a ',' at the last line before closing the array, make sure to remove that.


email889 comments:

Only your code not working. It is blank


mod mi comments:

Is your product archive accessible online? Could you post a link?


mod mi comments:

Could you please test it by visiting a category page and not the shop index?


mod mi comments:

This code works inside the taxonomy-product_cat.php template. Because archive-product.php is used by the shop homepage and lists all categories it can't find any current category. Please do the following:
1. Create a file taxonomy-product_id.php in your woo commerce theme folder.
2. Copy all the code from archive-product.php and paste it in the taxonomy-product_id.php.
3. Add my code in taxonomy-product_id.php.
4. Remove my code from archive-product.php


email889 comments:

This is custom page.


mod mi comments:

Could you please let me know the name of the php file you are using?


email889 comments:

I am creating a custom page for category not using a woo built in page.


mod mi comments:

Yes i understand, what is the name of the .php file you are using for this page template?


email889 comments:

I am using a code to shortcode converter plugin.

Here is a working of echo category names.

<?php
$taxonomy = 'product_cat';
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$show_option_none = '';

$wsubargs = array(
'taxonomy' => $taxonomy,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty,
'show_option_none' => $show_option_none,
);

echo'<ul class="subNav-container">';

$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc){
echo $wsc->name. '<br>';
}
echo '</ul>';
?>

How to put your code in?


mod mi comments:

And where you are putting the shortcode?


email889 comments:

on a blank page


mod mi comments:

And what template are you using to display the page please?
If it's not the template that displays a product category archive 'taxonomy-product_id.php' the code won't be able to find the current category and echo the appropriate pre next links, since you are not in a category.
Please use a template override for woocommerce. Create a folder 'woocommerce' inside your theme's folder and create 'taxonomy-product_id.php' and put only my code there. Visit a product category and the links should work!


email889 comments:

Sorry, I can't use this method.


mod mi comments:

How do you use the shortcode please? You paste it inside the content editor of a page (or post) in wp-admin? What template are you using to display this page? Or you paste it inside the page.php template in your theme's folder? could you please give more details?


email889 comments:

https://wordpress.org/plugins/search.php?type=term&q=code+to+shortcode


mod mi comments:

I understand you are using a plugin to build shortcakes from a given code. But where do you use the shortcode? Do you put shortcodes directly in a pages content at the back end editor in the dashboard, inside a template? And what is the template used for the page template?


email889 comments:

as I mentioned earlier, just a normal blank page. standard 2016 theme. Obviously page editor when use the plugin, that is the purpose.


mod mi comments:

So that page is using the standard page.php? If this is not inside the taxonomy-product_cat.php template of woocommerce you can't make it work, since inside the default page.php it's just the default page loop that doesn't query a product category so you can't get the current product category and display the next prev links.

2016-11-30

Rempty answers:

Hello use this code
$cats_per_page = 2;
$page = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$offset = ( $page - 1 );
$args=array(
'orderby' => 'name',
'order' => 'asc'
);
$categories=get_categories($args);
$total=ceil(count($categories) / $cats_per_page);
for( $i = $offset * $cats_per_page; $i < ( $offset + 1 ) * $cats_per_page; $i++ ) {
$category = $categories[$i];
echo '<a href="' . get_category_link( $category->term_id ) . '" >' . $category->name.'</a>';
}
echo '<div class="custom_pagination">';
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'current' => $page,
'end_size' => 0,
'total' => $total,
'mid_size' => 0,
'type' => 'list',
'prev_text' => __('Previous'),
'next_text' => __('Next'),
));
echo '</div>';

But you need to add this css to hide the page numbers and only show the prev and next links
<style>
.custom_pagination a.page-numbers,.custom_pagination span.page-numbers{
display:none;
}
.custom_pagination a.page-numbers.prev,.custom_pagination a.page-numbers.next{
display:block
}
</style>