I have a problem. I would like to show one time one post and one time 5 posts with this code but he shows two porst more than necessary. What I don't get, is that it's another site of me with the same theme does work. The completely same theme only a others website. Who can help me?! (check the wrong result http://radiozuid.com/ an websie correct result http://radiozuid.rtv-zuid.com/beta/ ) (Exact same theme)
<?php
$the_query = new WP_Query(array(
'posts_per_page' => 1
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<div class="item active">
<?php if(has_post_thumbnail()): ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'small-nivo-thumb'); ?>
<img src="<?php echo get_template_directory_uri(); ?>/timthumb.php?src=<?php echo $image[0]; ?>&w=620&h=350" class="postafbeelding" alt="<?php the_title(); ?>">
<?php else: ?>
<img src="<?php echo get_template_directory_uri(); ?>/timthumb.php?src=<?php echo get_template_directory_uri(); ?>/images/thumbnail.png&w=620&h=350" class="postafbeelding" alt="<?php the_title(); ?>">
<?php endif; ?>
<div class="carousel-caption">
<h3 class="titel"><?php the_title();?></h3>
<p><?php echo string_limit_words(get_the_excerpt(), 35); ?>... Lees Meer</p>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
<?php
$the_query = new WP_Query(array(
'posts_per_page' => 5,
'offset' => 1
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<div class="item">
<?php if(has_post_thumbnail()): ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'small-nivo-thumb'); ?>
<img src="<?php echo get_template_directory_uri(); ?>/timthumb.php?src=<?php echo $image[0]; ?>&w=620&h=350" class="postafbeelding" alt="<?php the_title(); ?>">
<?php else: ?>
<img src="<?php echo get_template_directory_uri(); ?>/timthumb.php?src=<?php echo get_template_directory_uri(); ?>/images/thumbnail.png&w=620&h=350" class="postafbeelding" alt="<?php the_title(); ?>">
<?php endif; ?>
<div class="carousel-caption">
<h3 class="titel"><?php the_title();?></h3>
<p><?php echo string_limit_words(get_the_excerpt(), 35); ?>... Lees Meer</p>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
The problem is that your query is returning sticky posts, which for whatever reason are tacked onto the post limit rather than counting as part of it. Add ignore_sticky_posts to your query and I think that will fix it.
$the_query = new WP_Query(array(
'posts_per_page' => 5,
'offset' => 1,
'ignore_sticky_posts' => true
));
Related
I have been trying to customize my site but I have met a problem... As I have stated in the title, what shall I add in order to make it possible? I will like the make the category with the latest post move to the first. I have tried for 5 hours and still failed to do it. Please teach me how to fix it.
<?php
//Get the desired categories and order by ID
$cat_args = array(
'orderby' => 'id'
);
//For each category show a random post
$categories = get_categories($cat_args);
foreach ($categories as $category) {
?>
<?php
$post_args = array(
'numberposts' => 1,
'category' => $category->term_id,
);
$posts = get_posts($post_args);
foreach ($posts as $post) {
?>
<article <?php post_class('post-list animated fadeIn'); ?> role="article">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<figure class="eyecatch<?php if (!has_post_thumbnail()) : ?> noimg<?php endif; ?>">
<?php the_post_thumbnail('home-thum'); ?>
<?php archivecatname(); ?>
</figure>
<section class="entry-content cf">
<h1 class="h2 entry-title"><?php the_title(); ?></h1>
<div class="byline entry-meta vcard">
<?php if (get_option('post_options_authordisplay', 'author_off') == 'author_on') : ?><span class="writer name author"><?php echo get_avatar(get_the_author_meta('ID'), 30); ?><span class="fn"><?php the_author(); ?></span></span><?php endif; ?>
</div>
<div class="description"><?php the_excerpt(); ?></div>
</section>
</a>
</article>
<?php get_template_part('loop'); ?>
<?php
}
}
?>
Query Arguments
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
);
Running the Query
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
I am creating a widget in wordpress that pulls through child pages content (thumbnail/Featured image, title and excerpt) from the 'About Page' and displays them on the 'Homepage'. All content except the thumbnail/featured displays correctly. The thumbnail/featured image displays in grey, is is viable in other post and in the back end. when I inspect the code the url for the image does not seem to be pulling through.
Can anyone assist with this issue?
$pgs = array(
'parent' => '344',
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => 99,
'orderby' => 'menu_order'
);
$pages = get_pages( $pgs );
echo $args['before_widget'];
?>
<div class="text-center">
<h2 class="blessed-widget-title">title</h2>
<div class="text-center" id="services">
<?php foreach( $pages as $page ) { ?>
<div class="our-services-post-box col-md-4 col-sm-6 col-xs-6" id="child-<?php the_ID(); ?>">
<a href="<?php echo get_permalink($page->ID); ?>" rel="bookmark" title="<?php echo $page->post_title; ?>">
<div class="service-thumbnail background-image" style="background-image: url(<?php echo blessed_get_attachment($page->ID) ?>) ">
<img class="image-invisible" src=" <?php echo blessed_get_attachment($page->ID); ?> ">
</div>
<h2><?php echo $page->post_title; ?></h2>
</a>
<p><?php echo get_the_excerpt($page->ID) ?></p>
</div><!--services-->
<?php } ?>
</div>
</div>
<?php
echo $args['after_widget'];
I found 2 missed semicolons.
1st was here:
<div class="service-thumbnail background-image" style="background-image: url(<?php echo blessed_get_attachment($page->ID) ?>) ">
2nd was here:
<p><?php echo get_the_excerpt($page->ID) ?></p>
Maybe it caused the problem. Also Quotes are not necessary in an url, but I added it to see what will be the result.
Try to use the following one instead.
$pgs = array(
'parent' => '344',
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => 99,
'orderby' => 'menu_order'
);
$pages = get_pages( $pgs );
echo $args['before_widget'];
?>
<div class="text-center">
<h2 class="blessed-widget-title">title</h2>
<div class="text-center" id="services">
<?php foreach( $pages as $page ) { ?>
<div class="our-services-post-box col-md-4 col-sm-6 col-xs-6" id="child-<?php the_ID(); ?>">
<a href="<?php echo get_permalink($page->ID); ?>" rel="bookmark" title="<?php echo $page->post_title; ?>">
<div class="service-thumbnail background-image" style="background-image: url('<?php echo blessed_get_attachment($page->ID); ?>') ">
<img class="image-invisible" src=" <?php echo blessed_get_attachment($page->ID); ?> ">
</div>
<h2><?php echo $page->post_title; ?></h2>
</a>
<p><?php echo get_the_excerpt($page->ID); ?></p>
</div><!--services-->
<?php } ?>
</div>
</div>
<?php
echo $args['after_widget'];
Also try to review your CSS Classes related to Featured images.
I've 2 post types and I want to output them alternately but without using many loops so I found this solution which does that.
However, it is not ideal as I need to output the_post_thumbnail which I find I am unable to do using this method (echo $smallPosts->posts[$i]->post_thumbnail; does nothing). Additonally I've read post_content is not the same as the_content(); - with the latter what I want to use.
Any suggestions on how I can loop through the alternating post types and have more control over the output so I can use the_post_thumnail etc.?
Below is my code that does work but just doesn't quite do what I require.
<?php $args = array(
'post_type' => 'small_post',
'posts_per_page' => 3
);
$smallPosts = new WP_Query($args);
$args = array(
'post_type' => 'full_post',
'posts_per_page' => 3
);
$fullPosts = new WP_Query($args);
for ($i = 0; $i < 3; $i++) {
if ($smallPosts->post_count > $i)
echo $smallPosts->posts[$i]->post_title;
echo '<br />';
echo $smallPosts->posts[$i]->post_content;
echo '<br />';
if ($fullPosts->post_count > $i)
echo $fullPosts->posts[$i]->post_title;
echo '<br />';
echo $fullPosts->posts[$i]->post_content;
echo '<br />';
}
?>
This is my solution which outputs both post types and using the time published they can be alternated and I can use the_thumbnail( ); and other functions I need. Additionally I've used if statements to add classes as the different post types need to be styled differently.
<ul>
<?php
$args = array(
'posts_per_page' => 10,
'post_type' => (array('small_post','full_post')),
);
query_posts($args); ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( get_post_type( get_the_ID() ) == 'small_post' ) { ?>
<li class="article small-post" style="">
<?php if(has_post_thumbnail()) :?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(''); ?>
</a>
<?php endif;?>
<a href="<?php the_permalink(); ?>">
<h3>
<?php the_title(); ?>
</h3>
</a>
<p><?php the_excerpt(); ?></p>
</li>
<?php } ?>
<?php if ( get_post_type( get_the_ID() ) == 'full_post' ) { ?>
<li class="article full-post" style="">
<?php if(has_post_thumbnail()) :?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(''); ?>
</a>
<?php endif;?>
<a href="<?php the_permalink(); ?>">
<h3>
<?php the_title(); ?>
</h3>
</a>
<p><?php the_excerpt(); ?></p>
</li>
<?php } ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</ul>
Not sure what i have done wrong here but i noticed that everything dissapears below my code.
If i take fields and put them in top of template then everything works fine.
Code
<?php
$blacklabelpost = array( 'post_type' => 'black-label', );
$loop = new WP_Query( $blacklabelpost );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$images = get_field('photos_of_product'); // get gallery
$image = $images[0]; // get first image in the gallery [1] for second, [2] for third, and so on.
if( $image ) : // only show the image if it exists ?>
<div class="col-sm-3 col-md-4 col-lg-3 product-col">
<a href="<?php the_permalink() ?>">
<img src="<?php echo $image['url']; ?>" class="img-responsive"/>
</a>
<div class="row text-center">
<div class="col-xs-12">
<h3 class="UC sTitle"><?php the_title()?></h3>
<div class="border-line center"></div>
<div class="subtitle about_product_short UC"><?php the_field('about_product_short'); ?></div>
</div>
<div class="col-xs-12">
Läs mer
</div>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
Below i just have couple of if get field.
Example
<?php if( get_field('full_width_photo_footer') ): ?>
<div class="F-W sida archive">
<?php
$image = get_field('full_width_photo_footer');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" class="img-responsive footer-fullwidth-img"/>
<?php endif; ?>
</div>
<?php endif; ?>
As soon as i delete this 'full_width_photo_footer' starts to work again:
<?php
$mypost = array( 'post_type' => 'white-label', );
$loop = new WP_Query( $mypost );
?>
What am i doing wrong ?
I have a custom WordPress post type, and want to make so if there are no posts in the query to display, it shows a message of saying no posts were found, I've tried a few suggestions but nothing seems to be working so far.
Full Code of the relevant section:
<?php
$k=0;
$args = array(
'post_type'=>'portfolios',
'posts_per_page'=>5,
'order'=>'DESC'
);
$the_query = new WP_Query($args);
while($the_query->have_posts()):$the_query->the_post();
$k++;
if($k==1){
$color= 'gray';
}else if($k==2){
$color= 'blue';
}else if($k==3){
$color= 'green';
}else if($k==4){
$color= 'light-blue';
}else if($k==5){
$color= 'yellow';
}
?>
<div itemprop="exampleOfWork" class="col-xs-12 col-sm-4 portfolio-item">
<div class="portfolio-holder">
<?php
if(has_post_thumbnail()){
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'singleport',true);
?>
<figure><a itemprop="url" href="<?php the_permalink();?>"><img src="<?php echo $image_url[0];?>" alt="Portfolio image" /></a></figure>
<?php }else{ ?>
<figure><a itemprop="url" href="<?php the_permalink();?>"><img src="<?php echo esc_url( get_template_directory_uri() );?>/assets/images/noimage.jpg" alt="No image" /></a></figure>
<?php } ?>
<h6 itemprop="name"><?php the_title(); ?></h6>
<span itemprop="genre"><?php the_field('name');?></span>
<div class="transparentbg <?php echo $color; ?>">
<?php
$image = get_field('logo');
if( !empty($image) ): ?>
<a itemprop="url" href="<?php the_permalink();?>">
<img src="<?php echo $image['url']; ?>" alt="logo" />
</a>
<?php endif; ?>
<a href="<?php the_permalink();?>" class="info">
<img itemprop="image" src="<?php echo esc_url( get_template_directory_uri() );?>/assets/images/port-hover-bg.png" alt="Port hover bg" /><br />
<img itemprop="image" src="<?php echo esc_url( get_template_directory_uri() );?>/assets/images/search-icon.png" height="20" width="20" alt="Search icon">
</a>
</div>
</div>
</div>
<?php endwhile;wp_reset_query();?>
</div>
</div>
</section>
How can we make it so, if there's no posts, it echo's a message?
Add a check -
if (!empty($the_query->have_posts())) {
while(...) {...}
} else {
echo "No Posts!";
}
Issue
You want to use WP Query class for fetching a particular type of posts ( based on custom fields ), and if it returns no posts at all, it should display a message.
Solution
$the_query = new WP_Query( $args );
Where "$args" is your list of parameters for WP_Query constructor.
Now $the_query is an object of class type WP-Query. Hence it contains a function that will return whether there is any post matching your particular query. If there is at least one post, it will return True, else it will return False.
That function to check if there are any posts matching your queries is have_posts() .
Usage is ObjectName->have_posts();
in your specific case it is
$the_query->have_posts();
This will return true or false based on the query ( ie, arguments you supply using $arg variable ).
hence:
if($the_query->have_posts()) {
//There are posts matching the query
}
else
{
//There are no posts matching the query
}
You can use the below code ( You will have to modify this, but still a good start ).
<?php
$args = array(
'post_type'=>'portfolios',
'posts_per_page'=>5,
'order'=>'DESC'
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Reference : https://codex.wordpress.org/Class_Reference/WP_Query
You need to add if condition before while loop like that way:
<?php
$k=0;
$args = array(
'post_type'=>'portfolios',
'posts_per_page'=>5,
'order'=>'DESC'
);
$the_query = new WP_Query($args);
if( $the_query->have_posts() ) {
while($the_query->have_posts()):
// your code
<?php endwhile;
} else {
echo "No data found.";
}
wp_reset_query();
?>
<?php
$k=0;
$args = array(
'post_type'=>'portfolios',
'posts_per_page'=>5,
'order'=>'DESC'
);
$the_query = new WP_Query($args);
if($the_query->have_posts()):
while($the_query->have_posts()):$the_query->the_post();
$k++;
if($k==1){
$color= 'gray';
}else if($k==2){
$color= 'blue';
}else if($k==3){
$color= 'green';
}else if($k==4){
$color= 'light-blue';
}else if($k==5){
$color= 'yellow';
}
?>
<div itemprop="exampleOfWork" class="col-xs-12 col-sm-4 portfolio-item">
<div class="portfolio-holder">
<?php
if(has_post_thumbnail()){
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'singleport',true);
?>
<figure><a itemprop="url" href="<?php the_permalink();?>"><img src="<?php echo $image_url[0];?>" alt="Portfolio image" /></a></figure>
<?php }else{ ?>
<figure><a itemprop="url" href="<?php the_permalink();?>"><img src="<?php echo esc_url( get_template_directory_uri() );?>/assets/images/noimage.jpg" alt="No image" /></a></figure>
<?php } ?>
<h6 itemprop="name"><?php the_title(); ?></h6>
<span itemprop="genre"><?php the_field('name');?></span>
<div class="transparentbg <?php echo $color; ?>">
<?php
$image = get_field('logo');
if( !empty($image) ): ?>
<a itemprop="url" href="<?php the_permalink();?>">
<img src="<?php echo $image['url']; ?>" alt="logo" />
</a>
<?php endif; ?>
<a href="<?php the_permalink();?>" class="info">
<img itemprop="image" src="<?php echo esc_url( get_template_directory_uri() );?>/assets/images/port-hover-bg.png" alt="Port hover bg" /><br />
<img itemprop="image" src="<?php echo esc_url( get_template_directory_uri() );?>/assets/images/search-icon.png" height="20" width="20" alt="Search icon">
</a>
</div>
</div>
</div>
<?php
endwhile;
else:
echo "Your message goes here";
endif;
wp_reset_query();
?>
</div>
</div>
</section>
Adding if condition will do it.