Custom post type pagination issues - php

Having trouble implementing the pagination for custom post type ('vehicle') in Genesis child theme, I'm using Genesis : 2.0.0 in wordpress 4.1.1
I've tried placing the following code in function.php
add_filter( 'genesis_prev_link_text', 'gt_review_prev_link_text' );
function gt_review_prev_link_text() {
$prevlink = '« Previous Reviews';
return $prevlink;
}
add_filter( 'genesis_next_link_text', 'gt_review_next_link_text' );
function gt_review_next_link_text() {
$nextlink = 'Next Reviews »';
return $nextlink;
}
i've tried the following too;
<?php genesis_posts_nav();?>
The code below works, but only as 'newer' and 'older' hyperlinks, i'm trying to achieve a numeric pagination.
<?php previous_posts_link( '« Newer' ); ?>
<?php next_posts_link( 'Older »', $vehicle->max_num_pages ); ?>
Here is my custom post type archive template.
<?php
/*
Template Name: Archive Vehicle(CAR)
*/
//* Force full width content
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
//* Remove Post Info
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
//* Remove Post Meta
remove_action( 'genesis_entry_footer', 'genesis_entry_footer_markup_open', 5 );
remove_action( 'genesis_entry_footer', 'genesis_entry_footer_markup_close', 15 );
remove_action( 'genesis_entry_footer', 'genesis_post_meta' );
/** Code for custom loop */
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'am_custom_loop' );
function am_custom_loop(){
$paged = 1;
if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
if ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
$paged = intval( $paged );
$args = array('post_type' => 'vehicle', 'posts_per_page' => '3', 'paged'=> $paged, );
$vehicle = new WP_query($args);
$wp_query = $vehicle;
if ($vehicle -> have_posts() ){
?>
<div class="first arc">
<h2> View our latest used car collection from KH cars Birmingham</h2>
<p>Need a car? Look no further, browse through our vast selection of used cars on site based in Birmingham. </p>
</div>
<?php
while ($vehicle -> have_posts() ){
$vehicle -> the_post();
?>
<div class="first archivecar">
<div class="one-fourth">
<?php
$images = get_field('image');
if( $images ):
$image = $images[0];
?>
<a href="<?php the_permalink($id); ?>">
<img src="<?php echo $images[0]['sizes']['large']; ?>" alt="<?php echo $image['alt']; ?>" />
</a>
<?php endif; ?>
</div>
<div class="two-fourths">
<br>
<h3><a style="text-decoration:none;" href="<?php the_permalink($id) ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<ul class="carinfo">
<li><p> Year: <?php $terms = get_the_terms( $post->ID , 'year', 'year' );
// Loop over each item since it's an array
if ( $terms != null ){
foreach( $terms as $term ) {
// Print the name method from $term which is an OBJECT
print $term->name ;
// Get rid of the other data stored in the object, since it's not needed
unset($term);
} } ?></p></li>
<li><p> Trans: <?php $terms = get_the_terms( $post->ID , 'trans', 'trans' );
// Loop over each item since it's an array
if ( $terms != null ){
foreach( $terms as $term ) {
// Print the name method from $term which is an OBJECT
print $term->name ;
// Get rid of the other data stored in the object, since it's not needed
unset($term);
} } ?></p></li>
<li><p> Fuel:
<?php // Get terms for post
$terms = get_the_terms( $post->ID , 'fuel', 'fuel' );
// Loop over each item since it's an array
if ( $terms != null ){
foreach( $terms as $term ) {
// Print the name method from $term which is an OBJECT
print $term->name ;
// Get rid of the other data stored in the object, since it's not needed
unset($term);
} } ?></li>
<?php if( get_field('Mileage') ): ?>
<li><p> Mileage: <?php the_field('Mileage');?></p></li>
<?php endif; ?>
<li><p>Color: <?php $terms = get_the_terms( $post->ID , 'colours', 'colours' );
// Loop over each item since it's an array
if ( $terms != null ){
foreach( $terms as $term ) {
// Print the name method from $term which is an OBJECT
print $term->name ;
// Get rid of the other data stored in the object, since it's not needed
unset($term);
} } ?></p></li>
<?php if( get_field('owners') ): ?>
<li><p>Owners: <?php $terms = get_the_terms( $post->ID , 'owners', 'owners' );
// Loop over each item since it's an array
if ( $terms != null ){
foreach( $terms as $term ) {
// Print the name method from $term which is an OBJECT
print $term->name ;
// Get rid of the other data stored in the object, since it's not needed
unset($term);
} }?></p></li>
<?php endif; ?>
</ul>
</div>
<div class="one-fourth">
<?php if ( get_field('price')):?>
<p class="price"><?php the_field('price');?></p>
<?php endif; ?>
<a class="btn" style="text-align:center" href="<?php the_permalink() ?>">view Details</a>
</div>
</div>
<?php
}
?>
<?php genesis_posts_nav();?>
<?php wp_reset_query();?>
<?php
}
}
genesis();
Any Help is much appreciated. Thanks in advance

First i Removed $wp_query = $vehicle; and then used the code below from wordpress codex and voila it works.
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $vehicle->max_num_pages
) );
?>

Related

dokan store products Category Layout woocommerce

I'm using astra team with WordPress and dokan for multivendor,
I had woocommerce shop page layout by category which still works fine, but for some reason it does not apply to dokan store product listing.
the php code for woocoomerce shop page
<php?
add_action( 'woocommerce_no_products_found', 'bbloomer_show_4_products_per_category' );
function bbloomer_show_4_products_per_category() {
$args = array(
'parent' => 0,
'hide_empty' => true,
'taxonomy' => 'product_cat',
'fields' => 'slugs',
);
$categories = get_categories( $args );
foreach ( $categories as $category_slug ) {
$term_object = get_term_by( 'slug', $category_slug , 'product_cat' );
echo '<hr><h2>' . $term_object->name . '</h2>';
echo do_shortcode( '[products limit="4" columns="4" category="' . $category_slug . '"]' );
echo '<p><a href="' . get_term_link( $category_slug, 'product_cat' ) . '">View all ' .
$term_object->name . ' products →</a>';
}
}
>?
the output:
on the dokan vendor store the layout is:
It does not change the layout
I searched the plugin and find the script that renders the vendors products, but can't seems how to adjust it.
<?php do_action( "dokan_store_before_{$section_id}_product_section", $vendor ); ?>
<div id="dokan_store_section_<?php echo esc_attr( $section_id ); ?>" class="dokan-store-product-section">
<h2 class="products-list-heading"><?php echo esc_html( $section_title ); ?></h2>
<div class="seller-items">
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; ?>
<?php woocommerce_product_loop_end(); ?>
<?php wp_reset_postdata(); ?>
</div>
</div>
<?php do_action( "dokan_store_after_{$section_id}_product_section", $vendor ); ?>

Change Number of Products Per Row Fetched in WooCommerce AJAX call

My live search field fetches the title of the product, thumbnail and add to cart button using AJAX.
It displays the output in singular columns, which is unreadable for a large product return.
How can I update my output so it displays in rows of 4 as below? (below)
Front End
<input type="text" name="keyword" id="keyword" onkeyup="fetch()">
<div id="datafetch">Your numbers will show here</div>
<script>
function fetch(){
$.post('<?php echo admin_url('admin-ajax.php'); ?>',{'action':'my_action'},
function(response){
$('#datafetch').append(response);
console.log(result);
});
}
</script>
Code in Functions.php
<?php
}// LOTTERY start the ajax function
add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
function data_fetch(){
$the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'product' ) );
if( $the_query->have_posts() ) :
while( $the_query->have_posts() ): $the_query->the_post();
global $product;
$product = get_product( get_the_ID() ); //set the global product object
$myquery = esc_attr( $_POST['keyword'] );
$a = $myquery;
$search = get_the_title();
if( stripos("/{$search}/", $a) !== false) {?>
<h4><?php the_title();?></h4>
<h4><?php the_post_thumbnail();?></h4>
<p><?php echo $product->get_price_html(); ?></p>
<?php woocommerce_template_loop_add_to_cart(); //ouptput the woocommerce loop add to cart button ?>
<?php
}
endwhile;
wp_reset_postdata();
endif;
die();
}
You can use a counter variable. try the below code.
add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
function data_fetch(){
$the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'product' ) );
if( $the_query->have_posts() ) : $count = 1; while( $the_query->have_posts() ): $the_query->the_post();
if ( $count %4 == 1 ) {
echo '<div class="row"">';
}
global $product;
$product = get_product( get_the_ID() ); //set the global product object
$myquery = esc_attr( $_POST['keyword'] );
$a = $myquery;
$search = get_the_title();
if( stripos("/{$search}/", $a) !== false) {?>
<h4><?php the_title();?></h4>
<h4><?php the_post_thumbnail();?></h4>
<p><?php echo $product->get_price_html(); ?></p>
<?php woocommerce_template_loop_add_to_cart(); //ouptput the woocommerce loop add to cart button ?>
<?php }
if ( $count %4 == 0 ) {
echo "</div>";
}
$count++;
endwhile;
if ( $count %4 != 1 ) echo "</div>";
wp_reset_postdata();
endif;
die();
}

Re-order custom post categories

I am using the following code to retrieve categories from a custom post type:
<?php
$taxonomy = 'treatment';
$terms = get_terms($taxonomy);
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li>
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
<h4><?php echo $term->name; ?></h4>
</a>
</li>
<?php } ?>
</ul>
<?php endif;?>
Now I want to be able to re-order these posts and have found a suitable plugin to do this. However, in order to make this work, I need to add 'orderby' => 'term_order' to the query.
Is this even possible with this loop? I have tried a loop like this within the above but it doesn't work. With the loop alone I cannot fetch the custom post categories:
<?php $loop = new WP_Query( array( 'post_type' => 'treatments', 'posts_per_page' => 1, 'orderby' => 'term_order' ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
Content here
<?php endwhile; wp_reset_query(); ?>
Place this given code inside the loop.And please replace "custom_taxonomy_name" with your custom taxonomy name which you have registered for custom post type
<?php $terms = get_the_terms(get_the_ID(),'custom_taxonomy_name' ); ?>
<?php foreach( $terms as $term): ?><br>
<span class="text-category">Category:<?php echo $term->name; ?></span>
<?php endforeach; ?>

How to write a more efficient query to get child categories and paginate

I'm using this code to show only child categories and paginate them, but this is horribly slow (49 queries in 9,799 seconds.).
I know that I'm doing this by using relying built-in Wordpress querying functions and this is not a good practice, but instead, one of the causes that make this so slow...
I couldn't find a better way to do this and I need help to write a more efficient query.
How can this run faster?
<?php
$current_cat = get_query_var('cat');
$attributes = array('terms_per_page' => 20, 'taxonomy' => 'category');
// Sanitize and validate our inputs and set variables
$tpp = filter_var( $attributes['terms_per_page'], FILTER_VALIDATE_INT );
$taxonomy = filter_var( $attributes['taxonomy'], FILTER_SANITIZE_STRING );
// Make sure our taxonomy exists to avoid unnecessary work
if ( !taxonomy_exists( $taxonomy ) )
return false;
// Our taxonomy exists, lets continue. Now, get the terms count to calculate pagination.
$term_count = count( get_terms('category',array('hide_empty'=>'1','child_of' => $current_cat) ) );
// We have terms, now calculate pagination
$max_num_pages = ceil( $term_count / $tpp );
// Get current page number. Take static front pages into account as well
if ( get_query_var('paged')){
$paged = get_query_var('paged');
} elseif ( get_query_var('page')) {
$paged = get_query_var( 'page' );
} else {
$paged = 1;
}
// Calculate term offset
$offset = ( ( $paged - 1 ) * $tpp );
// We can now get our terms and paginate it
$args = array(
'number' => $tpp, // Amount of terms to return
'offset' => $offset, // The amount to offset the list by for pagination
'child_of' => $current_cat,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => '1',
);
// Set our variable to hold our string
$output = '';
$wpbtags = get_terms( $taxonomy, $args );
if ( ! empty($wpbtags)) {
foreach ($wpbtags as $category) {
//first get the current category ID
$cat_prefix = "category_";
$cat_id = $category->term_id;
//then i get the data from the database
$join_cat = $cat_prefix . $cat_id;
$cat_data = get_option($join_cat);?>
<a href="<?php echo get_category_link($category->term_id); ?>" onclick="ga('send', 'event', 'ClickNav', 'cats', 'Cat - <?php echo $category- >cat_name; ?>');">
<div class="one_fourth<?php echo ( ++$counter == 4 )?' last_column':''; ?> home-colour" title="<?php echo $category->name; ?>">
<?php if (!empty($cat_data['cat_img_url'])) : ?>
<h2 style="text-align:center"> <?php echo $category->name; ?> </h2>
<img class="res_img" src="<?php echo $cat_data['cat_img_url'] ;?>" alt="<?php echo $category->cat_name; ?>" width="310" height="209" />
<?php elseif (empty($cat_data['cat_img_url'])) :?>
<h2 style="text-align:center"> <?php echo $category->name; ?> </h2>
<img class="res_img" src="<?php echo get_stylesheet_directory_uri(); ?>/images/subcat-img-na.png" alt="Posts - <?php echo $category->cat_name; ?>"/>
<?php else : ?>
<?php endif; ?>
</div><!-- eof cat div --> </a>
<?php } ?>
<?php /* COUNTS FOUR ADDS LAST COLUMN */ if ( $counter == 4 ) : ?>
<div class='clear'> </div>
<?php $counter = 0; endif; wp_reset_query(); ?>
<div class='clear'> </div>
<?php echo '<div class="wp-pagenavi" style="text-align:center">';
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $max_num_pages = (ceil( $term_count / $tpp))
) );
echo '</div>';
?>
<?php wp_reset_postdata(); ?>
<?php /* IF THERE'S NOTHING TO SHOW */ } else {?>
// Show something if there is no posts here
<?php } ?>

Get categories for a single post in a custom post type

I'm trying to get an unformatted list (preferably a list of slugs) of the categories for a single post in a custom post type loop. This list will eventually serve as a class for a div ($CATEGORYSLUGSWILLEVENTUALLYGOHERE).
I've found a few different methods of getting a list of ALL of the categories for a custom post type, but not the ones specific to a single one. Here's what I have so far:
<?php $projects_loop = new WP_Query( array( 'post_type' => 'projects', 'orderby' => 'menu_order' ) ); ?>
<?php while ( $projects_loop->have_posts() ) : $projects_loop->the_post(); ?>
<div class="box <?php $CATEGORYSLUGSWILLEVENTUALLYGOHERE; ?>">
<div class="port-item-home">
<?php the_post_thumbnail( 'portfolio-home' ); ?>
<p><?php the_title(); ?></p>
</div>
</div>
<?php endwhile; ?>
And here's what I've tried so far to get the category list:
<?php
$args = array(
'orderby' => 'name',
'parent' => 0,
'taxonomy' => 'project-type'
);
$categories = get_categories( $args );
echo '<p> '.print_r(array_values($categories)).'something</p>'
?>
I have it returning the array - but the array is showing that it'll display all categories instead of the ones pertaining to that specific post.
I also tried:
<?php
//list terms in a given taxonomy (useful as a widget for twentyten)
$taxonomy = 'project-type';
$tax_terms = get_terms($taxonomy);
?>
<?php
foreach ($tax_terms as $tax_term) {
echo $tax_term->name;
}
?>
And that also displays all categories instead of the ones pertaining to the post.
Any suggestions??
Got it! Found this article that helped me out:
https://wordpress.org/support/topic/how-to-get-the-category-name-for-a-custom-post-type
<!-- The Query -->
<?php
$args = array(
'post_type' => 'my_post_type',
'posts_per_page' => -1,
'orderby' => 'menu_order' );
$custom_query = new WP_Query( $args );
?>
<!-- The Loop -->
<?php
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
$terms_slugs_string = '';
$terms = get_the_terms( $post->ID, 'my_post_type' );
if ( $terms && ! is_wp_error( $terms ) ) {
$term_slugs_array = array();
foreach ( $terms as $term ) {
$term_slugs_array[] = $term->slug;
}
$terms_slugs_string = join( " ", $term_slugs_array );
}
?>
<div class="box<?php echo $terms_slugs_string ?>">
<div class="port-item-home">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'portfolio-home' ); ?>
</a>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</div>
</div>
<?php endwhile; ?>

Categories