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.
Related
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;
?>
I've made a shortcode function that returns some Advanced Custom Fields depending on which repeater field is chosen. It displays correctly and in the right order however any content typed underneath the shortcode is moved outside of its <article> container element.
Here is the code:
function boxes_shortcode($atts, $content = null) {
ob_start(); ?>
<div id="boxes">
<div class="box-gutter"></div>
<div class="box-sizer"></div>
<?php while( have_rows('box_repeater') ): the_row();
$image_id = get_sub_field('link_image');
$image_size = 'box-thumbnail';
$image_array = wp_get_attachment_image_src($image_id, $image_size);
$linkImage = $image_array[0]; ?>
<?php if(get_sub_field('box_type') == "box-link"): ?>
<div class="box">
<div class="link-box">
<img src="<?php echo $linkImage; ?>" alt="<?php echo $linkImage['alt']; ?>" />
<a class="caption-wrapper" href="http://www.google.com">
<span id="link-box-content" style="background:<?php the_sub_field('link_background_colour'); ?>">
<h6 style="color:<?php the_sub_field('link_title_colour'); ?>"><?php the_sub_field('link_title'); ?></h6>
<h4 style="color:<?php the_sub_field('link_text_colour'); ?>"><?php the_sub_field('link_text'); ?></h4>
<p style="color:<?php the_sub_field('link_text_colour'); ?>" href="<?php the_sub_field('link_url'); ?>"><?php the_sub_field('link_url_text'); ?> ></p>
</span>
</a>
</div>
</div>
<?php endif;
if(get_sub_field('box_type') == "box-quote"): ?>
<div class="box">
<div class="quote-box">
<div class="quotation-mark"></div>
<h4><?php the_sub_field('quote'); ?></h2>
<p><?php the_sub_field('quote_source'); ?></p>
<?php the_sub_field('quote_link_text'); ?> >
</div>
</div>
<?php endif;
if(get_sub_field('box_type') == "box-twitter"): ?>
<div class="box">
<div class="twitter">
<a class="twitter-timeline" href="<?php the_sub_field('twitter_url'); ?>" data-widget-id="<?php the_sub_field('twitter_widget_id'); ?>" data-chrome="noheader noscrollbar nofooter noborders transparent" data-tweet-limit="<?php the_sub_field('number_of_tweets'); ?>"></a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
<a class="twitter-badge" href="<?php the_sub_field('twitter_url'); ?>"></a>
<div class="twitter-bottom">See more ></div>
</div>
</div>
<?php endif;
endwhile;?>
</div>
</div>
</div>
<?php
/* Get the buffered content into a var */
$sc = ob_get_contents();
ob_end_clean();
return $sc;
}
add_shortcode('boxes', 'boxes_shortcode');
Can anyone help solve this mystery? Many thanks.
You have two extra closing <div>'s in your code.
Try this:
function boxes_shortcode($atts, $content = null) {
ob_start(); ?>
<div id="boxes">
<div class="box-gutter"></div>
<div class="box-sizer"></div>
<?php while( have_rows('box_repeater') ): the_row();
$image_id = get_sub_field('link_image');
$image_size = 'box-thumbnail';
$image_array = wp_get_attachment_image_src($image_id, $image_size);
$linkImage = $image_array[0]; ?>
<?php if(get_sub_field('box_type') == "box-link"): ?>
<div class="box">
<div class="link-box">
<img src="<?php echo $linkImage; ?>" alt="<?php echo $linkImage['alt']; ?>" />
<a class="caption-wrapper" href="http://www.google.com">
<span id="link-box-content" style="background:<?php the_sub_field('link_background_colour'); ?>">
<h6 style="color:<?php the_sub_field('link_title_colour'); ?>"><?php the_sub_field('link_title'); ?></h6>
<h4 style="color:<?php the_sub_field('link_text_colour'); ?>"><?php the_sub_field('link_text'); ?></h4>
<p style="color:<?php the_sub_field('link_text_colour'); ?>" href="<?php the_sub_field('link_url'); ?>"><?php the_sub_field('link_url_text'); ?> ></p>
</span>
</a>
</div>
</div>
<?php endif;
if(get_sub_field('box_type') == "box-quote"): ?>
<div class="box">
<div class="quote-box">
<div class="quotation-mark"></div>
<h4><?php the_sub_field('quote'); ?></h2>
<p><?php the_sub_field('quote_source'); ?></p>
<?php the_sub_field('quote_link_text'); ?> >
</div>
</div>
<?php endif;
if(get_sub_field('box_type') == "box-twitter"): ?>
<div class="box">
<div class="twitter">
<a class="twitter-timeline" href="<?php the_sub_field('twitter_url'); ?>" data-widget-id="<?php the_sub_field('twitter_widget_id'); ?>" data-chrome="noheader noscrollbar nofooter noborders transparent" data-tweet-limit="<?php the_sub_field('number_of_tweets'); ?>"></a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
<a class="twitter-badge" href="<?php the_sub_field('twitter_url'); ?>"></a>
<div class="twitter-bottom">See more ></div>
</div>
</div>
<?php endif;
endwhile;?>
</div>
<!--/div>
</div-->
<?php
/* Get the buffered content into a var */
$sc = ob_get_contents();
ob_end_clean();
return $sc;
}
add_shortcode('boxes', 'boxes_shortcode');
I have a problem with the way WordPress creates code.
As shown below, my articles should be wrapped inside an <a> tag. WordPress creates a few extra wraps and I don't know how to get rid of them.
Does anyone has an idea?
This:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="post-thumb">
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) {the_post_thumbnail();} ?>
<div class="post-thumb-caption">
<h2><?php the_title(); ?></h2>
</div>
<div class="post-thumb-caption-hover">
<div class="post-thumb-caption-hover-center">
<img id="menu-icon" class="icons svg" src="icons/menu21.svg"></img>
<h2><?php the_title(); ?></h2>
<h3><?php the_category(', ') ?></h3>
</div>
</div>
</a>
</article>
<?php endwhile; ?>
<?php post_navigation(); ?>
<?php else : ?>
<h2><?php _e('Nothing Found','html5reset'); ?></h2>
<?php endif; ?>
Should result in this:
<article class="post-thumb">
<a href="#">
<img src="3a.jpg" id="image">
<div class="post-thumb-caption">
<h2>Titel</h2>
</div>
<div class="post-thumb-caption-hover">
<div class="post-thumb-caption-hover-center">
<img id="menu-icon" class="icons svg" src="icons/menu21.svg">
<h2>Titel</h2>
<h3>Categorie</h3>
</div>
</div>
</a>
</article>
But WordPress creates this:
<article class="post-thumb">
<a href="http://localhost/sm/titel/">
<img width="300" height="300" src="http://localhost/sm/wp-content/uploads/2014/08/3a.jpg" class="attachment-post-thumbnail wp-post-image" alt="3a"> <div class="post-thumb-caption">
<h2>Titel</h2>
</div>
</a><div class="post-thumb-caption-hover"><a href="http://localhost/sm/Titel/">
</a><div class="post-thumb-caption-hover-center"><a href="http://localhost/sm/Titel/">
<img id="menu-icon" class="icons svg" src="icons/menu21.svg">
<h2>Willem en Henk</h2>
</a><h3>Ontbijt, Snacks</h3>
</div>
</div>
</article>
Change
<a href="<?php the_permalink(); ?>">
to
<a href="#">
and it should fix that problem.
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 get wordpress posts, 3 at a time, this is the code i'm using:
<?php while ( have_posts() ) : the_post() ?>
<?php if(get_post_meta($post->ID, 'feature', true) != true) {; ?>
<div class="show_col">
<?php for ($i = 1; $i <= 3; $i++) { ?>
<div class="set">
<a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark">
<img class="image" src="http://localhost/portpress/wp-content/themes/myTemp/portfolio/<?php echo get_post_meta($post->ID, 'intro_thump', true); ?>.jpg" width="300px" alt="AUREL #<?php the_ID(); ?>" />
</a>
<?php the_content("<P class='more'> Read More » </p>"); ?>
</div>
<?php }; ?>
</div>
<?php }; ?>
<?php endwhile; ?>
i know this loop is echoing ONE post THREE times - but what i really want is this end result
<div class="show_col">
<div class="show_col"> post1 </div>
<div class="show_col"> post2 </div>
<div class="show_col"> post3 </div>
</div>
<div class="show_col">
<div class="show_col"> post4 </div>
<div class="show_col"> post5 </div>
<div class="show_col"> post6 </div>
</div>
<!-- and so on -->
i am doing this as the height of each post varies - therefore i add something like
.show_col{clear:both} so that the next three posts go under neath
i hope you could help
thanks
This is the code you need (I corrected some of your syntax):
<?php $i = 0; ?>
<?php while ( have_posts() ): the_post();?>
<?php if(get_post_meta($post->ID, 'feature', true) != true): ?>
<div class="show_col">
<?php if(($i%3) == 0): ?>
</div>
<div class="show_col">
<?php endif;?>
<div class="set">
<a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark">
<img class="image" src="http://localhost/portpress/wp-content/themes/myTemp/portfolio/<?php echo get_post_meta($post->ID, 'intro_thump', true); ?>.jpg" width="300px" alt="AUREL #<?php the_ID(); ?>" />
</a>
<?php the_content("<P class='more'> Read More » </p>"); ?>
</div>
</div>
<?php endif; ?>
<?php $i++; ?>
<?php endwhile; ?>
Also you seem to add a semicolon after closing the function ( }; ), don't do that, there is no need for it.