I'm building a theme in WordPress, I'm a newbie, along with the image HTML is being outputted, please do help me resolve this issue, thank you.
<?php
$args = array( 'numberposts' => 4, 'order'=> 'ASC', 'orderby' => 'title', 'category' => '5' );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<li>
<div class="timeline-image">
<a href="<?php the_permalink(); ?>">
<img class="rounded-circle img-fluid" src="<?php echo the_post_thumbnail(); ?>">
</a>
</div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4 class="subheading text-left"><?php the_title(); ?></h4>
</div>
<div class="timeline-body">
<p class="text-muted text-justify"><?php the_excerpt(); ?>
Read More >>
</p>
</div>
</div>
</li>
<?php endforeach; ?>
You should be looping through the posts as such and not using foreach:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 4,
'orderby' => 'title',
'order' => 'ASC',
'category__in' => 5
);
$loop = new wp_query( $args );
while( $loop->have_rows() ) : $loop->the_row();
?>
Your article content…
<h4 class="subheading text-left"><?php the_title(); ?></h4>
<?php endif; ?>
If you need to loop through a different custom post types, use this guide.
Related
I have a pizza sales site which offers 'Ingredients' of which these are simple products, for example: Pizza NapolĂ is a bundle product, this product has ingredients grouped together.
What I'm looking for is to show in a custom loop (wp_query) the children products or ingredients of this pizza, which are 'Cheese(Queso)', 'Bacon(Tocineta)', etc.
Images reference here:
My code:
<?php
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => '_stock_status',
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array('pizzas'),
'field' => 'name',
'operator' => 'IN'
)
)
);
// End query
$products = new WP_Query( $args, 'woocommerce'); ?>
<div class="menu-grid-pizzas">
<?php if ($products->have_posts()): ?>
<div class="container">
<ul class="grid-pizzas-list">
<?php while ($products->have_posts()): $products->the_post(); ?>
<li class="grid-pizzas-content">
<div class="content-left">
<div class="grid-img">
<a href="<?php the_permalink();?>">
<?php the_post_thumbnail('medium');?>
</a>
</div>
</div>
<div class="content-right">
<div class="grid-product-title">
<a href="<?php the_permalink();?>">
<h4><?php the_title();?></h4>
</a>
</div>
<div class="grid-product-content">
<?php $content = get_the_content();?>
<p><?php echo mb_strimwidth($content, 0, 120, "...");?></p>
</div>
<div class="grid-product-ingredints">
<!-- HERE get DATA items bundles -->
</div>
<div class="grid-product-price">
<?php $precio = get_post_meta( get_the_ID(), '_price', true );?>
<p><?php echo wc_price( $precio ); ?></p>
</div>
<div class="grid-product-add-to-card">
<?php do_action('woocommerce_after_shop_loop_item' , 'woocommerce_template_loop_add_to_cart' , 10 );?>
</div>
</div>
</li>
<?php endwhile;?>
<?php wp_reset_postdata();?>
</ul>
</div>
<?php endif;?>
</div>
As you can see, it is a custom loop from which I call all products with the category 'Pizza' with a wp_query.
I hope you can help me, thank you.
Edit - I already tried wp_reset_query() it doesn't work, the first loop executes as intended but the second loop just jumps to the else and I get not working
I am using 2 custom WP_Query loops in one page, the first is to get posts from a certain category, and the second is just getting the posts by dates,
here's the code
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'category' => 3,
'posts_per_page' => 4);
$wpdb = new WP_Query($args);
if ($wpdb->have_posts()):
while ($wpdb->have_posts()):
$wpdb->the_post(); ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php the_post_thumbnail(); ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
Link
</p>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
second loop
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => 4);
$wpdb = new WP_Query($args);
if ($wpdb->have_posts()):
while ($wpdb->have_posts()):
$wpdb->the_post(); ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php if (the_post_thumbnail()): the_post_thumbnail(); else:echo 'https://lamasec.pythonanywhere.com/static/img/vulnhub.png';endif; ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
Link
</p>
</div>
<?php
endwhile;
wp_reset_postdata();
else: echo 'not working';
endif;
?>
I am using wp_reset_postdata(); but it doesn't seem to be working.
You should also use:
wp_reset_query()
After reading the documentation for multiple loops in one page here, I only had one WP_Query for both the loops. I stored the ID's of all the posts from the desired category and check for them in the second loop and continue over them. Here's the final code -
first loop
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'category' => 3,
'posts_per_page' => 4);
$wpdb = new WP_Query($args);
if ($wpdb->have_posts()):
while ($wpdb->have_posts()):
$wpdb->the_post();
$do_not_duplicate[] = $post->ID; ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php the_post_thumbnail(); ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
Link
</p>
</div>
<?php endwhile; ?>
Second Loop
if (have_posts()):
while (have_posts()):
the_post();
if (in_array($post->ID, $do_not_duplicate)) continue;
?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php the_post_thumbnail(); ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
Link
</p>
</div>
<?php
endwhile;
endif;
endif;
?>
So the case if following - i've got a wpquery like on the code below.
<section class="row service_block_row bgf" id="page-<?php the_ID(); ?>">
<div class="container">
<div class="row">
<div class="col-sm-12">
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<div class="row">
<div class="col-sm-12 col-lg-3">
<h1><?php the_title(); ?></h1>
</div>
</div>
<?php endwhile; ?>
<?php endif; wp_reset_query(); ?>
</div>
</div>
</div>
</section>
What I would like to achieve is to have the loop working like that:
<ROW>
<COL-LG-3>
<COL-LG-3>
<COL-LG-3>
<COL-LG-3>
</ROW>
SO in fact what i would lke to achieve is to have 4 elements inside row without creating different loops. I know i should use some counter but i have no clue how;/
thanks
Add new row after 4 cols
<section class="row service_block_row bgf" id="page-<?php the_ID(); ?>">
<div class="container">
<div class="row">
<div class="col-sm-12">
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) :
$count=0;
?>
<div class="row">
<?php while ( $parent->have_posts() ) : $parent->the_post();
$count++;
?>
<div class="col-sm-12 col-lg-3">
<h1><?php the_title(); ?></h1>
</div>
<?php
if($count%4==0)
{
echo '</div><div class="row">';
}
endwhile; ?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
</div>
I have used taxonomy template texonomy-blog_category.php file.
But gives 404 error when i moved to page 2 .
Below is my code
<?php
get_header('newheader');
$tax = get_query_var('taxonomy');
$term = get_query_var('term');
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$count_args=array( 'post_type' => 'blog_post',
'tax_query' => array(
array(
'taxonomy' => $tax,
'field' => 'slug',
'terms' => $term,
),
),
);
$count_detail=query_posts($count_args);
//echo count($count_detail);exit;
$posts_per_page = 2;// get_option( 'posts_per_page' );
$max_page=ceil(count($count_detail)/$posts_per_page);
//wp_reset_query();
$args = array( 'post_type' => 'blog_post',
'tax_query' => array(
array(
'taxonomy' => $tax,
'field' => 'slug',
'terms' => $term,
),
),
'posts_per_page'=>$posts_per_page,
'paged' => $paged,'page'=>$paged,
'max_num_pages'=>$max_page,
);
$post_detail=query_posts($args);
?>
<div class="container">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 main-conten">
<div class="col-lg-8 col-md-8 col-sm-12 col-xs-12 blog-main">
<ul class="blog_listing">
<?php
for($i=0;$i<count($post_detail);$i++)
{
$postId=$post_detail[$i]->ID;
$ttl=get_the_title($postId);
$url=$post_detail[$i]->guid;
//print_r($post_detail[$i]);
?>
<li>
<a href="<?php echo $post_detail[$i]->guid; ?>" ><h1><?php echo get_the_title($postId); ?></h1></a>
<div class="blog-img">
<a href="<?php echo $post_detail[$i]->guid; ?>" >
<?php //$retina = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) ?>
<?php echo get_the_post_thumbnail( $postId,array(760,331), $attr ); ?>
</a>
</div>
<span>
<?php echo date('d F , Y',strtotime($post_detail[$i]->post_date)); ?>
</span>
<p>
<?php echo $post_detail[$i]->post_excerpt; ?>
</p>
<a class="read-more" href="<?php echo $post_detail[$i]->guid; ?>">Read More</a> </li>
</li>
<?php }?>
</ul>
<div class="case_blog">
<?php
//wp_reset_postdata();
if (function_exists(custom_pagination)) {
custom_pagination($max_page,"",$paged);
}
?>
</div>
<?php //$custom_query = $temp; ?>
</div>
<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12 blog_right_list blog-side">
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Blog Sidebar") ) : ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php get_footer();
?>
I don't know what is going wrong.please help me to get out of this.
Thanks in advance
Remove page parameter from $args, your code look like this.
$args = array( 'post_type' => 'blog_post',
'tax_query' => array(
array(
'taxonomy' => $tax,
'field' => 'slug',
'terms' => $term,
),
),
'posts_per_page'=>$posts_per_page,
'paged' => $paged,
'max_num_pages'=>$max_page,
);
I'm using wp_query with infinite scroll to display posts in a specific cpt archive page. When i set the 'orderby' to 'date', everything works ok, but when i change it to 'rand' the query returns the correct number of posts, but some of them are duplicated.
This is the code i'm using:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loopb = new WP_Query( array( 'post_type' => 'my_post_type', 'posts_per_page' => 10, 'paged' => $paged, 'order' => 'rand' ) );
$value = get_field('thumbnail_sizing');
?>
<?php while ( $loopb->have_posts() ) : $loopb->the_post();
$thumb_img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'small-size' );
$full_img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full-size' );
$thumb_img_ratio = 70;
if (isset($thumb_img[1]) && isset($thumb_img[2]) && $thumb_img[1] > 0) {
$thumb_img_ratio = $thumb_img[2] * 100/$thumb_img[1];
} ?>
<div class="mosaic">
<a href="#" class="grayscale">
<div class="mosaic__image" style="padding-top: <?php echo $thumb_img_ratio;?>%;">
<img src="<?php echo $thumb_img[0]; ?>" data-src="<?php echo $full_img[0]; ?>" alt="<?php the_title();?>">
</div>
<div class="meta">
<div class="flex">
<div class="flex_item">
<h2 class="meta_title"><?php the_field('name1');?> <span class="divider">&</span> <?php the_field('name2');?></h2>
<hr class="separator">
<span class="cat">view image</span>
</div>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
Carry a random seed with you i don't know if WP_Query supports it but it would be RANDOM(SEED) (e.g. RANDOM(1234)) in straightmysql