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; ?>
Related
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();
?>
I'm trying to use offset on a specific WP_Query on a page that has a total of 3 Loops with the code described here.
For now, this works but I can only target all three loops. What (I think) I have to do is change this part:
//Before anything else, make sure this is the right query...
if ( ! $query->is_posts_page ) {
return;
}
To something like this:
//Before anything else, make sure this is the right query...
if ( ! $query->$THELOOPIWANTTOTARGET ) {
return;
}
Is there a way to do this?
The query:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$mainloop = new WP_Query(array('posts_per_page' => 3, 'paged' => $paged));
?>
<div class="full padding center isotope">
<?php if ( $mainloop->have_posts() ) : while ( $mainloop->have_posts() ) : $mainloop->the_post(); ?>
<article class="post">
<?php the_post_thumbnail(); ?>
<div class="textwrap">
<h2><?php the_title(); ?></h2>
<?php if ( get_field('bron')): ?>
<?php if ( get_field('bron')): ?><a href="<?php the_field('bron_link'); ?>" target="_blank" class="source"><?php endif; ?>
<?php the_field('bron'); ?>
<?php if ( get_field('bron')): ?></a><?php endif; ?>
<?php endif; ?>
<?php the_excerpt(); ?>
</div>
<div class="readmore">
<div class="fa fa-share share" id="<?php the_ID(); ?>">
<div class="share_block"><div class="addthis_sharing_toolbox" data-url="<?php the_permalink(); ?>"></div></div>
</div>
Lees verder
</div>
</article>
<?php endwhile; ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
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>
Thanks to some answers on here, I've managed to distinguish my posts into the latest post and all the rest. However, it needs to be the oldest post. Here is my current loop:
<?php if (have_posts()) : ?>
<?php $post = $posts[0]; $c=0;?>
<?php while (have_posts()) : the_post(); ?>
<!-- first post -->
<?php $c++;
if( $c == 1) :?>
<div class="container">
<div class="inner_box">
<ul>
<div class="title">
<a href="<?php the_permalink();?>">
<?php the_title(); ?>
</div>
<?php the_content(); ?>
</a>
</ul>
<div class="down">a</div>
</div>
</div>
<?php else :?>
<!-- second post -->
<div class="container">
<div class="inner_box">
<ul>
<div class="title">
<a href="<?php the_permalink();?>">
<?php the_title(); ?>
</div>
<?php the_content(); ?>
</a>
</ul>
<div class="up">b</div>
</div>
</div>
<?php endif; ?>`
I saw somewhere that you can use a while loop to target the last post from a post.length. However, I am unsure how to implement this.
Yes, you're right. Use count.
Suppose the total posts is 5 for $total_posts = count($posts);.
You'll have to check your $counter for total - 1 as the array is $posts[0], $posts[1], $posts[2], $posts[3], $posts[4].
<?php
if ( have_posts() ) :
$counter = 0;
$total_posts = count($posts) - 1;
while ( have_posts() ) :
the_post();
if( $counter == $total_posts ) {
?>
<div class="container"><?php // CUSTOM CODE FOR LAST ARTICLE ?></div>
<?php
} else {
?>
<div class="container"><?php // CUSTOM CODE FOR THE REST OF ARTICLES ?></div>
<?php
}
$counter++;
endwhile;
endif;
Note that you only need to open <?php and close ?> when changing from PHP to HTML, using them at every line is very confusing.
I'm making an online magazine. Right now I'm trying to show the posts I'm posting. I've managed to do that, but what I want is that if a post is categorized in "Streetstyle", the loop will be completely different then if it's categorized in "Photography". I've mangaged to do this with one category, how can I do the same with other categories, and style it another way? I tried using the same code as <?php if (is_category( 'Streetstyle' ) || in_category( 'Streetstyle' ) ) { ?>, but then the theme just gets bugged and the posts shows up twice. Do you know of a better way to do this?
This is my code:
<?php query_posts('posts_per_page=9' . '&orderby=date');
while ( have_posts() ) : the_post(); ?>
<?php if (is_category( 'Streetstyle' ) || in_category( 'Streetstyle' ) ) { ?>
<div <?php post_class('pin streetstyle'); ?>>
<a href="<?php the_permalink(); ?>">
<div class="heading">
<h1>Fashion</h1>
</div>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<div class="heading">
<h1><?php the_title(); ?></h1>
</div>
</a>
</div>
<?php } else { ?>
<div <?php post_class('pin'); ?>>
<h1>
<?php the_title(); ?>
</h1>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
the_content('Les mer <br><br>'); ?>
</div>
<?php } ?>
<?php endwhile;
// Reset Query
wp_reset_query(); ?>
If you have a lot of categories I would recommend using switch, it will save you time and your code will look a lot cleaner.
http://php.net/manual/en/control-structures.switch.php
I've slimmed this down a bit, but this is probably more what you're after
<?php query_posts('posts_per_page=9' . '&orderby=date');
while(have_posts()) : the_post(); ?>
<?php if(is_category('Streetstyle') || in_category('Streetstyle')) : ?>
<div <?php post_class('pin streetstyle'); ?>>
<a href="<?php the_permalink(); ?>">
<div class="heading">
<h1><?php the_title(); ?></h1>
</div>
</a>
</div>
<?php if(has_post_thumbnail()) the_post_thumbnail(); ?>
<?php else : ?>
<div <?php post_class('pin'); ?>>
<h1><?php the_title(); ?></h1>
<?php
if(has_post_thumbnail()) the_post_thumbnail();
the_content('Les mer <br><br>');
?>
</div>
<?php endif; ?>
<?php endwhile; wp_reset_query(); ?>