Pagination not working in the main index.php - php

So my link to show the older posts is not displaying on my main blog page http://surechamp.com/blog/ it's displaying fine in my archive.php so I'm not sure why it's not working with the index.php. I'm using underscores as my base theme to work off of. This is my code:
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
?>
<?php endwhile; ?>
<?php _s_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>

I typically run paging navigation outside the loop and test if there will be a previous or next link shown:
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<?php if($wp_query->get('paged') || $wp_query->get('paged') > 1): ?>
<a class="loadPrev" href="<?php previous_posts(); ?>">prev</a>
<?php endif; ?>
<?php if ($next_url = next_posts($wp_query->max_num_pages, false)): ?>
<a class="loadNext" href="<?php echo $next_url; ?>">next</a>
<?php endif;?>
<?php endif; ?>

Related

Set index.php file to only show posts from one category

Here is a copy of my index.php file, which i gather governs what is shown on the posts page. Im wanting the posts page to only show the posts from my "blog" category. Can someone help with the code changes that i need to make, to implement this?
Thanks heaps guys
Erin
<?php
$archive_page_layout = esc_attr(of_get_option('archive_page_layout'));
?>
<main id="main" class="site-main clearfix <?php echo esc_attr($archive_page_layout); ?>">
<?php if($archive_page_layout == 'both-sidebar'): ?>
<div id="primary-wrap" class="clearfix">
<?php endif; ?>
<div id="primary" class="content-area">
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content' , 'blog' );
?>
<?php endwhile; ?>
<?php the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div><!-- #primary -->
<?php
if($archive_page_layout == 'both-sidebar' || $archive_page_layout == 'left-sidebar'):
get_sidebar('left');
endif;
?>
<?php if($archive_page_layout == 'both-sidebar'): ?>
</div>
<?php endif; ?>
<?php
if($archive_page_layout == 'both-sidebar' || $archive_page_layout == 'right-sidebar'):
get_sidebar('right');
endif;
?>
</main><!-- #main -->
<?php get_footer(); ?>
you have to use argument parameters to specify a posts categorie/s please refer to
https://wordpress.stackexchange.com/questions/145125/display-content-from-a-specific-category for the code you will need.
:)

Wordpress loop not looping over posts

I am working on a Wordpress theme and for some reason even though I have posts it is showing that I do not have any posts.
<?php if(have_posts()){ ?>
<?php if ( is_home() && ! is_front_page() ) { ?>
<header>
<h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
</header>
<?php while ( have_posts() ){ the_post(); ?>
<?php
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part('template-parts/content', get_post_format()); ?>
<?php } ?>
<?php the_posts_navigation(); ?>
<?php }else{ ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php } ?>
I'm having issues with debugging it since it shows their are posts.
I checked your code and it seems like you made a mistake in templates paths, they should be "template-parts/post/content" - you lost "post/" directory.

Manually show specific posts on homepage

To loop and show all WordPress posts we use:
<?php
if( have_posts() ) :
while( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endwhile;
endif;
?>
And to show only the first recent post we use:
<?php
if( have_posts() ) :
if( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endif;
endif;
?>
My question is, can I manually select what posts I want to show on my homepage without doing a loop? For example let's say I want to only show post1, post3, and post6 on my homepage, can I do that?
Without using while loop because the post contain only one post.
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=>40'); ?>
<?php if (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endif;?>
<?php get_footer(); ?>
Try this code
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=40'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;?>
<?php get_footer(); ?>
The one I picked had an ID of 40. So, I set p=40. On the page I chose to use this on, I selected the "Query Single Post" post template. Clicked save and refreshed my page.
You need to use pre_get_posts filter.
Code:
<?php
add_action('pre_get_posts', function($query){
if ( !$query->is_home() or !$query->is_main_query() )
return false;
# And here you can use any parameters from [WP_Query Class](https://codex.wordpress.org/Class_Reference/WP_Query)
# For example
$query->set('post__in', [10, 15, 16]);
});

Wordpress loop for static front page template not working

I have a static wordpress front page. I want the code that works on my regular blog template to work on the homepage too.
Currently, it only shows one post. The code in the main index.php is this...
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() );
?>
<?php endwhile; ?>
<?php wp_pagenavi(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
content.php calls this....
<div class="col-md-4">
<?php the_excerpt(); ?>
</div><!-- /col-md-4 -->
This works great while on the blog page. But i can't get this exact same content on to the static home page. I know I need a different query for homepage but no idea what that is. Any help appreciated!
Your code looks right to me, you can try to reset the loop and query the posts again.
<?php
rewind_posts();
query_posts( $args );
while ( have_posts() ) : the_post();
?>
<!-- Do stuff... -->
<?php endwhile; ?>
global $query_string;
query_posts($query_string.'&post_type=post&posts_per_page=15');
Adding this on top of your code should work. Try it out.

Twentyeleven used post format

I am developing a theme and using twentyeleven theme as my base theme
Twenty eleven uses <?php get_template_part( 'content', get_post_format() ); ?>
To fetch post
SO this codes work properly in index.php
But when i use in custom page template
it doesnt get fomratted propely
<?php
/*
Template Name: Vote Page Template
*/
get_header(); ?>
<div id="primary">
<div class="container">
<head>
</head>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts()) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
<?php get_footer(); ?>
Proper Format
Unproper Format

Categories