Cannot get the Post data with specific term_id - wordpress-theming

I'm doing a nested tab by using bootstrap and so i want to query the post data with specific the term_id itself. please see my code what I'm doing wrong.
I'v get the result just the first one of the term_id but another term_id is not get the result.
I assume in my case the loop just working only one time.
<!-- Nav tabs -->
<ul class="nav nav-tabs" id="myTab-<?php echo $cat->slug?>" role="tablist">
<?php
$args = array('child_of' => $cat->term_id);
$categories = get_categories( $args );
//print_r($categories);
$ix = 1;
foreach($categories as $category) :?>
<li class="nav-item">
<a class="nav-link <?php echo $ix?> <?php if($ix==1){echo "active";}?>" id="tab-<?php echo $category->term_id?>" data-toggle="tab" href="#tab-<?php echo $category->term_id.$ix?>" role="tab" aria-controls="tab-<?php echo $category->term_id.$ix?>" aria-selected="<?php if($ix==1){echo "true";}else{echo "false";}?>"><?php echo $category->name;?></a>
</li>
<?php $ix++; endforeach ?>
</ul>
<div class="tab-content">
<?php
$ix = 1;
foreach($categories as $category) :?>
<div class="tab-pane <?php echo $ix?> <?php if($ix==1){echo "active";}?>" id="tab-<?php echo $category->term_id.$ix?>" role="tabpanel" aria-labelledby="tab-<?php echo $category->term_id.$ix?>">
<?php echo "term_ID: ". $category->term_id?>
<ul class="row child_of_cat">
<?php
$qr = new WP_Query( $args = array(
'post_type' => 'services',
'parent' => 1,
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => -1, // Limit: two products
//'post__not_in' => array( get_the_id() ), // Excluding current product
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'term_id', // can be 'term_id', 'slug' or 'name'
'terms' => $category->term_id,
), ),
));
?>
<?php
if ( $qr->have_posts() ):
while( $qr->have_posts() ):
$qr->the_post();
if(get_the_post_thumbnail($qr->post->ID) ==''){
$img = '<img class="img-fluid" src="https://via.placeholder.com/300/ddd/fff.png">';
} else{
$img = get_the_post_thumbnail($qr->post->ID);
}
echo '<li class="col-6 col-sm-3">
<div class="loop-box">';
if (has_term($term = 'best-seller', $taxonomy = 'collection', $post = $qr->post->ID)){
echo '<img class="conner-bage-bg" src="'.get_template_directory_uri().'/img/bage_new.png">';
}
echo ''.$img.'
<a href="'.get_permalink().'"><div class="description">
<p class="woocommerce-loop-product__title">'.$qr->post->post_title.'xx</p>
</div></a>
</div>
</li>';
endwhile;
//wp_reset_postdata();
rewind_posts();
endif; ?>
</ul>
</div><!-- tab-pane -->
<?php $ix++; endforeach ?>
</div>

I have already fix it by using get_posts().
enter link description here

Related

Relationship Field meta_query Not Working Why?

<?php
/*
* Query posts for a relationship value.
* This method uses the meta_query LIKE to match the string "123" to the database value a:1:{i:0;s:3:"123";} (serialized array)
*/
$doctors = get_posts(array(
'post_type' => 'star',
'meta_query' => array(
array(
'key' => 'location', // name of custom field
'value' => '"' . get_the_ID() . '"', // can you please explain this? my relationship field is currently Post Object not sure how
'compare' => 'LIKE'
)
)
));
?>
<?php if( $doctors ): ?>
<ul>
<?php foreach( $doctors as $doctor ): ?>
<?php
$photo = get_field('photo', $doctor->ID);
?>
<li>
<a href="<?php echo get_permalink( $doctor->ID ); ?>">
<img src="<?php echo $photo['url']; ?>" alt="<?php echo $photo['alt']; ?>" width="30" />
<?php echo get_the_title( $doctor->ID ); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
base on the documentation of acf this is the right code but testing it not working why?..........................................thank you guys for helping me..

ACF repeater field only show one row

I've got a problem with some code below
I use wordpress and the plugin ACF PRO.
I query some posts and in this posts their is a repeater field, but it seems that it shows only the first row of the repeater field 'evenement'.
I don't know if it's very clear ?
Somebody can help me.
Thanks
<?php $query = new WP_Query(
array(
'category__not_in' => array(520),
'category__and' => array(2905,1582),
'post_status' => array( 'draft'),
'post_type' => 'spectacles',
'lang' => 'fr',
'showposts' => -1,
'meta_key' => 'date_debut',
'orderby' => 'meta_value_num',
'order' => 'ASC')
);?>
<?php if($query->have_posts()) : while ($query->have_posts() ) : $query->the_post();?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<div id="bloc-picto">
<div class="item-picto">
<img src="<?php $picto = get_field('picto');if( !empty($picto) ): ?><?php echo $picto['url']; ?><?php else: ?><?php $image_id = get_post_thumbnail_id(); $image_url = wp_get_attachment_image_src($image_id,'icon', true); echo $image_url[0]; ?><?php endif; ?>" width="40px" height="40px">
</div>
<div class="legend-picto">
<b><?php the_title(); ?></b>
<?php if( get_field('compagnie') ): ?>
<?php the_field('compagnie'); ?>
<?php endif; ?>
<br/>
<?php if( get_field('acces_star') ): ?>
<div style="font-size:10px"><?php the_field('acces_star'); ?></div>
<?php endif; ?>
<?php if( have_rows('evenement') ): while ( have_rows('evenement') ) : the_row(); ?>
<?php $post_object = get_sub_field('lieu_evenement');if( $post_object ):
$post = $post_object; setup_postdata( $post ); ?>
<i><?php the_title(); ?></i>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
</a>
<?php wp_reset_postdata(); ?>
<?php endwhile; else: ?>
<?php endif; ?>
I ran into a similar issue with the have_rows() loop only returning the first layout when essentially nesting one query within another.
My solution was to redefine the $post variable after each layout. For example:
$post = 123;
setup_postdata( $post );
if ( have_rows( 'components' ) ) {
while ( have_rows( 'components' ) ) {
the_row();
$course_tmpl_name = str_replace( '_', '-', get_row_layout() );
// include your layout php here
$post = 123; // redefine $post
}
}
wp_reset_postdata();

Showing random post from specific category on Wordpress

I’m using the below code to show two images (which I've set up with Advanced Custom Fields) from a random post from in the category ‘homepage’ on each refresh. It doesn’t appear to be working however and when I google it there are so many different ways to do it! Does anybody know if mine looks correct? I’m concerned I might be missing a closing tag or something minor.
<div class="feature-container">
<?php
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'category_name' => 'homepage',
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
if( have_rows('images') ):
while ( have_rows("images") ) :
the_row();
if ( get_sub_field( 'image' ) ): ?>
<a href="<?php echo $link['url']; ?>" target="<?php echo $link['target']; ?>">
<div class="image feature-item">
<div class="overlay" style="background-color:<?php the_sub_field('hover_colour');?>">
<div class="caption pm-big">
<?php
$link = get_sub_field('link');
if( $link ): ?>
<?php echo $link['title']; ?>
<?php endif; ?>
</div>
</div>
<img src="<?php the_sub_field('image');?>" alt="" style="display: block;">
</div>
</a>
<?php else: ?>
<!-- no images found -->
<?php endif;
endwhile;
else :
// no images found
endif;
endwhile; ?>
</div>

Show wordpress posts in a grid - side by side divs

I tried the code below to show posts in a grid , in side by side manner. but , as shown in the image divs appear broken.
<?php
$args = array( 'posts_per_page' => 15, 'offset'=> 1, 'category' => '' );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<div class="container" style="width:960px;margin :opx auo;">
<div class="colk" style="width:200px;height:150px;border-style:dotted;border-width:thin;display:inline-block;
float:left;clear:top;margin: 5px 20px 15px 0px;margin:10px;margin-top:0px;clear:top;" >
<?php the_title(); echo '<br>' ?>
</div>
</div>
<?php echo '<br>'; ?>
<?php endforeach; ?>
<?php
wp_reset_postdata(); ?>
How can i show the divs inline?
<?php
$args = array( 'posts_per_page' => 15, 'offset'=> 1, 'category' => '' );
$myposts = get_posts( $args );?>
<div class="container" style="width:960px;margin :0 auto;">
<?php foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<div class="colk" style="width:200px;height:150px;border-tyle:dotted;border-width:thin;display:inline-block;float:left;clear:top;margin: 5px 20px 15px 0px;margin:10px;margin-top:0px;clear:top;" >
<?php the_title();?>
</div>
<?php endforeach; ?>
</div>

Add category slugs to loop in wordpress

I don't know if I've used the best title for this question but I'm not sure exactly how to phrase it. I've successfully setup a filterable portfolio script on my site but I need to apply the appropriate class to each item. Right now I've got this.
<?php $loop = new WP_Query( array( 'post_type' => 'productions', 'posts_per_page' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="tile web all"> <?php the_post_thumbnail ( 'home-page' ); ?>
<h1><?php the_title(); ?></h1>
</div>
<?php endwhile; wp_reset_query(); ?>
The class "web" is just an example, it needs to be replaced by the slug(s) for the categories used for that particular post, as I've setup the filters to automatically show all the categories like this:
<div class="filters">
<p href="" data-rel="all">All</p>
<?php
$args = array(
'type' => 'productions',
'orderby' => 'name',
'order' => 'ASC',
'taxonomy' => 'production_type',
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<p data-rel="' . $category->slug.'">' . $category->name.'</p> ';
}
?>
Hopefully that is enough info for some help. Thanks.
This worked:
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="tile <?php
$terms = get_the_terms( $post->ID , 'production_type' );
foreach ( $terms as $term ) {
echo $term->slug;
echo ' ';
}
?> all"> <?php the_post_thumbnail ( 'home-page' ); ?>
<h1><?php the_title(); ?></h1>
</div>
<?php endwhile; wp_reset_query(); ?>