Hi I have a loop with testimonials using advanced custom fields. I need the loop to only loop one post at a time randomly i have tried query_posts but its doest work.
<?php
query_posts( 'posts_per_page=1&orderby=rand' );
if(get_field('testimonials', 'options')): ?>
<?php while(has_sub_field('testimonials', 'options')): ?>
<ul>
<li class="title"><?php the_sub_field('name'); ?></li>
<li class="site"><?php the_sub_field('website'); ?></li>
<li class="desc"><?php the_sub_field('message'); ?></li>
</ul>
<?php endwhile; ?>
<?php endif; ?>
There's a problem with the while loop, you should do it like this:
<?php
$posts = new WP_Query();
$posts->query('posts_per_page=1&orderby=rand');
if (have_posts()) :
while (posts->have_posts()) : $posts->the_post();
if(get_field('testimonials', 'options')): //Ain't no sure what does this ?>
<ul>
<li class="title"><?php the_sub_field('title'); ?></li>
<li class="site"><a href="<?php the_sub_field('website'); ?>" target="_blank">
<?php the_sub_field('website'); ?></a></li>
<li class="desc"><?php the_sub_field('message'); ?></li>
</ul>
<?php
endif;
break; // Exit loop after first post
endwhile;
endif;
?>
Look how i'm using the while loop. I don't understand what get_field does, you should pass the post ID as second parameter.
Try this for looping out one post per page:
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand'
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<ul>';
echo '<li>' . get_the_title() . '</li>';
echo '</ul>';
echo '<li class="title">'.the_sub_field('name'). '</li>';
echo '<li class="site">'.the_sub_field('website').'</li>';
echo '<li class="desc">'.the_sub_field('message').'</li>';
endwhile;
wp_reset_postdata();
I found the solution here :)
http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'event',
'meta_key' => 'location',
'meta_value' => 'Melbourne'
);
// get results
$the_query = new WP_Query( $args );
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<?php the_title(); ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Related
I have custom post types 'stockist' and 'product', the latter having a taxonomy 'range'. Products are assigned to stockists using an ACF Post Object.
I'm trying to loop through 'stockist', return the 'range' term from the post object and then list the products for each term. So far I can loop all stockists and return all range terms however the foreach loop that returns the product list doesn't appear to be looping as it only returns the first set of values for each stockist.
Can anyone see where I'm going wrong?
<?php
$args2 = array( 'post_type' => 'stockist', 'posts_per_page' => -1 );
$stockistloop2 = new WP_Query( $args2 );
if ( $stockistloop2->have_posts() ): while ( $stockistloop2->have_posts() ): $stockistloop2->the_post();?>
<div class="col-1-1 clearfix nopad stockist-block-dropdown <?php the_title();?>-block">
<?php
$args = array( 'taxonomy' => 'range', 'hide_empty' => 0);
$categories = get_categories($args);
if($categories): foreach($categories as $category): $url = get_category_link( $category->term_id ); ?>
<div class="col-1-5">
<h4>
<?php echo ($category->name) ;?>
</h4>
<ul class="stockist-block-products clearfix">
<?php $post_objects = get_field('stocked_range'); if( $post_objects ):
foreach( $post_objects as $post): $post_terms_array = get_the_terms($post, 'range'); $post_term_name = $post_terms_array[0]->slug;
setup_postdata($post);
if($post_term_name == $category->slug):?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endif;
endforeach;
endif;?>
</ul>
</div>
<?php endforeach; ?>
<?php endif;?>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
UPDATE
Replacing the inner loop with the following has resolved the issue:
<?php $post_objects = get_field('stocked_range');
if( $post_objects ): ?>
<ul class="stockist-block-products clearfix">
<?php foreach( $post_objects as $post_object): $post_terms_array = get_the_terms($post_object, 'range'); $post_term_name = $post_terms_array[0]->slug;
if($post_term_name == $category->slug):?>
<li>
<?php echo get_the_title($post_object->ID); ?>
<span>Post Object Custom Field: <?php the_field('field_name', $post_object->ID); ?></span>
</li>
<?php endif; endforeach; ?>
</ul>
<?php endif;?>
I would like to Edit the Grid Artist page to only have 12 artists and land on the producers section instead of all artist.
Here' s the link of website page : http://cascaderecords.fr/roster/
PHP code:
<?php
if (ci_setting('artists_isotope') == 'enabled' AND !wp_is_mobile() AND !is_tax() ):
?>
<ul class="filters-nav group">
<li><?php _e('All Artists', 'ci_theme'); ?></li>
<?php
$args = array('hide_empty' => 1);
$cats = get_terms('artist-category', $args);
?>
<?php foreach ( $cats as $cat ): ?>
<li><?php echo $cat->name; ?></li>
<?php endforeach; ?>
</ul>
<?php
endif; // is isotope enabled
global $paged;
if ( ci_setting('artists_isotope') == 'enabled' AND !wp_is_mobile() AND !is_tax() ) {
$args = array(
'post_type' => 'cpt_artists',
'posts_per_page' => -1
);
} else {
$args = array(
'post_type' => 'cpt_artists',
'posts_per_page' => ci_setting('artists_per_page'),
'paged' => $paged
);
}
$the_query = !empty($the_query) ? $the_query : new WP_Query($args);
?>
<article class="row">
<ul class="col-md-12 items-list-grid filter-container <?php echo $filter; ?>">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php $cats = wp_get_object_terms($post->ID, 'artist-category'); ?>
<li class="<?php foreach ( $cats as $cat ) : echo $cat->slug.' '; endforeach; echo $grid; ?>">
<article id="artist-<?php the_ID(); ?>" <?php post_class('row'); ?>>
<div class="col-md-12">
<?php if ( has_post_thumbnail() ) : ?>
<figure>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('ci_thumb_square'); ?>
</a>
</figure>
<?php endif; ?>
<h2><?php the_title(); ?></h2>
</div><!-- /col-md-12 -->
</article><!-- /row -->
</li><!-- /col-md-4 -->
<?php endwhile; wp_reset_postdata(); ?>
</ul>
Try change:
'posts_per_page' => -1
to:
'posts_per_page' => 12
Not sure what "ci_setting('artists_per_page')" is in below variable, need more info:
'posts_per_page' => ci_setting('artists_per_page'),
I want a simple loop to get latest five posts, which only consist of post title and time i wrote the loop below and the title generates fine, however the time doesn't change. as it get the first post in the loop's time for other posts as well. so all the post time is same.
Please advise why time don't loop ?
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){ ?>
<li class="orbit-slide">
<div>
<a href="<?php echo get_permalink($recent["ID"]); ?>" class="ticker-a">
<span><?php echo get_the_time($recent["g:i a"]); ?> </span>
<?php echo $recent["post_title"]; ?>
</a>
</div>
</li>
<?php }
wp_reset_query();
?>
Try this one
echo get_the_time('', $recent["ID"]);
On a side note. This is a much more efficient piece of code and it is easier to output content.
<?php
$args = array( 'posts_per_page' => '5', 'orderby' => 'most_recent' );
$recent_posts = new WP_Query( $args );
if ( $recent_posts->have_posts() ) : while ( $recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="orbit-slide">
<div>
<a href="<?php the_permalink(); ?>" class="ticker-a">
<span><?php the_time("g:i a"); ?> </span>
<?php the_title(); ?>
</a>
</div>
</li>
<?php
endwhile; endif;
wp_reset_query();
?>
Thanks "Aron" for answer 1 :
echo get_the_time('', $recent["ID"]);
And Thanks "WizardCoder" for the other loop solution:
<?php
$args = array( 'posts_per_page' => '5', 'orderby' => 'most_recent' );
$recent_posts = new WP_Query( $args );
if ( $recent_posts->have_posts() ) : while ( $recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="orbit-slide">
<div>
<a href="<?php the_permalink(); ?>" class="ticker-a">
<span><?php the_time("g:i a"); ?> </span>
<?php the_title(); ?>
</a>
</div>
</li>
<?php
endwhile; endif;
wp_reset_query();
?>
My pagination code shows a full list of pages like 1,2,3,4,5,6,7,8,9,10 but i want it to show Prev,1,2,3,4,5,Next instead how can i do this with this code
if($tot>$perq) {
$output.="<div class='pagenation'>";
for($i=0;$i<($tot/$perq);$i++) {
$j=$i+1;
if($pg==$i)
$output.="<a href='".get_permalink()."?pg=$i' class='button active'>".$j."</a>";
else
$output.="<a href='".get_permalink()."?pg=$i' class='button'>".$j."</a>";
}
$output.="</div>";
}
Here is an example from a site I am working on. The keywords are 'previous_posts_link' and 'next_posts_link', which together with the $arg 'posts_per_page' will paginate for you.
Hope it helps.
<?php
the_content();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'bonsai','posts_per_page' =>12,'paged' => $paged);
$myposts = query_posts($args);
?>
<span id="tree-list-span">
<fieldset>
<legend>The Bonsai</legend>
<ul>
<?php
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li class="main_bonsai_list_item">
<a href="<?php echo get_permalink($post->id);?>">
<span class="grower_name"><?php the_title();?></span><br/>
<span class="grower_name">
<?php
$attr = array(
'id' => "link-".$post->ID,
'style' => 'max-width: 120px',
);
echo get_the_post_thumbnail($id,'bonsai-list-thumb',$attr);
?>
</span><br/>
<?php $custom = get_post_custom($post->ID);?>
<?php $grower = $custom["tree_grower"][0];?>
<span class="grower_name"><?php echo $grower;?></span>
</a>
</li>
<?php endforeach;
?>
</ul>
<span id="page_links">
<?php
previous_posts_link( '«' );
next_posts_link( '»', $myposts->max_num_pages );
wp_reset_postdata();
?>
</span>
</fieldset>
<?php
?>
I am having an issue getting the $do_not_duplicate working properly I have several title duplicating on my blog and i need it to stop. Here is what i have so far:
<?php if (is_single()): ?>
<section>
<h3>Related Posts</h3>
<?php
global $post;
$cats = wp_get_post_categories($post->ID);
$do_not_duplicate[] = $post->ID;
if ( count ( $cats ) > 0):
$args = array( 'numberposts' => 3, 'category' => implode($cats, ","), 'exclude' => $post->ID, 'post__not_in' => $do_not_duplicate );
$related_posts = get_posts( $args );
if (count($related_posts)): ?>
<ul>
<?php foreach ($related_posts as $post) : setup_postdata($post); ?>
<li><a href="<?php the_permalink() ?>"><?php while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; if ( get_the_title() ) the_title(); else the_ID(); ?><?php
endwhile;
wp_reset_query(); ?>
</a></li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>No related posts found.</p>
<?php endif; ?>
<?php else: ?>
<p>No related posts found.</p>
<?php endif; ?>
</section>
<?php endif; ?>
you have a while (have_posts() ) within a foreach and this produces the duplication. You may change your loop to something like this:
<?php
global $post;
$cats = wp_get_post_categories($post->ID);
$do_not_duplicate[] = $post->ID;
if ( count ( $cats ) > 0):
$args2 = array( 'numberposts' => 3, 'category' => implode($cats, ","), 'exclude' => $post->ID, 'post__not_in' => $do_not_duplicate );
$related_posts = get_posts( $args2 );
if (count($related_posts)):
?>
<ul>
<?php foreach ($related_posts as $post) : setup_postdata($post); ?>
<li><a href="<?php the_permalink() ?>" ><?php $do_not_duplicate[] = $post->ID; if ( get_the_title() ) the_title(); else the_ID(); ?></a></li>
<?php endforeach; ?>
</ul>
<?php wp_reset_query(); ?>
<?php endif;endif; ?>
At the end of the loop, the var $do_not_duplicate save the id of the post and all the id of the relative posts.