I want to output the results of a foreach statement but I want them grouped into a div in 3's
So like:
<div>image image image</div>
<div>image image image</div>
<div>image image image</div>
Here is my code so far:
$args = array( 'numberposts' => 3, 'offset'=> 0, 'category' => 9 );
$myrows = get_posts($args);
foreach($myrows as $row) { ?>
<div>
<?php if ( has_post_thumbnail($row->ID)) {
echo '<a href="' . get_permalink( $row->ID ) . '" title="' . esc_attr($row->post_title ) . '">';
echo get_the_post_thumbnail($row->ID);
echo '</a>';
}?>
</div>
<?php } ?>
$myrows = get_posts($args);
$chunks = array_chunk($myrows,3);
?>
<?php foreach($chunks as $myrows): ?>
<div>
<?php foreach($myrows as $row): ?>
<div>
<?php if(has_post_thumbnail($row->ID)): ?>
<a href="<?=get_permalink($row->ID)?>" title="<?=esc_attr($row->post_title)?>">
<?=get_the_post_thumbnail($row->ID)?>
</a>
<?php endif ?>
</div>
<?php endforeach ?>
</div>
<?php endforeach ?>
You can create blocks using array_chunk():
foreach (array_chunk($myrows) as $mychunk) {
echo '<div>';
foreach ($mychunk as $row) {
// print your entries
if (has_post_thumbnail($row->ID)) {
echo sprintf('%s',
get_permalink( $row->ID ),
esc_attr($row->post_title ),
get_the_post_thumbnail($row->ID)
);
}
}
echo '</div>';
}
Granted, if the if condition isn't met, you would get blocks of zero, one or two items instead of the expected three.
<div>
<?php
$args = array('numberposts' => 3, 'offset' => 0, 'category' => 9);
$myrows = get_posts($args);
foreach($myrows as $idx => $row) {
if ($idx % 3 == 0) echo "</div><div>";
if (has_post_thumbnail($row->ID)) {
echo '<a href="' . get_permalink($row->ID) . '" title="' . esc_attr($row->post_title) . '">';
echo get_the_post_thumbnail($row->ID);
echo '</a>';
} ?>
</div>
Why not use the modulo operator?
$counter = 0;
echo '<div>';
foreach ($myrows as $row)
{
$counter++;
if ($counter % 3 == 0) echo '</div><div>';
echo $row;
}
echo '</div>';
Try this code.
<?php
$args = array( 'numberposts' => 3, 'offset'=> 0, 'category' => 9 );
$myrows = get_posts($args);
$tempCnt=0;
foreach($myrows as $row) {
//12
if($tempCnt==3)
{
$tempCnt=0;
//do your reset code here.
}
$tempCnt++;
?>
<div>
<?php if ( has_post_thumbnail($row->ID)) {
echo '<a href="' . get_permalink( $row->ID ) . '" title="' . esc_attr($row->post_title ) . '">';
echo get_the_post_thumbnail($row->ID);
echo '</a>';
}?>
</div>
<?php } ?>
Related
I want to insert php code in between tabby tabs shortcodes.
I am using a plugin tabby tab for tab view and have added this code in my theme template:
<?php echo do_shortcode('[tabby title="Gallary Name"]
name content
[tabby title="Images"]
[tabbyending]'); ?>
I want to use a custom fields gallery under images tab using code like this:
<?php echo do_shortcode('[tabby title="Gallary Name"]
name content
[tabby title="Images"]
<?php
$i = 0;
$images = get_field('vil_pics');
if( $images ): ?>
<div>
<ul>
<?php foreach( $images as $image ): ?>
<li<?php if ( $i % 3 == 0 ) echo ' class="break"' ?>>
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a><p>.</p>
</li>
<?php endforeach; ?>
</ul></div>
<?php endif; ?>
[tabbyending]'); ?>
This code is not working, it's showing a blank page. How can I fix this?
Tabby uses a global variable to track what's going on, so I think either one of these will work. The first one is a little more straightforward, but the second one will definitely work.
Option 1: output everything in order:
echo do_shortcode( '[tabby title="Gallery Name"] name content' );
echo do_shortcode( '[tabby title="Images"]' );
// your php code as-is
$i = 0;
$images = get_field('vil_pics');
if( $images ): ?>
<div>
<ul>
<?php foreach( $images as $image ):
$i++ ?>
<li<?php if ( $i % 3 == 0 ) echo ' class="break"' ?>>
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a><p>.</p>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif;
echo do_shortcode( '[tabbyending]' );
or Option 2: save everything to a variable and output it all at once:
$output = '';
$output .= '[tabby title="Gallery Name"] name content';
$output .= '[tabby title="Images"]';
$i = 0;
$images = get_field('vil_pics');
if ( $images ) {
$output .= '<div><ul>';
foreach( $images as $image ) {
$i++;
$li_class = ( $i % 3 == 0 ) ? ' class="break"' : '';
$output .= '<li' . $li_class . '>';
$output .= '<a href="' . $image['url'] . '">';
$output .= '<img src="' . $image['sizes']['thumbnail'] . '" alt="' . $image['alt'] . '" />';
$output .= '</a><p>.</p></li>';
}
$output .= '</div></ul>';
}
$output .= '[tabbyending]';
echo do_shortcode( $output );
Note that I didn't see anything increasing $i so I added that. Everything else is as-is.
i've created a colophon for my website in which i post all the logos of the different sponsors i have. I add all the sponsors via custom post type. i also added a specific custom taxonomy to distinguish between the different typologies of sponsorships.
I use this code in the footer.php to display them:
<?php $terms = get_terms('sponsor_tipology');
$count = count( $terms );
if ( $count > 0 ) {
foreach ( $terms as $term ) { ?>
<div class="col-xs-12 <?php echo $term->slug ;?>">
<h3><?php echo $term->name;?></h3>
<?php $arg = array (
'post_type' => 'colophone',
'post_per_page' => -1,
'sponsor_edition' => 'current',
'sponsor_tipology' => $term->slug,
);
$pesca_post = new WP_Query ($arg);
$quanti_post = $pesca_post->post_count;
if(have_posts()){
while ($pesca_post->have_posts()) : $pesca_post->the_post();
$featured = get_the_post_thumbnail_url(get_the_ID(),'large');
if ($quanti_post == 5){
$classe_bootstrap = 15;
}elseif ($quanti_post > 5){
$classe_bootstrap = "2 text-center";
}elseif($quanti_post < 5){
$classe_bootstrap = 12/$quanti_post;
}
echo '<div class="col-md-' . $classe_bootstrap . '">';
if (isset($featured)){
$img = $featured;
}else{
$img = get_template_directory_uri() . '/img/placeholder.png';
} ?>
<a href="<?php echo esc_attr(get_permalink($msd_settings['partner_page'])); ?>" title="<?php echo get_the_title($post->ID);?>" >
<div class="col-xs-12" style="background-image:url(<?php echo esc_url($img); ?>); height:100px;background-size:contain;background-repeat:no-repeat;background-position:center center;"></div>
</a>
<?php echo '</div>';
endwhile;
}?>
</div>
<?php }
}?>
my problem is that this code is completely working just on some pages, on other it shows the contents avoiding the ones belonging to the first term, no matter which it will be.
I have noticed that it works in pagaes where i use other queries.
What am i doing wrong?
i changed it in this way and now it's working!
$terms = get_terms('sponsor_tipology');
$count = count( $terms );
if ( $count > 0 ) {
foreach ( $terms as $term ) { //per ogni termine presente
$nome = $term->slug;?>
<div class="col-xs-12 <?php echo $term->slug ;?>">
<h3><?php echo $term->name;?></h3>
<?php $arg = array (
'post_type' => 'colophone',
'post_per_page' => -1,
'sponsor_edition' => 'current',
'sponsor_tipology' => $nome,
);
$elementi = get_posts($arg);
$quanti_post = count( $elementi );
if ($quanti_post == 5){
$classe_bootstrap = 15;
}
elseif ($quanti_post > 5){
$classe_bootstrap = "2 text-center";
}
elseif($quanti_post < 5){
$classe_bootstrap = 12/$quanti_post;
}
foreach($elementi as $elemento){
$featured = get_the_post_thumbnail_url($elemento->ID,'large');
if (isset($featured)){
$img = $featured;
}
else{
$img = get_template_directory_uri() . '/img/placeholder.png';
} ?>
<div class="col-md-<?php echo $classe_bootstrap; ?>">
<a href="<?php echo esc_attr(get_permalink($msd_settings['partner_page'])); ?>" title="<?php echo get_the_title($elemento->ID);?>" >
<div class="col-xs-12" style="background-image:url(<?php echo esc_url($img); ?>); height:100px;background-size:contain;background-repeat:no-repeat;background-position:center center;"></div>
</a>
</div>
<?php }?>
</div>
<?php }
}?>
I am trying to modify a portfolio template in a Wordpress theme. I'd like to apply a specific css class to a specific category of the portfolio, using its ID.
I've tried many things thanks to the different answers already brought here about conditional css (How to apply a CSS class depending on a value contained in PHP variable?), php variables, or about the use of the " foreach", but I wasn't able to find a solution :
May be I didn't put my condition at the right place, or may be is it because of the use of the foreach, or may is there a misplaced tag??
The result remains the same, a full blank page.
I hope you'll have few time to have a look on this and give me a little help.
Here is the original code extract :
<?php $terms_array = array(); $id = $cat = null; ?>
<?php foreach ( $p_terms as $term ) { ?>
<?php $terms_array[$term->term_id] = array( 'name' => $term->name ); ?>
<?php } ?>
<?php if ( count( $terms_array ) > 0 ) { ?>
<div class="row">
<div class="col-md-12">
<ul id="ef-portfolio-filter" class="list-inline text-center">
<?php if ( count( $terms_array ) > 1 ) { ?>
<li class="cbp-filter-item-active"><a href="#" data-filter="*"><?php _e( 'All', EF_TDM ) ?>
<span class="cbp-filter-counter"></span>
</a>
</li>
<?php } ?>
<?php $cur = count( $terms_array ) == 1 ? ' class="cbp-filter-item-active"' : ''; ?>
<?php foreach ( $terms_array as $id => $cat ) { ?>
<?php echo '<li' . $cur . '><a data-filter=".cbp-filter-' . $id . '" href="' . esc_url( get_term_link( $id, EF_CPT_TAX_CAT ) ) . '">' . $cat['name'] . '<span class="cbp-filter-counter"></span></a></li>'; ?>
<?php } ?>
</ul><!-- #ef-portfolio-filter -->
</div><!-- .col-md-12 -->
</div><!-- .row -->
<?php } ?>
<?php } ?>
And here is one of my tries. I just paste an extract as I didn't change anything above it and just introduce my condition after the foreach :
<?php $cur = count( $terms_array ) == 1 ? ' class="cbp-filter-item-active"' : ''; ?>
<?php foreach ( $terms_array as $id => $cat ) { ?>
<?php
if ($id = myspecific id)
{echo '<li class="cbp-filter-item-custom"' . $cur . '><a data-filter=".cbp-filter-' . $id . '" href="' . esc_url( get_term_link( $id, EF_CPT_TAX_CAT ) ) . '">' . $cat['name'] . '<span class="cbp-filter-counter-custom"></span></a></li>';
}else{
echo '<li ' . $cur . '><a data-filter=".cbp-filter-' . $id . '" href="' . esc_url( get_term_link( $id, EF_CPT_TAX_CAT ) ) . '">' . $cat['name'] . '<span class="cbp-filter-counter"></span></a></li>' } ?>
</ul><!-- #ef-portfolio-filter -->
I have a bootstrap "tab" system going on where each tab is it's own category name:
<?php $categories= get_categories();
$firstCat = 1;
foreach ($categories as $cat) {
$trimmedCatName = str_replace(' ', '', $cat->cat_name);
echo '<li';
if ($firstCat == 1) {
echo ' class="active"';
}
echo '>'.''.$cat->cat_name.' <small style="color:#447294">('.$cat->category_count.')</small></li>';
$firstCat++;
}
?>
This above ^ code works fine and sets the tabs up nicely.
The problem I have is with cycling through the categories as "tab-content" and then for each separate category, showing all the post titles/images for that category. Here's what I have so far:
<div class="tab-content">
<?php $categories= get_categories();
$firstCat = 1;
foreach ($categories as $cat) {
$trimmedCatName = str_replace(' ', '', $cat->cat_name);
echo '<div class="tab-pane ';
if ($firstCat == 1) {
echo 'active';
}
echo '" id="#'.$trimmedCatName.'">'.
'<select class="image-picker">';
$posts = get_posts($cat);
if ($posts) {
foreach ($posts as $p) {
echo '<option>';
echo get_the_post_thumbnail( $p->ID, 'medium' ).'<br>';
echo '</option>';
}
}
echo '</select>';
$firstCat++;
}
?>
</div>
I'm confused on how to get this code correctly.
<div class="tab-content">
<?php $categories= get_categories();
$firstCat = 1;
foreach ($categories as $cat) {
$trimmedCatName = str_replace(' ', '', $cat->cat_name);
echo '<div class="tab-pane ';
if ($firstCat == 1) {
echo 'active';
}
echo '" id="#'.$trimmedCatName.'">'.
'<select class="image-picker">';
$posts = get_posts(array('category' => $cat->term_id));
if ($posts) {
foreach ($posts as $p) {
echo '<option>';
echo get_the_post_thumbnail( $p->ID, 'medium' ).'<br>';
echo '</option>';
}
}
echo '</select>';
$firstCat++;
}
?>
</div>
notice i replaced $cat->term_id
if you are still getting nothing try hardcoding the category ID and see if you get any results.
try this..
add_action('init','test');
function test(){
var_dump(get_posts(array('category' => 1)));
}
I want the page http://zanifesto.com/product/boise-infographic/ go back to http://zanifesto.com/gallery, not the Infographics category page at the "Back to" link at the top.
When I replace the .home_url etc etc you see below with the gallery url mentioned above, it errors out.
I am looking to understand why the php code doesn't act like I am replacing one url for another.
<div class="product_navigation desktops">
<?php
$term_list = '';
$j=0;
foreach ($terms as $term) {
if($term->parent==0){
$j++;
if( $j <= 1 ){
$term_list .= '' . $term->name . '';
}
}
}
if(strlen($term_list) > 0){ echo '<div class="nav-back">‹ '. __('Back to ', 'theretailer').$term_list.'</div>'; };
?>
<?php if (function_exists('be_previous_post_link')) { ?>
<div class="nav-next-single"><?php be_next_post_link( '%link', '', true,'', 'product_cat' ); ?></div>
<div class="nav-previous-single"><?php be_previous_post_link( '%link', '', true,'', 'product_cat' ); ?></div>
<div class="nav-prev-next-txt"><?php _e('Prev / Next', 'theretailer'); ?></div>
<?php } ?>
<div class="clr"></div>
</div>
<?php } ?>
this code hack could do what you want...
<?php
$term_list = '';
$j=0;
foreach ($terms as $term) {
if($term->parent==0){
$j++;
if( $j <= 1 ){
$url=home_url() . '/' . $category_slug . '/'. $term->slug;
if($url=='http://zanifesto.com/product-category/infographics'){
$url='http://zanifesto.com/gallery';
}
$term_list .= '' . $term->name . '';
}
}
}
if(strlen($term_list) > 0){ echo '<div class="nav-back">‹ '. __('Back to ', 'theretailer').$term_list.'</div>'; };
?>