I need to insert a pagination to the post instead of the load more button and I don't know how how to do it. I require pagination of Next & Previous or numbers.
Here's our dev work, but I don't know where to start or where I should put this example.
<?php
/*
* Template Name: Blog
* description: >-
Page template without sidebar
*/
get_header(); ?>
<body class=" pageHeader--gray">
<div class="wrapper">
<div class="content">
<div class="pageHeader">
<div class="container animate">
<h1 class="pageHeader__title"><?php the_field('title'); ?>
</h1>
</div>
<div class="pageHeader__background">
<svg xmlns="http://www.w3.org/2000/svg" width="1920" viewBox="0 0 1920 126">
<path fill="#FFF" fill-rule="evenodd" d="M9.09494702e-13,7.95807864e-13 L1920,7.95807864e-13 L1920,22.1174168 C1622.41146,91.3724723 1302.41146,126 960,126 C617.588542,126 297.588542,91.3724723 9.09494702e-13,22.1174168 L9.09494702e-13,7.95807864e-13 Z" transform="rotate(180 960 63)"></path>
</svg>
</div>
<div class="pageHeader__decor animate animate--wrapper">
<div class="pageHeader__decor-1 animate__child">
</div>
<div class="pageHeader__decor-2 animate__child">
</div>
</div>
</div>
<div class="blog">
<div class="container">
<div class="blog__tags animate">
<?php
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
$active = "";
if ( null == #$_GET['category_id'] ) {
$active = "active";
}
echo '<a class="blog__tag '.$active.'" href="'. site_url() .'/blog">All Categories</a>';
foreach( $categories as $category ) {
$active = "";
if ( $category->name == #$_GET['category_id']) {
$active = "active";
}
echo '<a class="blog__tag '.$active.'" href="'. site_url() .'/blog?category_id='.$category->name.'">'.$category->name.'</a>';
}
?>
</div>
<div class="blog__wrapper">
<?php
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1,
'category_name' => #$_GET['category_id']
);
$sticky_query = new WP_Query( $args );
while ( $sticky_query->have_posts() ) : $sticky_query->the_post();
?>
<a class="newsCard newsCard--big animate" href="<?php the_permalink(); ?>">
<?php if (has_post_thumbnail( get_the_ID() ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' ); ?>
<div class="newsCard__image"><img src="<?php echo $image[0]; ?>" alt=""/></div>
<?php endif; ?>
<div class="newsCard__content">
<div class="newsCard__tag">
<?php
$categories="";
foreach((get_the_category()) as $category){
$categories = $category->name.",";
}
echo substr($categories, 0, -1);
?>
</div>
<div class="newsCard__title"><?php the_title(); ?>
</div>
<div class="newsCard__description"><?php the_excerpt(); ?>
</div>
<div class="newsCard__link">Learn more
<div class="newsCard__link-icon">
</div>
</div>
</div>
</a>
<?php
endwhile;
wp_reset_postdata();
?>
<?php
$load = 0;
$total_post =wp_count_posts()->publish;
if (null !== #$_GET['load']){
$load = $_GET['load'];
}
$i = 6 + $load;
?>
<?php
// the query
$wpb_all_query = new WP_Query(array(
'post_type'=>'post',
'post_status'=>'publish',
'posts_per_page'=>$i,
'ignore_sticky_posts' => 1,
'category_name' => #$_GET['category_id']
));
$count = $wpb_all_query->found_posts;
?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<a class="newsCard newsCard animate" href="<?php the_permalink(); ?>">
<?php if (has_post_thumbnail( get_the_ID() ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' ); ?>
<div class="newsCard__image"><img src="<?php echo $image[0]; ?>" alt=""/></div>
<?php endif; ?>
<div class="newsCard__content">
<div class="newsCard__tag">
<?php
$categories="";
foreach((get_the_category()) as $category){
$categories = $category->name.",";
}
echo substr($categories, 0, -1);
?>
</div>
<div class="newsCard__title"><?php the_title(); ?>
</div>
<div class="newsCard__description"><?php the_excerpt(); ?>
</div>
<div class="newsCard__link">Learn more
<div class="newsCard__link-icon">
</div>
</div>
</div>
</a>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
<?php if( null == #$_GET['load'] && $count > 6 ) : ?>
<div class="blog__button animate">
<?php
if ( null == #$_GET['category_id'] ) {
?>
<a class="btn" href="<?php echo site_url(); ?>/blog?load=<?php echo $load + $total_post; ?>"><?php the_field('load_more_button_label'); ?></a>
<?php
} else {
?>
<a class="btn" href="<?php echo site_url(); ?>/blog?category_id=<?php echo $_GET['category_id']; ?>&load=<?php echo $load + $total_post; ?>"><?php the_field('load_more_button_label'); ?></a>
<?php
}
?>
</div>
<?php endif; ?>
</div>
</div>
<div class="achievments">
<div class="container">
<div class="blockTitle blockTitle--center blockTitle--small">
<h2><?php the_field('achievement_title'); ?></h2>
</div>
<div class="rewardsCards2 animate animate--wrapper">
<?php
// the query
$wpb_all_query = new WP_Query(array(
'post_type'=>'achievement_cpt',
'post_status'=>'publish',
'posts_per_page'=>-1,
// 'ignore_sticky_posts' => 1,
// 'meta_key' => 'departament',
// 'meta_value' => 'Information Technology and Security'
));
?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<div class="rewardsCards2__item animate__child"><img src="<?php echo get_field('image', get_the_ID()); ?>" alt=""/>
<div class="rewardsCards2__tooltip">
<div class="rewardsCards2__tooltip-logo"><img src="<?php echo get_field('image', get_the_ID()); ?>" alt=""/>
</div>
<div class="rewardsCards2__tooltip-content">
<div class="rewardsCards2__tooltip-title"><?php echo get_field('title', get_the_ID());?>
</div>
<div class="rewardsCards2__tooltip-description"><?php echo get_field('content', get_the_ID());?>
</div><a class="rewardsCards2__tooltip-link" href="<?php echo get_field('url',get_the_ID());?>">Read More</a>
</div>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
</div>
</div>
<?php get_footer('phone'); ?>
</div>
</body>
<?php get_footer(); ?>
So I'm not going to repeat what's already said in the article, but I hope these help you:
Remove the $load (or $_GET['load']) and $i part:
// Find and remove this:
$load = 0;
$total_post =wp_count_posts()->publish;
if (null !== #$_GET['load']){
$load = $_GET['load'];
}
$i = 2 + $load;
Add paged in the first // the query. We also set a static posts_per_page, although you may make it dynamic later.
// Find and replace this:
// the query
$wpb_all_query = new WP_Query(array(
'post_type'=>'post',
'post_status'=>'publish',
'posts_per_page'=>$i,
'ignore_sticky_posts' => 1,
'category_name' => #$_GET['category_id'],
));
$count = $wpb_all_query->found_posts;
// With this one:
// the query
$wpb_all_query = new WP_Query(array(
'post_type'=>'post',
'post_status'=>'publish',
// Set a static posts_per_page value.
'posts_per_page'=> 6,
'ignore_sticky_posts' => 1,
'category_name' => #$_GET['category_id'],
// Add the "paged" arg.
'paged' => max( get_query_var( 'paged' ), 1 ),
));
// The $count is not needed (anymore).
After that, find and replace this:
<?php if( null == #$_GET['load'] && $count > 6 ) : ?>
<div class="blog__button animate">
<?php
if ( null == #$_GET['category_id'] ) {
?>
<a class="btn" href="<?php echo site_url(); ?>/blog?load=<?php echo $load + $total_post; ?>"><?php the_field('load_more_button_label'); ?></a>
<?php
} else {
?>
<a class="btn" href="<?php echo site_url(); ?>/blog?category_id=<?php echo $_GET['category_id']; ?>&load=<?php echo $load + $total_post; ?>"><?php the_field('load_more_button_label'); ?></a>
<?php
}
?>
</div>
<?php endif; ?>
With one of the following, whichever you prefer (either simple or numerical pagination):
1) Simple previous-and-next Pagination
Here, we are using previous_posts_link() and next_posts_link().
<?php
if ( $wpb_all_query->max_num_pages > 1 ) :
$paged = max( get_query_var( 'paged' ), 1 );
?>
<div class="simple-pagination"><?php
previous_posts_link( '← Older Posts' );
if ( $paged > 1 && $paged < $wpb_all_query->max_num_pages ) {
echo ' <span class="sep">•</span> ';
}
next_posts_link( 'Newer Posts →', $wpb_all_query->max_num_pages );
?></div>
<?php endif; ?>
2) Numerical Pagination
Here, we are using paginate_links() — please check the function reference for more details on the parameters.
<?php
$links = paginate_links( array(
'total' => $wpb_all_query->max_num_pages,
'prev_text' => '← Older Posts',
'next_text' => 'Newer Posts →',
) );
if ( $links ) {
echo "<div class='numeric-pagination'>$links</div>";
}
?>
Two important things to note:
In your new WP_Query() call, the args need to have the paged arg so that WordPress knows the current page number and then retrieves only the posts for that specific page.
With next_posts_link() and paginate_links(), you need to pass the $wpb_all_query->max_num_pages which is the total number of pages for your custom WordPress query (WP_Query).
That's all, and once again, I hope this answer helps and note that I used only basic HTML in my examples above, so just modify it to your liking.
Paste this code in functions.php
<?php
//Custom pagination
function the_pagination() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<ul class="pagination " itemscope itemtype="http://schema.org/SiteNavigationElement/Pagination">' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( ' <li class="page-item">%s</li>
' . "\n", get_previous_posts_link('
<span aria-hidden="true" class="page-link ripple">«</span>
<span class="sr-only">Previous</span>
') );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="page-item active"' : '';
printf( '<li%s class="page-item">%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="page-item active"' : '';
printf( '<li%s class="page-item">%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="page-item active"' : '';
printf( '<li%s class="page-item">%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li class="page-item ">%s</li>
' . "\n", get_next_posts_link('<span aria-hidden="true" class="page-link ripple">»</span>
<span class="sr-only">Next</span>') );
echo '</ul>' . "\n";
}
?>
And use <?php the_pagination(); ?> where you want the pagination
Related
Im trying to add a pagination to my post query.
I tried all the samples i found in the internet to add a pagination without any success :/
This is my query code with the pagination code I tried:
<?php
$count = 0;
// Query Code
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'category_name' => 'Recognitions', 'post_status'=>'publish', 'posts_per_page=3&paged=' . $paged));
if ( $wpb_all_query->have_posts() ) :
while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post();
?>
<?php $linktab = 'http://' . "localhost:8888/Lemon/wordpress/?page_id=158" ."&featuredimage=".get_the_post_thumbnail_url(); ?>
<div class="wraper-tab
<?php
$count++;
$current_image = $_GET["featuredimage"];
if (!isset($current_image) && $count == 1) {
$_GET["featuredimage"] = get_the_post_thumbnail_url();
$current_image = get_the_post_thumbnail_url();
}
if ($current_image == get_the_post_thumbnail_url()) {
echo "active";
}
?>
">
<a class="mitch-button to-load dark" href="<?php echo $linktab ?>" data-i="1" data-title="Lapka">
<article class="tab vertical-center" data-background-l="dark" data-background-p="dark" itemscope itemtype="http://schema.org/CreativeWork">
<header class="page-header caption">
<div>
<h6 class="tab-date"><?php echo get_the_date(); ?></h6>
<h2 class="title" itemprop="name"><?php the_title(); echo $count;?></h2>
</div>
</header>
<div class="media">
<img src="" />
</div>
</article>
</a>
</div>
<?php endwhile; ?>
<?php
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
?>
</div>
<?php
// Pagination code
next_posts_link();
previous_posts_link();
?>
I want to make so that it shows 3 posts per page. thats why I added 3 in the posts_per_page parameter.
I want a numbered list.
Please use this code
<?php $count = 0; $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; $wpb_all_query = new WP_Query(array('post_type'=>'post', 'category_name' => 'Recognitions', 'post_status'=>'publish', 'posts_per_page'=>3, 'paged' => $paged )); $wpb_all_query->query_vars[ 'paged' ] > 1 ? $current = $wpb_all_query->query_vars[ 'paged' ] : $current = 1;
$pagination = array(
'base' => 'page/%#%',
//'format' => '',
'showall' => false,
'end_size' => 1,
'mid_size' => 6,
'total' => $wpb_all_query->max_num_pages,
'current' => $current,
'prev_text' => __('←'),
'next_text' => __('→'),
'type' => 'list'
);
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url = str_replace('&page='.$current, '', $actual_link);
global $wp_rewrite;
if ( $wp_rewrite->using_permalinks() )
$pagination[ 'base' ] = $url. '&page=%#%' ;
if ( $wpb_all_query->have_posts() ) :
while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post();
?>
<?php $linktab = 'http://' . "localhost:8888/Lemon/wordpress/?page_id=158" ."&featuredimage=".get_the_post_thumbnail_url(); ?>
<div class="wraper-tab
<?php
$count++;
$current_image = $_GET["featuredimage"];
if (!isset($current_image) && $count == 1) {
$_GET["featuredimage"] = get_the_post_thumbnail_url();
$current_image = get_the_post_thumbnail_url();
}
if ($current_image == get_the_post_thumbnail_url()) {
echo "active";
}
?>
">
<a class="mitch-button to-load dark" href="<?php echo $linktab ?>" data-i="1" data-title="Lapka">
<article class="tab vertical-center" data-background-l="dark" data-background-p="dark" itemscope itemtype="http://schema.org/CreativeWork">
<header class="page-header caption">
<div>
<h6 class="tab-date"><?php echo get_the_date(); ?></h6>
<h2 class="title" itemprop="name"><?php the_title(); echo $count;?></h2>
</div>
</header>
<div class="media">
<img src="" />
</div>
</article>
</a>
</div>
<?php endwhile; ?>
<?php
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
?>
</div><nav class="woocommerce-pagination"><?php echo paginate_links($pagination); ?></nav>
I've tried to add the <?php next_posts_link('Read more »', 10); ?> but it doesn't work with this loop. How can I solve this case? I want to show 10 posts and read more link for the next 10 posts etc. Thanks.
<div id="posts" class="row">
<?php $loop = new WP_Query( array( 'post_type' => 'post', 'orderby' => 'post_id', 'posts_per_page' => '10', 'order' => 'DESC' ) ); ?>
<?php while( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="item col-sm-4">
<div class="well">
<a href="<?php the_permalink(); ?>"><?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?></a>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<div class="readmore-wrapper">
<a class="readmore" href="<?php the_permalink(); ?>">Read more</a>
</div>
</div></div>
<?php endwhile; wp_reset_query(); ?>
</div>
You need to use the paged variable to tell where you are in the paginated list.
Add this line before your loop: $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : '1'; and then add this to your query args: 'nopaging' => false, 'paged' => $paged,
You can then use previous_posts_link( '« Newer Entries' ); and next_posts_link( 'Older Entries »', $query->max_num_pages ); to get your previous/next links before and after the while/endwhile loop respectively.
BTW, your while loop should ideally be wrapped in an if ( $loop->have_posts() ) statement.
Hope that helps
Try this with pagination.
<?php
$paged = ( isset( $_GET['pg'] ) && intval( $_GET['pg'] ) > 0 )? intval( $_GET['pg'] ) : 1;
query_posts( array( 'post_type' => 'post', 'paged' => $paged, 'posts_per_page' => 10 ) );
?>
<?php if ( have_posts() ) : ?>
<?php while( have_posts() ) : the_post(); ?>
<div id="post-<?php echo $post->ID; ?>" <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<div class="post-excerpt">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div class="pagination">
<?php for ( $i = 1; $i <= $wp_query->max_num_pages; $i ++ ) {
$link = $i == 1 ? remove_query_arg( 'pg' ) : add_query_arg( 'pg', $i );
echo '<a href="' . $link . '"' . ( $i == $paged ? ' class="active"' : '' ) . '>' . $i . '</a>';
} ?>
</div>
<?php endif ?>
<?php else : ?>
<div class="404 not-found">
<h3>Not Found</h3>
<div class="post-excerpt">
<p>Sorry, but there are no more posts here... Please try going back to the main page</p>
</div>
</div>
<?php endif;
// Make sure the default query stays intact
wp_reset_query();
My project need to list all categories in one page with pagination.
Example: I had 100 categories and need to show 10 categories for each page with number pagination below.
How can I do that in Wordpress?
$taxonomy = 'category';
if( !isset($_GET['showall']) ):
$total_terms = wp_count_terms( 'category' );
$pages = ceil($total_terms/$per_page);
// if there's more than one page
if( $pages > 1 ):
echo '<ul>';
for ($pagecount = 1; $pagecount <= $pages; $pagecount++):
echo '<li>'.$pagecount.'</li>';
endfor;
echo '</ul>';
endif;
else:
endif;
It return as domain/category/news/page/number but i click to page number it's return to homepage.
Finally i did it by my self...
Here is the code , so feel free to use if u want.
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$category_per_page = 2;
$page = 1;
$offset = 0;
$taxonomy = 'category';
if (!empty($_GET['page'])){
$offset = ($_GET['page'] - 1) * $category_per_page;
}
$tax_terms = get_terms( $taxonomy, array( 'parent' => $cat_id,'number' => $category_per_page, 'offset' => $offset , 'hide_empty' => false ) );
$totalCategories = get_terms( $taxonomy, array( 'parent' => $cat_id, 'hide_empty' => false ) );
$totalNumber = sizeof($totalCategories);
$totalPagination = round($totalNumber / $category_per_page);
foreach ($tax_terms as $tax_term) :
$args=array('cat' => $tax_term->term_id,'showposts'=>6);
$query=new WP_Query($args);
?>
<div class="col-xs-12">
<div class="heading">
<h3 class="heading-title"><?= $tax_term->name;?></h3>
</div>
<section class="list-items owl-carousel owl-theme owl-loaded owl-drag" id="projects-slide">
<?php while($query->have_posts()):$query->the_post();?>
<div class="item">
<figure class="item-featured-img">
<a href="<?=the_permalink();?>">
<?php if ( has_post_thumbnail() ) :
the_post_thumbnail('thumbnail',array('class' => 'img-responsive center-block'));
else : ?>
<img class="img-responsive center-block" src="<?php bloginfo('template_url');?>/img/no-img.jpg" alt="<?php echo $alt_text; ?>">
<?php endif ?>
</a>
<figcaption class="title-item">
<h2><?=the_title();?></h2>
</figcaption>
</figure>
<div class="excerpt">
<p><?php echo cut_string(get_the_content(),65,'');?></p>
</div>
</div>
<?php endwhile;?>
</section>
</div>
<?php endforeach;?>
<form method="GET">
<?php for ($i=0; $i < $totalPagination ; $i++) { ?>
<button name="page" value="<?= $i+1 ?>"><?= $i+1 ?></button>
<?php } ?>
</form>
We added Yoast SEO in our website and added this for page title
%%title%% %%page%% %%sep%% %%sitename%%
But we have total 7 pages and its showing "page 5 of 10". Dont know why its showing wrong page numbers on page title.
Here is our index.php file
<?php get_header(); ?>
<div class="page-container">
<div class="grid-wrap">
<div class="grid-col col-full bp3-col-one-third display-desk">
<div class="grid-wrap">
<div class="grid-col col-full bp2-col-one-half bp3-col-full">
<div class="mini-menu">
<h3>About Treework</h3>
<?php wp_nav_menu (array ('theme_location' => 'about-menu'));?>
</div>
</div>
<div class="grid-col col-full bp2-col-one-half bp3-col-full">
<?php the_block('Quote'); ?>
<?php get_the_block('Quote'); ?>
</div>
<div class="grid-col col-full bp2-col-one-half bp3-col-full">
<?php $cat_id = 8; //the certain category ID
$latest_cat_post = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($cat_id)));
if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post(); ?>
<a href="<?php echo get_permalink(); ?>"><div class="latest-post">
<p class="latest-date">Latest - <?php echo get_the_date(); ?></p>
<h4><?php the_title(); ?></h4>
<div class="excerpt"><?php the_excerpt(); ?></div>
<p class="readmore">Read more <span class="right-arrow"></span></p>
<div class="clear"></div>
</div></a>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
<?php rewind_posts(); ?>
</div>
</div>
</div>
<div class="grid-col col-full bp3-col-two-thirds">
<div class="grid-wrap">
<div class="grid-col col-full">
<p id="breadcrumbs">
<?php if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('');
} ?>
</p>
</div>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$cat_id = 8; //the certain category ID
$latest_cat_post = new WP_Query(
array('posts_per_page' => 5,
'order' => 'DESC',
'category__in' => array($cat_id),
'paged' => $paged));
if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post(); ?>
<div class="grid-col col-full">
<a href="<?php echo get_permalink(); ?>"><div class="latest-post">
<p class="latest-date"><?php echo get_the_date(); ?></p>
<h2><?php the_title(); ?></h2>
<div class="excerpt"><?php the_excerpt(); ?></div>
<p class="readmore">Read more <span class="right-arrow"></span></p>
<div class="clear"></div>
</div></a>
</div>
<?php endwhile; endif; ?>
<?php
$temp = $wp_query; // since wpbeginner_numeric_posts_nav works with the global $wp_query, temporarily replace it with the current query
$wp_query = $latest_cat_post;
wpbeginner_numeric_posts_nav();
$wp_query = $temp;
?>
<?php wp_reset_postdata(); ?>
<?php rewind_posts(); ?>
</div>
</div>
<div class="grid-col col-full bp3-col-one-third display-mob">
<div class="grid-wrap">
<div class="grid-col col-full bp2-col-one-half bp3-col-full">
<div class="mini-menu">
<h3>About Treework</h3>
<?php wp_nav_menu (array ('theme_location' => 'about-menu'));?>
</div>
</div>
<div class="grid-col col-full bp2-col-one-half bp3-col-full">
<?php the_block('Quote'); ?>
<?php get_the_block('Quote'); ?>
</div>
<div class="grid-col col-full bp2-col-one-half bp3-col-full">
<?php $cat_id = 8; //the certain category ID
$latest_cat_post = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($cat_id)));
if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post(); ?>
<a href="<?php echo get_permalink(); ?>"><div class="latest-post">
<p class="latest-date">Latest - <?php echo get_the_date(); ?></p>
<h4><?php the_title(); ?></h4>
<div class="excerpt"><?php the_excerpt(); ?></div>
<p class="readmore">Read more <span class="right-arrow"></span></p>
<div class="clear"></div>
</div></a>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
<?php rewind_posts(); ?>
</div>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
and for pagination we are using this function
function wpbeginner_numeric_posts_nav() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li></li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>...</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li>%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n";
}
I'm about to finish a wordpress-site with a custom theme and several custom-post-types. Now i wanted to change the permalink settings to %post-name%, to make the url's nicer and this is where the problems start.
I have 3 custom-post-types next to the default post-format: events, galleries, shop. Now when I load the page "events (using the template-events.php, what does a wp_query to get all the events), I get all posts. It basically ignores the defined template and loads the one defined for the posts-page, same happens for the shop-page.
The strange thing is that the gallery page (loading the same kind of template file to do the wp_query) works fine.
When I change back to the default permalink-setting everything works fine...
Thanks for any hint!
here my template-events.php
<?php
/*
Template Name: Events
*/
?>
<?php get_header(); ?>
<?php
//Fix homepage pagination
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$now = strtotime("now");
$args = array(
'post_type' => 'events',
'posts_per_page' => 40,
'post_status' => 'publish',
'meta_key' => '_cmb_date_timestamp',
'orderby' => 'meta_value',
'order' => 'ASC',
'paged' => $paged,
'meta_query' => array(
array(
'key' => '_cmb_date_timestamp',
'value' => $now,
'type' => 'NUMERIC',
'compare' => '>=' )
)
);
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last 30 days
$where .= " AND _cmb_date_timestamp < '" . date('Y-m-d') . "'";
return $where;
}
//add_filter( 'posts_where', 'filter_where' );
$events_query = new WP_Query($args);
if( $events_query->have_posts() ) :
$derLudwig = get_theme_mod( 'eventsHeading', '');
$url = esc_url( get_theme_mod( 'events_image' ) );
echo '<div class="overview-header" style="background-image: url('.$url.');"></div>';
echo '<h2 class="overview">der Ludwig '.$derLudwig.'</h2>';
?>
<div id="filters">
<div id="filterButtons">
<a id="filterOpen" class="filterB" href="#"><img src="<?php echo get_bloginfo("template_url"); ?>/images/sub-navi2.png"/></a>
<a id="filterClose" class="filterB" href="#"><img src="<?php echo get_bloginfo("template_url"); ?>/images/filter-close.png"/></a>
</div>
<?php
/* FILTERS */
$args = array(
'type' => 'events',
'taxonomy' => 'event_category',
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
echo '<div id="categories">';
echo '<h5>Kategorien: </h5>';
echo '<ul>';
foreach ($categories as $category) {
echo '<li>' . $category->name . '</li>';
}
echo '</ul>';
echo '</div>';
?>
Clear
</div>
<!--BEGIN #content -->
<div id="content" class="clearfix">
<?php
echo '<img class="loader" src="'.get_bloginfo('template_url').'/images/loader.gif"/>';
echo '<div id="primary" class="hfeed event-overview">';
while( $events_query->have_posts() ) : $events_query->the_post();
// infos
$time = get_post_meta($post->ID, '_cmb_time', true);
$cdateStamp = get_post_meta($post->ID, '_cmb_date_timestamp');
$monthName = date_i18n('F', $cdateStamp[0]);
$weekdayNumber = date_i18n('j', $cdateStamp[0]);
$weekday = date_i18n('D', $cdateStamp[0]);
$locations = get_the_terms( $post->ID, 'locations' );
$tickets = get_post_meta($post->ID, '_ludwig_events_tickets', true);
$featured = get_post_meta($post->ID, '_ludwig_events_display_featured', true);
$reservation = get_post_meta($post->ID, '_ludwig_events_display_reservation', true);
// grab everything else
$custom_bg = get_post_meta($post->ID, '_zilla_portfolio_display_background', true);
$portfolio_caption = get_post_meta($post->ID, '_zilla_portfolio_caption', true);
?>
<?php
if(!isset($currentMonth) || $currentMonth != $monthName){
?>
<h2 class="<?php echo lcfirst($monthName); ?>"><?php echo $monthName; ?></h2>
<?php
}
$currentMonth = $monthName;
?>
<!--BEGIN .hentry-->
<div id="post-<?php the_ID(); ?>" class="event-small <?php echo lcfirst($monthName); ?>">
<!--BEGIN .entry-content -->
<div class="entry-content">
<div class="eTop">
<span class="dateDay"><?echo $weekdayNumber; ?></span>
<span class="dateMonth"><? _e($monthName, 'ludwig-events'); ?></span>
<span class="dateWeekDay"><? _e($weekday, 'ludwig-events'); ?></span>
</div>
<?php the_post_thumbnail('event-small'); ?>
<div class="eBottom">
<?php the_title('<h3>', '</h3>'); ?>
<span class="eventWo">
<?php
if ( $locations && !is_wp_error( $locations ) ) {
foreach ( $locations as $location ) {
echo $location->name;
}
}
?>
, <?php echo $time; ?>Uhr</span>
<div class="event-buttons">
<div class="inner">
<div class="centerContainer">
<h4>Tickets</h4>
<?php ticketsLink($tickets); ?>
<?php reservationLink($reservation); ?>
</div>
</div>
</div>
</div>
<!--END .entry-content -->
</div>
</div>
<?php endwhile; ?>
<!--END #primary .hfeed-->
</div>
<?php else: ?>
<div id="content" class="nocontent">
<!--BEGIN #post-0-->
<div id="post-0" <?php post_class(); ?>>
<h2 class="entry-title"><?php _e('Keine Events gefunden', 'ludwig') ?></h2>
<!--BEGIN .entry-content-->
<div class="entry-content">
<p><?php _e("Hoppala, das sollte eigentlich nicht passieren", "ludwig") ?></p>
<!--END .entry-content-->
</div>
<!--END #post-0-->
</div>
</div>
<?php endif; ?>
<?php remove_filter( 'posts_where', 'filter_where' ); ?>
<?php get_footer(); ?>
here the working template-gallery.php
<?php
/*
Template Name: Fotos
*/
?>
<?php get_header(); ?>
<?php
//Fix homepage pagination
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
// CHECK THAT THIS ONE ISTN DOWN
/*
$args = array(
'post_type' => 'gallery',
'posts_per_page' => 1,
'post_status' => 'publish',
'orderby' => 'modified',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'gallery_category',
'field' => 'id',
'terms' => array( $term_id ),
'operator' => 'IN'
)
),
'paged' => $paged
);
$sina_query = new WP_Query($args);
$sinaString = '';
if( $posts_query->have_posts() ) :
while( $posts_query->have_posts() ) : $posts_query->the_post();
?>
<?php endwhile; ?>
<?php endif; ?>
*/
$args = array(
'post_type' => 'gallery',
'orderby' => 'modified',
'order' => 'DESC',
'posts_per_page' => 10,
'paged' => $paged
);
$posts_query = new WP_Query($args);
if( $posts_query->have_posts() ) :
$derLudwig = get_theme_mod( 'fotosHeading', '');
$url = esc_url( get_theme_mod( 'fotos_image' ) );
echo '<div class="overview-header" style="background-image: url('.$url.');"></div>';
echo '<h2 class="overview">der Ludwig '.$derLudwig.'</h2>';
?>
<div id="filters">
<div id="filterButtons">
<a id="filterOpen" class="filterB" href="#"><img src="<?php echo get_bloginfo("template_url"); ?>/images/sub-navi2.png"/></a>
<a id="filterClose" class="filterB" href="#"><img src="<?php echo get_bloginfo("template_url"); ?>/images/filter-close.png"/></a>
</div>
<?php
/* FILTERS */
$args = array(
'type' => 'events',
'taxonomy' => 'gallery_category',
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
echo '<div id="categories">';
echo '<h5>Kategorien: </h5>';
echo '<ul>';
foreach ($categories as $category) {
echo '<li>' . $category->name . '</li>';
}
echo '</ul>';
echo '</div>';
?>
Clear
</div>
<!--BEGIN #content -->
<div id="content" class="clearfix">
<?php
echo '<img class="loader" src="'.get_bloginfo('template_url').'/images/loader.gif"/>';
echo '<div id="primary" class="hfeed gallery-overview">';
$counter = 0;
while( $posts_query->have_posts() ) : $posts_query->the_post();
$fotograf = get_post_meta($post->ID, '_ludwig_gallery_fotograf', true);
?>
<?php
if ($counter == 1) {
?>
<div id="post-<?php the_ID(); ?>" class="gallery-grid post-<?php echo $counter++; ?>">
<!--BEGIN .entry-content -->
<div class="entry-content">
<a href="<?php echo get_permalink(); ?>">
<div class="overlayContainer">
<div class="overlay">
<div class="content">
<div class="inner">
<?php
/*
$terms = get_the_terms( $post->ID, 'gallery_category');
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->name;
}
$on_draught = join( ", ", $draught_links );
*/
?>
<p class="galleryCategories">
<span><?php //echo $on_draught; ?></span>
</p>
<?php //endif; ?>
<h3><?php the_title(); ?></h3>
<p class="fotograf">von<br><span><?php echo $fotograf; ?></span></p>
</div>
</div>
</div>
</div>
<?php the_post_thumbnail('gallery-mid'); ?></a>
</div>
</div>
<?php echo $counter++; ?>
<?php
} else {
?>
<div id="post-<?php the_ID(); ?>" class="gallery-grid post-<?php echo $counter++; ?>">
<!--BEGIN .entry-content -->
<div class="entry-content">
<a href="<?php echo get_permalink(); ?>">
<div class="overlayContainer">
<div class="overlay">
<div class="content">
<div class="inner">
<?php
/*
$terms = get_the_terms( $post->ID, 'gallery_category');
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->name;
}
$on_draught = join( ", ", $draught_links );
*/
?>
<p class="galleryCategories">
<span><?php //echo $on_draught; ?></span>
</p>
<?php //endif; ?>
<h3><?php the_title(); ?></h3>
<p class="fotograf">von<br><span><?php echo $fotograf; ?></span></p>
</div>
</div>
</div>
</div>
<?php the_post_thumbnail('gallery-mid'); ?></a>
</div>
</div>
<?php } ?>
<?php endwhile; ?>
<!--END .hfeed -->
</div>
<a class="archivlink" href="#">⌸ Archiv</a>
<div class="navigation">
<div class="next-posts"><?php next_posts_link('« Older Entries', $posts_query->max_num_pages) ?></div>
<div class="prev-posts"><?php previous_posts_link('Newer Entries »', $posts_query->max_num_pages) ?></div>
</div>
<?php else: ?>
<div id="content" class="nocontent">
<!--BEGIN #post-0-->
<div id="post-0" <?php post_class(); ?>>
<h2 class="entry-title"><?php _e('Keine Fotos gefunden', 'ludwig') ?></h2>
<!--BEGIN .entry-content-->
<div class="entry-content">
<p><?php _e("Hoppala, das sollte eigentlich nicht passieren", "ludwig") ?></p>
<!--END .entry-content-->
</div>
<!--END #post-0-->
</div>
</div>
<?php endif; ?>
<?php get_footer(); ?>
Hint from me (Since you do not put a code):
Nothing wrong with the permalink. It should be work. I believe what cause this is your code. Please, double check your code and re-install your WordPress, and try again.