How to echo the total of increment counter? - php

The $counter variable is working fine counting the loop, but I need to get the total amount of elements for each loop. How would I go about doing that?
<div id="<?php echo $term->slug; ?>" class="lity-hide resource-pop-up">
<?php
if($the_posts->have_posts()):
$counter = 1;
while($the_posts->have_posts()):
$the_posts->the_post();
//vars
$section_one = apply_filters('the_content', get_field('section_one'));
$section_two = apply_filters('the_content', get_field('section_two'));
$learn_more_link = get_field('learn_more_link');
?>
<section class="pop-up">
<div class="title">
<div class="brand">
<img src="https://via.placeholder.com/125x125" alt="Brand">
<?php the_title('<h3>','</h3>'); ?>
</div>
<aside>
<h4><?php echo $counter; ?>/<?php echo $counter->length; ?></h4>
</aside>
</div>
<div class="row pop-up-content">
<aside class="col-sm-12 col-md-6">
<?php echo $section_one; ?>
</aside>
<aside class="col-sm-12 col-md-6">
<?php echo $section_one; ?>
</aside>
</div>
<div class="learn-more">Learn More</div>
</section>
<?php
$counter++;
endwhile;
wp_reset_postdata();
endif;
?>
</div>
I should expect (number of element)/(total number of elements) or 2/10, basically like saying 2 of 10.

For the number of posts, you need
echo $the_posts->post_count
which is a total of all the posts, as opposed to
echo $counter->length
$counter is only a number and wouldn't have a length property anyway.

Related

Looking to make archive.php display in two columns for Wordpress site

Very new Wordpress apprentice here. Trying to get my archive page to display posts in two columns going like:
Post 1 Post 2
Post 3 Post 4
Here is an example from a figma we were working on: https://ibb.co/N3XwtwD
My question is, what code can I add to my files to allow this? I currently have some bootstrap classes on the html portion I inserted here and it is displaying as one column, so I don't know if those classes will interfere with anything. Here is my archive.php code below:
<?php
get_header();
?>
<div class="container mt-5 mb-5">
<p class="font-size"><?php the_archive_title(); ?></p>
<hr>
</div>
<div class="container">
<?php
while(have_posts()) {
the_post(); ?>
<div class="row">
<div class="col-lg-6">
<p class="font-size text-center"><?php the_title();?></p>
<img class="img-fluid mb-3"<?php
the_post_thumbnail();
?>
<p class="category-font">Published on <?php the_time('n.j.y');?></p>
<p>Posted by <?php the_author_posts_link() ?></p>
</div>
</div>
<?php }
echo paginate_links();
?>
</div>
<?php
get_footer();
?>
First time posting here so apologies if I left anything important out. Really appreciate this place!
Thanks!
You need to create a row every two posts and you have to add <div class="col-lg-6"> two times in a row. check below code.
<?php get_header(); ?>
<div class="container mt-5 mb-5">
<p class="font-size"><?php the_archive_title(); ?></p>
<hr>
</div>
<div class="container">
<?php
$count = 1;
while( have_posts() ) { the_post();
if ( $count % 2 == 1){ ?>
<div class="row">
<?php } ?>
<div class="col-lg-6">
<p class="font-size text-center"><?php the_title();?></p>
<?php the_post_thumbnail(); ?>
<p class="category-font">Published on <?php the_time('n.j.y');?></p>
<p>Posted by <?php the_author_posts_link() ?></p>
</div>
<?php if ( $count % 2 == 0 ){ ?>
</div>
<?php } $count++; ?>
<?php }
if ( $count % 2 != 1 ) echo "</div>";
echo paginate_links(); ?>
</div>
Please replace with the below code that helps you.
<?php
get_header();
?>
<div class="container mt-5 mb-5">
<p class="font-size"><?php the_archive_title(); ?></p>
<hr>
</div>
<div class="container">
<?php
$counterval = 0;
while(have_posts()) {
the_post();
$counterval++;
if($counterval % 2 != 0)
{ ?>
<div class="row">
<?php } ?>
<div class="col-lg-6">
<p class="font-size text-center"><?php the_title();?></p>
<?php $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
if(!empty($featured_img_url)){ ?>
<img class="img-fluid mb-3" src="<?php echo esc_url($featured_img_url); ?>" />
<?php } ?>
<p class="category-font">Published on <?php the_time('n.j.y');?></p>
<p>Posted by <?php the_author_posts_link() ?></p>
</div>
<?php
if($counterval % 2 == 0)
{ ?>
</div>
<?php } ?>
<?php } ?>
<?php echo paginate_links();
?>
</div>
<?php
get_footer();
?>

While($query->fetch()) Twice

I start web development about 3 weeks ago. Today I ran into this issue where only the first while($query->fetch()) works, and the second one doesn't.
I searched around and I'm supposed to use a $rows array, but I guess I implemented it wrong since it didn't work.
<div class="container-fluid">
<div class="row">
<aside class="sidebar col-md-3 col-md-offset-1 col-md-push-8">
<div class="widget">
<h2>Recent Posts</h2>
<ul>
<?php while($query->fetch()): ?>
<li><?php echo "<a href='post.php?
id=$post_id'>$title</a>" ?></li>
<?php endwhile ?>
</ul>
</div>
</aside>
<div class="col-md-8 posts col-md-pull-4">
<div class="row">
<?php
while($query->fetch()):
$lastspace = strrpos($content, ' ');
?>
<article class="post-excerpt">
<div class="col-md-6 post">
<a href="#">
<img src="../img/blog_posts/<?php echo $image ?>" class="img-responsive">
</a>
<header>
<a href="#">
<h3><?php echo $title ?></h3>
</a>
<p class="post-meta">
<?php echo $category ?>
<span><?php echo $date ?></span>
</p>
</header>
<p>
<?php echo substr($content, 0, $lastspace)."<a href='post.php?id=$post_id'> ..Read More</a>" ?>
</p>
</article>
<?php endwhile ?>
</div>
</div>
<?php
if($prev > 0){
echo "<a href='blog.php?p=$prev'>Prev</a>";
}
if($page < $pages){
echo "<a href='blog.php?p=$next'>Next</a>";
}
?>
</div>
</div>
</div>
Query:
$query = $db->prepare("SELECT id, title, date, image, LEFT(content,500) AS content, category FROM blog INNER JOIN categories ON categories.category_id=blog.category_id order by id desc limit $start, $per_page");
$query->execute();
$query->bind_result($post_id, $title, $date, $image, $content, $category);
if you want to fetching the data twice form result set means you need to use $query->data_seek(0); before second while loop to reset the internal data pointer to 0 . otherwise you will get error .
update 1: use this one before second loop
$query->data_seek(0);
I solved it by using an array in the first loop and then echoing it in a for loop where the second while loop was.
<div class="container-fluid">
<div class="row">
<div class="col-md-8 posts">
<div class="row">
<?php
while($query->fetch()):
$lastspace = strrpos($content, ' ');
$link[] = "<li><a href='post.php?id=$post_id'>$title</a></li>";
?>
<article class="post-excerpt">
<div class="col-md-6 post">
<a href="#">
<img src="../img/blog_posts/<?php echo $image ?>" class="img-responsive">
</a>
<header>
<a href="#">
<h3><?php echo $title ?></h3>
</a>
<p class="post-meta">
<?php echo $category ?>
<span><?php echo $date ?></span>
</p>
</header>
<p>
<?php echo substr($content, 0, $lastspace)."<a href='post.php?id=$post_id'> ..Read More</a>" ?>
</p>
</div>
</article>
<?php endwhile ?>
</div>
<div class="col-md-6 pagination">
<?php
if($prev > 0){
echo "<a href='blog.php?p=$prev' id='previous'>Prev</a>";
}
if($page < $pages){
echo "<a href='blog.php?p=$next' id='next'>Next</a>";
}
?>
</div>
</div>
<aside class="sidebar col-md-3">
<div class="widget">
<h2>About me</h2>
<p>
I'm Yousef. Aspiring web, mobile, and soon-to-be game developer. Well I'm also a geek, a nerd, and I love comic books and comic book accesories. One day I will make the world's coolest website.
</p>
</div>
<div class="widget">
<h2>Recent Posts</h2>
<ul>
<?php
for ($x = 0; $x <= 2; $x++) {
echo $link[$x];
}
?>
</ul>
</div>
</aside>
</div>

In wordpress, how to show next and previous post

I am able to display next and previous post in worspress but unable to show second-previous or third-previous || second-next or third-next like
1 - I want to show this too
2 - this is showing
3 - My Current post
4 - This is showing
5 - I want to show this too
any help would be appreciated.
In the and I am showing you my code so you can judge.
CODE:
<?php $next = get_permalink(get_adjacent_post(false,'',false)); if
($next != get_permalink()) { ?><a href="<?php echo $next; ?>">
<li class="col-xs-12 col-md-4">
<div class="article">
<div class="contain-image">
<?php $nextPost = get_next_post(true); $nextthumbnail = get_the_post_thumbnail($nextPost->ID); echo $nextthumbnail; ?>
</div>
<div class="content">
<div class="double-content">
<div class="information">
<span class="category"><?php echo get_cat_name(1);?></span>
<span class="time"><?php the_time('M j, Y') ?></span>
</div>
<div class="title">
<?php next_post_link('%link', "%title", TRUE); ?>
</div>
<p>
<?php
$Nextpost = get_next_post($id);
echo apply_filters(‘the_content’, $Nextpost->post_content);
?>
</p>
</div>
</div>
</div>
</li>
</a>
<?php } ?>
<?php $prev = get_permalink(get_adjacent_post(true,'',true)); if
($prev != get_permalink()) { ?><a href="<?php echo $prev; ?>">
<li class="col-xs-12 col-md-4">
<div class="article">
<div class="contain-image">
<?php $prevPost = get_previous_post(true); $prevThumbnail = get_the_post_thumbnail($prevPost->ID); echo $prevThumbnail; ?>
</div>
<div class="content">
<div class="double-content">
<div class="information">
<span class="category"><?php echo get_cat_name(1);?></span>
<span class="time"><?php the_time('M j, Y') ?></span>
</div>
<div class="title">
<?php previous_post_link('%link', "%title", TRUE); ?>
</div>
<p>
<?php
$Prevpost = get_previous_post($id);
echo apply_filters(‘the_content’, $Prevpost->post_content);
?>
</p>
</div>
</div>
</div>
</li>
</a>
<?php } ?>
WordPress provides several navigational template tags to make it easy for visitors to surf your pages. There are basically two different types of template tags used for chronological post navigation:
posts_nav_link() – for navigating various archive (non-single) pages
previous_post_link() & next_post_link() – for navigating single-post pages
<?php $posts = query_posts($query_string); if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php previous_post_link(); ?> | <?php next_post_link(); ?>
<?php endwhile; endif; ?>
try this:
global $post;
$post_curr = $post;
//get last post
$post_last1 = get_previous_post();
setup_postdata($post_last1);
//get second last post
$post_last2 = get_previous_post();
setup_postdata($post_curr);
//get next post now
$post_next1 = get_next_post();
setup_postdata($post_next1);
//get second next post
$post_next2 = get_next_post();
//reset current post data
setup_postdata($post_curr);

Wordpress getting posts from category in personal layout

I am trying to get the posts from evenementen. I only want 4 posts because my layout is a 2x2 column. This column has a special template for the top and bot. I want to show the 4 evenementen in this column but I don't want to change the layout if there are less.
In the PHP file I loop all the posts in evenementen and want to do add these in the specific columns.
PHP FILE
<?php
$args = array( 'posts_per_page' => 4, 'offset'=> 1, 'category' => 0 );
$myposts = get_posts( $args );
$count = 0;
foreach ( $myposts as $post ) : setup_postdata( $post );
$count++;
if ($count == 1) {
$title1 = the_title();
$date1 = the_date();
$link1 = the_permalink();
}
elseif ($count == 2) {
$title2 = the_title();
$date2 = the_date();
$link2 = the_permalink();
}
elseif ($count == 3) {
$title3 = the_title();
$date3 = the_date();
$link3 = the_permalink();
}
elseif ($count == 4) {
$title4 = the_title();
$date4 = the_date();
$link4 = the_permalink();
}
else {
}
endforeach;
?>
<div class="wrapper">
<div id="bigone">
<div class="wrapper">
<h4 class="push"><?php echo $title1; ?></h4>
<div id="one"> <p class="greytext"><?php echo $date1; ?></p></div>
<div id="two"> <p class="opmaak">Evenementen</p></div>
</div>
</div>
<div id="bigtwo">
<div class="evenementenborder">
<div class="wrapper">
<h4 class="push"><?php echo $title2; ?></h4>
<div id="one"> <p><?php echo $date2; ?></p> </div>
<div id="two"> <p class="opmaak">Evenementen </p></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<div class="evenementenfooter">
<div class="wrapper">
<div id="bigone">
<div class="wrapper">
<h4 class="push"><?php echo $title3; ?></h4>
<div id="one"> <p class="greytext"><?php echo $date3; ?></p></div>
<div id="two"> <p>Evenementen</p></div>
</div>
</div>
<div id="bigtwo">
<div class="evenementenborder">
<div class="wrapper">
<h4 class="push"><?php echo $title4; ?></h4>
<div id="one"> <p class="greytext"><?php echo $date4; ?></p> </div>
<div id="two"> <p>Evenementen </p></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
This is the closest I got so far. But this does not put the text on the proper position and the hyperlink is also in the text.
Webpage
I want to ask you guys: what is the best way to solve this? Or if I made a mistake in the code why it is not on the proper position?
I found out that informatie is directly placed on the page and the echo can't find the information because on that moment it is empty. I don't know how to solve this. Please post if you see what I am doing wrong.
i think its the buffering, Put in the beggining
ob_start();
And in the end of the document
ob_end_flush();
And try again.
Check here for documentation about buffering

How can I loop into two different DIVS without repeating in wordpress custom post

this is the code of my custom post.I am not so expert in php.So Please help me in this matter.
<?php $featuresitems=new WP_Query(array( 'post_type'=>'scbleftfeatures' )); ?>
<?php while( $featuresitems->have_posts()) : $featuresitems->the_post(); ?>
<div class="col-md-6 left-grid">
<div class="features-grid1">
<div class="feature">
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</div>
<div class="icon5">
<?php the_post_thumbnail('features_icon_size'); ?>
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="col-md-6 right-grid">
<div class="features-grid1">
<div class="feature">
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</div>
<div class="icon5">
<?php the_post_thumbnail(); ?>
</div>
<div class="clearfix"></div>
</div>
</div>
<?php endwhile; ?>
Question is a little vague but I'm assuming what you actually want to do is just change the classes left-grid and right-grid for every second div. In that case you don't have to repeat the whole divs, just change the classes. This can done with help of a "counter" ($i).
<?php
$featuresitems = new WP_Query(array(
'post_type' => 'scbleftfeatures'
));
$i = 0;
?>
<?php
while( $featuresitems->have_posts()) : $featuresitems->the_post();
$i_is_even = ($i % 2 == 0);
?>
<div class="col-md-6 <?php if ($i_is_even) {echo 'left-grid'; } else { echo 'right-grid'; }; ?>">
<div class="features-grid1">
<div class="feature">
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</div>
<div class="icon5">
<?php the_post_thumbnail('features_icon_size'); ?>
</div>
<div class="clearfix"></div>
</div>
</div>
<?php
$i ++;
endwhile;
?>
If you're curious about the % operator I suggest you check out the Modulus operator on http://php.net/manual/en/language.operators.arithmetic.php
And if you actually want to alter between different divs you can put the whole divs inside the if/else blocks instead of just the classes.
Edit:
Not sure why you'd want this as it seems you're using the bootstrap grid anyways.

Categories