WordPress Featured Image Caption is Repeating - wordpress

I'm trying to create a div containing the featured image's caption appear in the middle of the slider.
Why is the caption looping?
link: http://natashamcdiarmid.com/clients/JLP/wp/
how it should look: http://natashamcdiarmid.com/clients/JLP/
<div class="slider">
<?php query_posts( 'post_type=page' ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_post_thumbnail('slider'); ?>
<div class="caption-wrap"><?php the_post_thumbnail_caption(); ?></div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>

I never see this function the_post_thumbnail_caption();, so change it by get_post( get_post_thumbnail_id() )->post_excerpt
<div class="slider">
<?php query_posts( 'post_type=page' ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_post_thumbnail('slider'); ?>
<div class="caption-wrap"><?php echo get_post( get_post_thumbnail_id() )->post_excerpt ?></div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>

Ok I figured it out:
<div class="slider">
<?php query_posts( 'post_type=page&page_id=48' ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_post_thumbnail('slider');
echo '<div class="caption-wrap">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>'
; ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>

Related

Regular posts first (descending order), then custom post type (alphabetical) in WP search

I've been cobbling together bits and pieces of various examples together but can't seem to wrap my head around this.
I have regular blog posts (News) that I would like displayed with the most recent on top followed by a custom post type (Businesses) that I would like grouped below the News. I am using Sage and this is my first theme.
Here is my beginner code so far:
<?php get_template_part('templates/page', 'header'); ?>
<?php if (!have_posts()) : ?>
<div class="alert alert-warning">
<?php _e('Sorry, no results were found.', 'sage'); ?>
</div>
<?php get_search_form(); ?>
<?php endif; ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $post_type = get_post_type_object( get_post_type() ); ?>
<?php $type = get_post_type(); ?>
<?php if ($type == 'post') { ?>
<h2>News Results</h2>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div>
<a href="<?php the_permalink(); ?>">
<?php $cats=get_the_category(); ?>
<?php echo $cats[0]->cat_name; ?>
</a>
</div>
<div>
<h3><?php the_title(); ?></h3>
<div class="result-excerpt">
<?php if ( has_excerpt( $post->ID ) ) {
echo the_excerpt();
} else {
echo get_excerpt();
} ?>
</div>
</div>
</article>
<?php } elseif ($type == 'business') { ?>
<h2>Business Results</h2>
<article <?php post_class(); ?>>
<header>
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php if (get_post_type() === 'post') { get_template_part('templates/entry-meta'); } ?>
</header>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
<a href="<?php the_permalink(); ?>">
<?php $cats=get_the_category(); ?>
<?php echo $cats[0]->cat_name; ?> -
<?php the_title(); ?>
</a>
<h3><?php echo the_sub_field('title'); ?></h3>
<?php if( get_sub_field('content') ): ?>
<div class="result-excerpt">
<?php echo custom_field_excerpt(); ?>
</div>
<?php endif; ?>
</article>
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>
I think the problem is that you're looping over every post, in order, and deciding what to do with it. This will result in the output, whatever it is, in the same order as the input.
I don't speak very fluent PHP, but I think what's happening is that the algorithm reads like this:
-for each post:
- if it's a 'post', display it like «this»
- otherwise, if it's a 'business', display it instead like «this»
I think you want to have two loops, like this:
-for each post:
- if it's a 'post', display it like «this»
-for each post:
- if it's a 'business', display it like «this»
Unfortunately, I don't see an obvious reference to store in an array and loop over. I really don't know how sage works, so I'll have to leave the implementation to you. My best guess - and I sincerely doubt this will work - is as follows:
<?php get_template_part('templates/page', 'header'); ?>
<?php if (!have_posts()) : ?>
<div class="alert alert-warning">
<?php _e('Sorry, no results were found.', 'sage'); ?>
</div>
<?php get_search_form(); ?>
<?php endif; ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $post_type = get_post_type_object( get_post_type() ); ?>
<?php $type = get_post_type(); ?>
<?php if ($type == 'post') { ?>
<h2>News Results</h2>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div>
<a href="<?php the_permalink(); ?>">
<?php $cats=get_the_category(); ?>
<?php echo $cats[0]->cat_name; ?>
</a>
</div>
<div>
<h3><?php the_title(); ?></h3>
<div class="result-excerpt">
<?php if ( has_excerpt( $post->ID ) ) {
echo the_excerpt();
} else {
echo get_excerpt();
} ?>
</div>
</div>
</article>
<?php endwhile; ?>
<?php endif; ?>
<?php if (!have_posts()) : ?>
<div class="alert alert-warning">
<?php _e('Sorry, no results were found.', 'sage'); ?>
</div>
<?php get_search_form(); ?>
<?php endif; ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $post_type = get_post_type_object( get_post_type() ); ?>
<?php $type = get_post_type(); ?>
<?php if ($type == 'business') { ?>
<h2>Business Results</h2>
<article <?php post_class(); ?>>
<header>
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php if (get_post_type() === 'post') { get_template_part('templates/entry-meta'); } ?>
</header>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
<a href="<?php the_permalink(); ?>">
<?php $cats=get_the_category(); ?>
<?php echo $cats[0]->cat_name; ?> -
<?php the_title(); ?>
</a>
<h3><?php echo the_sub_field('title'); ?></h3>
<?php if( get_sub_field('content') ): ?>
<div class="result-excerpt">
<?php echo custom_field_excerpt(); ?>
</div>
<?php endif; ?>
</article>
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>
Good luck!

WordPress posts are not showing at all

I have tried several codes to display WordPress posts in my theme I create but nothing works - why?
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="entry">
<?php the_content(); ?>
</div>
<div class="postmetadata">
<?php the_tags('Tags: ', ', ', '<br />'); ?>
Posted in <?php the_category(', ') ?> |
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
</div>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
<?php
if (have_posts()) :
while (have_posts()) :
the_post();
the_content();
endwhile;
endif; ?>
Nothing is showing the posts why? I have the following files in my theme: index.php, header.php, style.css, footer.php, page.php, single.php
<?php
query_posts(array(
'post_type' => 'YOUR_POST_TYPE_NAME', // Add your post-type name
'showposts' => 10
) );
?>
Please add this code in your above If statement.
<?php
$args = array(
'post_type' => 'post',
'post_status'=>'publish'
);
$wpquery = new WP_Query($args);
if ($wpquery->have_posts()) : while ($wpquery->have_posts()) : $wpquery->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="entry">
<?php the_content(); ?>
</div>
<div class="postmetadata">
<?php the_tags('Tags: ', ', ', '<br />'); ?>
Posted in <?php the_category(', ') ?> |
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
</div>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
<?php
if ($wpquery->have_posts()) :
while ($wpquery->have_posts()) :
the_post();
the_content();
endwhile;
endif; ?>

Wordpress query_posts() not returning list of posts whilst excluding one category

I'm having some trouble witin one of my page templates for my Wordpress theme. I wish to list all post but exclude those from one category (id: 4).
Below I have used query_posts() as per the Codex documentation:
<?php get_header(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<header
<?php
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
echo 'style="background-image:url(' . $large_image_url[0] . ')"';
}
?>
>
<div class="row">
<div class="page-title small-12 large-8 columns">
<h1>
<?php the_title(); ?>
</h1>
</div>
</div>
</header>
<?php get_template_part( 'content', 'headerbanner' ); ?>
<?php endwhile;?>
<section class="container main-content">
<div class="row">
<div id="content" class="small-12 columns">
<?php query_posts($query_string . '&cat=-4'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
</div>
</div>
</section>
<?php get_footer(); ?>
The problem is that this does not return a list of the posts. Instead it only returns the details for the page itself (in this case called "blog").
Amending the code from inside #content to the following solved this problem:
<?php query_posts('cat=-4'); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
here you need to pass post_type
query_posts($query_string . '&cat=-4&post_type=post');

elseif in wordpress template

how can I add some echo to render a paragraf if there is no posts?
<?php query_posts('cat=1'.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="thumb"><?php the_post_thumbnail('thumbnail'); ?></div>
<h1><?php the_title(); ?></h1>
<p><?php $content = get_the_content();
echo substr($content, 0, 20000); ?>...
<span class="more">czytaj więcej...</span>
</p>
<?php endwhile; ?>
<?php endif; ?>
you can put else after the while loop:
<?php endwhile; ?>
<?php else: ?>
<p> no posts found</p>
<?php endif; ?>
<?php query_posts('cat=1'.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="thumb"><?php the_post_thumbnail('thumbnail'); ?></div>
<h1><?php the_title(); ?></h1>
<p><?php $content = get_the_content();
echo substr($content, 0, 20000); ?>...
<span class="more">czytaj więcej...</span>
</p>
<?php endwhile; ?>
<?php else : ?>
<!-- Content to show when there are no posts. -->
<?php endif; ?>

get post thumbnail image in wordpress and display in 3 coulmn

I am trying to display one particular category posts as a three column layout The problem I have is I am not sure how I can use for or forwach to loop the display of each post thumbnail so whn it comes more than three I can use one_thrid_last. css class.
<?php while ( have_posts() ) : the_post(); ?>
<?php if (is_category('actress-gallery') ):?>
<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) : ?>
<div class="one_fourth_last">
<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'tie' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
<?php echo $image_url = wp_get_attachment_image( get_post_thumbnail_id($post->ID) , 'thumbnail' ); ?>
<h2><?php the_title(); ?></h2>
</a>
</div><!-- post-thumbnail /-->
<?php endif; ?>
<div class="wrapper" style="width:800px; height:auto;">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php if ( has_post_thumbnail() ) { ?>
<div class="image-wrapper" style=" width:250px; height:300px;" >
<?php the_title();?>
<?php the_content();?>
<?php the_post_thumbnail(); ?>
</div>
<?php } endwhile; endif; ?>
</div>
<div class="wrapper" style="width:750px; height:700px;">
<?php while (have_posts()) : the_post(); ?>
<?php if ( has_post_thumbnail() ) { ?>
<div style="width:250px; height:300px;" >
<?php the_title();?>
<?php the_post_thumbnail(); ?>
</div>
<?php } endif; ?>
</div>
You can use a $count variable and check for the 3rd thumbnail.
<?php if ( have_posts() ) : ?>
<?php $count = 1; ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( has_post_thumbnail() ) : ?>
<?php if ($count % 3 == 0) : ?>
<div class = "one_third_last">
<?php else : ?>
<div class = "other_class">
<?php endif; ?>
<?php the_title();?>
<?php the_content();?>
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<?php $count++; ?>
<?php endwhile; ?>
<?php endif; ?>

Resources