Style latest post WordPress differently - php

My current blog page shows all my blog posts in a grid of 3 by 'x'. However at the top I want to display the latest blog post as some sort of a featured post and thus style it a bit different (i.e full width). I tried doing it through css with :first-child but that didn't really work well. So now I'm trying the php approach. I however have no clue how to approach this. Can anyone show me where to start? This is my current code.
<section id="blogs" class="cards-list">
<div class="container cards">
<div class="row center-xs">
<?php
if(get_post_type() == 'post') {
$currentBlog = get_the_ID();
} else {
$currentBlog = '';
}
$loopBlog = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => -1,
'post__not_in' => array($currentBlog)
));
while ( $loopBlog->have_posts() ) : $loopBlog->the_post();
$blogIntro = get_field('blog_intro');
$blogImage = get_field('blog_image');
$blogImageUrl = $blogImage['sizes']['large'];
?>
<div class="col col-xs-12 col-md-4">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="card card-event">
<figure style="<?php if($blogImageUrl != '') { echo "background-image:url('".$blogImageUrl."');"; } ?>"></figure>
<div class="content">
<span class="tag"><?php the_time('M d Y'); ?></span>
<div class="link"><h3><span><?php the_title(); ?></span></h3></div>
</div>
</a>
</div>
<?php
endwhile; wp_reset_query();
?>
</div>
</div>

You should be able to use current_post inside the loop and output different markup for the first post:
while ( $loopBlog->have_posts() ) : $loopBlog->the_post();
$blogIntro = get_field('blog_intro');
$blogImage = get_field('blog_image');
$blogImageUrl = $blogImage['sizes']['large'];
?>
<?php if ($loopBlog->current_post == 0): ?>
<!-- Output some other markup for the first post here -->
<div class="container-fluid">
</div>
<?php else: ?>
<div class="col col-xs-12 col-md-4">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="card card-event">
<figure style="<?php if($blogImageUrl != '') { echo "background-image:url('".$blogImageUrl."');"; } ?>"></figure>
<div class="content">
<span class="tag"><?php the_time('M d Y'); ?></span>
<div class="link"><h3><span><?php the_title(); ?></span></h3></div>
</div>
</a>
</div>
<?php endif; ?>
<?php endwhile; wp_reset_query(); ?>

Related

ACF Flexible Content - Looping two layouts (parse error?)

Getting to grips with ACF and it's flexible content field.
I have two layouts as shown below but I can't seem to get the second part (image_block) working correctly as it keeps popping up with a (parse error) I'm probably missing something obvious but if anyone has some advice it would be very helpful.
<?php if( have_rows('content-h') ):?>
<?php while ( have_rows('content-h') ) : the_row();?>
<?php if( get_row_layout() == 'text_block' ):
$title = get_sub_field('title');
$description = get_sub_field('description');
$subheading = get_sub_field('subheading');
?>
<div class="col-lg-6">
<div class="section-title">
<span class="text-color"><?php echo $subheading; ?></span>
<h2 class="mt-3 content-title"><?php echo $title; ?></h2>
</div>
</div>
<div class="col-lg-6">
<p><?php echo $description; ?></p>
</div>
<?php endif; ?>
</div>
<div class="row justify-content-center">
<?php if( get_row_layout() == 'image_block' ):
$image = get_sub_field('image');
$title = get_sub_field('title');
$description = get_sub_field('description');
$link = get_sub_field('link');
?>
<div class="col-lg-3 col-md-6 col-12">
<div class="intro-item mb-5 mb-lg-0">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" alt="" class="img-fluid w-100">
<h4 class="mt-4 mb-3"><?php echo $title; ?></h4>
<p><?php echo $description; ?></p>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
Any help is appreciated.
This is a straight PHP parsing error, nothing to do with ACF or flexible fields.
See the comment where I believe you are missing an endif. The best thing to do in these situations is to go through your code line by line noting where you are starting a conditional and whether you have ended it correctly (so the blocks are nested).
Also check that your HTML elements are formed correctly. There appears to be an extra closing div tag - see comments in the code.
<?php if( have_rows('content-h') ):?>
<?php while ( have_rows('content-h') ) : the_row();?>
<?php if( get_row_layout() == 'text_block' ):
$title = get_sub_field('title');
$description = get_sub_field('description');
$subheading = get_sub_field('subheading');
?>
<div class="col-lg-6">
<div class="section-title">
<span class="text-color"><?php echo $subheading; ?></span>
<h2 class="mt-3 content-title"><?php echo $title; ?></h2>
</div>
</div>
<div class="col-lg-6">
<p><?php echo $description; ?></p>
</div>
<?php endif; ?>
</div> <!-- THIS closing div tag seems to be spurious -->
<div class="row justify-content-center">
<?php if( get_row_layout() == 'image_block' ):
$image = get_sub_field('image');
$title = get_sub_field('title');
$description = get_sub_field('description');
$link = get_sub_field('link');
?>
<div class="col-lg-3 col-md-6 col-12">
<div class="intro-item mb-5 mb-lg-0">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" alt="" class="img-fluid w-100">
<h4 class="mt-4 mb-3"><?php echo $title; ?></h4>
<p><?php echo $description; ?></p>
</div>
</div>
<?php endif; //THIS IS WHAT NEEDS TO BE ADDED CHECK IT IS IN THE RIGHT PLACE ?>
<?php endwhile; ?>
<?php endif; ?>

Dynamically change (or user change) the number of columns in Wordpress

I'm trying to display a custom post type ' products' here:
The issue is, in the close future they will have 7 products which will leave one on a row on its own.
Is there a way I can alter the number of columns dynamically, So if there are 6 and under product it displays the posts I 3 columns, but if there are 7 items, it will display the posts section in 4 columns etc etc...
Or, is there a way I can allow the user to choose how many columns it displays in manually from the backend? I guess using something like Advanced Custom fields.
Ill be using a bootstrap based grid with a layout like this:
<div class="container">
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
</div>
</div>
so if either needs to change this structure dynamically based on the number of posts OR based on the number a user selects from a dropdown in the backend, to:
<div class="container">
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-3"></div>
<div class="col-md-3"></div>
<div class="col-md-3"></div>
</div>
</div>
Can anyone point me in the right direction to achieve this? I'm wondering if I'm overthinking it or indeed under-thinking it!
Thanks so much for looking!
PS - Heres what's I'm trying currently but it's not working...
<div class="container-flex our-products-strip">
<div class="lines-background-overlay" style="background-image: url(<?php bloginfo('stylesheet_directory'); ?>/images/background-stripes2.png);"></div>
<div class="container strip-padding">
<h2>Our Products</h2>
<hr class="hr-blue-more-bottom-space">
<div class="row justify-content-center">
<?php
$args = array(
'post_type' => 'products',
'posts_per_page' => 9999,
'orderby' => 'none'
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php $data = new WP_Query(array( 'post_type' => 'products' ));?>
<?php if(count($data) < 6 ){?>
<div class="col-md-6 col-lg-4 products-item-outer" style="--product-color: <?php the_field('product_colour'); ?>;">
<div class="col-12 products-item-inner">
<a href="<?php the_permalink(); ?>">
<div class="click-overlay"></div>
</a>
<div class="logo">
<?php
$image = get_field('logo_light');
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
</div>
<div class="excerpt"><p><?php the_field('excerpt_text'); ?></p></div>
<div class="read-more-link">Read More<span class="arrow-right"></span></div>
</div>
</div>
<?php }
else{ ?>
<div class="col-md-3 products-item-outer" style="--product-color: <?php the_field('product_colour'); ?>;">
<div class="col-12 products-item-inner">
<a href="<?php the_permalink(); ?>">
<div class="click-overlay"></div>
</a>
<div class="logo">
<?php
$image = get_field('logo_light');
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
</div>
<div class="excerpt"><p><?php the_field('excerpt_text'); ?></p></div>
<div class="read-more-link">Read More<span class="arrow-right"></span></div>
</div>
</div>
<?php } ?>
<?php endwhile; wp_reset_postdata(); endif; ?>
</div>
</div>
</div>
</div>
If you are using php then definitely you can put if condition to perform the check
below is the example
<?php if(count($data) <= 6){?>
<div class="container">
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-3"></div>
<div class="col-md-3"></div>
<div class="col-md-3"></div>
</div>
</div>
<?php }
else{ ?>
<div class="container">
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
</div>
</div>
<? } >

PHP interfering with Advanced Custom Fields?

The PHP code seems to be blocking or interfering with my advanced custom fields as it doesn't display unless I remove the PHP code then it does. I can't figure out where the issue is.
Any help is much appreciated.
PHP
<?php if(strpos($_SERVER['REQUEST_URI'], 'gaeilge') !== false) {
$newsCat = 'cat=5,7&showposts=3';
} else {
$newsCat = 'cat=6,8&showposts=3';
}; ?>
Advanced Custom Fields
<div class="carousel-item active">
<div class="row py-5">
<?php if( have_rows('block') ): ?>
<?php while( have_rows('block') ): the_row();
// vars
$content = get_sub_field('content');
?>
<div class="col-lg-4 col-md-4">
<?php if(strpos($_SERVER['REQUEST_URI'], 'gaeilge') !== false) { ?> <!--Check if url contains the word "items" -->
<h2 class="fw-b c-blue mt-0">Ár bhFís</h2>
<?php } else { ?>
<h2 class="fw-b c-blue mt-0">Our Vision</h2>
<?php } ?>
</div>
<div class="col-lg-8 col-md-8">
<p class="c-blue mb-0"><?php echo $content; ?></p>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
Fixed it by just changing how News articles where added.
<div class="row pt-4 pb-3">
<?php
// args
$args = array(
'posts_per_page' => -1,
'post_type' => 'post'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="col-lg-4 col-md-4 col-sm-6 mb-5">
<div class="w-100 mb-2 px-2">
<img class="w-100" src="<?php $featimage = the_post_thumbnail_url('news-image'); ?>" alt="">
<p class="text-muted mt-4 mb-2"><?php echo get_the_date('dS M, Y'); ?></p>
<h3 class="c-blue"><?php the_title(); ?></h3>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
</div>

In wordpress, how to show next and previous post

I am able to display next and previous post in worspress but unable to show second-previous or third-previous || second-next or third-next like
1 - I want to show this too
2 - this is showing
3 - My Current post
4 - This is showing
5 - I want to show this too
any help would be appreciated.
In the and I am showing you my code so you can judge.
CODE:
<?php $next = get_permalink(get_adjacent_post(false,'',false)); if
($next != get_permalink()) { ?><a href="<?php echo $next; ?>">
<li class="col-xs-12 col-md-4">
<div class="article">
<div class="contain-image">
<?php $nextPost = get_next_post(true); $nextthumbnail = get_the_post_thumbnail($nextPost->ID); echo $nextthumbnail; ?>
</div>
<div class="content">
<div class="double-content">
<div class="information">
<span class="category"><?php echo get_cat_name(1);?></span>
<span class="time"><?php the_time('M j, Y') ?></span>
</div>
<div class="title">
<?php next_post_link('%link', "%title", TRUE); ?>
</div>
<p>
<?php
$Nextpost = get_next_post($id);
echo apply_filters(‘the_content’, $Nextpost->post_content);
?>
</p>
</div>
</div>
</div>
</li>
</a>
<?php } ?>
<?php $prev = get_permalink(get_adjacent_post(true,'',true)); if
($prev != get_permalink()) { ?><a href="<?php echo $prev; ?>">
<li class="col-xs-12 col-md-4">
<div class="article">
<div class="contain-image">
<?php $prevPost = get_previous_post(true); $prevThumbnail = get_the_post_thumbnail($prevPost->ID); echo $prevThumbnail; ?>
</div>
<div class="content">
<div class="double-content">
<div class="information">
<span class="category"><?php echo get_cat_name(1);?></span>
<span class="time"><?php the_time('M j, Y') ?></span>
</div>
<div class="title">
<?php previous_post_link('%link', "%title", TRUE); ?>
</div>
<p>
<?php
$Prevpost = get_previous_post($id);
echo apply_filters(‘the_content’, $Prevpost->post_content);
?>
</p>
</div>
</div>
</div>
</li>
</a>
<?php } ?>
WordPress provides several navigational template tags to make it easy for visitors to surf your pages. There are basically two different types of template tags used for chronological post navigation:
posts_nav_link() – for navigating various archive (non-single) pages
previous_post_link() & next_post_link() – for navigating single-post pages
<?php $posts = query_posts($query_string); if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php previous_post_link(); ?> | <?php next_post_link(); ?>
<?php endwhile; endif; ?>
try this:
global $post;
$post_curr = $post;
//get last post
$post_last1 = get_previous_post();
setup_postdata($post_last1);
//get second last post
$post_last2 = get_previous_post();
setup_postdata($post_curr);
//get next post now
$post_next1 = get_next_post();
setup_postdata($post_next1);
//get second next post
$post_next2 = get_next_post();
//reset current post data
setup_postdata($post_curr);

Wordpress getting posts from category in personal layout

I am trying to get the posts from evenementen. I only want 4 posts because my layout is a 2x2 column. This column has a special template for the top and bot. I want to show the 4 evenementen in this column but I don't want to change the layout if there are less.
In the PHP file I loop all the posts in evenementen and want to do add these in the specific columns.
PHP FILE
<?php
$args = array( 'posts_per_page' => 4, 'offset'=> 1, 'category' => 0 );
$myposts = get_posts( $args );
$count = 0;
foreach ( $myposts as $post ) : setup_postdata( $post );
$count++;
if ($count == 1) {
$title1 = the_title();
$date1 = the_date();
$link1 = the_permalink();
}
elseif ($count == 2) {
$title2 = the_title();
$date2 = the_date();
$link2 = the_permalink();
}
elseif ($count == 3) {
$title3 = the_title();
$date3 = the_date();
$link3 = the_permalink();
}
elseif ($count == 4) {
$title4 = the_title();
$date4 = the_date();
$link4 = the_permalink();
}
else {
}
endforeach;
?>
<div class="wrapper">
<div id="bigone">
<div class="wrapper">
<h4 class="push"><?php echo $title1; ?></h4>
<div id="one"> <p class="greytext"><?php echo $date1; ?></p></div>
<div id="two"> <p class="opmaak">Evenementen</p></div>
</div>
</div>
<div id="bigtwo">
<div class="evenementenborder">
<div class="wrapper">
<h4 class="push"><?php echo $title2; ?></h4>
<div id="one"> <p><?php echo $date2; ?></p> </div>
<div id="two"> <p class="opmaak">Evenementen </p></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<div class="evenementenfooter">
<div class="wrapper">
<div id="bigone">
<div class="wrapper">
<h4 class="push"><?php echo $title3; ?></h4>
<div id="one"> <p class="greytext"><?php echo $date3; ?></p></div>
<div id="two"> <p>Evenementen</p></div>
</div>
</div>
<div id="bigtwo">
<div class="evenementenborder">
<div class="wrapper">
<h4 class="push"><?php echo $title4; ?></h4>
<div id="one"> <p class="greytext"><?php echo $date4; ?></p> </div>
<div id="two"> <p>Evenementen </p></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
This is the closest I got so far. But this does not put the text on the proper position and the hyperlink is also in the text.
Webpage
I want to ask you guys: what is the best way to solve this? Or if I made a mistake in the code why it is not on the proper position?
I found out that informatie is directly placed on the page and the echo can't find the information because on that moment it is empty. I don't know how to solve this. Please post if you see what I am doing wrong.
i think its the buffering, Put in the beggining
ob_start();
And in the end of the document
ob_end_flush();
And try again.
Check here for documentation about buffering

Categories