Wordpress Loop: how to wrap each 3 posts into a div? - php

I'm trying this:
<?php query_posts('cat=6'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div>
<?php $counter=3; ?>
<?php the_post_thumbnail(); ?>
<?php $counter++; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
But it's not working! :/
Thank you!

Thanks for your support guys! :)
I tried both solutions but didn't work,
I ended up with this and works perfectly!
<?php query_posts('cat=6'); ?>
<?php $variable=0;?>
<div>
<?php while ( have_posts() ) : the_post(); ?>
<?php if(($variable+1)<4){ ?>
<a href="<?php echo get_post_meta($post->ID, 'colaborador-link', true); ?>" target="blank">
<?php the_post_thumbnail(); ?>
</a>
<?php $variable+=1; ?>
<?php }else{ ?>
<?php $variable=1; ?>
</div>
<div>
<a href="<?php echo get_post_meta($post->ID, 'colaborador-link', true); ?>" target="blank">
<?php the_post_thumbnail(); ?>
</a>
<?php }?>
<?php endwhile; ?>
</div>

I haven't tested it with wordpress so i'm not 100% sure it'll work.
The idea is to use the modulus operator (see an example that matches your needs here http://codepad.org/78d2aAKp)
<?php query_posts('cat=6'); ?>
<?php if (have_posts()) : ?>
<!-- Your div starts here -->
<div>
<?php
while (have_posts()) :
the_post();
$counter = 0;
if($counter%3 == 0 && $counter > 0):
?>
<!--Close and then open the div-->
</div><div>
<?php
endif;
?>
<?php the_post_thumbnail(); ?>
<?php $counter++; ?>
<?php endwhile; ?>
</div><!--/Your div ends here -->
<?php endif; ?>

<?php query_posts('cat=6'); ?>
<?php if (have_posts()) : ?>
<?php $counter=0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php if($counter%3==0) : ?>
<div>
<?php $counter=3; ?>
<?php the_post_thumbnail(); ?>
<?php $counter++; ?>
</div>
<?php else:
//Some code for other posts..
endif;
?>
<?php $counter++; ?>
<?php endwhile; ?>
<?php endif; ?>

Related

Get parameter of shortcode in Insert PHP Code Snippet plugin using ACF repeater

Everybody hello! I'm sorry for asking such kind easy question, I'm pretty new at this. I have problem with transfering parameter value and echoing it to itself.
PHP code snippet on page:
[xyz-ips snippet="Generating-content" paramSet="1"]
Inside 'generating content':
<?php if( have_rows('information') ): ?>
<?php while( have_rows('information') ): the_row(); ?>
<?php $sub_value = get_sub_field('number');
if($sub_value == 1): ?>
<div class="d-block d-md-flex d-lg-flex">
<img src="<?php the_sub_field('image'); ?>">
<div>
<?php if( have_rows('repeatable_content') ): ?>
<?php while( have_rows('repeatable_content') ): the_row(); ?>
<p>
<?php the_sub_field('text'); ?>
</p>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
Right now I need to change condition if($sub_value == 1) to if($sub_value == "PARAMETER VALUE")
Please help, guys!
I think that is what you are looking for.Not tested but it should work.
<?php
$paramSet = (!isset($atts['paramSet'])) ? $atts['paramSet'] : 'default';
if (have_rows('information')) : ?>
<?php while (have_rows('information')) : the_row(); ?>
<?php $sub_value = get_sub_field('number');
if ($sub_value == $paramSet) : ?>
<div class="d-block d-md-flex d-lg-flex">
<img src="<?php the_sub_field('image'); ?>">
<div>
<?php if (have_rows('repeatable_content')) : ?>
<?php while (have_rows('repeatable_content')) : the_row(); ?>
<p>
<?php the_sub_field('text'); ?>
</p>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
Read More - https://developer.wordpress.org/reference/functions/shortcode_atts/
Thank you guys for helping! The main issue of this problem is that you have to buy premium version of this plugin, your version is working in another plugins :)
Working plugin's name: Shortcodes Blocks Creator Ultimate

How do I get the link of a current WordPress post while it is in the Loop?

Below is my loop:
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php get_post_permalink(); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
Get <?php get_post_permalink(); ?> should display the link yet this is what is being rendered. It is not displaying the permalink of the post
None of the other answers are correct. get_the_permalink() (you can use get_permalink() as well, since it's an alias) RETURNS the data, not ECHO. So, it will never be printed to the screen (most WP functions with get_ prefix work this way.)
You have two options:
Use get_permalink( get_the_ID() ) pass the current post id (if not in the loop) and echo it.
Use the_permalink() which will echo out the permalink (in the loop);
the_permalink():
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php the_permalink(); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
get_permalink():
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php echo get_permalink(); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
This will echo out the URL, but will not make the link clickable - you need to add it to an <a> tag:
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
Click Here
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
If you want to get Post Permalink you should use get_permalink($post_id)
Wordpress get_permalink function Reference
Please try this:
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post();
$id = get_the_ID();
?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php get_the_permalink($id); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>

I have a error on last line of index.php im not sure what it could be

Hello could someone please help me I'm having trouble with my PHP being successfully compiled it says it is the last line but I think last line is right not experienced enough to know if it's right or not
<?php get_header(); ?>
<?php if( have_posts() ) { ?>
<?php while( have_posts() ) { ?>
<?php the_post(); ?>
<h2>
<?php the_title() ?>
</h2>
} else { ?>
<p>Sorry, No post matched</p>
<?php } ?>
<?php get_footer(); ?>
First of all you haven't closed the while loop. and there is no opening php. you cannot do </h2> } else { ?>
<?php get_header(); ?>
<?php if(have_posts()) { ?>
<?php while(have_posts()) { ?>
<?php the_post(); ?>
<h2>
<?php the_title() ?>
</h2>
<?php } //while end
} else { ?>
<p>Sorry, No post matched</p>
<?php } ?>
<?php get_footer(); ?>
Try this code.
<?php get_header(); ?>
<?php if( have_posts() ) { ?>
<?php while( have_posts() ) : the_post(); ?>
<h2>
<?php the_title() ?>
</h2>
<?php endwhile; ?>
<?php } else { ?>
<p>Sorry, No post matched</p>
<?php } ?>
<?php get_footer(); ?>
You have not closes the while loop. Try this one:
<?php get_header(); ?>
<?php if(have_posts()) { ?>
<?php while(have_posts()) { ?>
<?php the_post(); ?>
<h2>
<?php the_title() ?>
</h2>
<?php } // end while loop (here was you mistake) ?>
<?php } else { ?>
<p>Sorry, No post matched</p>
<?php } ?>
<?php get_footer(); ?>

Screaming frog sees a link to a page which is not there but shows

This is the selector script I believe below so what should happen is as you select a product then you select if its your an individual or couple or family then you click show me and it takes you to the correct link
<div class="finder__selector">
<?php /* Set first option as the default option */ ?>
<?php $count = 1; while ( have_rows('links') ) : the_row(); ?>
<?php $link_url = get_sub_field('link_url'); ?>
<?php if($product_name): ?>
<?php //echo $product_name; ?>
<?php //echo $link_url; ?>
<?php if(strpos($link_url, $product_name) !== false): ?>
<?php the_sub_field('link_title'); ?><i class="down-arrow"></i>
<?php endif; ?>
<?php else: ?>
<?php if($count == 1) : ?>
<?php the_sub_field('link_title'); ?><i class="down-arrow"></i>
<?php endif; ?>
<?php endif; ?>
<?php $count++; ?>
<?php endwhile; ?>
<div class="finder__options">
<?php $count = 1; ?>
<?php while ( have_rows('links') ) : the_row(); ?>
<?php $link_url_select = get_sub_field('link_url'); ?>
<?php $link_title_select = get_sub_field('link_title'); ?>
<?php if($product_name): ?>
<?php if(strpos($link_url_select, $product_name) !== false): ?>
<?php echo $link_title_select; ?><i class="down-arrow"></i>
<?php else: ?>
<?php echo $link_title_select; ?><i class="down-arrow"></i>
<?php endif; ?>
<?php else: ?>
<?php echo $link_title_select; ?><i class="down-arrow"></i>
<?php endif; ?>
<?php $count++; ?>
<?php endwhile; ?>
</div>
So the above works but whats its actually showing on view source and what creaming frog is 404 is
href="&audience=couples" class=finder__option>
my partner and I <i
class=down-arrow></i>
</a>
<a
href="&audience=families" class=finder__option>
my family <i
class=down-arrow></i>

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

Categories