Ok.. So I want this code to make something like this.....
Category 1
..Post Z
..Post Y
..Post X
Category 2
..Post Z
..Post Y
..Post X
Category 3
..Post Z
..Post Y
..Post X
Category N
..Post Z
..Post X
..Post Y
Code is listing the categories just fine, but when it gets to the part of bringing the posts of each category, is getting random and repeated posts.
You can take a look a this code here [[LINK href="http://www.favoritosdominicana.com/videos/"]]http://www.favoritosdominicana.com/videos/[[/LINK]]
My idea is to make something like youtube frontpage.
If you have a better code and shorter best for me.
Sorry for my english.
<?php
$taxonomy = 'medios'; /* Custom Post Type Category... register_taxonomy( 'medios',array ( 0 => 'noticias-de-hoy', ) */
$terms = get_terms($taxonomy);
if ( $terms && !is_wp_error( $terms ) ) :
foreach ( $terms as $term ) :
$args = array(
'post_type' => 'noticias-de-hoy', /* Custom Post Type register_post_type( 'noticias-de-hoy' , $args ); */
'posts_per_page' => 3,
'tax_query' => array( /* I'm using tax_query, I dont know if this is ok here */
'taxonomy' => 'medios',
'term' => $term->term_id
)
);
?>
<ul>
<div class="clear"></div>
<?php echo $term->term_id.' '; /*I'm printing category ID */?> <a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><h2><?php echo $term->name; /*Name of the Category */ ?></h2></a></div>
<div class="clear"></div>
<?php query_posts($args); if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="video-item">
<a class="entry-link" href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail()) { ?>
<?php //echo the_post_thumbnail('thumbnail'); Posts dont have thumbnails now; using a demo images ?>
<img src="http://placehold.it/200x125&text=No+Image" alt="article image" />
<?php } else { ?><img src="http://placehold.it/200x125&text=No+Image" alt="article image" /><?php } //Also here demo images ?>
<?php the_title(); ?>
</a>
</div>
<?php endwhile; ?>
<?php else: ?>
<li>Doesnt have any post</li>
<? endif; ?>
</ul>
<?php endforeach; ?>
<?php endif;?>
Bob answers:
are you adding same posts in multiple taxonomy?
<?php
$taxonomy = 'medios'; /* Custom Post Type Category... register_taxonomy( 'medios',array ( 0 => 'noticias-de-hoy', ) */
$terms = get_terms($taxonomy);
$exclude =array();
if ( $terms && !is_wp_error( $terms ) ) :
foreach ( $terms as $term ) :
$args = array(
'post_type' => 'noticias-de-hoy', /* Custom Post Type register_post_type( 'noticias-de-hoy' , $args ); */
'post__not_in' => $exclude, // do not repeat
'posts_per_page' => 3,
'tax_query' => array( /* I'm using tax_query, I dont know if this is ok here */
'taxonomy' => 'medios',
'term' => $term->term_id
)
);
?>
<ul>
<div class="clear"></div>
<?php echo $term->term_id.' '; /*I'm printing category ID */?> <a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><h2><?php echo $term->name; /*Name of the Category */ ?></h2></a></div>
<div class="clear"></div>
<?php query_posts($args); if (have_posts()) : while (have_posts()) : the_post();
$exclude[] = $post->ID; // for not repeated posts
?>
<div class="video-item">
<a class="entry-link" href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail()) { ?>
<?php //echo the_post_thumbnail('thumbnail'); Posts dont have thumbnails now; using a demo images ?>
<img src="http://placehold.it/200x125&text=No+Image" alt="article image" />
<?php } else { ?><img src="http://placehold.it/200x125&text=No+Image" alt="article image" /><?php } //Also here demo images ?>
<?php the_title(); ?>
</a>
</div>
<?php endwhile; ?>
<?php else: ?>
<li>Doesnt have any post</li>
<? endif; ?>
</ul>
<?php endforeach; ?>
<?php endif;?>
ziport comments:
No, every post has only 1 category!! Your code is the one the site right now, still isnt working.
Bob comments:
<?php // get all the categories from the database
$cats = get_categories(array('taxonomy' => 'medios'));
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo "<h2>".$cat->name."</h2>";
?>
<ul>
<div class="clear"></div>
<a href="<?php echo esc_url( get_category_link( $cat_id ) ); ?>"><h2><?php echo $cat->name; /*Name of the Category */ ?></h2></a></div>
<div class="clear"></div>
<?
// create a custom wordpress query
query_posts("cat=$cat_id&post_per_page=3");
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // create our link now that the post is setup ?>
<div class="video-item">
<a class="entry-link" href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail()) { ?>
<?php //echo the_post_thumbnail('thumbnail'); Posts dont have thumbnails now; using a demo images ?>
<img src="http://placehold.it/200x125&text=No+Image" alt="article image" />
<?php } else { ?><img src="http://placehold.it/200x125&text=No+Image" alt="article image" /><?php } //Also here demo images ?>
<?php the_title(); ?>
</a>
</div>
<?php endwhile;
else: ?>
<li>Doesnt have any post</li>
<? endif; ?>
<?php } // done the foreach statement ?>
Bob comments:
Please remove
echo "<h2>".$cat->name."</h2>"
Bob comments:
I am also missing <strong></ul></strong> right after <em>endif;</em>
please add it.
ziport comments:
Ok, this one was working for post type "post" but not the custom post type "noticias-de-hoy"
Bob comments:
change line query_posts and add post type parameter
query_posts("cat=$cat_id&post_per_page=3&post_type=noticias-de-hoy");
ziport comments:
Still the same, and I'm not sure if cat=$cat_id works with CPTs
Bob comments:
replace query_posts line with this one
$postArgs = array(
'posts_per_page'=>3,
'order' => 'ASC',
'post_type'=>'noticias-de-hoy',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'medios',
'field' => 'id',
'terms' => $cat->term_id
)
)
);
query_posts( $postArgs );
ziport comments:
Thanks bro! I made work from my side here!
Here's the code that I used....
$args = array(
'post_type' => 'noticias-de-hoy', /* Custom Post Type register_post_type( 'noticias-de-hoy' , $args ); */
'posts_per_page' => 3,
'taxonomy' => 'medios',
'field' => 'id',
'term' => $term->slug,
);
Full code:
<?php
$taxonomy = 'medios'; /* Custom Post Type Category... register_taxonomy( 'medios',array ( 0 => 'noticias-de-hoy', ) */
$terms = get_terms($taxonomy);
if ( $terms && !is_wp_error( $terms ) ) :
foreach ( $terms as $term ) :
$args = array(
'post_type' => 'noticias-de-hoy', /* Custom Post Type register_post_type( 'noticias-de-hoy' , $args ); */
'posts_per_page' => 3,
'taxonomy' => 'medios',
'field' => 'id',
'term' => $term->slug,
);
?>
<ul>
<div class="clear"></div>
<?php echo $term->term_id.' '; /*I'm printing category ID */?> <a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><h2><?php echo $term->name; /*Name of the Category */ ?></h2></a></div>
<div class="clear"></div>
<?php query_posts($args); if (have_posts()) : while (have_posts()) : the_post(); $urlsource = rwmb_meta('favoritos_url', true ); ?>
<div class="video-item">
<a class="entry-link" href="<?php echo $urlsource; ?>">
<?php if ( has_post_thumbnail()) { ?>
<?php //echo the_post_thumbnail('thumbnail'); Posts dont have thumbnails now; using a demo images ?>
<img src="http://placehold.it/200x125&text=No+Image" alt="article image" />
<?php } else { ?><img src="http://placehold.it/200x125&text=No+Image" alt="article image" /><?php } //Also here demo images ?>
<?php the_title(); ?>
</a>
</div>
<?php endwhile; ?>
<?php else: ?>
<li>Doesnt have any post</li>
<? endif; ?>
<?php rewind_posts(); ?>
</ul>
<?php endforeach; ?>
<?php endif;?>
Bob comments:
to make easy copy paste here is full code.
<?php // get all the categories from the database
$cats = get_categories(array('taxonomy' => 'medios'));
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
?>
<ul>
<div class="clear"></div>
<a href="<?php echo esc_url( get_category_link( $cat_id ) ); ?>"><h2><?php echo $cat->name; /*Name of the Category */ ?></h2></a></div>
<div class="clear"></div>
<?
$postArgs = array(
'posts_per_page'=>3,
'order' => 'ASC',
'post_type'=>'noticias-de-hoy',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'medios',
'field' => 'id',
'terms' => $cat->term_id
)
)
);
// create a custom wordpress query
query_posts( $postArgs );
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // create our link now that the post is setup ?>
<div class="video-item">
<a class="entry-link" href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail()) { ?>
<?php //echo the_post_thumbnail('thumbnail'); Posts dont have thumbnails now; using a demo images ?>
<img src="http://placehold.it/200x125&text=No+Image" alt="article image" />
<?php } else { ?><img src="http://placehold.it/200x125&text=No+Image" alt="article image" /><?php } //Also here demo images ?>
<?php the_title(); ?>
</a>
</div>
<?php endwhile;
else: ?>
<li>Doesnt have any post</li>
<? endif; ?>
</ul>
<?php } // done the foreach statement ?>
Bob comments:
great! I am glad to know that your problem is solved
'taxonomy' => 'medios',
'field' => 'id',
'term' => $term->slug,
but I think there should be <em>$term->term_id</em> instead of <em>$term->slug</em>
if you want to use slug then <em>'field' => 'id',</em> should be <em>'field' => 'slug'</em>
however if it works for you without any change then it's okay.
have a nice day :)
ziport comments:
Thanks bro! I made it work from my side here! I'm new at this http://wpquestions.com system ... Really like how this works...Hope you get the money, I voted for you!
Navjot Singh answers:
Add
<?php wp_reset_query(); ?>
before
</ul>
<?php endforeach; ?>
ziport comments:
Update code! Still the same!
Navjot Singh comments:
Try changing
'tax_query' => array( /* I'm using tax_query, I dont know if this is ok here */
'taxonomy' => 'medios',
'term' => $term->term_id
)
to
'tax_query' => array( /* I'm using tax_query, I dont know if this is ok here */
'taxonomy' => 'medios',
'field' => 'id',
'term' => $term->term_id
)
Navjot Singh comments:
Errm Ignore that.
Navjot Singh comments:
Add
<?php rewind_posts(); ?>
before
</ul>
<?php endforeach; ?>
ziport comments:
Didnt work with any of those changes, still the same!
Ryan S answers:
Try something like this
$taxonomies = array(
'medios'
);
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
'fields' => 'all',
'hierarchical' => true,
'child_of' => 0,
'pad_counts' => false,
'cache_domain' => 'core'
);
$terms = get_terms( $taxonomies, $args );
if( $terms ) {
foreach( $terms as $key => $term ) {
// assign portfolio item in a single variable
$custom_args = array(
'post_type' => 'noticias-de-hoy',
'category_name' => $term->slug
'posts_per_page' => -1
);
// The Query
$the_query = new WP_Query( $custom_args );
// The Loop
if ( $the_query->have_posts() )
{
while ( $the_query->have_posts() ) { $the_query->the_post();
// your code here
the_title();
}
}
}
}
Hope that helps
ziport comments:
This is just bringing big list of a lot posts and not sorted per categories!