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>
Related
I've been searching through questions/answers on here, but can't seem to find something that works with my code.
Most of the solutions I've found cause the whole page to get a 500 error. They're generally just snippets of the odd/even php, and aren't working for me to easily integrate with the custom post type loop php I have. I'm probably just not putting it in the right place, but nothing seem to be working.
I'm not super great with php, but this is the one thing I've always had an issue getting to work.
Goal:
Odd posts have the headshot on the left, bio info on the right.
Even posts have the headshot on the right, bio info on the left.
Below is my code, which does load on the page (no 500 error), but doesn't output the alternating layout, just outputs the same layout as if I didn't have the odd/even code.
<?php // theloop
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php $loop = new WP_Query( array( 'post_type' => 'team', 'posts_per_page' => -1, 'order' => 'ASC') ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php if ($wp_query->current_post % 2 == 0): ?>
<div class="row team-member"> <!--ODD LAYOUT // HEADSHOT LEFT - BIO RIGHT-->
<div class="container">
<div class="row is-table-row">
<div class="col-sm-6 headshot" style="background-image:url(<?php the_field('bio_photo'); ?>);">
<div class="box">
</div>
</div>
<div class="col-sm-6 bio cream-bg">
<div class="box">
<?php if( get_field('additional_logo') ): ?>
<div class="additional-logo"><img src="<?php the_field('additional_logo'); ?>"></div>
<?php endif; ?>
<h2><?php the_field('name'); ?></h2>
<div class="bio-content"><?php the_field('bio'); ?></div>
<div class="contact-container">
<h4>Contact me!</h4>
<?php if( get_field('phone_number') ): ?>
<p><i class="fa fa-phone" aria-hidden="true"></i> <?php the_field('phone_number'); ?></p>
<?php endif; ?>
<p><i class="fa fa-envelope" aria-hidden="true"></i> <?php the_field('email'); ?></p>
</div>
</div>
</div>
</div>
</div>
</div>
<?php else: ?>
<div class="row team-member"> <!--EVEN LAYOUT // HEADSHOT RIGHT - BIO LEFT-->
<div class="container">
<div class="row is-table-row">
<div class="col-sm-6 bio cream-bg">
<div class="box">
<?php if( get_field('additional_logo') ): ?>
<div class="additional-logo"><img src="<?php the_field('additional_logo'); ?>"></div>
<?php endif; ?>
<h2><?php the_field('name'); ?></h2>
<div class="bio-content"><?php the_field('bio'); ?></div>
<div class="contact-container">
<h4>Contact me!</h4>
<?php if( get_field('phone_number') ): ?>
<p><i class="fa fa-phone" aria-hidden="true"></i> <?php the_field('phone_number'); ?></p>
<?php endif; ?>
<p><i class="fa fa-envelope" aria-hidden="true"></i> <?php the_field('email'); ?></p>
</div>
</div>
</div>
<div class="col-sm-6 headshot" style="background-image:url(<?php the_field('bio_photo'); ?>);">
<div class="box">
</div>
</div>
</div>
</div>
</div>
<?php endif ?>
<?php endwhile; wp_reset_query(); ?>
Is there anything I've done wrong here, or a better solution to this? I'm frustrated that this is theoretically a simple request that I just can't seem to make work properly. Any help is much appreciated!
OK, pro tip: programmers are lazy. We like the DRY principle. We don't like to duplicate code, nor do we like to maintain giant blocks of duplicated code.
So, below is a modified version of your loop that is somewhat simpler, with less duplication. I would encourage you to consider other ways to reduce duplication, for example, using CSS classes (floats, possibly) to alternate which is on the left or right, and only render one version of the HTML one time.
The specific problem is that you're not accessing the $current_post property of the correct query object. You should be using $loop->current_post instead of $wpdb->current_post. However, to be super clear / explicit, I would manually set a counter, and use that instead:
<?php // theloop
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php $loop = new WP_Query( array( 'post_type' => 'team', 'posts_per_page' => -1, 'order' => 'ASC') ); ?>
<?php
// initialize the counter here
$post_count = 0;
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="row team-member">
<div class="container">
<div class="row is-table-row">
<?php
// move the if condition here to reduce / simplify code
// reference (and increment) the counter
if ($post_count++ % 2 == 0): ?>
<div class="col-sm-6 headshot" style="background-image:url(<?php the_field('bio_photo'); ?>);">
<div class="box">
</div>
</div>
<div class="col-sm-6 bio cream-bg">
<div class="box">
<?php if( get_field('additional_logo') ): ?>
<div class="additional-logo"><img src="<?php the_field('additional_logo'); ?>"></div>
<?php endif; ?>
<h2><?php the_field('name'); ?></h2>
<div class="bio-content"><?php the_field('bio'); ?></div>
<div class="contact-container">
<h4>Contact me!</h4>
<?php if( get_field('phone_number') ): ?>
<p><i class="fa fa-phone" aria-hidden="true"></i> <?php the_field('phone_number'); ?></p>
<?php endif; ?>
<p><i class="fa fa-envelope" aria-hidden="true"></i> <?php the_field('email'); ?></p>
</div>
</div>
</div>
<?php else: ?>
<div class="col-sm-6 bio cream-bg">
<div class="box">
<?php if( get_field('additional_logo') ): ?>
<div class="additional-logo"><img src="<?php the_field('additional_logo'); ?>"></div>
<?php endif; ?>
<h2><?php the_field('name'); ?></h2>
<div class="bio-content"><?php the_field('bio'); ?></div>
<div class="contact-container">
<h4>Contact me!</h4>
<?php if( get_field('phone_number') ): ?>
<p><i class="fa fa-phone" aria-hidden="true"></i> <?php the_field('phone_number'); ?></p>
<?php endif; ?>
<p><i class="fa fa-envelope" aria-hidden="true"></i> <?php the_field('email'); ?></p>
</div>
</div>
</div>
<div class="col-sm-6 headshot" style="background-image:url(<?php the_field('bio_photo'); ?>);">
<div class="box">
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
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(); ?>
i am searching since a while for a solution to set my custom post loop like this:
first post> img left, content right // second post> content left, img right
this is my code so far:
<div class="container">
<?php $loop = new WP_Query( array( 'post_type' => 'profile', 'posts_per_page' => 10 ) ); ?>
<?php if (have_posts()) : while(have_posts()) : the_post(); $i++; if(($i % 2) == 0) : ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?> role="article" itemscope itemprop="blogPost" itemtype="http://schema.org/BlogPosting">
<div class="row centered wow fadeInUpBig" data-wow-duration="2s">
<div class="col col-4">
<?php the_post_thumbnail(600); ?>
</div>
<div class="col col-6">
<section class="entry-content cf" itemprop="articleBody">
<span class="bold function"><?php echo get_the_term_list( $post->ID, 'Funktion', '', ', ', '' ); ?></span>
<h2 class="entry-title single-title" itemprop="headline" rel="bookmark"><?php the_title(); ?></h2>
<?php the_content();?>
</section>
</div>
</div>
</article>
<?php else : ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?> role="article" itemscope itemprop="blogPost" itemtype="http://schema.org/BlogPosting">
<div class="row centered wow fadeInUpBig" data-wow-duration="2s">
<div class="col col-6">
<section class="entry-content cf" itemprop="articleBody">
<span class="bold function"><?php echo get_the_term_list( $post->ID, 'Funktion', '', ', ', '' ); ?></span>
<h2 class="entry-title single-title" itemprop="headline" rel="bookmark"><?php the_title(); ?></h2>
<?php the_content();?>
</section>
</div>
<div class="col col-4">
<?php the_post_thumbnail(600); ?>
</div>
</div>
</article>
<?php endif; endwhile; endif; ?>
</div>
I know, this question has been asked a couple of times, and i tried already this and this and i read this but nothing works for me. What am i doing wrong?
I imagine its only outputting one post, and that post is actually the post content attached to the page. The problem is how you are initialising your additional loop within the page. You are creating a new post object - but you are not assigning it to the if / while statements:
<?php if (have_posts()) : while(have_posts()) : the_post(); $i++; if(($i % 2) == 0) : ?>
should be:
<?php if ($loop->have_posts()) : while($loop->have_posts()) : $loop->the_post(); $i++; if(($i % 2) == 0) : ?>
Notice the addition of the $loop variable where you are setting your post object and arguments.
i got this code (not altering) working already:
<div class="container">
<?php $loop = new WP_Query( array( 'post_type' => 'profile', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?> role="article" itemscope itemprop="blogPost" itemtype="http://schema.org/BlogPosting">
<div class="row centered wow fadeInUpBig" data-wow-duration="2s">
<div class="col col-4">
<?php the_post_thumbnail(600); ?>
</div>
<div class="col col-6">
<section class="entry-content cf" itemprop="articleBody">
<span class="bold function"><?php echo get_the_term_list( $post->ID, 'Funktion', '', ', ', '' ); ?></span>
<h2 class="entry-title single-title" itemprop="headline" rel="bookmark"><?php the_title(); ?></h2>
<?php the_content();?>
</section>
</div><!--.end col-->
</div><!--.end row-->
</article>
<?php endwhile; wp_reset_query(); ?>
</div><!--.end container-->
I have added posts on a section of a page in which I have written a query to display only two latest posts with their thumbnail on one side and text parallel to the thumbnail, what I want to do is that after each iteration I want the display to invert i.e: on first iteration thumbnail would be on right side and text on left while in the next iteration I want the thumbnail to be on left and text on right.
Here is my code for posts:
<div class="container">
<div class="col-md-12">
<h2 class="main-hadding">our blog news</h2>
</div>
<div class="blog-section">
<div class="row">
<?php
//display 2 posts for category id 47
$args=array(
// 'cat' => 47,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 2,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$Post_ID = get_the_ID ();
?>
<!-- Post Thumbnail-->
<div class="col-md-6 col-sm-6">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
</div>
<!-- Post Title and Content-->
<div class="col-md-6 col-sm-6">
<div class=" blog-contant">
<h1> <?php the_title(); ?></h1>
<p><?php the_excerpt();?></p>
</div>
</div>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
</div>
</div>
<div class="col-md-12 text-center btn-more"> MORE BLOG NEWS </div>
</div>
A long and dirty solution, but this will work.. :D
$count = 0;
while ($my_query->have_posts()) :
$my_query->the_post();
$Post_ID = get_the_ID ();
if($count == 0) : ?>
<!--Post Thumbnail-->
<div class="col-md-6 col-sm-6">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
</div>
<!--Post Title and Content-->
<div class="col-md-6 col-sm-6">
<div class=" blog-contant">
<h1> <?php the_title(); ?></h1>
<p><?php the_excerpt();?></p>
</div>
</div>
<?php else: ?>
<!--Post Title and Content-->
<div class="col-md-6 col-sm-6">
<div class=" blog-contant">
<h1> <?php the_title(); ?></h1>
<p><?php the_excerpt();?></p>
</div>
</div>
<!--Post Thumbnail-->
<div class="col-md-6 col-sm-6">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
</div>
<?php
endif;
$count++;
endwhile;
I have a custom post type set up called TESTIMONIALS and two CPT categories set up which are CLIENT TESTIMONALS & CLINIC TESTIMONIALS
I am trying to display only the posts from the CLIENT TESTIMONALS CPT category.
What would I need to add to the below to achieve this?
<div role="tabpanel" class="tab-pane fade" id="profile">
<?php query_posts('post_type=testimonials'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="testimonial-holder wrap ">
<div class="three-quarters">
<h2>
<?php the_title(); ?>
</h2>
<div class="testi">
<?php the_content(); ?>
</div>
</div>
<div class="four-col right center">
<div class="testimonial-autor-image"> <img src="<?php the_field('author_image_or_clinic_logo'); ?>" alt="Author Image">
<div class="mt20">
<?php the_field('testimonial_author'); ?>
</div>
</div>
</div>
</div>
<?php endwhile; // end of the loop. ?>
</div>
You can use something like this.
<?php
$type = 'testimonials';
$args=array(
'post_type' => $type,
'category'=>'CPT',
'post_status' => 'publish'
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="testimonial-holder wrap ">
<div class="three-quarters">
<h2>
<?php the_title(); ?>
</h2>
<div class="testi">
<?php the_content(); ?>
</div>
</div>
<div class="four-col right center">
<div class="testimonial-autor-image"> <img src="<?php the_field('author_image_or_clinic_logo'); ?>" alt="Author Image">
<div class="mt20">
<?php the_field('testimonial_author'); ?>
</div>
</div>
</div>
</div>
<?php
endwhile;
}
?>