Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

foreach category post six post WordPress

  • SOLVED

i would like to
grab all categories
then echo

centered title of category

thumbnail thumbnail thumbnail
post title post title post title

thumbnail thumbnail thumbnail
post title post title post title


title of category

thumbnail thumbnail thumbnail
post title post title post title


sample that i have /**
* The Loop
*
*/
if ( have_posts() ) : ?>


<section id="featured" role="contentinfo">

<?php
global $is_page;
if ( $is_page == 'template-post-gallery' ) : ?>

<header class="featured-header">

<h3 class="section-title">


this is Hip HOP


</h3><!-- .section-title -->



<?php
/**
* Section Link
*
*/

// Get theme options
$of_featured_section_link_title = c7s_get_option( 'featured_section_link_title' );
$of_featured_section_link_url = c7s_get_option( 'featured_section_link_url' );

if ( $of_featured_section_link_title && $of_featured_section_link_url ) : ?>

<a href="<?php echo esc_url( $of_featured_section_link_url ); ?>" title="<?php esc_attr_e( $of_featured_section_link_title, 'framework' ); ?> " >&raquo; <?php esc_html_e( $of_featured_section_link_title, 'framework' ); ?></a>

<?php endif; // end section title and link check ?>

</header><!-- #featured-header -->

<?php endif; // end page template check ?>






<div class="entry-container">






<?php while ( have_posts() ) : the_post(); ?>



<?php $of_featured_hide = c7s_get_option( 'featured_hide' ); ?>

<article id="post-<?php the_ID(); ?>" <?php post_class( 'entry' ) ?>>

<?php
/**
* Entry Thumbnail
*
*/




if ( $of_featured_hide['images'] == 0 ) : // Check if thumbnail is disabled in theme options ?>

<?php locate_template( 'includes/entry-thumbnail.php', true, false ); ?>

<?php endif; // end hide featured image check ?>


<?php
/**
* Entry Header
*
*/
if ( $of_featured_hide['titles'] == 0 ) : // Check if title is disabled in theme options ?>

<header class="entry-header">

<h4><a href="<?php the_permalink() ?>" title="<?php c7s_the_title_attribute(); ?>"><?php the_title(); ?></a></h4>

</header><!-- .entry-header -->

<?php endif; // end show title check ?>
<div id="bottom-left"> <?php if(function_exists('the_views')) { the_views(); } ?> </div>

<?php
/**
* Entry Summary
*
*/
if ( $of_featured_hide['content'] == 0 ) : // Check if content is disabled in theme options ?>

<div class="entry-summary">

<?php the_excerpt(); ?>

</div><!-- .entry-summary -->

<?php endif; // end show summary check ?>


<?php
/**
* More Link
*
*/
if ( $of_featured_hide['read_more'] == 0 ) : // Check if read more is disabled in theme options ?>

<footer class="entry-footer">

<a class="more-link" href="<?php the_permalink() ?>" title="<?php c7s_the_title_attribute(); ?>"><span><?php _e( 'View Video &rarr;', 'framework' ); ?></span></a>

</footer><!-- .entry-footer -->

<?php endif; // end read more check ?>

</article><!-- #post-## -->


<?php
/**
* Instant View Modal Box
*
*/
do_action( 'reveal_modal', $post->ID, true ); ?>


<?php endwhile; // end while loop ?>



</div><!-- .entry-container -->

<?php
/**
* Pagination
*
*/

// Get theme options
$featured_enable_pagination = c7s_get_option( 'feautred_enable_pagination', 0 );

if ( $featured_enable_pagination == 1 && $wp_query->max_num_pages > 1 ) : // Check for pages ?>

<div id="nav-below" class="pagenavi">

<?php if ( function_exists( 'wp_pagenavi' ) ) : // Check for WP Page Navi Plugin ?>

<?php wp_pagenavi(); ?>

<?php else : ?>

<div class="nav-previous"><?php next_posts_link( '<span class="meta-nav">&larr;</span>' . __( ' Older posts', 'framework' ) ); ?></div>

<div class="nav-next"><?php previous_posts_link( __( 'Newer posts ', 'framework' ) . '<span class="meta-nav">&rarr;</span>' ); ?></div>

<?php endif; // End WP Page Navi plugin check ?>

</div><!-- #nav-below -->

<?php endif; // end page check ?>

</section><!-- #featured -->

<?php endif; // end loop ?>

Answers (3)

2011-07-25

Jason Manheim answers:

<?php
$taxonomy = 'category';
$param_type = 'category__in';
$term_args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
foreach( $terms as $term ) {
$args=array(
"$param_type" => array($term->term_id),
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 6,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { ?>
<div class="category section">
<h3><?php echo $term->name;?></h3>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="post"><?php the_post_thumbnail(); ?><br /><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
<?php endwhile; ?>
</div><div class="clear"></div>
<?php }
}
}
wp_reset_query(); ?>


Then just use CSS to arrange it the way you want. Something like this:

.category {clear: both;}
.category h3 {text-align: center;padding:20px;font-size: 24px;}
.category .post {width: 33.3%;float: left; text-align: center;margin-bottom: 10px;}
.category .post a {font-size: 14px;}
.clear {clear: both;}


Here it is in all her glory (very rough, obviously): [[LINK href="http://wp-tester.com/postscategories/"]]http://wp-tester.com/postscategories/[[/LINK]]

2011-07-25

Christianto answers:

Hi Rod,

If each post only assign to a single category this might work, please try this..
untested, change posts_per_page to what you need

<?php
/**
* The Loop
*
*/
$all_cat = get_all_category_ids();

$cat_saved = '';
$cat_title_saved = '';

$args = array(
'category__in' => $all_cat,
'posts_per_page' => 8
);

query_posts($args);

if ( have_posts() ) :?>

<section id="featured" role="contentinfo">

<?php while ( have_posts() ) : the_post();

$category = get_the_category();

$cat_now = $category[0]->cat_ID;
$cat_title_now = get_cat_name($cat_now);

if($cat_now != $cat_saved && $cat_title_now != $cat_title_saved){
?>

<?php

global $is_page;

if ( $is_page == 'template-post-gallery' ) : ?>

<header class="featured-header">

<h3 class="section-title"><?php echo $cat_title_now ?></h3><!-- .section-title -->

<?php

/**
* Section Link
*
*/

// Get theme options

$of_featured_section_link_title = c7s_get_option( 'featured_section_link_title' );
$of_featured_section_link_url = c7s_get_option( 'featured_section_link_url' );

if ( $of_featured_section_link_title && $of_featured_section_link_url ) : ?>

<a href="<?php echo esc_url( $of_featured_section_link_url ); ?>" title="<?php esc_attr_e( $of_featured_section_link_title, 'framework' ); ?> " >&raquo; <?php esc_html_e( $of_featured_section_link_title, 'framework' ); ?></a>

<?php endif; // end section title and link check ?>

</header><!-- #featured-header -->



<?php endif; // end page template check

$cat_saved = $category[0]->cat_ID;
$cat_title_saved = get_cat_name($cat_now);

}
?>

<div class="entry-container">

<?php $of_featured_hide = c7s_get_option( 'featured_hide' ); ?>

<article id="post-<?php the_ID(); ?>" <?php post_class( 'entry' ) ?>>

<?php

/**
* Entry Thumbnail
*
*/

if ( $of_featured_hide['images'] == 0 ) : // Check if thumbnail is disabled in theme options ?>

<?php locate_template( 'includes/entry-thumbnail.php', true, false ); ?>

<?php endif; // end hide featured image check ?>

<?php

/**
* Entry Header
*
*/

if ( $of_featured_hide['titles'] == 0 ) : // Check if title is disabled in theme options ?>

<header class="entry-header">
<h4><a href="<?php the_permalink() ?>" title="<?php c7s_the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
</header><!-- .entry-header -->

<?php endif; // end show title check ?>

<div id="bottom-left"> <?php if(function_exists('the_views')) { the_views(); } ?> </div>

<?php

/**
* Entry Summary
*
*/

if ( $of_featured_hide['content'] == 0 ) : // Check if content is disabled in theme options ?>

<div class="entry-summary">

<?php the_excerpt(); ?>

</div><!-- .entry-summary -->


<?php endif; // end show summary check ?>
<?php

/**
* More Link
*
*/

if ( $of_featured_hide['read_more'] == 0 ) : // Check if read more is disabled in theme options ?>

<footer class="entry-footer">
<a class="more-link" href="<?php the_permalink() ?>" title="<?php c7s_the_title_attribute(); ?>"><span><?php _e( 'View Video &rarr;', 'framework' ); ?></span></a>
</footer><!-- .entry-footer -->

<?php endif; // end read more check ?>

</article><!-- #post-## -->


<?php

/**
* Instant View Modal Box
*
*/

do_action( 'reveal_modal', $post->ID, true ); ?>


<?php endwhile; // end while loop ?>

</div><!-- .entry-container -->



<?php

/**
* Pagination
*
*/

// Get theme options

$featured_enable_pagination = c7s_get_option( 'feautred_enable_pagination', 0 );
if ( $featured_enable_pagination == 1 && $wp_query->max_num_pages > 1 ) : // Check for pages ?>

<div id="nav-below" class="pagenavi">

<?php if ( function_exists( 'wp_pagenavi' ) ) : // Check for WP Page Navi Plugin ?>

<?php wp_pagenavi(); ?>

<?php else : ?>

<div class="nav-previous"><?php next_posts_link( '<span class="meta-nav">&larr;</span>' . __( ' Older posts', 'framework' ) ); ?></div>

<div class="nav-next"><?php previous_posts_link( __( 'Newer posts ', 'framework' ) . '<span class="meta-nav">&rarr;</span>' ); ?></div>

<?php endif; // End WP Page Navi plugin check ?>

</div><!-- #nav-below -->

<?php endif; // end page check ?>

</section><!-- #featured -->


<?php

endif; // end loop

?>



Update:
I believe my answer proved to be wrong..
the content should be sort in category order so it will show correctly, and the pagination also show correctly.
wordpress didn't provide to sort query by category without additional plugin.

Also the way I get the category id, you should see Jason Manheim answer and integrate it to the code. The only down side is the pagination will not show correctly - if you need to set how many post per page...

I don't deserve for the answer prize

Thanks,
Christianto

2011-07-25

Dylan Kuhn answers:

I think the template code for what you describe is pretty straightforward, but it's not clear how it relates to your example code. You just want the additional output at some point in that template? I'm guessing you might need more than a code snippet, but also help placing it and styling it - is that right? That kind of stuff is hard to accomplish without access to the site, but if you want to try to take a snippet and run with it I'll give it a shot.

edit: Looks like Jason has done it - I'll let him run with it :)