Count posts generated by loop wordpress - php

On my category pages I am displaying each post as an li. I would like to assign each li an ID starting from 1 and counting up incrementally (i.e. if a category has 10 posts, the output is an ul with each li having an id of a number going 1 through 10. How would i achieve this?
category.php:
<?php
/**
* The template for displaying category pages.
**/
get_header();
$counter = 0;?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
if ( have_posts() ) : ?>
<header class="page-header">
<?php
the_archive_title( '<h1 class="page-title">', '</h1>' );
the_archive_description( '<div class="taxonomy-description">', '</div>' );
?>
</header><!-- .page-header -->
<div class="slider-cont">
<div class="slider-nav">
<i class="fa fa-chevron-left"></i>
</div>
<div id="slider">
<ul class="slides">
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
$counter++;
/*
* 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-category', get_post_format() );
endwhile;?>
</ul>
</div>
<div class="slider-nav next">
<i class="fa fa-chevron-right"></i>
</div>
<?php the_posts_navigation();
else :
get_template_part( 'template-parts/content', 'none' );
endif; ?>
</div><!-- .slider-cont -->
</main><!-- #main -->
</div><!-- #primary -->
And content-category.php:
<?php
get_footer();
<li class="slide" id="post<?php $counter; ?>">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h2><?php echo the_title();
if ( 'post' === get_post_type() ) : ?></h2>
<?php
endif; ?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php echo the_excerpt();?>
Read More.
</div><!-- .entry-content -->
</article><!-- #post-## -->
</li>
Thanks in advance!

Before your loop add:
<? $counter = 0;?>
Then inside the loop before the li add:
<? $counter++;?>
and change the li to:
<li class="slide" id="post<?= $counter;?>">

The get_footer(); Method is placed wrong.
Move it from content-category.php to the end of the category.php

Related

wordpress footer.php will not appear in single.php

My footer.php file will pull into every page except the single.php, I have no idea why this is occurring! Any ideas?
Below is the Single.php code
<?php get_header(); ?>
<div class="page-content">
<div class="small-banner">
<h1><?php the_title(); ?></h1>
<img src="http://www.quorngrangehotel.local/wp-content/uploads/2017/04/new-events-banner.jpg">
</div>
<main role="main" class="mainContent">
<div id="inner-content" class="row">
<main id="main" class="large-8 medium-8 columns" role="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php get_template_part( 'parts/loop', 'single' ); ?>
<?php endwhile; else : ?>
<?php get_template_part( 'parts/content', 'missing' ); ?>
<?php endif; ?>
</main> <!-- end #main -->
</div> <!-- end #inner-content -->
</main><!-- end #content -->
<?php get_footer(); ?>
I found out the issue it was something to do with the comments section being pulled in via the single template part!

Use PHP and Bootstrap to make a column of content for ACF

Before I decided to post, I read all I could on this site with people of the same issues. Nothing seemed to work... I will explain it the best I can, I am using ACF + Repeater add-on to make A Restaurant Menu. I have Bootstrap loaded to help with making things easier, I want to have 3 columns going across. This is the HTML and PHP side of things.. I am using Bridge Theme, so I had to change the Bootstrap container class to container-acf because it kept going to the Bridge style instead. My end Goal if for it Look Similar To This All help is appreciated Thanks
My guess is I will need a Foreach loop.
`<?php
/*
Template Name: Restaurant Menu Template
*/
get_header(); ?>
<div class="content-fill-in-menu">HERE</div>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="entry-content wpb_wrapper container-acf">
<?php if ( have_rows('menu_sections') ):
while ( have_rows('menu_sections') ): the_row(); ?>
<h2 class="section_desc"><?php the_sub_field('section_title'); ?></h2>
<?php if ( have_rows('section_items') ): ?>
<?php while ( have_rows('section_items') ): the_row(); ?>
<article class="lmenu">
<ul>
<li>
<div class="container-acf">
<div class="row">
<div class="col-md-3 item_info">
<img class="dish_pic" src="<?php the_sub_field('dish_pic'); ?>" alt="<?php the_sub_field('dish_name'); ?>">
<h3 class="item_name"><?php the_sub_field('dish_name'); ?></h3>
<p class="item_desc"><?php the_sub_field('dish_description'); ?></p>
<h4 class="price">$<?php the_sub_field('dish_price'); ?></h4>
<span class="separator"></span>
</div>
</div>
</div>
</li>
</ul>
</article>
<?php endwhile; ?>
</table>
<?php endif; ?>
<?php endwhile;
endif; ?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
<?php endwhile; // End the loop. ?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<div class="content-fill-in-menu">HERE</div>
<?php get_footer(); ?>`
I think a lot of your problems are in the loop into "section_items" repeater field.
In the "while" loop, you open and close a container and a row for each item, so you don't have your render in columns.
Here in a idea of changes, you need open a row before the start of the loop. I put a counter ($i) to restart a row each 4 items to prevent problems :
<?php if ( have_rows('menu_sections') ): ?>
<?php while ( have_rows('menu_sections') ): the_row(); ?>
<h2 class="section_desc"><?php the_sub_field('section_title'); ?></h2>
<?php if ( have_rows('section_items') ): ?>
<?php $i = 1; ?>
<div class="row">
<?php while ( have_rows('section_items') ): the_row(); ?>
<div class="col-md-3 item_info">
<article class="lmenu">
<img class="dish_pic" src="<?php the_sub_field('dish_pic'); ?>" alt="<?php the_sub_field('dish_name'); ?>">
<h3 class="item_name"><?php the_sub_field('dish_name'); ?></h3>
<p class="item_desc"><?php the_sub_field('dish_description'); ?></p>
<h4 class="price">$<?php the_sub_field('dish_price'); ?></h4>
<span class="separator"></span>
</article>
</div><!-- /.col -->
<?php
if( $i == 4 ){
echo '</div><div class="row">';
$i = 0;
}
$i++;
?>
<?php endwhile; ?>
</div><!-- /.row -->
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>

Page Template does not display in drop down menu

I'm new to PHP and I made my first custom page template using the Genesis framework.
I've created a theme child to the main parent Genesis, it is active and recognizable as the child, there are no errors on the dashboard of the themes.
The problem is straight forward, when I place the template in the parent directory, no problem, it is displayed in the drop down menu of the page template selection when creating a new page, however, when I place it in the child theme's dir, it does now show.
I tried to copy index.php to the child dir but no go, it only shows when it's in the parent's.
I've looked everywhere, and having a problem which I think is sourced through the method of wordpress looking for theme templates only on the parent's dir and excluding the child.
Any suggestions would be gladly appreciated.
there's the code for the template:
<?php
/*
Template Name: About Template
*/
?>
<?php get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php if ( have_rows('about') ):
while ( have_rows('about') ): the_row(); ?>
<h2><?php the_sub_field('content_area'); ?></h2>
</div><!-- .entry-content -->
</article><!-- #post-## -->
<?php endwhile; // End the loop. ?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>
Use this code it will works
<?php
/*
Template Name:New template name
*/
get_header();?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php if ( have_rows('about') ):
while ( have_rows('about') ): the_row(); ?>
<h2><?php the_sub_field('content_area'); ?></h2>
</div><!-- .entry-content -->
</article><!-- #post-## -->
<?php endwhile; // End the loop. ?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer();?>
Use this it is working in my child genesis theme
<?php
/*
Template Name: Template Nmae
*/
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php if ( have_rows('about') ):
while ( have_rows('about') ): the_row(); ?>
<h2><?php the_sub_field('content_area'); ?></h2>
</div><!-- .entry-content -->
</article><!-- #post-## -->
<?php endwhile; // End the loop. ?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>

Single.php showing blank page

I'm trying to build a theme of my own and uploading it to Wordpress, but my single.php file it's not working at all. It's just showing a blank page. I've tried so many things to get it to work, but now I don't know what to do anymore. This is my php-file for the blog page:
<?php
/**
*Template Name: Blog Posts
*/
get_header('header4'); ?>
<section id="headerbox">
<header>
<h2 class="referensrubrik">Nyheter</h2>
</header>
<p class="referenstext">Det senaste från AL Konsult.</p>
</section>
<main id="blog">
<?php // Display blog posts on any page # http://m0n.co/l
$temp = $wp_query; $wp_query= null;
$wp_query = new WP_Query(); $wp_query->query('showposts=5' . '&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<article id="blogpost" id="post-<?php get_the_ID(); ?>" <?php post_class(); ?>>
<h2><?php the_title(); ?></h2>
<h5><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></h5>
<?php the_excerpt(); ?>
<hr>
</article>
<?php endwhile; ?>
<?php if ($paged > 1) { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Previous Posts'); ?></div>
<div class="next"><?php previous_posts_link('Newer Posts »'); ?></div>
</nav>
<?php } else { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Previous Posts'); ?></div>
</nav>
<?php } ?>
<?php wp_reset_postdata(); ?>
</main>
<?php get_footer(); ?>
Now my single.php just looks like this (I've tried the loop, but it's just not working...):
<?php
/**
* The Template for displaying all single posts.
*/
get_header('header3'); ?>
<section id="headerbox">
<header>
<h2 class="referensrubrik">Rubrik</h2>
</header>
<p class="referenstext">Text</p>
</section>
<?php
error_reporting(-1);
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content(); ?>
</div>
</article>
<?php endwhile; ?>
<?php get_footer(); ?>
What am I doing wrong!?
I fixed it! The single.php code that works for me looks like this:
<?php
/**
* The Template for displaying all single posts.
*/
get_header('header3'); ?>
<section id="headerbox">
<header>
<h2 class="referensrubrik">Rubrik</h2>
</header>
<p class="referenstext">Text</p>
</section>
<?php
$post = $wp_query->post;
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<article id="blogpost" id="post-<?php get_the_ID(); ?>" <?php post_class(); ?>>
<h2><?php the_title(); ?></h2>
<h5><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></h5>
<?php the_content(); ?>
<hr>
</article>
<?php endwhile; ?>
<?php if ($paged > 1) { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Äldre inlägg'); ?></div>
<div class="next"><?php previous_posts_link('Nyare inlägg »'); ?></div>
</nav>
<?php } else { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Äldre inlägg'); ?></div>
</nav>
<?php } ?>
<?php wp_reset_postdata(); ?>
<?php get_footer(); ?>
Your template tag is outside of the block.
<!-- /*
Template Name: Blog Posts
*/ -->
<?php
It should be:
<?php
/*
Template Name: Blog Posts
*/
Make sure there are no blank spaces or carriage returns outside the tags (especially in WP page templates): The spaces/carriage returns will cause the page not to render by throwing an exception (which you are likely not seeing because you dont have reporting enabled).
Specifically I am referring to this:
<!-- /*
Template Name: Blog Posts
*/ -->
It needs to be inside the opening PHP tag, and there should be no empty lines at the top of the page.
It should look like (no empty line breaks):
<?php
/**
* The Template for displaying all single posts.
*/
require_once('../../../wp-load.php');
get_header('header3'); ?>
Remove the following line from your code:
require_once('../../../wp-load.php');

wordpress featured image (thumbnail) on header

I'm working on making fixes to someones wordpress site, and i encountered this strange code in the loop.php and in a template file for a specific page. My goal is to change this to display the featured image in the header.I did some research into how to output the featured image using the get_thumbnail syntax, I don't normally do much in the back end / FTP of wordpress so thank you for your help and patience.
<?php
/**
* #package WordPress
* #subpackage WP-Skeleton
*/
?>
</div>
<div id="primary">
<div id="content">
**<?php the_post(); ?>
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID('full') ), 'page-header' );
$url = $thumb['0'];?>
<div id="page-header" style="background-image:url('<?php echo $url; ?>');">**
</div>
<div style="clear"></div>
<div class="container">
<section class="sixteen columns alpha">
<header class="entry-header">
<h2 class="entry-title">Events</h2>
</header>
<div class="entry-content ">
<?php while ( have_posts() ) : the_post(); ?> <!-- the Loop -->
<article id="post-<?php the_ID(); ?>" class="event">
<div class="title">
<?php the_title('<h3>', '</h3>'); ?> <!--Post titles-->
</div>
<div class="event-img"><?php the_post_thumbnail('event-img'); ?></div>
<?php the_content("Continue reading " . the_title('', '', false)); ?> <!--The Content-->
</article>
<?php endwhile; ?><!-- End the Loop -->
</div>
</section>
<div>
</div> <!-- End two-thirds column -->
</div><!-- End Content -->
</diV>
If you want to display the featured image inside the header, use the below code in it.
<?php
if(have_posts()) :
while (have_posts()) : the_post();
the_post_thumbnail();
endwhile;
endif;
?>

Categories