Somehow in my custom posts-blog-page in wordpress I want to be able to add the class "selected" on the first-post.
What I am doing is the following:
<?php if (have_posts()) : ?>
<?php $postcount = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $postcount++; ?>
<?php if ($postCount == 0) { ?>
<li class="selected" data-date="<?php the_time('F jS, Y'); ?>">
<a class="news-box" href="<?php the_permalink(); ?>" target="_self">
<?php the_post_thumbnail(); ?>
<div class="news-inner">
<div class="news-inner-wrapper">
<h4><?php the_title(); ?></h4>
<div class="read-more"><?php the_excerpt(__('Continue reading »','example')); ?></div>
<div class="news-inner-article-date"><small>By <?php the_author_link(); ?></small></div>
</div>
</div>
</a>
</li>
<?php } else { ?>
<li data-date="<?php the_time('F jS, Y'); ?>">
<a class="news-box" href="<?php the_permalink(); ?>" target="_self">
<?php the_post_thumbnail(); ?>
<div class="news-inner">
<div class="news-inner-wrapper">
<h4><?php the_title(); ?></h4>
<div class="read-more"><?php the_excerpt(__('Continue reading »','example')); ?></div>
<div class="news-inner-article-date"><small>By <?php the_author_link(); ?></small></div>
</div>
</div>
</a>
</li>
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>
However this applies the class "selected" on all li's.
Any ideas?
There's a lot of duplication there, just to display the class if $postcount == 0
Also, it shouldn't evaluate to true, since you $postcount++ too early.
Try this:
<?php if (have_posts()) : ?>
<?php $postcount = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<li class="class="<?php if($postcount == 0) { echo 'selected'; } ?>" data-date="<?php the_time('F jS, Y'); ?>">
<a class="news-box" href="<?php the_permalink(); ?>" target="_self">
<?php the_post_thumbnail(); ?>
<div class="news-inner">
<div class="news-inner-wrapper">
<h4><?php the_title(); ?></h4>
<div class="read-more"><?php the_excerpt(__('Continue reading »','example')); ?></div>
<div class="news-inner-article-date"><small>By <?php the_author_link(); ?></small></div>
</div>
</div>
</a>
</li>
<?php $postcount++; ?>
<?php endwhile; ?>
<?php endif; ?>
Theres useless code there. Try this
<?php if (have_posts()) {
$postcount = 0;
while (have_posts()) : the_post();
if ($postcount == 0) { ?>
<li class="selected" data-date="<?php the_time('F jS, Y'); ?>">
<?php } else { ?>
<li data-date="<?php the_time('F jS, Y'); ?>">
<?php } ?>
<a class="news-box" href="<?php the_permalink(); ?>" target="_self">
<?php the_post_thumbnail(); ?>
<div class="news-inner">
<div class="news-inner-wrapper">
<h4><?php the_title(); ?></h4>
<div class="read-more"><?php the_excerpt(__('Continue reading »','example')); ?></div>
<div class="news-inner-article-date"><small>By <?php the_author_link(); ?></small></div>
</div>
</div>
</a>
</li>
<?php
$postcount++;
endwhile;
?>
Related
I've put this code to the index.php on wordpress, but it doesn't show up anything. I thought it should shows all the posts latest to newest. Anyone can solve this?
<div class="card">
<div class="card-image">
<a href="<?php echo the_permalink(); ?>">
<img src="<?php echo get_the_post_thumbnail_url(get_the_ID); ?>" alt="Card Image">
</a>
</div>
<div class="card-description">
<a href="<?php the_permalink(); ?>">
<h3><?php the_title(); ?></h3>
</a>
<div class="card-meta">
Đăng bởi <?php the_author(); ?> vào <?php the_time('j F, Y') ?> trong <?php echo get_the_category_list(',') ?>
</div>
<p>
<?php echo wp_trim_words(get_the_excerpt(), 30); ?>
</p>
Tìm hiểu thêm
</div>
</div>
<?php }
wp_reset_query();
?>
Thank you very much.
You're missing half of a loop pretty much ...
If you want to use the post template that you have in you question, then you can do something like this:
<?php if ( have_posts() ):
$i = 0;
while ( have_posts() ):
$i++;
if ( $i > 1 ):
echo '<hr>';
endif; ?>
<div class="card">
<div class="card-image">
<a href="<?php echo the_permalink(); ?>">
<img src="<?php echo get_the_post_thumbnail_url(get_the_ID); ?>" alt="Card Image">
</a>
</div>
<div class="card-description">
<a href="<?php the_permalink(); ?>">
<h3><?php the_title(); ?></h3>
</a>
<div class="card-meta">
Đăng bởi <?php the_author(); ?> vào <?php the_time('j F, Y') ?> trong <?php echo get_the_category_list(',') ?>
</div>
<p>
<?php echo wp_trim_words(get_the_excerpt(), 30); ?>
</p>
Tìm hiểu thêm
</div>
</div>
<?php
endwhile;
endif; ?>
Should be working.
I am not finding out why archive.php only shows maximum 10 posts in a category... Could you please help to find where the bug is?
<div id="blogg_header" class="col col-lg-12 col-sm-12"><div class="well">
<?php if (is_category()) {?>
<h1 class="title">Kategori: <i><?php single_cat_title(); ?> </i></h1>
<?php } elseif (is_month()) { ?>
<h2 clss="title"><?php the_time('F, Y'); ?></h2>
<?php } ?>
</div></div>
<div id="blogg_innlegg_left" class="col col-lg-8 col-sm-8"><div class="well">
<ul>
<?php if (have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<a class id="linkforside" href="<?php the_permalink(); ?>">
<?php get_the_post_thumbnail($header_thumb->ID);?><br/>
</a>
<p class id="infotekst"><?php the_time('d.m.Y') ?></p>
<p class="blogg_innlegg_teksten"><?php echo substr(strip_tags($post->post_content), 0, 250);?></p>
<?php endwhile; ?>
<?php else : ?>
<h2>Ingenting ble funnet.</h2>
<?php endif; ?>
</ul>
</div></div>
I guess I may need to insert the following:
<?php wp_get_archives('type=postbypost&limit=none'); ?>
I've made a home page with four different categories to be shown.I think it was working well, but for now the all posts have the same content as the first post. Links, and featured images, are good, but text somehow is being overwritte.
Heress the screen all texts are the same:
http://imagizer.imageshack.us/v2/800x600q90/713/m1j6.jpg
Edited: so this works with <?php setup_postdata( $post ); ?> inside loops
My code:
<div class="bmw">
<h2>bmw news</h2>
<?php $k = 1;
$posts = get_posts('category=7&orderby=date&numberposts=2'); foreach($posts as $post) { ?>
<?php setup_postdata( $post ); ?>
<div id="home_post<?php if($k%2 == 0) echo "last" ;?>">
<?php if ( has_post_thumbnail() ) {?>
<a href="<?php the_permalink() ?>"><div id="img">
<span>
<?php the_post_thumbnail(); ?>
</span>
</div>
</a>
<?php }; ?>
<h3><?php the_title(); ?></h3>
<span id="tags"><?php the_tags(); ?></span>
<?php the_content('...'); ?>
<div class="button_more">Czytaj więcej<span><img style="vertical-align:middle;width:auto;margin-left:5%" src="http://test.startujac.pl/images/strzalka_czytaj_wiecej.png"></span></div>
</div>
<?php $k++; ?>
<?php } ?>
</div>
<div class="news">
<h2>VW news</h2>
<?php $v = 1;
$posts = get_posts('category=12&orderby=date&numberposts=2'); foreach($posts as $post) { ?>
<?php setup_postdata( $post ); ?>
<div id="home_post<?php if($v%2 == 0) echo "last" ;?>">
<?php if ( has_post_thumbnail() ) {?>
<a href="<?php the_permalink() ?>"><div id="img">
<span>
<?php the_post_thumbnail(); ?>
</span>
</div></a>
<?php } ?>
<h3><?php the_title(); ?></h3>
<span id="tags"><?php the_tags(); ?></span>
<?php the_content('...'); ?>
<div class="button_more">Czytaj więcej<span><img style="vertical-align:middle;width:auto;margin-left:5%" src="http://test.startujac.pl/images/strzalka_czytaj_wiecej.png"></span></div>
</div>
<?php $v++; ?>
<?php }?>
</div>
<div class="japan">
<h2>japan news</h2>
<?php $k = 1;
$posts = get_posts('category=13&orderby=date&numberposts=2'); foreach($posts as $post) { ?>
<?php setup_postdata( $post ); ?>
<div id="home_post<?php if($k%2 == 0) echo "last" ;?>">
<?php if ( has_post_thumbnail() ) {?>
<a href="<?php the_permalink() ?>"><div id="img">
<span>
<?php the_post_thumbnail(); ?>
</span>
</div></a>
<?php } ?>
<h3><?php the_title(); ?></h3>
<span id="tags"><?php the_tags(); ?></span>
<?php the_content('...'); ?>
<div class="button_more">Czytaj więcej<span><img style="vertical-align:middle;width:auto;margin-left:5%" src="http://test.startujac.pl/images/strzalka_czytaj_wiecej.png"></span></div>
</div>
<?php $k++; ?>
<?php } ?>
</div>
<div class="events">
<h2>imprezy i zloty</h2>
<?php $k = 1;
$posts = get_posts('category=1&orderby=date&numberposts=4'); foreach($posts as $post) { ?>
<?php setup_postdata( $post ); ?>
<div id="home_post<?php if($k == 1) echo "first" ;?>">
<?php if ( has_post_thumbnail() ) {?>
<div id="img">
<span>
<?php the_post_thumbnail(); ?>
</span>
</div>
<?php } ?>
<div id="content">
<h3><?php the_title(); ?></h3>
<span id="tags"><?php the_tags(); ?></span>
<?php the_content('...'); ?>
</div>
</div>
<?php $k++; ?>
<?php } ?>
</div>
Have you tried putting the_post(); on the beginning of your template file? This solves sometimes this kind of problems.
This is my WordPress loop code... :)
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<?php if (is_category('instagram')) {?>
<?php
$__width = '225';
$__height = '225';
?>
<div id="instagram-photos">
<a href="<?php the_permalink() ?>"class="instagram-photo">
<div class="photo"><?php custom_get_post_attachments(get_the_ID(), $__width, $__height, get_the_title()); ?> </div>
</a>
</div>
<?php }elseif(is_category('runway')) { ?>
<?php
$__width = '298';
$__height = '500';
?>
<div id="runway-category">
<a href="<?php the_permalink() ?>" class="runway-category-posts">
<div class="photo"><?php custom_get_post_attachments(get_the_ID(), $__width, $__height, get_the_title()); ?><div class="runway-title"><?php the_title(); ?></div></div>
</a>
</div>
<?php } elseif(is_category('')){?>
<article <?php post_class(); ?>>
<div class="latest-posts">
<div class="latest-posts-info">
<div class="title"><h1><?php the_title(); ?><h1></div>
<div class="text">
<?php the_excerpt(); ?>
</div>
<div class="post-share">
<div class="facebook-like" style="float:left;">
<img src="<?php echo get_template_directory_uri(); ?>/images/facebook-icon.png" />
</div>
<div class="twitter-follow" style="float:left;">
<a href="http://twitter.com/share?text=<?php the_title(); ?>&url=<?php the_permalink() ?>" target="_blank" ><img src="<?php echo get_template_directory_uri(); ?>/images/twitter-icon.png" /></a>
</div>
<div class="google-share">
<a href="https://plus.google.com/share?url=<?php the_permalink() ?>" target="_blank" ><img src="<?php echo get_template_directory_uri(); ?>/images/google-icon.png" /></a>
</div>
</div>
<div class="clear"></div><div class="post-read-more">Read More</div>
<div class="clear"></div>
</div>
<div class="latest-posts-img">
<?php //echo get_the_post_thumbnail(); ?>
<?php custom_get_post_attachments(get_the_ID(), $__width, $__height, get_the_title()); ?>
</div>
<div class="clear"></div>
</div>
</article>
<?php }?>
<?php endwhile; else: ?>
<div class="content">
<p class="not-found-p">No articles found!</p>
</div>
<?php endif; ?>
As you see I have few conditions as is_category('instagram').
The thing is: I need to limit the post quantity in exact category, not at all them.
How can I do that?
i am uncertain if we can do this in a wordpress way .. but here is the workaround --
$no_of_posts = 5;
$post_counter = 0;
if(is_Category('instagram') && ($no_of_posts<=$post_counter)) {
// bunch of code
$post_counter++;
}
untested but something like could get you going
I am trying to fetch and show all posts in a featured listing style for tag my-guide
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<div class="thumbnail">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
This code fetch all featured image from posts, however I am trying to fetch it via particular tag, I tried this also but its not working for tag query_posts('tag=my-guide'); -
<?php query_posts('tag=my-guide');
while (have_posts()) : the_post();
?>
<div class="breaking">
<img src="<?php echo get_post_meta($post->ID, 'thumbnail',true) ?>" alt="Post Image" class="postimg" />
<h2><?php the_title(); ?></h2>
<p class="datetime"><?php the_time('l, F j, Y G:i'); ?></p>
<?php the_content('Continue...'); ?>
<div class="postmeta">
<p><?php the_category(', '); ?> - <a href="<?php the_permalink() ?>#commenting" title="View Comments">
<span class="comm"><?php comments_number('0 Comments','1 Comment','% Comments'); ?></span></a></p>
</div><!--/postmeta-->
</div><!--/breaking-->
<?php endwhile; ?>
Source of this -> http://net.tutsplus.com/tutorials/wordpress/build-a-featured-posts-section-for-wordpress/
I fixed the code on my own -
The problem with the second code block is that, no value is pouring in for this - $post->ID
So this method call is not doing any good in the code -
<?php echo get_post_meta($post->ID, 'thumbnail',true) ?>
To fix this, simply call, <?php the_post_thumbnail(); ?> instead of <img src="<?php echo get_post_meta($post->ID, 'thumbnail',true) ?>" alt="Post Image" class="postimg" />
So this is the working code in general for a particular tag post display -
<?php query_posts('tag=your-tag'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="thumbnail">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
<p class="datetime"><?php the_time('l, F j, Y G:i'); ?></p>
<?php the_content('Continue...'); ?>
<div class="postmeta">
<p><?php the_category(', '); ?> - <a href="<?php the_permalink() ?>#commenting" title="View Comments">
<span class="comm"><?php comments_number('0 Comments','1 Comment','% Comments'); ?></span></a></p>
</div>
</div>
<?php endwhile; endif; ?>