I'm not quite sure where I've gone wrong here. One of my template parts that I'm using for listing a bunch of posts of a certain category doesn't seem to be working.
Here is the code where I'm trying to get a template part named "content-offer.php" to display these posts, but it will not show any.
<?php query_posts('cat=3'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php static $count = 0;
if ($count == "4") { break; }
else { ?>
<?php get_template_part( 'content', 'offer', get_post_format() ); ?>
<?php $count++; } ?>
<?php endwhile;
endif;
wp_reset_query(); ?>
Here's what's actually within the template part file:
<div class="offer-box">
<div class="offer-left">
<?php
if(has_post_thumbnail()) {
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
echo '<img src="' . $image_src[0] . '" height="100%" height="auto" />';
}
?>
</div>
<div class="offer-right">
<h2 class="offer-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
<p class="offer-text">
<a href="<?php the_permalink(); ?>">
<?php the_excerpt(); ?>
</a>
</p>
<a href="<?php the_permalink(); ?>">
<div class="offer-cta">Claim now!</div>
</a>
</div>
Take note that all my files are in the root folder of the wordpress theme currently, so it's surely nothing to do with sub directories.
I have another area that displays posts with the same method just fine, so I'm confused as to why this one doesn't work.
If anyone can help me out and point where I've gone wrong it would be greatly appreciated.
So, it looks like I had the "content-offer.php" file in the root wordpress folder, not the theme root folder. Oops.
Related
This is my first time on here so apologies if something like this has been asked before.
My current sites has 2 posts per row but I want the first/latest post to be styled slightly different.
Basically I want the first/latest post to be 100% wide on the page then all the other posts to follow, 2 per row but only on the home page. Category pages etc I'm happy for that to stay 2 per row.
My current code is:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<article class="post-item clearfix" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<figure>
<?php the_post_thumbnail('single-post-feature'); ?>
</figure>
<header>
<div class="post-header-title">
<div class="post-category-feature">
<?php echo get_the_category_list(); ?>
</div>
<?php if ( is_singular() ) { echo '<h1>'; } else { echo '<h2>'; } ?><span><a class="black" href="<?php the_permalink(); ?>"title="<?php the_title_attribute(); ?>" rel="bookmark"><?php the_title(); ?></a></span><?php if ( is_singular() ) { echo '</h1>'; } else { echo '</h2>'; } ?>
<div id="excerpt"><?php the_excerpt(); ?></div>
<div id="author_date"><p><span><?php the_time('jS F Y') ?> | by <?php the_author(''); ?></span></p></div>
</div>
</header>
</article>
<?php endwhile; ?>
<?php endif; ?>
</div>
Any help would be greatly appreciated. If you would like to see the site to understand what I mean it is www.ltstlife.com. Thanks again.
The image is set to return a URL in the Custom Fields Plugin however it is just coming up with img src Unknown, everything else works on the page except this... code is as follows: Thanks in advance for any help!
<?php get_header(); ?>
<?php
$args = array(
'post_type' => 'artists',
);
$query = new WP_Query( $args );
?>
<section class="row artists">
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col-sm-12 col-md-6 col-lg-3">
<a href="<?php if( get_post_field('artist_website') ) { ?>
<?php echo $artist_website; ?>
<?php } else { the_permalink(); } ?>">
<img src="<?php get_post_field('artist_feature_image'); ?>" alt="<?php echo the_title() ; ?>">
<p><?php echo the_title() ;?></p>
</a>
</div>
<?php endwhile; endif; wp_reset_postdata(); ?>
</section>
<?php get_footer(); ?>
The problem is that get_post_field('artist_feature_image') only return the image.
You need to display it with "echo"
<img src="<?php echo get_post_field('artist_feature_image'); ?>" />
And don't do echo the_title() ;, because the_title() is already doing an echo on get_the_title();
function the_title() {
echo get_the_title();
}
So if you want to display it you just have to do: the_title();
got another question to ask:
I would like to display post thumbnails with the post title underneath. I have managed to work that out through asking on here, however now I would like to add a function that checks for a thumbnail and if none is available displays a dummy image.
Here is my try, which does display the thumbnail (if there) but not the dummy (if no thumbnail is attached):
<div class="gallery_container_wrapper">
<?php query_posts( $args ); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="gallery_image_container">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<div class="gallery_image_thumb">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail'); }
else {
echo
'<img src="http://www.kunstamkasten.de/wp-content/uploads/2014/08/gallery_dummy.jpg" />'
; } ?>
</div>
<div class="gallery_title">
<h2>
<?php the_title(); ?>
</h2>
</div>
</a>
</div>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
What am I doing wrong here?
This code works for me. Yours is virtually identical.
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail(); ?>
<?php else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/sunlit_path_banner.jpg" alt="Sunlit Path" />
<?php endif; ?>
Note: I also cut and pasted your code and tested on my WP install, and it worked fine.
Or... try this alternative to the if condition:
<?php if ( get_the_post_thumbnail( get_the_ID() ) ) {
the_post_thumbnail('thumbnail'); }
else {
echo
'<img src="http://www.kunstamkasten.de/wp-content/uploads/2014/08/gallery_dummy.jpg" />'
; } ?>
I have a set-up a custom theme on my wordpress install. I have been developing with it locally and everything was working fine.
I uploaded to my server tonight and was setting up the fresh wordpress install. I turned on permalinks and all of a sudden my custom category pages are causing infinite re-direct loops.
My .htaccess is writable so I don't think its that problem (I have seen this mentioned a lot online).
The code from one of my custom pages is below - it pulls from a specific category - does anyone know how to fix this issue?
<?php get_header(); ?>
<?php
/*
Template Name: Podcasts
*/
?>
<ul class="mcol">
<?php
query_posts("cat=1");
while(have_posts()) : the_post(); ?>
<li class="article" id="post-<?php the_ID(); ?>">
<?php
if ( has_post_thumbnail() ) { ?>
<?php
$imgsrcparam = array(
'alt' => trim(strip_tags( $post- >post_excerpt )),
'title' => trim(strip_tags( $post->post_title )),
);
$thumbID = get_the_post_thumbnail( $post->ID, array(200,200), $imgsrcparam ); ?>
<div class="preview"><?php echo "$thumbID"; ?><div class="front-titles"><?php the_title(); ?></div></div>
<?php } else {?>
<div class="preview"><img src="<?php bloginfo('template_url'); ?>/images/default-thumbnail.jpg" alt="<?php the_title(); ?>" /></div>
<?php } ?>
<div class="article-over">
</div>
</li> <?php ?>
<?php endwhile;
//Reset Query
wp_reset_query();
?>
</ul>
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php else : ?>
<h1 id="error"><?php _e("Sorry, but you are looking for something that isn’t here."); ?></h1>
<?php endif; ?>
<div class="pagination"><?php previous_posts_link('<< Newer Entries', 0) ?> <?php next_posts_link('Older Entries >>', 0); ?> </div>
<?php get_footer(); ?>
Anyone any ideas? I'm quite new to this so am unsure as to what the problem might be...
What your doing is weird.
query_posts is meant to alter the main loop.
You have 2 loops with different syntax on the same page.
The second loop is not doing anything, so remove it, and use get_posts or WP Query for your custom query.
I wondered if someone could help me with something that is probably quite simple when you know how.
I'm creating a game based website and I'm just working on the directory page where a list of all the games will be shown. I'm using a plug-in called 'Advanced Custom Fields' to create all my custom fields for each game.
At the moment I'm running a loop to pull in each game :-
<?php $loop = new WP_Query(array('post_type' => 'games', 'posts_per_page' => 10)); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
And then within this loop calling for different custom fields of the game (title, screenshots, description, download buttons etc...). But I have a problem when it comes to the screenshots, I can happily grab all the screenshots, but I just want to pull out 4. I've searched around all the threads, looked through the documentation, hit google, but whatever method I've tried I haven't achieved any success yet :(
My current code within the above loop for pulling out the screenshots is :-
<?php if(get_field('screenshots')): ?>
<ul class="screenshots">
<?php while (the_repeater_field('screenshots')): ?>
<li><img src="<?php the_sub_field('thumbnail_screenshot'); ?>" title="<?php the_sub_field('screenshot_title'); ?>"/></li>
<?php endwhile; ?>
</ul> <!--- end of screenshots !-->
<?php endif; ?>
Is there a way to just limit my results to a total of 4 instead of displaying all available screenshots? A random 4 would be an even nicer option, but isn't paramount.
I'm sure the solution is far simpler than some of the things I've tried without success so far.
Many thanks for any help received.
Cheers
Rob
I don't know much about the plugin you are using. but a simple solution would be using a counter like this:
<?php $i=0; ?>
<?php while (the_repeater_field('screenshots')): ?>
<li><img src="<?php the_sub_field('thumbnail_screenshot'); ?>" title="<?php the_sub_field('screenshot_title'); ?>"/></li>
<?php if($i<4) $i++;
else break; ?>
<?php endwhile; ?>
I am curious as to how you got more than 1 loop to run, by definition it should work like you're experience but for some reason when I post the same concept I can only get the while to produce one result even though there are 10 images.
while(the_repeater_field('horse_images')):
$image_id = get_sub_field('image');
$image_src = wp_get_attachment_image_src($image_id,medium);
echo '<img src="'.$image_src[0].'">';
endwhile;
I wonder if it would work if you modified your code like with && $i <= 4
<?php if(get_field('screenshots')): ?>
<?php $i = 0; ?>
<ul class="screenshots">
<?php while (the_repeater_field('screenshots') && $i <= 4 ): ?>
<li><img src="<?php the_sub_field('thumbnail_screenshot'); ?>" title="<?php the_sub_field('screenshot_title'); ?>"/></li>
$i++
<?php endwhile; ?>
Look:
<?php if( have_rows('gallery_img') ): ?>
<div class="gl-images">
<div class="flexslider">
<ul class="slides">
<?php $i=0; ?>
<?php while( have_rows('gallery_img') ): the_row();
// vars
$image = get_sub_field('img_item');
?>
<?php if( $image): ?>
<li class="slide">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
</li>
<?php endif; ?>
<?php if($i>1) $i++;
else break;
?>
<?php endwhile; ?>
</ul>
</div>
</div>
<?php endif; ?>
<?php if (have_rows('service_priceing')): ?>
<?php $n=0; while (have_rows('service_priceing')): the_row(); ?>
<?php $package = get_sub_field('package'); ?>
<?php $rent = get_sub_field('rent'); ?>
<?php $price = get_sub_field('price'); ?>
<div class="columns price-columns">
<ul class="price">
<li class="header" style="background-color:<?php if($n==1){ echo "Red"; }else{echo "Blue";}?>"><?php echo $package; ?></li>
<li class="dates">
<p><?php echo $rent; ?></p>
</li>
</ul>
</div>
<?php $n++; if($n == 3){ break; } endwhile; ?>
<?php endif; ?>