Show First Post in Full, Followed by Truncated Posts - php

I am trying to write a custom default loop for my blog that will display the first post in full, then have the following posts appear in truncated format with a thumbnail of featured image. I have tried everything I can think of, but cannot figure out how to properly parse the loop. I tried the code from the Wordpress forums, but neither work.
<?php
if (is_front_page() && ++$count == 1) {
the_content();
} else {
$excerpt = get_the_excerpt(); echo string_limit_words($excerpt,80); ?>
}
and
<?php
$count = 0;
if ($count > 0) {
$excerpt = get_the_excerpt(); echo string_limit_words($excerpt,80);
} else {
the_content();
}
$count++;
?>
I have been working on this for weeks. I would greatly appreciate if anyone can help me with this.

As you are using a "default" loop I'm sure you can use this variable, $current_post, which is defined inside the Loop.
<?php
if (is_front_page() && $wp_query->current_post === 1) {
the_content();
} else {
$excerpt = get_the_excerpt(); echo string_limit_words($excerpt,80);
}
?>

Try This :
<?php $i=0; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php $i+=1; ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="post-date"><?php the_time('F j, Y') ?></div>
<h2><?php the_title(); ?></h2>
<?php if($i<2): the_content(); ?>
<?php else: ?>
<?php the_excerpt(); ?>
...

Related

Query Advanced Custom Field not displaying

We have ACF Pro for WP and we have created an ACF which show a location which is a select.
When trying to output we are getting this:
Notice: Trying to get property of non-object in
/home/cwplantactiveint/public_html/wp-content/themes/cwplant/loop-jobs.php
on line 66
Which is this
<?php $location = get_field('job_location'); echo $location->post_title; ?>
Now oddly, it outputs another custom field which was createdto output the date:
<?php if(get_field('closing_date')) { ?>
<?php the_field('closing_date'); ?>
<?php } else { ?>
Ongoing
<?php } ?>
The whole code block looks like this:
<?php while ( have_posts() ) : the_post(); ?>
<?php /* Check closing date is not past. */
$today = strtotime("now");
$closedate = strtotime(get_field('closing_date'));
if ($today < $closedate || !get_field('closing_date')) {
?>
<div class="singlepost infobox info-job content cfix">
<h2><?php the_title(); ?></h2>
<p><span class="red first">Location:</span> <?php $location = get_field('job_location'); echo $location->post_title; ?>
<span class="red">Closing Date:</span>
<?php if(get_field('closing_date')) { ?>
<?php the_field('closing_date'); ?>
<?php } else { ?>
Ongoing
<?php } ?>
</p>
<?php if ( is_archive() || is_search() || is_home()) : // Only display excerpts for archives and search. ?>
<?php the_excerpt(); ?>
<a class="button" href="<?php the_permalink(); ?>">View Details</a>
<?php else : ?>
<?php the_content( __( 'Continue reading →', 'twentyten' ) ); ?>
<?php endif; ?>
</div>
<?php $jobstrue = 'true'; ?>
<?php } else { ?>
<?php $jobsfalse = 'true'; ?>
<?php } ?>
<?php endwhile; // End the loop. Whew. ?>
I think your problem is that you haven't reset the $post object with wp_reset_postdata() so your query is returning the last thing in $post (likely createdto in your case).
Whenever I handle any object in WP, I always set it up and then reset it like this:
<?php
// YOUR CUSTOM FIELD
// 0- Reset post object from last query (this should really be part of your last query)
wp_reset_postdata();
// 1- Put custom field into post object and check for content
$post_object = get_field('location');
// 2- Check to make sure object exists and setup $post (must use $post variable name)
if ($post_object) {
$post = $post_object;
setup_postdata($post);
// 3- Do some magic
echo get_field('closing_date');
// 4- reset postdata so other parts of the page can use it
wp_reset_postdata();
}
?>

how to display odd number post in left side even number post in right side?

<?php $counter = 3; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( in_category('3') ): ?>
<?php else: ?>
<?php endif; ?>
<?php if($counter%2 == 0){echo 'floatRight';} else { echo 'floatLeft'; } ?>
<?php the_ID(); ?>
<h1 > <?php the_title(); ?></h1>
<?php the_post_thumbnail('full'); ?>
<?php the_content(__('(more...)')); ?>
<?php comments_template(); // Get wp-comments.php template ?>
<?php if($counter%2 == 0){ echo "<div class='clear'></div>";} ?>
<?php $counter++; ?>
<?php endwhile; else: ?>
<?php endif; ?>
i am trying to display in this way and i want to display particular category of post
post1
post2
post3
post4
please give me the solution ...
use the below code.
<?php while(have_posts()) : ?>
<?php $i++; if(($i % 2) == 0) : $wp_query->next_post(); else : the_post(); ?>
<?php the_content(); ?>
<?php endif; endwhile; ?>
<?php $i = 0; rewind_posts(); ?>
<?php while(have_posts()) : ?>
<?php $i++; if(($i % 2) !== 0) : $wp_query->next_post(); else : the_post(); ?>
<?php the_content(); ?>
<?php endif; endwhile; ?>
You can use this css3 property to do it (This would take care odd and even elements, you don't have to write down the loop explicitly):
p:nth-child(odd) //you can do the same for div
{
float:left;
}
p:nth-child(even)
{
float:right;
}
You can use query_post() or Wp_query() in wordpress .
Use this code. to get the particular categories post .
<?php
// The Query
query_posts( 'cat=3' );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>

Getting last item in count variable

So I'm using WordPress and created a variable $post_count to track the number of posts.
Right now I'm using if($post_count == 1) to add a class if it's the first post, which works fine, but I can't figure out how to get the last post.
Would that be possible using just a variable to count posts? Or is there a better way to do this than creating a count variable? Here's my code so far:
if($query->have_posts()): $post_count = 0; ?>
<div class="image-grid">
<?php while($query->have_posts()): $post_count++; $query->the_post(); ?>
<div class="item <?php if($post_count == 1) { ?>first_item<?php
} elseif() { ?>last item<?php } ?>">post content here</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
I believe you can do
$query->found_posts;
to get the total number of posts. So:
if($query->found_posts == $post_count)
should work.
<?php if($query->have_posts()): $post_count = 0; ?>
<div class="image-grid">
<?php while($query->have_posts()):
$post_count++;
$query->the_post();
?>
<div class="item <?php if($post_count == 1) { echo 'first_item'; } if( $query->found_posts == $post_count ) { echo 'last item'; } ?>">
<?php //post content here ?>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>

Wordpress: Most recent post in one column, other posts in two columns

My client is looking to change her homepage to look similar to this site: http://www.eatliverun.com/
Where the most recent post is up top in full in one column and the others are in two column excerpts.
She wants to have just one recent post display and the rest in the two column format. I'm just not sure how to accomplish this correctly.
Her website is located at http://pamelastable.com/
Integrating a counter into your loop can easily help you achieve this layout. Try something as follows (note: the 'six columns' class represents your 2 column layout in a 12 column grid):
<?php $count = 1; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ($count == 1) { ?>
<article>
<div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</article>
<?php $count++; ?>
<?php } else { ?>
<?php if($count % 2 == 0) { ?>
<div>
<? } ?>
<div class="six columns">
<article>
<div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</article>
</div>
<?php if($count % 2 == 1) { ?>
</div>
<? } ?>
<?php $count++; ?>
<? } ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Just add a counter to the loop and style the html differently for the first iteration.
<?php $i = 0; while ( have_posts() ) : the_post(); //inside loop ?>
<?php if ($i++ == 0) : ?>
<div>First Post</div>
<?php else: ?>
<div>Other Posts</div>
<?php endif; ?>
<?php endwhile; ?>
In your index.php or other template file^
Just use a counter to see which post number you are on.
<?php
if (have_posts()) {
$i = 0;
while (have_posts()) {
the_post();
if (++$i == 1) {
echo '<div class="entry-large">';
} else {
echo '<div class="entry-small">';
}
the_content();
echo '</div>'
}
}

If Else with a twist - maybe

Can i make a If Else-statement that finishes a div before doing "else"?
I want to cycle through my posts and put the posts from "specialCat" in one div.
<div id="the-div-i-want-to-close">
<?php if (in_category('specialCat')) { ?>
<h1><?php the_title(); ?></h1>
<?php } else { ?>
<h1><?php the_title(); ?></h1>
<?php } ?>
Thanks a lot :)
Your best bet is to modify the query calling your special category then finishing the loop with the rest of the posts.
<?php
$args = array(
'category_name' => 'special-cat', // Use Category slug
'posts_per_page' => -1 // Get all the post in SpecialCat
);
$special_query = new WP_Query( $args );
?>
<div id="the-div-i-want-to-close">
<?php
while ( $special_query->have_posts() ) : $special_query->the_post();
$no_repeat[] = $post->ID ?>
<h1><?php the_title(); ?></h1>
<?php endwhile; ?>
</div> <?php // Close your special div after looping through all SpecialCat posts
// Start the loop again
if (have_posts() ) : while (have_posts() ) : the_post();
if (in_array( $post->ID, $no_repeat ) ) continue; // Exclude the posts we already got above ?>
<h1><?php the_title(); ?></h1>
<?php endwhile; endif; ?>
You can include HTML within a for each loop, so long as you close the PHP tags (or include the HTML in an echo statement).
Would this do it for you?
<div id="the-div-i-want-to-close">
<?php $isOpen = true; ?>
<?php if (in_category('specialCat')) { ?>
<?php the_title(); ?>
<?php } else { ?>
<?php if($isOpen) { ?>
</div>
<?php $isOpen = false; ?>
<?php } ?>
<?php the_title(); ?>
<?php } ?>

Categories