Depending how many posts in a row have conditional HTML - php

I'm using Advanced Custom Fields to display a CPT on a certain page. I have it set up so that it wraps every 3 posts in a div class of o-table-cell with a parent div using the class o-table, which is working fine.
I'm using display:table; and display:table-cell as the content all has to relate to each other in terms of height and positioning and this works best.
How it works so far:
<div class="o-table">
<div class="o-table-cell">Dynamic Content</div>
<div class="o-table-cell">Dynamic Content</div>
<div class="o-table-cell">Dynamic Content</div>
</div>
<div class="o-table">
<div class="o-table-cell">Dynamic Content</div>
<div class="o-table-cell">Dynamic Content</div>
<div class="o-table-cell">Dynamic Content</div>
</div>
However sometimes I only have one or two posts per row, which causes the o-table-cell to fill the o-table, regardless of width and max-width being set. So I would like to also count if there is only 1 post or two posts in a row, so I can add in some blank o-table-cell divs.
Example of how I would like it work:
<div class="o-table">
<div class="o-table-cell">Dynamic Content</div>
<div class="o-table-cell">Dynamic Content</div>
<div class="o-table-cell">Dynamic Content</div>
</div>
<div class="o-table">
<div class="o-table-cell">Dynamic Content</div>
<div class="o-table-cell">Dynamic Content</div>
<div class="o-table-cell">Empty table cell</div>
</div>
<div class="o-table">
<div class="o-table-cell">Dynamic Content</div>
<div class="o-table-cell">Empty table cell</div>
<div class="o-table-cell">Empty table cell</div>
</div>
<div class="o-table">
<div class="o-table-cell">Dynamic Content</div>
<div class="o-table-cell">Dynamic Content</div>
<div class="o-table-cell">Dynamic Content</div>
</div>
My code so far:
<?php
$posts = get_field('training_courses_posts');
if( $posts ): ?>
<div class="o-table o-mrg-btm-1">
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT)
if ($i > 0 && ($i %3 == 0)) {
?>
</div>
<div class="o-table o-mrg-btm-1">
<?php } ?>
<?php setup_postdata($post); ?>
<div class="o-table-cell o-table-vt classes-item-wrapper classes-item-alt">
<div class="classes-item <?php the_field('color'); ?>">
<a href="<?php the_permalink(); ?>" class="classes-image">
<?php
$thumb = get_post_thumbnail_id();
$img_url = wp_get_attachment_url($thumb, 'full'); //get img URL
$image = aae_aq_resize($img_url, 550, 400, true, '', true);
?>
<img src="<?php echo $image[0]; ?>">
</a>
<h2 class="classes-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
<div class="classes-excerpt"><?php the_field('next_course');?></div>
find out more
</div>
</div>
<?php $i++; endforeach; ?>
</div>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
CSS:
.o-table {
display: table;
height: 100%;
width: 100%;
}
.o-table-cell {
display: table-cell;
-webkit-box-flex: 0;
-ms-flex: 0 0 33.3333333333%;
flex: 0 0 33.3333333333%;
max-width: 33.3333333333%;
width: 33.3333333333%;
margin-bottom: 30px;
height: 100%;
}

The Code is very much similar to yours, instead of using modulus I used equality check. I am using it in same way, in my website also.
I hope it will work for you as well.
$count = 0;
$posts = get_field('training_courses_posts');
if( $posts ): ?>
<div class="o-table o-mrg-btm-1">
<?php
foreach( $posts as $post): // variable must be called $post (IMPORTANT)
setup_postdata($post);
$thumb = get_post_thumbnail_id();
$img_url = wp_get_attachment_url($thumb, 'full'); //get img URL
$image = aae_aq_resize($img_url, 550, 400, true, '', true);
if($count == 3)
{
echo '</div><div class="o-table o-mrg-btm-1">';
}
?>
<div class="o-table-cell o-table-vt classes-item-wrapper classes-item-alt">
<div class="classes-item <?php the_field('color'); ?>">
<a href="<?php the_permalink(); ?>" class="classes-image">
<img src="<?php echo $image[0]; ?>">
</a>
<h2 class="classes-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
<div class="classes-excerpt">
<?php the_field('next_course');?>
</div>
find out more
</div>
</div>
<?php $count++;
endforeach;
?>
</div>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
?>

Related

Only show <php "the_field()" ?> if it has a specifik value

So, i was wondering if there's a way to target a specific value in a <?php the_field('title'); ?> loop.
I'm iterating over a result set of people on a site. If title equals CEO it should display in another row.
<?php
/*
Template Name: Personal
*/
?>
<?php get_header(); ?>
<div id="main" class="fullpage" style="background-image: url(<?php echo get_template_directory_uri(); ?>/img/fullpagebg.png);">
<section class="banner innerBanner">
<div class="container">
<div class="bannerContent">
<h1>Vår <strong>personal</strong></h1>
</div>
</div>
</section>
<section class="padtop0">
<div class="container-fluid">
<div class="productsMain">
<div class="row justify-content-center">
<?php
$loop = new WP_Query( array( 'post_type' => 'Persons') );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-xl-3 col-lg-4 col-md-6 wow fadeInUp" data-wow-delay=".2s">
<div class="personalbox" style="background-color: #f3f3f3; background-image: url(<?php echo get_field('bild'); ?>); background-size: <?php echo $background_size; ?>;">
<div class="personalbox-description">
<h4 style="color:#000000 !important;"><strong><?php the_field('Namn'); ?></strong></h4>
<h6 style="color:#000000 !important;;"><?php the_field('title'); ?></h6>
<br>
<h6 style="color:#000000 !important;"><strong>Telefon:</strong> <?php the_field('telefon'); ?></h6>
<h6 style="color:#000000 !important;"><strong>Mailt:</strong> <?php the_field('e-post'); ?></h6>
</div>
</div>
</a>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
</div>
</div>
</div>
</section>
<?php get_footer(); ?>

How to show first post differently from other posts?

I created the design that will be used to show first post differently from the others. It's not that easy because the first post needs to be in his own div id, which is completely different from the other posts.
Here is the code I currently use in wordpress php:
<?php get_header(); ?>
<!-- Begin Content -->
<div id="content-wide">
<div class="post">
<div class="imgwide" style="background: linear-gradient(to bottom, rgba(0,0,0,0) 60%, rgba(0,0,0,0.8)), url(<?php echo catch_that_image() ?>); background-repeat: no-repeat; background-size: 100%; background-position: center;">
<div class="p-heading">
<h1>
<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h1>
<div class="p-content">
Excerpt text of the first post goes here but php the_excerpt doesnt work for this wide paragraph
</div>
</div>
</div>
</div>
</div>
<div id="content-a">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<div class="imgdiv"><img src="<?php echo catch_that_image() ?>" width="250"></div>
<div class="p-heading">
<h1>
<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h1>
</div>
<div class="p-content">
<?php the_excerpt(); ?>
</div>
<div class="p-info">
<?php the_time('j.m.Y') ?> |
<?php the_category(', '); ?> | <img src="http://www.virmodrosti.com/wp-content/uploads/comments.png" alt="Komentarji" width="26" height="12">
<?php $totalcomments = get_comments_number(); echo $totalcomments; ?>
</div>
</div>
<?php endwhile; ?>
and here is my site url http://www.virmodrosti.com/zdravje/
All I want is that first post isn't displayed twice, but is only moved to the wide post design. The big post is in content-wide. Let me know how to do that. Thank you.
try to add a counter that starts in zero and increment it inside your while loop, then use if else statement to check the value of counter if zero display the first post else the other posts.
EDITED
<?php $counter = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php if($counter == 0) { ?>
<div id="content-wide">
<div class="post">
<div class="imgwide" style="background: linear-gradient(to bottom, rgba(0,0,0,0) 60%, rgba(0,0,0,0.8)), url(<?php echo catch_that_image(); ?>); background-repeat: no-repeat; background-size: 100%; background-position: center;">
<div class="p-heading">
<h1><?php the_title(); ?></h1>
<div class="p-content">
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
</div>
<?php } ?>
<?php if($counter > 0) { ?>
<div class="post">
<div class="imgdiv"><img src="<?php echo catch_that_image() ?>" width="250"></div>
<div class="p-heading">
<h1><?php the_title(); ?></h1></div>
<div class="p-content">
<?php the_excerpt(); ?>
</div>
<div class="p-info">
<?php the_time('j.m.Y') ?> |
<?php the_category(', '); ?> | <img src="http://www.virmodrosti.com/wp-content/uploads/comments.png" alt="Komentarji" width="26" height="12">
<?php $totalcomments = get_comments_number(); echo $totalcomments; ?>
</div>
</div>
<?php } ?>
<?php $counter ++; ?>
<?php endwhile; ?>

Text moves out of Heading Tags

Text within my h2 tags is floating to the top of the division. I feel like there may be something my eyes are overlooking that is causing the title of the post to float to the top of the division. Unfortunatly I cannot see another area where I output the tite.
Here is my HTML/PHP. I appologize for the messyness.
<!-- locations page array -->
<?php $args = array( 'post_type' => 'weblizar_portfolio','posts_per_page' => -1 );
$portfolio = new WP_Query( $args );
if( $portfolio->have_posts() )
{ while ( $portfolio->have_posts() ) : $portfolio->the_post(); ?>
<div class="col-lg-4 col-md-4">
<!--<div class="img-wrapper">-->
<?php $defalt_arg = array('class' => "enigma_img_responsive");
$portfolio_button_link = get_post_permalink( $sample );
$dates = get_post_meta(get_the_ID(), 'date', true);
$postTitle = the_title(); ?>
<?php if(!isMobile()){ ?>
<a href='<?php echo $portfolio_button_link; ?>'>
<div class="author-item">
<div class="imageItem">
<?php if(has_post_thumbnail()):
the_post_thumbnail('portfo_4_thumb', $defalt_arg);
$port_thumbnail_id = get_post_thumbnail_id();
$image_thumbnail_url = wp_get_attachment_url($port_thumbnail_id);
endif; ?>
</div>
<div class="author-info">
<!-- THIS IS THE AREA NOT WORKING -->
<h2><?php echo $postTitle; ?></h2>
<h4><?php echo $dates; ?></h4>
</div>
</div>
</a>
<?php } else { ?>
<table>
<a href='<?php echo $portfolio_button_link;?>'>
<tr>
<td>
<?php echo $postTitle;?>
</td>
<td>
<?php echo $dates; ?>
</td>
</tr>
</a>
</table>
<?php } ?>
And here is the executed html:
<div class="col-lg-4 col-md-4">
<!--<div class="img-wrapper">-->
San Jose <a href="">
<div class="author-item">
<div class="imageItem">
<img width="260" height="160" src="" class="enigma_img_responsive wp-post-image" alt="San Antonio" srcset="" sizes="(max-width: 260px) 100vw, 260px"> </div>
<div class="author-info">
<h2></h2>
<h4>April 23rd, 2016</h4>
</div>
</div>
</a>
<!--</div>-->
</div>
You should replace the_title with get_the_title. The reason is that the_title just echos the title wherever you use it, while get_the_title returns it (which is what you want).
Edit:
Another solution is to call the_title() where are displaying your h2 tag like this:
<h2><?php the_title() ?></h2>

Change order by date on category wordpress

I need help with a product category page in Wordpress. Original code was made by someone else, and I'm fairly new to php. I may need help with looking for a way to make the product sort by date published. right now, here is the code that I have.
category-books.php
<?php
/**
* Template Name: Store Template
* #package WordPress
*/
get_header(); ?>
<div id="page-title">
<!-- 960 Container Start -->
<div class="container">
<div class="sixteen columns">
<h1>
<?php single_cat_title( '', true ); ?>
</h1>
</div>
</div>
<!-- 960 Container End -->
</div>
<?php
get_template_part( 'content', 'product4' ); ?>
<div class="blogsidebar" style="float:left !important">
<?php dynamic_sidebar('sidebar-1'); ?>
</div>
<?php get_footer(); ?>
content-product4.php
<?php function bac_wp_strip_header_tags_only( $excerpt ) {
$regex = '#(<h([1-6])[^>]*>)#';
$excerpt = preg_replace($regex,'', $excerpt);
return $excerpt;
}
add_filter( 'the_content', 'bac_wp_strip_header_tags_only', 0); ?>
<div class="container">
<?php $sidebar_side = get_post_meta($post->ID, 'incr_sidebar_layout', true);
if($sidebar_side == "left-sidebar") {
get_sidebar();
} ?>
<!-- Blog Posts ================================================== -->
<div class="twelve columns" style="float:right !important;">
<div class="product-container">
<?php $posts = query_posts($query_string . 'orderby=date&order=DESC&cat=238'); ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Post -->
<div <?php post_class('post'); ?> id="product-post-2Col" > <a class="post_title" href="<?php the_permalink() ?>">
<div style="margin-bottom:10px;width: 130px;height: 180px !important; border: 2px solid #000 !important;padding: 3px;float: left;margin: 0 15px 15px 0;overlay:hidden;">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail( 'medium', array( 'class' => 'store-featured-image' ) );
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/product-image.png" alt="<?php the_title(); ?>" />
<?php } ?>
</div>
<div style="min-height:40px !important;width:50% !important;float:left;">
<h4>
<?php the_title(); ?>
</h4>
</a> </div>
<?php?>
<div class="product-description-2col">
<?php the_excerpt()?>
<a style="margin-bottom: 8px;" class="post_title" href="<?php the_permalink() ?>">Learn More</a><br/>
<?php ?>
<a href="<?php the_field('buy_now_1')?>">
<div id="buybtn">Buy Now</div>
</a>
<?php ?>
</div>
<?php ?>
<p> </p>
<p> </p>
</div>
<!-- Post -->
<?php endwhile; // End the loop. Whew. ?>
</div>
<div style="text-align:center;">
<?php posts_nav_link( ' · ', 'previous page', 'next page' ); ?>
</div>
</div>
<!-- eof eleven column -->
is there a way to add something to functions.php? I've added this line but still not working:
<?php $posts = query_posts($query_string . 'orderby=date&order=DESC&cat=238'); ?>
<?php while (have_posts()) : the_post(); ?>
Thanks for the help.
Instead of using functions.php for this, create a child theme and edit a copy of the file you want to change there.
https://codex.wordpress.org/Child_Themes

How to break the content in two parts to use php the_content1 and php the_content2

I have a chunk of content in a textarea that I can call dynamically to the front by using <php the_content ?> See full code below.
The problem is, I dont have an assigned area for the image. (I'm open to options to create one but I think it will be too much effort), anyway, is there anything I can do (JQuery / JS is fine) that I can call the image to fill the: <?php echo $the_content['image'] ?> that you see in my code, which obviously does not work? Meanwhile leaving the actual text part or rest of the content separate.
I am attaching an illustration for a more visual idea of what I am looking for
Here is the entire code for that section
<section class="box classes-box" id="classes_box"><!-- Section Events -->
<div class="container light-grey-background">
<div class="row">
<div class="col-md-6 no-padding">
<div class="boxing-classes" style="background-image: url('<?php echo $the_content['image'] ?>')">
<div class="classes">
<nav class="classes-nav">
<ul class="clean-list toggle-list clearfix">
<?php foreach($slides as $i => $slide): ?>
<li class="classes-menu-item ">
<input type="radio" id="toggle-<?php echo $slide['post']->ID; ?>" name="toggle-helper" autocomplete="off">
<label for="toggle-<?php echo $slide['post']->ID; ?>"><?php echo get_the_title( $slide['post']->ID ) ?></label>
</li>
<?php endforeach; ?>
</ul>
</nav>
</div>
</div>
</div>
<div class="col-md-6 no-padding ">
<?php foreach($slides as $i => $slide): ?>
<div class="classes-content-block" id="classes_content_<?php echo $slide['post']->ID ?>" style="display:none;">
<header class="padding white-background">
<h2 class="entry-header black-background"><?php echo get_the_title( $slide['post']->ID ) ?></h2>
</header>
<div class="entry-content padding">
<?php echo apply_filters('the_content', $slide['post']->post_content); ?>
<?php if ( !empty( $slide['options']['button'] )): ?>
<div class="white-background">
<a class="read-more text-center red-black-hover" href="<?php echo $slide['options']['button']['link'] ? $slide['options']['button']['link'] : '#'; ?>"><?php echo $slide['options']['button']['link_text'] ? $slide['options']['button']['link_text'] : 'View Timeline' ; ?></a>
</div>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
Normally, what people do is to create a Shortcode that will produce the desired HTML, something like:
[image-block src="apple-touch-icon.png" class="all-imgs" id="my-img"]The content[/image-block]
This would be the shortcode:
add_shortcode( 'image-block', function( $atts, $content) {
return sprintf(
'<div class="left"><img src="%1$s" class="%2$s" id="%3$s"></div><div class="right">%4$s</div>',
$atts['src'],
$atts['class'],
$atts['id'],
$content
);
});
And this the output:
See CSS property FLOAT : left|right
<div id=container class="clearfix">
<div class="image-wrapper" style="float:left;max-width:50%;padding:0;margin:0">
<img src="images/mypicture.jpg" alt="" class="rwd-image" id="mymage">
</div>
<div class="content-wrapper" style="float:left;max-width:50%;padding:0;margin:0">
<p>Lorem Ipsum</p>
</div>
</div>

Categories