WP posts_per_page or showposts? - php

New to PHP. Below is the code to show posts and pagination. I'm trying to get 10 posts per page to show and am confused on what code to write to do this. I tried changing Reading Settings to 10 blog posts, but when I save it, it overwrites back to one. So I figured the setting is being overwritten in php somewhere. I'm looking to overwrite that here. Please help.
I tried adding:
but not only does 10 posts show, so does a second category list below the posts.
<?php get_template_part('templates/page', 'header'); ?>
<?php if (!have_posts()) : ?>
<div class="alert">
<?php _e('Sorry, no results were found.', 'roots'); ?>
</div>
<?php get_search_form(); ?>
<?php endif; ?>
<?php $i = 0; ?>
<?php while (have_posts()) : the_post(); $i++; ?>
<article class="<?php $allClasses = get_post_class(); foreach ($allClasses as $class) { echo $class . " "; } if($i&1) { echo 'odd';} else {echo 'even';}; ?> block clearfix">
<?php get_template_part('templates/content-category', get_post_format()); ?>
</article>
<?php endwhile; ?>
<?php if ($wp_query->max_num_pages > 1) : ?>
<nav class="post-nav">
<ul class="pager">
<li class="previous"><?php next_posts_link(__('← Older posts', 'roots')); ?></li>
<li class="next"><?php previous_posts_link(__('Newer posts →', 'roots')); ?></li>
</ul>
</nav>
<?php endif; ?>

try adding this to your functions.php
function trance_posts_per_page( $query ) {
if (! is_main_query())
return;
$query->set( 'posts_per_page', 20 );
}
add_action( 'pre_get_posts', 'trance_posts_per_page' );
check the plugin WP-PageNavi if this doesn't help you

Got to work by removing this:
<?php $i = 0; ?>
<?php while (have_posts()) : the_post(); $i++; ?>
<article class="<?php $allClasses = get_post_class(); foreach ($allClasses as $class) { echo $class . " "; } if($i&1) { echo 'odd';} else {echo 'even';}; ?> block clearfix">
<?php get_template_part('templates/content-category', get_post_format()); ?>
</article>
<?php endwhile; ?>
and replacing it with this:
<?php query_posts( $query_string . '&posts_per_page=-10' );?>
<?php while (have_posts()) : the_post(); ?>
<article class="block clearfix">
<?php get_template_part('templates/contentcategory',
get_post_format()); ?>
</article>
<br />
<?php endwhile; ?>

Related

Wordpress child page include feutured images

So this is my code it shows all the titles of the child pages but i also want all the feutured images of the child pages. And i have no clue how to accomplish this. I have incldue page.php and my function.php script. I know alot of people allready ask this type of quistion but i can't seem to figure it out.
Page.php
<?php
get_header();
if(have_posts()) :
while (have_posts()) : the_post(); ?>
<article class="post page">
<?php
if ( has_children() OR $post->post_parent > 0 ) { ?>
<nav class="site-nav children-links clearfix">
<span class="parent-link"><?php echo get_the_title(get_top_ancestor_id()); ?> </span>
<ul>
<?php
$args = array(
'child_of' => get_top_ancestor_id(),
'title_li' => ''
);
?>
<?php wp_list_pages($args); ?>
</ul>
</nav>
<?php } ?>
</article>
<?php endwhile;
else :
echo '<p> No content found</p>';
endif;
get_footer();
?>
Function.php
function get_top_ancestor_id() {
global $post;
if ($post->post_parent) {
$ancestors = array_reverse(get_post_ancestors($post->ID));
return $ancestors[0];
}
return $post->ID;
}
// Does page have children?
function has_children() {
global $post;
$pages = get_pages('child_of=' . $post->ID);
return count($pages);
}
So i found the answer finally for people that also strungle:
<?php
$args = array(
'child_of' => get_top_ancestor_id(),
'title_li' => ''
);
?>
<?php $our_pages = get_pages($args); ?>
<?php if (!empty($our_pages)): ?>
<?php foreach ($our_pages as $key => $page_item): ?>
<div class="col-*-* product-object">
<a class="product-article" href="<?php echo esc_url(get_permalink($page_item->ID)); ?>">
<div class="product-image" style="background: url(<?php echo get_the_post_thumbnail_url($page_item->ID,'product-image');?>); ">
<h2 class="product-h2"><?php echo $page_item->post_title ; ?></h2>
</div>
</a>
</div>
<?php endforeach ?>
<?php endif ?>
</article>
</div>
<?php

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;
}
?>

Custom page template pagination not working when permalink structure is changed

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>

Wordpress posts under content

How should look my code in order to display a preview of a categories with main main page?
Now its is
<?php $posts = get_posts ("category=2&orderby=date&numberposts=3"); ?>
<?php if ($posts) : ?>
<?php foreach ($posts as $post) : setup_postdata ($post); ?>
<div>
<?php the_title(); ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
and below this
<?php if(have_posts()): ?>
<?php while(have_posts()): the_post(); ?>
<div class="pageContainer"><?php the_content(); ?></div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?><div>Sorry, posts are not found.</div>
<?php endif; ?>
This leads to ignore second part...
Sorry for my english.
<?php
// Category posts
$posts = get_posts("category=2&orderby=date&numberposts=3");
if ($posts){
foreach ($posts as $article){
echo '<div><a href="'.get_permalink($article->ID) ?>" rel="bookmark">'.
$article->post_title.'</a></div>';
}
}
// Current page content
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<div class="pageContainer">
<?php the_content(); ?>
</div>
<?php endwhile;
else:
echo '<div>Sorry, posts are not found.</div>';
endif;
?>

how to display odd number post in left side even number post in right side?

<?php $counter = 3; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( in_category('3') ): ?>
<?php else: ?>
<?php endif; ?>
<?php if($counter%2 == 0){echo 'floatRight';} else { echo 'floatLeft'; } ?>
<?php the_ID(); ?>
<h1 > <?php the_title(); ?></h1>
<?php the_post_thumbnail('full'); ?>
<?php the_content(__('(more...)')); ?>
<?php comments_template(); // Get wp-comments.php template ?>
<?php if($counter%2 == 0){ echo "<div class='clear'></div>";} ?>
<?php $counter++; ?>
<?php endwhile; else: ?>
<?php endif; ?>
i am trying to display in this way and i want to display particular category of post
post1
post2
post3
post4
please give me the solution ...
use the below code.
<?php while(have_posts()) : ?>
<?php $i++; if(($i % 2) == 0) : $wp_query->next_post(); else : the_post(); ?>
<?php the_content(); ?>
<?php endif; endwhile; ?>
<?php $i = 0; rewind_posts(); ?>
<?php while(have_posts()) : ?>
<?php $i++; if(($i % 2) !== 0) : $wp_query->next_post(); else : the_post(); ?>
<?php the_content(); ?>
<?php endif; endwhile; ?>
You can use this css3 property to do it (This would take care odd and even elements, you don't have to write down the loop explicitly):
p:nth-child(odd) //you can do the same for div
{
float:left;
}
p:nth-child(even)
{
float:right;
}
You can use query_post() or Wp_query() in wordpress .
Use this code. to get the particular categories post .
<?php
// The Query
query_posts( 'cat=3' );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>

Categories