Custom first post on home page wordpress - wordpress

I need to change this code so the first post displays with the class "first" and also has the firsttop class along with firstbottom. The rest of the inside pages eg page/2/ , page/3/ and so on will not have this class. I have tried many things please help not sure whats wrong here.
<?php
if (have_posts()) :
$count = 0;
while (have_posts()):
the_post();
if (get_post_type() == 'post'):
?>
<?php
if (!is_single() && $count == 0):
?>
<div class="firsttop<?php echo !is_home() ? "notop" : "" ?>"></div>
<?php endif; ?>
<article class="post <?php echo !is_single() ? "preview" : "" ?> <?php echo $count == 0 ? "first" : "" ?> <?php echo !is_home() ? "full" : "" ?>">
<p class="byline">
<?php the_time('F j, Y'); ?>
</p>
<h3>
<?php the_title(); ?>
</h3>
<div>
<?php the_content('<span class="more">Read More...</span>'); ?>
</div>
<?php include ('post-info.php'); ?>
</article>
<?php
if (!is_single() && $count == 0):
?>
<div class="firstbottom<?php echo !is_home() ? "nobottom" : "" ?>"></div>
<?php endif; ?>
<?php
endif;
$count++;
endwhile;
endif;
?>

Have you tried to use a filter for this?
Read more about wordpress add_filter here http://codex.wordpress.org/Function_Reference/add_filter
You can make a new plugin or add it to your functions.php file in your theme directory.
This guy did it with post_class, but it might not work for you if you didn't use the post_class in your posts loop page. http://wpsnipp.com/index.php/functions-php/add-class-to-first-post-in-the-loop/
Here's a snippet of his code:
add_filter( 'post_class', 'wps_first_post_class' );
function wps_first_post_class( $classes ) {
global $wp_query;
if( 0 == $wp_query->current_post )
$classes[] = 'first';
return $classes;
}

Related

Wordpress how to display query results in specific div class if total number of posts is < 2

I'm working on a new Wordpress site and am trying to achieve something specific. I'd like to do a query_post and if there is only 1 post available, display that information in a specific div class, otherwise use a different class. Here is a simplified version of the code I am currently using. The one I will end up using, actually has additional query_posts within this:
<?php query_posts('post_type=events&showposts=-1'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$type = get_field('event_type');
if( $type && in_array('public', $type) ): ?>
<a href="<?php the_permalink(); ?>">
<div id="event" class="public secondary left">
<?php else: ?>
<a href="mailto:johnsmith#gmail.com?subject=<?php echo the_field('event_date'); ?> - <?php the_title(); ?> Event Inquiry">
<div id="event" class="private secondary left">
<?php endif; ?>
<?php else: ?>
<?php
$type = get_field('event_type');
if( $type && in_array('public', $type) ): ?>
<a href="<?php the_permalink(); ?>">
<div id="event" class="public secondary right">
<?php else: ?>
<a href="mailto:johnsmith#gmail.com?subject=<?php echo the_field('event_date'); ?> - <?php the_title(); ?> Event Inquiry">
<div id="event" class="private secondary right">
<?php endif; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
I hope this makes sense..
You can check the global query after query_posts() like this before you run through the posts and display them:
<?php query_posts( 'post_type=events&showposts=-1' );
global $wp_query;
$classes = array( 'secondary' );
if( $wp_query->post_count == 1 ) :
$classes[] = 'left';
else :
$classes[] = 'right';
endif; ?>
With the above, your code can be shortened to something like this:
<?php if ( have_posts() ) :
while ( have_posts() ) : the_post();
$type = get_field( 'event_type' );
if ( $type && in_array( 'public', $type ) ) :
$classes[] = 'public';
$action = get_permalink();
else :
$classes[] = 'private';
$action = 'mailto:johnsmith#gmail.com';
endif; ?>
<!-- Output HTML -->
<a href="<?php echo $action; ?>">
<div id="event" class="<?php echo join( ' ', $classes ); ?>">
<?php endwhile;
endif; ?>

Need to Remove Duplicate Posts in a Foreach Loop in WordPress code

I am currently looping an open position (post type) and cities in a state (position-location). The problem that I'm having is the position locations if there are more than two are duplicating the open position:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $termsCo = get_the_terms($post->ID, 'position-location' );
foreach ($termsCo as $termCo) :
$locationCo = $termCo->name;
$parentCo = $termCo->parent;
if ($parentCo === 95) : ?>
<a class="ui grid segment" href="<?php echo get_permalink(); ?>">
<div class="twelve wide column">
<?php echo the_title(); ?>
</div>
<div class="four wide column">
<?php foreach ($termsCo as $location) : ?>
<?php if ( $location->name === 'Fort Collins' || $location->name === "Western Colorado" ) : ?><span class="comma"><?php echo $location->name; ?></span><?php endif; ?>
<?php endforeach; wp_reset_postdata(); ?>
</div>
</a>
<?php endif; endforeach; ?>
<?php endwhile; wp_reset_postdata(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
See visual here: https://web.jub.com/open-positions/
You have two foreach loops (one in line 4 and one in line 13). If I understand correctly, the problem is that the first loop should only enter the if part once per post ID. Have you tried a break statement in the end of your if?
Namely line 18:
<?php break; endif; endforeach; ?>

Wordpress: check if current post is first in the loop

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; ?>

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; ?>

960gs different classes on teasers posts

I'm having hard time trying to use teasers post in my wordpress theme (based on 960gs), as you can see here http://img17.imageshack.us/img17/794/schermata20110420a15045.png what I got till now is one "featured" post and three teasers post with thumbnails that will probably be six (so it'll have seven posts displaied in the homepage). The problem is that to do so I have to assign a class "grid_2 alpha" to the teasers post and I don't know how to assign this class to just the first teaser on the left, lefting the other ones with no alpha or omega class and putting the omega class to just the last teaser post (the seventh).
If can help, here's the code I'm using for the loop:
<?php $firstClass = 'firstpost'; ?>
<?php /* Start loop */ ?>
<?php while (have_posts()) : the_post(); ?>
<?php if (function_exists('yoast_breadcrumb')) { if (is_page() && $post->post_parent) { yoast_breadcrumb('<p id="breadcrumbs">','</p>'); } } ?>
<div class="post <?php echo $firstClass; ?>">
<?php $firstClass = 'grid_2 alpha'; ?>
<img src="<?php echo get_post_meta($post->ID, "Thumbnail", true);?>" width="140" height="100" style="padding-bottom:20px;" />
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<nav id="page-nav"><p>' . __('Pages:', 'roots'), 'after' => '</p></nav>' )); ?>
</div>
<?php endwhile; // End the loop ?>
also I would like to know how I can add some text above the teaser section under the first featured post. Sorry for the too many questions and for my bad english, as you can understand I'm not a developer but I searched for one week and couldn't find anything helpful for my problems. Thanks in advance for any help, I really appreciate it.
<?php $count = 0; ?>
<?php /* Start loop */ ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1):
$class = "firstpost';
elseif ($count == 2):
$class = "grid_2 alpha";
elseif ($count == $wp_query->post_count):
$class = "grid_2 omega";
else:
$class = "grid_2";
endif;
?>
<?php if (function_exists('yoast_breadcrumb')) { if (is_page() && $post->post_parent) { yoast_breadcrumb('<p id="breadcrumbs">','</p>'); } } ?>
<div class="post <?php echo $class; ?>">
<img src="<?php echo get_post_meta($post->ID, "Thumbnail", true);?>" style="padding-bottom:20px;" />
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<nav id="page-nav"><p>' . __('Pages:', 'roots'), 'after' => '</p></nav>' )); ?>
</div>
<?php endwhile; // End the loop ?>

Resources