Wordpress loop not looping over posts - php

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.

Related

Wordpress Custom Theme: Plugins Won't Load or Display Only Code

This is my first attempt at creating a theme purely from scratch. Before this I just used underscores_me and made most of my changes to style.css and left the majority of PHP alone, because I'm a novice with it.
My issue is that plugins are not working. None of the ones I have installed work. At this point I have been trying plugins that create an event calendar, but I'm assuming that any and all plugins will have issues. In the areas that are meant to display the calendar, there are two things I'm seeing. Plugin-generated pages show nothing at all (aside from the theme visuals) and plugins that are inserted into an admin-created page display plugin-generated code.
I am using WampServer. I have wp_footer(); and wp_head(); in the correct places. My functions.php file was created from the example found at https://scanwp.net/blog/create-a-wordpress-starter-theme-from-scratch/ and the only adjustment I have made to it so far is to remove the fontawesome line of code.
My index.php file looks like this:
<?php get_header(); ?>
<h1><?php echo "&#9755 "; ?><?php the_title(); ?></h1>
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Display post content
endwhile;
endif;
?>
<?php get_footer(); ?>
And my page.php file looks like this:
<?php get_header(); ?>
<h1><?php echo "&#9755 "; ?><?php the_title(); ?></h1>
<?= get_post_field('post_content', $post->ID) ?>
<?php get_footer(); ?>
First of all, you have to understand that in your displayed files, there are no functions that recall objects that will later show the page content.
Then, in your index.php file there is an error, besides what was said above, because you're calling the_title () (function) that extrapolates the title of a post or page via the post object, which in this case should be extracted within the while loop contained within the if condition.
Then try editing the files as follows.
index.php
<?php
get_header(); ?>
<?php if( have_posts() ) : ?>
<?php while( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php if( is_singular() ) : ?>
<?php the_content(); ?>
<?php else : ?>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else: ?>
<h1>No posts to display</h1>
<?php endif; ?>
<?php get_footer(); ?>
and page.php
<?php
get_header(); ?>
<?php while( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
<?php get_footer(); ?>
However, within the wordpress codex you will find all the guides well written on any type of function, look no further.
source: https://codex.wordpress.org/

PHP experts - if start and end php tags are used on just about every line in a page, willit hurt response time?

I'm looking at theme files for a wordpress installation and I am seeing the use of the php start and end tags on just about every line of code.
Example
<?php if ( have_posts() ) : ?>
<header class="page-header">
<?php
the_archive_title( '<h1 style="font-size: 1.5em; color: black;" class="page-title">', '</h1>' );
the_archive_description( '<div class="taxonomy-description">', '</div>' );
?>
</header><!-- .page-header -->
<?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( 'template-parts/content', get_post_format() );
?>
<?php endwhile; ?>
<!--? php the_posts_navigation(); ?-->
<?php INDEX_prev_next_nav(); ?>
<?php else : ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php endif; ?>
Will all of those tags increase execution time?
Also, say you have the following:
<?php some-code ?>
<h1> this is the header</h1)
<?php more code ?>
Will that take more time than doing it like this:
<?php some-code;
echo '<h1> this is the header</h1>';
php more code; ?>
I know that any difference will be quite small, I'm just curious if one way or the other would take less time while being handled by the server.

Pagination not working in the main index.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; ?>

WordPress Blog Not Displaying Posts

I've created a custom template using the twentythirteen template. When I activate the template the blog posts disappear from the front page. I've used the same process to make the new template as I did for my own.
You can see my blog page here, http://www.gnarlydigital.com/blog/ and the new blog that I'm working on is here, http://www.flowdirectpumps.com/news/.
I've tripled checked all the code and both templates are identical. I've uploaded the Gnarly Digital template and it works.
Can anyone see anything obvious?
This is the index.php file
<?php get_header(); ?>
<div class="container pageContent">
<div class="row">
<div class="col-md-12">
<?php if ( have_posts() ) : ?>
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentythirteen_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div>
</div>
</div>
<?php get_footer(); ?>

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