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.
Related
On my wordpress site, I have a section only displaying a lidt of post excerpts from within a specific parent category. Each post has a single sub-category which I'd like to also display alongside the title and excerpt.
At the moment I'm able to get and siaply the required posts but each post is displaying every subcategory, even ones not assigned to it. How can I modify this to show only assigned subcategories?
<div class="blog-items">
<?php
$args = array( 'posts_per_page' => 8, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<div class="blue">
<p><span>
<?php
$args2 = array('child_of' => 1);
$categories = get_categories( $args2 );
foreach($categories as $category) {
$the_sub = $category->name;
echo $the_sub;
} ?>
</span></p>
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(__('(more…)')); ?></p>
read more
</div>
<?php endforeach;
wp_reset_postdata();?>
</div>
EDIT:
With Growdzens help I ended up with this that worked.
<div class="blog-items">
<?php
$args = array( 'posts_per_page' => 8, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<div class="blue">
<p>
<span>
<?php
$categories = get_the_category( $post->ID);
foreach($categories as $category) {
$the_sub = $category->name;
$cat1 = 4;
$cat2 = $category->cat_ID;
if( $cat2 != 1 ){
echo $the_sub;
}
}
?>
</span>
</p>
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(__('(more…)')); ?></p>
read more
</div>
<?php endforeach;
wp_reset_postdata();?>
</div>
Wordpress has a function for getting all categories of a post on which you can read more here: get_the_category()
This function can be used in the loop like the example below to get the desired results.
<div class="blog-items">
<?php
$args = array( 'posts_per_page' => 8, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<div class="blue">
<p><span>
<?php
$categories = get_the_category( $post->ID);
foreach($categories as $category) {
$the_sub = $category->name;
echo $the_sub;
} ?>
</span></p>
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(__('(more…)')); ?></p>
read more
</div>
<?php endforeach;
wp_reset_postdata();?>
</div>
Update:
To check if a category is a child of a specific other category you can use the Wordpress function cat_is_ancestor_of()
<div class="blog-items">
<?php
$args = array( 'posts_per_page' => 8, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<div class="blue">
<p><span>
<?php
$categories = get_the_category( $post->ID);
$parent_id = 4; //change this ID to the parent ID you want to show the subcategories for
foreach($categories as $category) {
$the_sub = $category->name;
if(cat_is_ancestor_of( $parent_id, $category->cat_ID )){
echo $the_sub;
}
} ?>
</span></p>
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(__('(more…)')); ?></p>
read more
</div>
<?php endforeach;
wp_reset_postdata();?>
</div>
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 have issue with getting all posts with a category("case"), if I remove the 'cat' => $cat_id line i get all posts in posttype "Feed"
How do I get out only posts with category 'case'?
$cat_id = get_cat_ID('case');
var_dump($cat_id); //responce is 40
$args = array(
'post_type' => 'Feed',
'posts_per_page' => -1,
'cat' => $cat_id,
);
while ( $loop->have_posts() ) : $loop->the_post();
var_dump($loop);
endwhile;
wp_reset_query();
<ul>
<?php
global $post;
$args = array( 'category' => 'your category name id''offset'=> 1, );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
</ul>
<?php } ?>
for more information check https://codex.wordpress.org/Template_Tags/get_posts
Please try this code :
change your category id or post id
<?php if (is_category('3') ) { ?>
<?php query_posts('p=17'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;
} elseif (is_category('13') ) { ?>
<?php query_posts('p=11'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;
} else { ?>
//whatever goes in here
<?php } ?>
For example I have some category that includes five posts. When I select the post, need to display the rest four posts from category in which it situated.
<?php
$infocat = get_the_category();
$info = $infocat[0]->cat_ID;
$array = "orderby=rand&showposts=10&cat=$info";
query_posts($array);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<a class="permlinccat" href="<?php the_permalink() ?>" title="Перейти к посту: <?php the_title(); ?>" ><?php the_title(); ?></a>
<?php endwhile; else: ?>
<?php endif; wp_reset_query(); ?>
Tried this function - almost good, but it get only titles of the posts
found a solution...
<?php
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category- >term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'showposts'=>5 //
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<h3>Current category</h3><ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<a href="<?php the_permalink() ?>" rel="bookmark" title=" <?php the_title_attribute(); ? >"><?php the_title(); ?>
<img src="<?php echo first_image() ?>"title="<?php the_title(); ?>" alt="<?php the_title(); ?>"/>
</a>
<?php
}
echo '</ul>';
}
}
?>
Try this :) Just simply get the current category and exclude the current post from the query .
<?php
$cat = get_the_category();
$catOK = $cat[0]->cat_ID; //if multiple categories
$postID = $post->ID;
query_posts(
array(
'cat' => $catOK,
'post__not_in' => array($postID)
)
);
while (have_posts()) : the_post(); ?>
YOUR HTML CODE
<?php endwhile; ?>
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(). ?>