Jetpack Infinite Scroll not working in Front page - php

I am working on a theme which has post types - 'tour'.
I am using JetPack's infinite scroll to render new posts:
function.php
function tweakjp_custom_is_support() {
$supported = current_theme_supports( 'infinite-scroll' ) && ( is_home() || is_front_page() || is_archive() || is_single());
return $supported;
}
add_filter( 'infinite_scroll_archive_supported', 'tweakjp_custom_is_support' );
function my_child_infinite_scroll_render() {
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_type());
endwhile;
}
function my_child_setup() {
add_theme_support( 'infinite-scroll', array(
'type' => 'scroll',
'container' => 'mob_infinite',
'render' => 'my_child_infinite_scroll_render',
'posts_per_page' => 2,
) );
}
add_action( 'after_setup_theme', 'my_child_setup' );
Front Page
<div class="container">
<div id="mob_infinite">
$args = array ('post_type' => 'post');
$myquery = new wp_query($args);
if($myquery->have_posts() ){
while($myquery->have_posts() ) : $myquery->the_post();
get_template_part( 'content', get_post_type() );
endwhile;
}
</div>
</div>

Related

Custom post type archive : display subcategories of current category

In my custom post type archive page, I would like to be able to filter posts by categories.
I have achieved this, but I can only manage to display all categories. I would like same level categories below the current category to be visible only.
For example if I have taxonomies like :
Category 1
-- Subcategory 1
--- subsub 1.1
-- Subcategory 2
When I am on the page https://example.com/mycpt/?getby=cat&cat=category-1
Only Subcategories 1 and Subcategory 2 should be visible
Here is my code:
In archive-mycpt.php
<div class="filter-custom-taxonomy">
<?php
$terms = get_terms( ['taxonomy' => 'product_cat'] );
foreach ( $terms as $term ) : ?>
<a href="<?php echo home_url() ?>/tutos/?getby=cat&cat=<?php echo esc_attr( $term->slug ); ?>">
<?php echo esc_html( $term->name ); ?>
</a>
<?php endforeach; ?>
</div>
<div class="row">
<div class="small-12 columns">
<?php do_action( 'thb_archive_title' ); ?>
<div class="row">
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
get_template_part( 'inc/templates/post-styles/post-style1' );
endwhile;
endif;
?>
</div>
<?php
the_posts_pagination(
array(
'prev_text' => '',
'next_text' => '',
'mid_size' => 2,
)
);
?>
</div>
</div>
and in function.php
function kbites_words_filter_archive( $query ) {
if ( ! $query->is_main_query() )
return $query;
if ( is_admin() ) {
return;
}
if ( is_post_type_archive('tutos') ) {
if (isset($_GET['getby'])) {
if ( 'cat' === $_GET['getby'] ) {
$taxquery = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $_GET['cat'],
),
);
$query->set( 'tax_query', $taxquery );
}
}
$query->set( 'posts_per_page', 30 );
}
return $query;
}
add_action( 'pre_get_posts', 'kbites_words_filter_archive');

How to display random product inside related product in woocommerce single product page?

I am using Woodmart wordpress template.
By default related product show only category and tag.
I want category, then tag, then random product. And if some product have not present related product in same category or tag, it will showing random product on related product section
I found a code on this https://vincoding.com/randomize-woocommerce-related-products/ But I don't understand it will works or not.
The related product template are down blow. I am using grid template. Please help
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
$related_product_view = woodmart_get_opt( 'related_product_view' );
if ( $related_products ) : ?>
<div class="related-products">
<?php
$heading = apply_filters( 'woocommerce_product_related_products_heading', __( 'Related products', 'woocommerce' ) );
if ( $heading ) :
?>
<h3 class="title slider-title"><?php echo esc_html( $heading ); ?></h3>
<?php endif; ?>
<?php
if ( $related_product_view == 'slider' ) {
$slider_args = array(
'slides_per_view' => ( woodmart_get_opt( 'related_product_columns' ) ) ? woodmart_get_opt( 'related_product_columns' ) : apply_filters( 'woodmart_related_products_per_view', 4 ),
'img_size' => 'woocommerce_thumbnail',
'products_bordered_grid' => woodmart_get_opt( 'products_bordered_grid' ),
'custom_sizes' => apply_filters( 'woodmart_product_related_custom_sizes', false ),
'product_quantity' => woodmart_get_opt( 'product_quantity' )
);
woodmart_set_loop_prop( 'products_view', 'carousel' );
echo woodmart_generate_posts_slider( $slider_args, false, $related_products );
}elseif ( $related_product_view == 'grid' ) {
woodmart_set_loop_prop( 'products_columns', woodmart_get_opt( 'related_product_columns' ) );
woodmart_set_loop_prop( 'products_different_sizes', false );
woodmart_set_loop_prop( 'products_masonry', false );
woodmart_set_loop_prop( 'products_view', 'grid' );
woocommerce_product_loop_start();
foreach ( $related_products as $related_product ) {
$post_object = get_post( $related_product->get_id() );
setup_postdata( $GLOBALS['post'] = $post_object );
wc_get_template_part( 'content', 'product' );
}
woocommerce_product_loop_end();
woodmart_reset_loop();
if ( function_exists( 'woocommerce_reset_loop' ) ) woocommerce_reset_loop();
}
?>
</div>
<?php endif;
wp_reset_postdata();

Archive.php isn't sorting posts from one single category

My archive page with added numeric pagination won't sort out the right category and instead shows posts from all the categories. Let's say the category is bananas (http://localhost/tkeblog/category/bananas/) and I get post from categories bananas, oranges and apples. Also the pagination system doesn't show posts with thumbail yet it works on my index.php page. What am I doing incorrectly to filter the posts by category?
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
query_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 5
));
if ( have_posts() ) : the_post(); ?>
<div class="blogitem a">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('catalog',get_post_format()); ?>
<?php endwhile; ?>
<div class="pagination">
<?php my_pagination(); ?>
</div>
</div>
<?php else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php wp_reset_query(); ?>
<?php endif; ?>
If we take a look at the default TwentyTwentyOne Wordpress theme archive.php we can see that the archive template is just using a default loop to display all posts from all categories, not a custom query.
I believe this answers your question.
<?php
if( have_posts() ):
while( have_posts() ): the_post();
// ... template
endwhile;
else:
// ... fallback
endif; ?>
If you want to customize the default query output from the archive.php page, the best practice is to do it from your function.php page. You can use the the action hook filter pre_get_posts.
Source # https://developer.wordpress.org/reference/hooks/pre_get_posts/
Fires after the query variable object is created, but before the actual query is run. Be aware of the queries you are changing when using the pre_get_posts action. Make use of conditional tags to target the right query.
<?php
add_action( 'pre_get_posts', function ( $query ) {
if ( ! is_admin() && $query->is_archive() && $query->is_main_query() ) {
if ( get_query_var( 'post_type' ) == 'post' ) {
$query->set( 'post_type', array( 'post' ) );
$query->set( 'posts_per_page', 12 );
$query->set( 'orderby', array( 'date' ) );
$query->set( 'order', array( 'ASC' ) );
} else {
$query->set( 'posts_per_page', 6 );
$query->set( 'orderby', array( 'date' ) );
$query->set( 'order', array( 'ASC' ) );
};
};
}; ?>

Wordpress Custom Post type Category View with Pagination

I have a custom post type 'charters' and within my category.php page I have a loop pulling in 'charter' posts for the current category id. I have a custom pagination setup displaying page numbers when I click page 2 link, it goes to URL /page/2 which displays a 404 error. I can't figure out why I am getting the 404. If I adjust my posts per page the pagination updates and displays the correct number of pages but the links do not show up.
Category.php Code:
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$product_args = array(
'post_type' => 'charters',
'posts_per_page' => 10, //the same as the parse_query filter in our functions.php file
'paged' => $paged,
'page' => $paged,
'cat' => $cat
);
$product_query = new WP_Query( $product_args ); ?>
<?php if ( $product_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $product_query->have_posts() ) : $product_query->the_post(); ?>
<article class="loop">
<h3><?php the_title(); ?></h3>
<div class="content">
<?php the_excerpt(); ?>
</div>
</article>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php
if (function_exists( 'custom_pagination' )) :
custom_pagination( $product_query->max_num_pages,"",$paged );
endif;
?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Functions.php Code:
function prefix_change_cpt_archive_per_page( $query ) {
//* for cpt or any post type main archive
if ( $query->is_main_query() && ! is_admin() && is_post_type_archive( 'product' ) ) {
$query->set( 'posts_per_page', '10' );
}
}
add_action( 'pre_get_posts', 'prefix_change_cpt_archive_per_page' );
function prefix_change_category_cpt_posts_per_page( $query ) {
if ( $query->is_main_query() && ! is_admin() && is_category( 'test-category' ) ) {
$query->set( 'post_type', array( 'product' ) );
$query->set( 'posts_per_page', '2' );
}
}
add_action( 'pre_get_posts', 'prefix_change_category_cpt_posts_per_page' );
function custom_pagination( $numpages = '', $pagerange = '', $paged='' ) {
if (empty($pagerange)) {
$pagerange = 2;
}
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class='custom-pagination'>";
echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
My Permalink Settings are set to "Post Name" I have a feeling this has something to do with rewrites but I can not figure out how to get the page urls to work correctly. An example of my categories are as follows:
/category/long-beach/
/category/san-diego/
you should try this:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'charters',
'cat'=> $cat,
'post_per_page'=> 6,
'paged' => $paged
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p><?php
endwhile;
}
wp_reset_query();
?>
<?php echo pnavigation( $query ); ?>
then you need to add following code to function.php :
function pnavigation( $wp_query ) {
$big = 999999999; // need an unlikely integer
$pages = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_next' => false,
'type' => 'array',
'prev_next' => TRUE,
'prev_text' => '←',
'next_text' => '→',
) );
if( is_array( $pages ) ) {
$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
echo '<ul class="pagination pagination-lg">';
foreach ( $pages as $page ) {
echo "<li>$page</li>";
}
echo '</ul>';
}
}
you need to just replace url (client-testimonials) with your url
function pagination_rewrite() {
add_rewrite_rule('client-testimonials/page/?([0-9]{1,})/?$', 'index.php?pagename=client-testimonials&paged=$matches[1]', 'top');
}
add_action('init', 'pagination_rewrite');
If you are using default pagination then please use below code.
<?php
$current_page = get_queried_object();
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'charters',
'paged' => $paged,
'cat'=> $cat);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p><?php
endwhile;
}
next_posts_link( 'Older Entries', $my_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
wp_reset_query();
?>
Add below code in your function.php
function posts_on_categorypage( $query ) {
if ( $query->is_category()) {
$query->set( 'posts_per_page', '10' );
}
}
add_action( 'pre_get_posts', 'posts_on_categorypage' );
There was a couple of errors in my code. This is what I get for copy/pasting code and not going through it line by line. Thanks everyone who helped out, but I got this to work by changing:
Bad Code:
function prefix_change_category_cpt_posts_per_page( $query ) {
if ( $query->is_main_query() && ! is_admin() && is_category( 'test-category' ) ) {
$query->set( 'post_type', array( 'product' ) );
$query->set( 'posts_per_page', '2' );
}
}
Good Code:
function prefix_change_category_cpt_posts_per_page( $query ) {
if ( $query->is_main_query() && ! is_admin() && is_category( $cat ) ) {
$query->set( 'post_type', array( 'charters' ) );
$query->set( 'posts_per_page', '10' );
}
}

why page 2 not working in wp query

Why http://localhost/wp/?paged=2 and older not working in wp_query?
I have movie and series from "custom post type" more than 15 posts.
It's separated in 2 pages in the front-end, but when I press the older post, the page is still first page.
Here is my index.php loop code:
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query_args = array(
'post_type' => array('post','series','movie'),
'posts_per_page' => 10,
'paged' => $paged
);
$the_query = new WP_Query( $query_args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h1><?php the_title();?></h1>
<?php endwhile; ?>
<div class="col-md-12">
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
</div>
</nav>
<?php } ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<article>
<h1>Sorry...</h1>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</article>
<?php endif; ?>
I've found a solution:
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( $query->is_main_query() && is_home() ) {
$query->set( 'post_type', array( 'post', 'movie', 'series' ) );
}
if ( $query->is_main_query() && is_category() ) {
$query->set( 'post_type', array( 'post', 'movie', 'series' ) );
}
if ( $query->is_main_query() && is_search() ) {
$query->set( 'post_type', array( 'post', 'movie', 'series' ) );
}
if ( $query->is_main_query() && is_tag() ) {
$query->set( 'post_type', array( 'post', 'movie', 'series' ) );
}
return $query;
}
this code isn't stop appearing navigation menu

Categories