Wordpress functions not available in template - php

I am editing front-page.php and there I use get_posts() to output posts.
The other ways like using the_post are not working. So I need to output excerpt, but $post->post_excerpt is empty and the_excerpt function does nothing. I don't understand why, cause there are no errors. Here is the code:
<?php
foreach ( get_posts() as $post ) { ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('article-item well'); ?>>
<h2 class="title text-primary">
<a href="<?php echo $post->guid; ?>">
<?php echo $post->post_title; ?>
</a>
</h2>
<p class="article-info text-center">
<span class="date">Posted on <time pubdate="" title="12:19 pm" datetime="<?php echo $post->post_date; ?>" class="time">
<?php echo $post->post_date; ?>
</time>
</span>
</p>
<?php if (has_post_thumbnail()) { ?>
<figure class="img-wrap">
<?php the_post_thumbnail('full'); ?>
<figcaption class="label label-primary">
<?php foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
} ?>
</figcaption>
</figure>
<?php } ?>
<p>
<?php the_excerpt(); ?>
</p>
BTW the_ID function is working and outputs post's id properly.

On each iteration of the loop you need to 'setup' post data, you do this using setup_postdata( $post );
Amend your code as follows:
<?php
foreach ( get_posts() as $post ) {
setup_postdata( $post );
?>
<article id="post-<?php the_ID(); ?>" <?php post_class('article-item well'); ?>>
<h2 class="title text-primary">
<a href="<?php echo $post->guid; ?>">
<?php echo $post->post_title; ?>
</a>
</h2>
<p class="article-info text-center">
<span class="date">Posted on <time pubdate="" title="12:19 pm" datetime="<?php echo $post->post_date; ?>" class="time">
<?php echo $post->post_date; ?>
</time>
</span>
</p>
<?php if (has_post_thumbnail()) { ?>
<figure class="img-wrap">
<?php the_post_thumbnail('full'); ?>
<figcaption class="label label-primary">
<?php foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
} ?>
</figcaption>
</figure>
<?php } ?>
<p>
<?php the_excerpt(); ?>
</p>

Related

Code shows nothing on wordpress: while (have_posts()) {the_post();?>

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.

Wordpress - Hide blog meta data if in specific category

I have a standard wordpress blog here: http://webserver-meetandengage-com.m11e.net/insights/ and I've created a new category called clients.
The clients posts on this archive page will have different meta data that the standard blog post, so I want to get rid of the excerpt, date and author etc.
To achieve this I tried adding a conditional bit of code that said, IF the category of this post area is 'client' then echo style="display:none;" inside the div.
Here's the line of code I'm trying:
<p<?php if ( in_category( 'client' )) { echo 'style="display:none;"' }?>>This is not client</p>
Here's the loop it appears in:
<div class="container blog-card-container">
<div class="row">
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="col-md-4">
<a href="<?php the_permalink(); ?>">
<div class="card">
<div class="blog-thumb-container">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?>
</div>
<div class="blog-clients-card-block">
<?php if( get_field('quote_name') ): ?><p class="client-name" style="color:<?php the_field('client_brand_colour'); ?>;"><?php the_field('quote_name'); ?></p><?php endif; ?>
<p<?php if ( in_category( 'client' )) { echo 'style="display:none;"' }?>>This is not client</p>
<p class="blog-cat-label"><?php the_category(', '); ?></p>
<h2 class="blog-card-title"><?php the_title(); ?></h2>
<p class="card-text"><?php the_excerpt(__('(more…)')); ?></p>
<p><strong><?php the_author(); ?></strong> | <?php the_date(); ?> </p>
</div>
</div>
</a>
</div>
<?php understrap_pagination(); ?>
<?php endwhile; wp_reset_postdata(); endif; ?>
</div>
</div>
But including it here breaks the loop and the page doesn't load... I'm not sure what I'm doing wrong, or even if there might be a better solution?
I essentially want to show one set of meta for post thumbnails with the category 'client' and then another set for all other categories in the blog.
I guess it could be IF category of container is client then show META1 else show META2.
Any help would be massively appreciated :)
Managed to achieve this with two IF ELSE statements:
<div class="container blog-card-container">
<div class="card-columns">
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink(); ?>">
<div class="card">
<!-- Image if loop =========================================== -->
<?php if ( in_category('14') ) : ?>
<div class="client-header-logo-card" style="background-color: <?php the_field('client_brand_colour'); ?>;">
<?php
$image = get_field('client_logo');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div>
<?php else: ?>
<div class="blog-thumb-container">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?>
</div>
<?php endif ?>
<!-- Meta Data if loop =========================================== -->
<div class="blog-clients-card-block">
<?php if ( in_category('14') ) : ?>
<p class="blog-cat-label"><?php the_category(', '); ?></p>
<h2><?php the_title(); ?></h2>
<?php if( get_field('quote') ): ?><p class="client-quote"><?php echo custom_field_excerpt(); ?></p><?php endif; ?>
<?php if( get_field('quote_name') ): ?><p class="client-name" style="color:<?php the_field('client_brand_colour'); ?>;"><?php the_field('quote_name'); ?></p><?php endif; ?>
<?php if( get_field('quote_position') ): ?><p class="client-position" style="color:<?php the_field('client_brand_colour'); ?>;"><?php the_field('quote_position'); ?></p><?php endif; ?>
<?php if( get_field('button_text') ): ?>
<a class="btn btn-sm btn-client-archive" href="<?php the_permalink(); ?>" style="background-color:<?php the_field('client_brand_colour'); ?>;" role="button"><?php the_field('button_text'); ?></a>
<?php endif; ?>
<?php if( get_field('video_url') ): ?>
<div class="embed-container">
<?php the_field('video_url'); ?>
</div>
<?php endif; ?>
<?php else: ?>
<p class="blog-cat-label"><?php the_category(', '); ?></p>
<h2 class="blog-card-title"><?php the_title(); ?></h2>
<p class="card-text"><?php the_excerpt(__('(more…)')); ?></p>
<p><strong><?php the_author(); ?></strong> | <?php the_date(); ?> </p>
<?php endif ?>
</div>
</a>
</div>
You need to set it up in a Variable and echo the variable. For instance if client display:none is stored in the $style var if not then it doesn't place a style
<?php
if(in_category('client')){$style = 'display:none;';} else {$style = '';}
?>
<p style="<?php echo $style; ?>">This is not client</p>

Wordpress - If first post add the class selected

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

wordpress post content duplicate

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.

How to fetch and show particular posts for a tag on home page in wordpress

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

Categories