Custom page template pagination not working when permalink structure is changed - php

I created a page template at http://www.durgeshsound.com/gallery/
Here my pagination buttons are not working. this issue arises when permalink format is http://www.testsite.com/sample-post/
But when I set permalink format to default for example http://testsite.com/?p=123 then it starts working
and creates a working pagination link like this http://www.durgeshsound.com/?page_id=81&paged=2.
I want this format http://www.testsite.com/sample-post/ in the links.
here is what i tried for custom page template
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('post_type=gallery&posts_per_page=9&paged=' . $paged); ?>
<?php if (have_posts()) : while (have_posts()) : the_post();
if ( get_post_gallery() ) :
$gallery = get_post_gallery( get_the_ID(), false );
?>
<?php the_post_thumbnail('large'); ?>
<?php the_title( ); ?>
<?php
endif;
endwhile; endif;
?>
<?php kriesi_pagination(); ?>
<?php get_sidebar('gallery'); ?>
<?php get_footer(); ?>
Please help.

Below is custom Page : Replace $cat with your resp. ID
<ul class="clearfix">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$recentPosts = new WP_Query();
$recentPosts->query("cat=$cat&showposts=10&paged=".$paged); ?>
<?php if($recentPosts){ ?>
<?php if ( $recentPosts->have_posts() ): while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<li id="post-<?php the_ID(); ?>" <?php post_class('interview-list'); ?>>
<div class="video-thumb">
<a href="<?php the_permalink(); ?>" rel="bookmark"><?php if(has_post_thumbnail()) {
the_post_thumbnail(array(500,393));
}else {
echo '<img src="'.get_bloginfo("template_url").'/images/no-image.jpg" width="500px" height="393px"/>';
}
?> </a>
</div>
<div class="image-con">
<div class="int-title">
<?php the_title(); ?>
</div>
</div>
</li>
<?php endwhile; ?>
<?php else: ?>
<h2 class="post-title">No Photos</h2>
<?php endif; ?>
<div class="older-link"><?php next_posts_link('« Older Entries', $recentPosts->max_num_pages) ?></div>
<div class="newer-link"><?php previous_posts_link('Newer Entries »') ?></div>
<?php } ?>
</ul>

Related

How do i only display posts with a certain category?

So I'm building a website for my new business.
An I want to only run the following code if the post have a category of 'certain-category'.
How do I do this? I tried a number of things. but they do not work..``
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?>>
<?php if ( has_post_thumbnail() ) : ?>
<div class="entry-thumb">
<a href="<?php the_permalink(); ?>" class="H1-posts" title="<?php the_title(); ?>" >
<?php the_post_thumbnail('moesia-thumb'); ?>
</a>
</div>
<?php endif; ?>
<?php if (has_post_thumbnail()) : ?>
<?php $has_thumb = ""; ?>
<?php else : ?>
<?php $has_thumb = ""; ?>
<?php endif; ?>
<div class="post-content <?php echo $has_thumb; ?>">
<header class="entry-header">
<?php the_title( sprintf( '<h1 class="entry-title">', esc_url( get_permalink() ) ), '</h1>' ); ?>
<?php if ( 'post' == get_post_type() ) : ?>
<?php endif; ?>
</header><!-- .entry-header -->
<div class="entry-summary">
<?php if ( (get_theme_mod('full_content') == 1) && is_home() ) : ?>
<?php the_content(); ?>
<?php else : ?>
<?php the_excerpt(); ?>
<?php endif; ?>
</div><!-- .entry-content -->
Use has_category()
if(has_category('certain-category', get_the_ID())):
// Do something
endif;
WordPress now has a native block which does this for you if you want the easy route the block is called Post and Page Grid it allows you to select what category of posts or pages it will show and you can select what information is shown e.g. Title, thumbnail, excerpt etc

WordPress loop get post not page single

I am trying to get latest posts to display through a template page i am building for pages, the loop is not running the latest post only one page
ok, I have a simple loop that gets latest post
my loop
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile; endif;
?>
and content.php
<div class="blog-post">
<h2 class="blog-post-title">
<?php the_title(); ?>
</h2>
<p class="blog-post-meta">
<?php the_date(); ?>by <?php the_author(); ?>
<a href="<?php comments_link(); ?>">
<?php printf(_nx('One Comment', '%1$s Comments', get_comments_number(), 'comments title', 'textdomain'), number_format_i18n(get_comments_number())); ?>
</a>
</p>
<?php if ( has_post_thumbnail() ) {?>
<div class="row">
<div class="col-md-4">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<div class="col-md-6">
<?php the_excerpt(); ?>
</div>
</div>
<?php } else { ?>
<?php the_excerpt(); ?>
<?php } ?>
</div>
when i run the loop in index.php i get my latest blog post, perfect.
however, i am building a template page, i try and include the loop in this page, i just get one page (not all posts).
my template
<div class="row">
<div class="col-sm-12">
// content bar
<?php get_template_part('advicecentre_bar', get_post_format()) ?>
// cmd driven content
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content_page', get_post_format());
endwhile; endif;
?>
// recent post
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile; endif;
?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
If you are using multiple loops on the same page, you must use rewind_posts() like so:
<div class="row">
<div class="col-sm-12">
// content bar
<?php get_template_part('advicecentre_bar', get_post_format()); ?>
// cmd driven content
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content_page', get_post_format());
endwhile; endif;
?>
<?php rewind_posts(); ?>
// recent post
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile; endif;
?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
This "resets" the loop to it's original state and allows you to look through the posts again. In your original code you scan through all the posts, and then in your second loop scan through nothing, as you have already scanned through all the posts!
Hmm I have found this solution using a for each rather than the while loop seems to work, but im not sure if its the best way around.
<ul>
<?php
$recent_posts = wp_get_recent_posts();
foreach( $recent_posts as $recent ){
echo '<li>' . $recent["post_title"].' </li> ';
}
wp_reset_query();
?>
</ul>
UPDATE
<?php
$args = array('numberposts' => 5);
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $recent) {
$excerpt = wp_trim_excerpt($recent['post_content']);
$permalink = get_permalink($recent["ID"]);
$title = esc_attr($recent["post_title"]);
$thumbnail = get_the_post_thumbnail($recent["ID"], 'thumbnail');
echo '<li><a href="' . $permalink . '" title="Look ' . $title . '" >' . $thumbnail . $title . '</a></li>';
echo $excerpt;
}
?>

Wordpress pagination system

I have a problem with my pagination system (wordpress). It's on my search page (display results), currently, the search system include only 2 post types, and the search system works fine, but clearly impossible to create a pagination system. I just want to create a simple previous and next page nav with the number of the current page.
<div class="contenu cont-menu fixed-cont aff">
<div class="page p-spe">Résults :
<span class="blue"><?php the_search_query(); ?></span></div>
<div class="nombre"><?php global $wp_query; echo ' ' .
$wp_query->found_posts . ' Result(s)'; ?></div>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<article class="article-menu">
<div class="article-menu a-m-sp">
<a href="<?php the_permalink(); ?>">
<span class="rollover r2"> </span>
<?php the_post_thumbnail(array(226,150)); ?>
</a>
<div class="right p-special">
<h2 class="h2-article-menu h2-ma"><a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a></h2>
<p class="date"><?php the_time('j F Y'); ?></p>
<?php the_excerpt(); ?>
Know more
</div>
</div>
<div class="sep sp-s"> </div>
</article>
<?php endwhile; ?>
<nav class="nav-pages">
<?php previous_posts_link('« Previous') ?>
<span class="nombre-pages"><?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
echo $paged.' / '.$wp_query->max_num_pages;
?></span>
<?php next_posts_link('Next »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
<?php else : ?>
<div class="aucun-resultat">
<?php _e( 'No results' ); ?>
</div>
<?php endif; ?>
<?php
get_footer();
?>
So i add the pages nav just after the endwhile of the search results. I don't know why my system doesn't work. I'm a wordpress beginner, thank you for your help. Bernard
You don't need a custom query for this. Just add paginate_links() which generates pagination with arrows and numbers (can be configured to display as you wish).
http://codex.wordpress.org/Function_Reference/paginate_links
I update my post, because I made a pagination for 1 post type and It works fine. But now I would like to do a pagination of 2 post types. How i can change this code to make it works ?
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=4&post_type=dossiers'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>

Wordpress first post only on first page different then others

Got the next code to get the first Wordpress post on only the first page to be different. But it doesn't work. Looked at other questions and answers but there doesn't seem to be a good answer for this.
This is what i've got but isn't working for me:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 5, 'paged' => $paged );
query_posts($args); ?>
<?php if (have_posts()) : ?>
<?php $postcount = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $postcount++; ?>
<?php if ($postcount == 1 && $paged == 1) : // if this is the first post & first page ?>
<div class="large-10">
<?php the_post_thumbnail('large'); ?>
</div>
<?php else : //if this is NOT the first post ?>
<div class="large-6 columns">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="portfolio">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('large'); ?>
<span><h6><?php the_title(); ?></h6></span>
</a>
</div>
</article>
</div>
<?php endwhile; ?>
<?php endif; ?>
I get this syntax error "unexpected T_ENDWHILE" but can't figure out why.
Does anyone know how to properly get this done?
Thanks in advance!
Joeri
It looks like you don't have an endif for that inner if. Perhaps should be:
<?php if ($postcount == 1 && $paged == 1) : // if this is the first post & first page ?>
<div class="large-10">
<?php the_post_thumbnail('large'); ?>
</div>
<?php else : //if this is NOT the first post ?>
<div class="large-6 columns">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="portfolio">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('large'); ?>
<span><h6><?php the_title(); ?></h6></span>
</a>
</div>
</article>
</div>
<?php endif; ?>

How to add default pagination to this code?

I have this code to get the latest 3 posts from "novels" category and display the content inside html tags.
<? $novels = get_option('of_novels') ?>
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts('category_name=$novels&posts_per_page=3'); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="sliderunit">
<?php the_post_thumbnail(); ?>
<div class="novelsslidertitle">
<div class="arrow-left"></div>
<img class="cross" src="<?php bloginfo('stylesheet_directory'); ?>/images/cross.png"/>
<?php the_title(); ?>
<h3>رواية</h3>
</div>
</div>
<?php endwhile;?>
What I need to know is how to add default next post in wordpress to this code?

Categories