Wordpress: check if current post is first in the loop - wordpress

I need to style the first post in the loop differently from the rest in a wordpress theme template. Is there a specific wordpress function to check for this or do I just need to set a "first" flag like the below?
<?php while ( have_posts() ) : ?>
<?php the_post(); ?>
<?php if ( $first = !isset( $first ) ) : ?>
<!-- First Post HTML -->
<?php else : ?>
<!-- Every other posts HTML -->
<?php endif; ?>
<?php endwhile; ?>
The function I'm looking for would replace the $first = ! isset( $first) check. Does this function exist in WordPress?

Try this
<?php $Inc=0; ?>
<?php while ( have_posts() ) : ?>
<?php the_post(); ?>
<?php if ( $Inc==0 ) : ?>
<!-- First Post HTML -->
<?php else : ?>
<!-- Every other posts HTML -->
<?php endif; ?>
<?php $Inc++; ?>
<?php endwhile; ?>

Related

ACF Group field not displaying on the front end

I have created a group field in ACF to display on posts, pages and custom post type. Please see below screenshots.
And here's the code I'm trying for front-end.
<?php $footerCTA = get_field('footer_cta');
echo $footerCTA['title']; ?>
The code above doesn't output anything on the front-end. Am I missing here something?
Thanks.
try this:
if( have_rows('footer_cta') ):
while( have_rows('footer_cta') ) : the_row();
?>
<p><?php the_sub_field('title'); ?></p>
<?php
endwhile;
endif;
?>
Try using.
echo the_field('footer_cta');
Other Way.
You can do this by adding a second parameter to the get_field or the_field functions. This second parameter will contain the correct ID which can be found using get_option('page_for_posts') like so
<h1><?php the_field('footer_cta', get_option('page_for_posts')); ?></h1>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( have_posts() ) : ?>
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div><!-- #content -->
</div><!-- #primary -->

Wordpress loop for one category set by an ACF

So I want to return a Wordpress loop that returns just one category, so currently I have something like this
<?php query_posts('cat=5'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
This works great and does exactly what I want it to do
But I want the category number to be set by an ACF, so I have that set up and it's returning my value just as a string on the site, all good again, so now my code looks like this
<p>My category number is - <?php the_field('category_number'); ?></p>
<?php query_posts('cat=5'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
What I want to do is something like this
<p>My category number is - <?php the_field('category_number'); ?></p>
<?php query_posts('"cat=", the_field("category_number")'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
I don't think I'm going about this the right way and can't seem to find an answer for this as I don't think I'm looking for the right thing .. if someone could point me in the right direction on this I'd be really grateful
Use get_the_field instead of the_field in query posts method something like this
query_posts('cat=get_the_field("category_number")');
note:- the_field() is echo everything which field contain.
get_the_field is only get the field not echo it.
Hope it will help you :)
Have you tried something along these line?
https://wordpress.stackexchange.com/questions/127940/use-advance-custom-field-inside-query-post-command
<?php query_posts('cat=<?php the_field("category_number"); ?>'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
Or Maybe something like this?
https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

the_title function for Wordpress doesn't work

I'm convering a html one page to wordpress but I can't seem to get the title for each section to return the right vaule.
This code is form page.php which displays the one page:
<?php
// Template Name: One Page Layout
get_header();
?>
<?php
// Main Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
// GET HEADER
get_template_part( 'front-header');
// PROMOTION SECTION
get_template_part( 'front');
// LATEST WORK
get_template_part( 'lwork');
// LATEST POSTS
get_template_part( 'lpost');
// ABOUT ME
get_template_part( 'about');
// CONTACT
get_template_part( 'contact');
endwhile; endif; wp_reset_query();
get_footer();
?>
Here is the title I want to show (lpost.php):
<!-- section heading -->
<section class="section-heading">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</section>
What am I missing out on??
You need to put loop above the section
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<section class="section-heading">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</section>
<?php endwhile; endif; wp_reset_query(); ?>
Your code should be:
<?php
query_posts(array('post_type' => 'post'));
// Main Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
you can use this code : get_title()
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<section class="section-heading">
<h2><?php get_title(); ?></h2>
<?php the_content(); ?>
</section>
<?php endwhile; endif; wp_reset_query(); ?>

Display ACF Repeater Fields on Child Pages

I'd like to display the results of an ACF repeater field on child pages. An example of the current code:
<?php if( have_rows('recommended-properties') ): ?>
<?php while ( have_rows('recommended-properties') ) : the_row(); ?>
<?php the_sub_field('recommended-properties-title'); ?>
<?php the_sub_field('recommended-properties-description'); ?>
<?php endwhile; ?>
<?php endif; ?>
Now, I thought I may be able to store the parent page ID in a variable and use that the same way you can get values from another post (see: http://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/)
I tried the following:
<?php $parentPageId = wp_get_post_parent_id( $post_ID ); ?>
<?php if( have_rows('recommended-properties', echo $parentPageId;) ): ?>
<?php while ( have_rows('recommended-properties', echo $parentPageId;) ) : the_row(); ?>
<?php the_sub_field('recommended-properties-title'); ?>
<?php the_sub_field('recommended-properties-description'); ?>
<?php endwhile; ?>
<?php endif; ?>
But no luck unfortunately. Any suggestions for where I can go from here?
Many thanks!
You don't need add echo when you add parameter in function
try below code
<?php $parentPageId = wp_get_post_parent_id( $post_ID ); ?>
<?php if( have_rows('recommended-properties', $parentPageId) ): ?>
<?php while ( have_rows('recommended-properties', $parentPageId) ) : the_row(); ?>
<?php the_sub_field('recommended-properties-title'); ?>
<?php the_sub_field('recommended-properties-description'); ?>
<?php endwhile; ?>
<?php endif; ?>

why the_author() doesn't working

I am learning WordPress theme development and I have developed a theme. All work has been done, but the author name not displaying in a blog page. I used the following:
<?php the_author(); ?>
but it is not working. Is it necessary to do any function or code in functions.php?
Basically, this is the loop of a page in wordpress:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
some wordrpess code
<?php endif; ?>
You have to enter the get request INTO the loop, something like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php $author = get_the_author(); ?>
or
<?php the_author(); ?>
<?php endif; ?>
If not within the loop you can try this
<?php the_author_meta( 'display_name', $userID ); ?>
From Codex

Resources