I am trying to redirect the user to the first page of the pagination if the pagination is empty.
I have three bootstrap tabs (Both, Blog, News). The blog tab contains 5 posts. The news tab contains more than 12 posts. I am displaying 6 posts per page. The issue here is that if I am on the second page of News tab and click the Blog tab, the second page of the blog tab is opening (which does not exist).
For better Understanding Here is my code.
<ul class="nav nav-tabs" id="myTab">
<li class="nav-item">
Section A
</li>
<li class="nav-item">
Section B
</li>
<li class="nav-item">
Section C
</li>
</ul>
<div class="tab-content" id="myTabContent">
<div id="sectionA" class="tab-pane fade show active">
<?php
if(1 != $paged) {
}
?>
<section class="vendor-section-2 mt-5">
<div class="container">
<div class="row">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$the_query = new WP_Query(
array(
'posts_per_page' => 6,
'post_type' => 'post',
'paged' => $paged
)
);
$page_amount = $the_query->max_num_pages;
?>
<div class="container pagination-count">
<div class="row">
<div class="col-6">
<p>Page <span><?php echo $paged ?></span> of <span><?php echo $page_amount ?></span> </p>
</div>
</div>
</div>
<?php
if( $the_query->have_posts() ) :
while( $the_query->have_posts() ): $the_query->the_post();
$post_id = get_the_ID();
?>
<div class="col-md-4 mt-5">
<div class="card">
<img src="<?php the_post_thumbnail_url() ?>" class="card-img-top" alt="...">
<div class="card-body">
<h2 class="card-title"><?php the_title() ?></h2>
<p class="card-text"><?php the_excerpt(); ?></p>
<a href="<?php the_permalink() ?>" class="read-more" >
<svg xmlns="http://www.w3.org/2000/svg" width="24.111" height="23.646" viewBox="0 0 24.111 23.646" style="margin-right: 10px; margin-top: -5px;">
<path
id="Path_84"
data-name="Path 84"
d="M9.544.241a3.061,3.061,0,0,1,3.348.642c1.4,1.427,4.322,1.638,5.638,2.976,1.49,1.518,1.358,4.151,2.856,5.584a7.9,7.9,0,0,1,2.163,8.913,7.942,7.942,0,0,1-3.789,4.323c-6.319,2.6-14.617-.268-18.3-5.889-1.8-2.745-2.12-6.848.122-9.133C3.947,5.245,5.608,1.887,9.544.241Z"
fill="#f3943f"
/>
</svg>
Read More
</a>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;?>
<div class="next-prev-btn">
<!-- then the pagination links -->
<?php next_posts_link( 'Next →', $the_query ->max_num_pages); ?>
<?php previous_posts_link( 'Previous ← ' ); wp_reset_query();?>
</div>
</div>
</div>
</section>
</div>
<div id="sectionB" class="tab-pane fade">
<section class="vendor-section-2 mt-5">
<div class="container">
<div class="row">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$the_query = new WP_Query(
array(
'posts_per_page' => 6,
'post_type' => 'post',
'category_name' => 'blog',
'paged' => $paged
)
);
$page_amount = $the_query->max_num_pages;
?>
<div class="container pagination-count">
<div class="row">
<div class="col-6">
<p>Page <span><?php echo $paged ?></span> of <span><?php echo $page_amount ?></span> </p>
</div>
</div>
</div>
<?php
if( $the_query->have_posts() ) :
while( $the_query->have_posts() ): $the_query->the_post();
$post_id = get_the_ID();
?>
<div class="col-md-4 mt-5">
<div class="card">
<img src="<?php the_post_thumbnail_url() ?>" class="card-img-top" alt="...">
<div class="card-body">
<h2 class="card-title"><?php the_title() ?></h2>
<p class="card-text"><?php the_excerpt(); ?></p>
<a href="<?php the_permalink() ?>" class="read-more" >
<svg xmlns="http://www.w3.org/2000/svg" width="24.111" height="23.646" viewBox="0 0 24.111 23.646" style="margin-right: 10px; margin-top: -5px;">
<path
id="Path_84"
data-name="Path 84"
d="M9.544.241a3.061,3.061,0,0,1,3.348.642c1.4,1.427,4.322,1.638,5.638,2.976,1.49,1.518,1.358,4.151,2.856,5.584a7.9,7.9,0,0,1,2.163,8.913,7.942,7.942,0,0,1-3.789,4.323c-6.319,2.6-14.617-.268-18.3-5.889-1.8-2.745-2.12-6.848.122-9.133C3.947,5.245,5.608,1.887,9.544.241Z"
fill="#f3943f"
/>
</svg>
Read More
</a>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;?>
<div class="next-prev-btn">
<!-- then the pagination links -->
<?php next_posts_link( 'Next →', $the_query ->max_num_pages); ?>
<?php previous_posts_link( 'Previous ← ' ); wp_reset_query();?>
</div>
</div>
</div>
</section>
</div>
<div id="sectionC" class="tab-pane fade">
<section class="vendor-section-2 mt-5">
<div class="container">
<div class="row">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$the_query = new WP_Query(
array(
'posts_per_page' => 6,
'post_type' => 'post',
'category_name' => 'news',
'paged' => $paged
)
);
$page_amount = $the_query->max_num_pages;
?>
<div class="container pagination-count">
<div class="row">
<div class="col-6">
<p>Page <span><?php echo $paged ?></span> of <span><?php echo $page_amount ?></span> </p>
</div>
</div>
</div>
<?php
if( $the_query->have_posts() ) :
while( $the_query->have_posts() ): $the_query->the_post();
$post_id = get_the_ID();
?>
<div class="col-md-4 mt-5">
<div class="card">
<img src="<?php the_post_thumbnail_url() ?>" class="card-img-top" alt="...">
<div class="card-body">
<h2 class="card-title"><?php the_title() ?></h2>
<p class="card-text"><?php the_excerpt(); ?></p>
<a href="<?php the_permalink() ?>" class="read-more" >
<svg xmlns="http://www.w3.org/2000/svg" width="24.111" height="23.646" viewBox="0 0 24.111 23.646" style="margin-right: 10px; margin-top: -5px;">
<path
id="Path_84"
data-name="Path 84"
d="M9.544.241a3.061,3.061,0,0,1,3.348.642c1.4,1.427,4.322,1.638,5.638,2.976,1.49,1.518,1.358,4.151,2.856,5.584a7.9,7.9,0,0,1,2.163,8.913,7.942,7.942,0,0,1-3.789,4.323c-6.319,2.6-14.617-.268-18.3-5.889-1.8-2.745-2.12-6.848.122-9.133C3.947,5.245,5.608,1.887,9.544.241Z"
fill="#f3943f"
/>
</svg>
Read More
</a>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;?>
<div class="next-prev-btn">
<!-- then the pagination links -->
<?php next_posts_link( 'Next →', $the_query ->max_num_pages); ?>
<?php previous_posts_link( 'Previous ← ' ); wp_reset_query();?>
</div>
</div>
</div>
</section>
</div>
</div>
<script>
$(document).ready(function(){
$('a[data-toggle="tab"]').on('show.bs.tab', function(e) {
localStorage.setItem('activeTab', $(e.target).attr('href'));
});
var activeTab = localStorage.getItem('activeTab');
if(activeTab){
$('#myTab a[href="' + activeTab + '"]').tab('show');
}
});
</script>
I have a problem with a custom wordpress template.
I've made a categories page. Works fine on the firs view.
But when I make click on 2nd page for older posts, the browser shows me a 404 error.
I think my query needs something more, but I don't know what is or if this is the problem.
Can somebody help, please?
This is my code:
<?php
/**
* The template for displaying Category pages
*/
?>
<?php get_header(); ?>
<?php get_template_part( 'partials/linea-content', 'page' ); ?>
<div class="index categories">
<div class="col-xs-12 pre-content">
<div class="container">
<?php //Para añadir contenido se interesa ?>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-xs-12 categories-title">
<h2>Categoria: <span><?php echo get_category(get_query_var('cat'))->name; ?></span></h2>
</div>
<div id="posts" class="col-xs-12 col-sm-9">
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// Cambiamos los argumentos para buscar desde la última
$args = array(
'posts_per_page' => 2,
'orderby' => 'date',
'order' => ASC,
'category__in' => get_category(get_query_var('cat'))->cat_ID,
'paged' => $paged
);
// '&orderby=date&order=ASC&category=' . get_category(get_query_var('cat'))->cat_ID;
// volvemos a crear la consulta principal
$aux = query_posts( $args );
$cont_items = 1;
?>
<?php if ( have_posts() ) : ?>
<div class="row items-row">
<div class="col-xs-12">
<?php while ( have_posts() ) : the_post(); ?>
<!-- post -->
<div id="post-id-<?php echo $post->ID; ?>" class="item col-xs-12 col-sm-6">
<div class="row">
<div class="col-xs-12 text-center item-content">
<span class="meta-category">
<span class="meta-category-inner">
<?php
$cat = get_the_category($post->ID);
?>
<?php echo $cat[0]->name; ?>
</span>
</span>
<?php
global $wpdb;
$ppbv_tablename = $wpdb->prefix . 'popular_by_views';
$currentRow = $wpdb->get_row("SELECT * FROM {$ppbv_tablename} WHERE post_id = {$post->ID}");
$curView = 0;
if(isset($currentRow))
{
$curView = $currentRow->views;
}
?>
<div class="item-title-content">
<a href="<?php the_permalink(); ?>" title="Popsicase">
<h2 class="item-title col-xs-10 col-xs-offset-1"><?php the_title(); ?></h2>
</a>
</div>
<p class="date-cat">
<small class="col-xs-12">
<i class="fa fa-link"></i> <strong><?php the_author(); ?></strong> | <span><i class="fa fa-calendar"></i> <?php echo get_the_date();?> | <i class="fa fa-eye"></i> <?php echo $curView; ?></span>
</small>
</p>
<?php
$img_url = get_template_directory_uri() . '/assets/img/podcast.jpg';
if (get_the_post_thumbnail())
{
$img_url = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
}
?>
<a href="<?php the_permalink(); ?>" title="Popsicase">
<div class="item-thumbnail" style="background:url(<?php echo $img_url; ?>) no-repeat center center;">
</div>
</a>
<div class="row">
<div class="col-xs-6 text-right">
<?php /*<span class="comment"> Comentarios: <?php comments_number( '0','1','%'); ?></span>*/ ?>
</div>
</div>
<div class="item-excerpt text-left">
<div class="col-xs-12">
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
</div>
<?php if ($cont_items % 2 == 0) : ?>
</div>
</div>
<div class="row items-row">
<div class="col-xs-12">
<?php
endif;
$cont_items++;
?>
<?php endwhile; ?>
</div>
</div>
<!-- End of the main loop -->
<!-- Add the pagination functions here. -->
<?php
the_posts_pagination( array(
'mid_size' => 4,
'prev_text' => __( 'Artículos antiguos', 'textdomain' ),
'next_text' => __( 'Artículos nuevos', 'textdomain' ),
) );
?>
<?php /*
<div class="nav-previous alignleft"><?php next_posts_link( 'Artículos antiguos' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Artículos nuevos' ); ?></div>
*/ ?>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
<div class="hidden-xs col-sm-3 sidebar">
<div class="row">
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('main-sidebar')) : ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<div class="col-xs-12 pos-content">
<div class="container">
<?php //Para añadir contenido se interesa ?>
</div>
</div>
<div class="clearfix"></div>
</div>
I want to show 5 posts, first one is bigger and has a other class around itself.
I've found a few other posts which are basically the same, but none of them shows how to 'wrap' the first post and 'wrap' all the other posts together.
I've made an example with html/css how it should look.
Hope someone can help me with this. :)
<?php
$args = array(
'post_type' => 'services',
'posts_per_page' => 5
);
$loop = new WP_Query($args);
while ($loop->have_posts()) :
$loop->the_post();
if ($loop->current_post == 0) {
echo '<div class="col-md-6">';
the_post_thumbnail();
the_title();
echo '</div>';
}
else {
echo '<div class="col-md-6">';
the_post_thumbnail();
the_title();
echo '</div>';
}
endwhile; wp_reset_postdata(); ?>
.col-md-6 {
width: 50%;
padding: 0 !important;
float: left;
}
img {
width: 100%;
height: auto;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<section>
<div class="col-md-6">
<img src="http://placehold.it/100x100" alt="">
</div>
<div class="col-md-6">
<div class="col-md-6"><img src="http://placehold.it/100x100" alt=""></div>
<div class="col-md-6"><img src="http://placehold.it/100x100" alt=""></div>
<div class="col-md-6"><img src="http://placehold.it/100x100" alt=""></div>
<div class="col-md-6"><img src="http://placehold.it/100x100" alt=""></div>
</div>
</section>
<?php
$args = array(
'post_type' => 'services',
'posts_per_page' => 5
);
$loop = new WP_Query($args);
while ($loop->have_posts()) :
$loop->the_post();
echo '<div class="some-div">';
if ($loop->current_post == 0) {
echo '<div class="col-md-6">';
the_post_thumbnail();
the_title();
echo '</div>';
echo '</div>'; // close .some-div
echo '<div class="other-div">';
}
else {
echo '<div class="col-md-6">';
the_post_thumbnail();
the_title();
echo '</div>';
}
echo '</div>'; // close .other-div
endwhile; wp_reset_postdata(); ?>
I've fixed this the way I want.
Here's the code I've uesd if someone's looking for something like this.
<section class="no-gutter">
<?php
$args = array(
'post_type' => 'services',
'posts_per_page' => 5
);
$loop = new WP_Query($args);
while ($loop->have_posts()) :
$loop->the_post(); ?>
<?php if ($loop->current_post == 0) : ?>
<!-- large -->
<div class="col-md-6 large">
<a href="<?php the_permalink(); ?>" title="<?php the_title();?>">
<div class="wrapper">
<?php the_post_thumbnail(); ?>
<span><?php the_title(); ?></span>
</div>
</a>
</div><!-- end large -->
<!-- small -->
<div class="col-md-6">
<?php else : ?>
<div class="col-md-6">
<a href="<?php the_permalink(); ?>" title="<?php the_title();?>">
<div class="wrapper">
<?php the_post_thumbnail(); ?>
<span><?php the_title(); ?></span>
</div>
</a>
</div>
<?php endif; ?>
<?php endwhile; wp_reset_postdata(); ?>
</div><!-- end small -->
I'm working on a Wordpress site. I'm trying to make a category with the id 968 show up on the Wordpress site under the stories and successes div. However for some reason the stories and successes div is not having the content from the category inserted into it. Any information on how I can improve my query or PHP is much appreciated. If I need to update this post with more information please let me know. Below you will see the php template file I am using.
<?php
/**
* Template name: Random Template
*
*
*/
// Custom styles
wp_enqueue_style('stories-styles', get_bloginfo('stylesheet_directory') . '/assets/css/stories.css');
// TEMP -- Get the category from the slug
the_post();
$categorySlugs = array(
'religious-freedom' => 'religious-freedom',
'social-and-economic-justice' => 'social-and-economic-justice',
'civil-and-human-rights' => 'civil-and-human-rights'
);
// Get category
$categorySlugs = $categorySlugs[$post->post_name];
$storyQuery = new WP_Query(array(
'post_type' => array('stories', 'press-releases'),
'category_name' => $categorySlugs,
'posts_per_page' => 4,
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
));
$successesQuery = new WP_Query(array(
'post_type' => 'stories',
'cat' => 968,
'posts_per_page' => 1,
'offset' => 5
));
// Remove yarpp
remove_filter('the_content', array($yarpp, 'the_content'), 1200);
?>
<?php get_header(); ?>
<div class="category-title">
<div class="container">
<h2><?php the_title() ?></h2>
</div>
</div>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<div class="container">
<div class="row">
<div class="col-md-12">
<?php if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?>
<article class="featured-issue-article">
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail('full', array( 'class' => "img-responsive attachment-post-thumbnail")); ?>
</div>
<?php endif; ?>
<div class="content">
<?php the_content() ?>
</div>
</article>
</div>
</div>
<hr class="large blue" />
<div class="row">
<div class="col-md-8">
<ul class="section-list">
<?php while ($storyQuery->have_posts()) : $storyQuery->the_post(); ?>
<li>
<h3><?php the_title() ?></h3>
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail('post-thumbnail', array( 'class' => "img-responsive attachment-post-thumbnail")); ?>
<?php endif; ?>
<?php if( get_field('custom_excerpt', $values_id)): ?>
<p><?php echo the_field('custom_excerpt'); ?></p>
<?php endif; ?>
<a class="more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">Read More...</a>
</li>
<?php endwhile; ?>
</ul>
<?php
set_query_var('category_slug', $categorySlug);
get_template_part('issues-nav', 'bottom')
?>
<hr class="small blue" />
<div class="success-stories">
<h2>Stories & Successes</h2>
<ul>
<?php while ($successesQuery->have_posts()) : $successesQuery->the_post(); ?>
<li>
<h4>
<?php the_title() ?>
</h4>
<?= strip_tags($post->post_excerpt) ?>
<a class="more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">Read More...</a>
</li>
<?php endwhile; ?>
</ul>
</div>
</div>
<div class="col-md-4">
<?php get_sidebar(); ?>
</div>
</div>
</div>
</div>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
I'm trying to paginate a current category. But when I go to the next page, wordpress not show only all post of the current category, wordpress show all post of all categories. Somebody can help me?
Category.php
<!-- loop -->
<?php $cat_ID = get_query_var('cat'); ?>
<?php query_posts('&cat=<' . $cat_ID . '&showposts=6'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="col-sm-12 col-md-6 col-xs-12 col-lg-4">
<div class="thumbnail">
<article>
<?php the_post_thumbnail('photo-thumbnail') ?>
<div class="caption">
<h3><?php the_title(); ?></h3>
<!-- añade custom field autor del relato -->
</div>
</article>
</div>
<div class="thumbnailFooter">
<div class="pull-left" style="margin-bottom: 4px;">
<?php echo get_avatar(get_the_author_meta('ID'), 32); ?>
</div>
<div class="smallFont pull-left" style="margin-left: 5px; width: 270px;">
<span class="blue bold"><?php the_author(); ?></span>
</div>
<div class="verySmallFont pull-left" style="margin-left: 5px; margin-top: -3px;">
en <?php the_category(none); ?> el <?php the_date(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php my_pagination(); ?>
<?php else : ?>
<?php endif; ?>
<!-- / fin del loop -->
Function.php
<?php
if (!function_exists('my_pagination')) :
function my_pagination() {
global $wp_query;
$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' => $wp_query->max_num_pages,
'prev_text' => __('« Anterior'),
'next_text' => __('Siguiente »'),
)
);
}
endif;
EDIT::::::::::
After do suggested changes (but don't works). Wordpress not limit 5 post per page and the paginator not appear.
<!-- loop de post -->
<?php
$project_category = wp_get_post_categories($post->ID); ?>
<?php $query = new WP_Query(array('posts_per_page' => 5)); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="col-sm-12 col-md-6 col-xs-12 col-lg-4">
<div class="thumbnail">
<article>
<?php the_post_thumbnail('photo-thumbnail') ?>
<div class="caption">
<a href="<?php the_permalink(); ?>">
<h3 style="font-weight:bold; margin-top: 0px; line-height: 1.3;"><?php the_title(); ?></h3>
</a>
<!-- añade custom field autor del relato -->
</div>
</article>
</div>
<div class="thumbnailFooter">
<div class="pull-left" style="margin-bottom: 4px;">
<?php echo get_avatar(get_the_author_meta('ID'), 32); ?>
</div>
<div class="smallFont pull-left" style="margin-left: 5px; width: 270px;">
<span class="blue bold"><?php the_author(); ?></span>
</div>
<div class="verySmallFont pull-left" style="margin-left: 5px; margin-top: -3px;">
en <span class="bold gray"><?php the_category(none); ?></span>
el <?php the_date(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<div style="text-align: right; padding-right: 12px;">
<?php my_pagination(); ?>
</div>
<?php else : ?>
<?php endif; ?>
<!-- / fin del loop -->
You seem to have a left angle bracket < inside your query_posts function. Try removing that.