I have a related posts slider but currently there is not enough posts to actually slide. The design is such that it's not a simple case of displaying the posts but not sliding them until there is more.
As a short term solution, I am trying to find a way to loop through the posts twice to give the appearance of an infinite loop.
I am using slick slider and initially tried the settings:
infinite: true,
loop: true
but I can't get it to work even though apparently that should do the trick.
I am now trying to just pull through the posts twice
I have tried adjusting the count to things like
$count+1;
$count = 2
$count+=5;
All sorts of variations but I think I am way off base.
I would appreciate any help or a point in the right documentation. I have been reading about iterations but I can't grasp how they would be included in this as I had help with this from another developer.
<div class="log-book-carousel">
<?php
$current_page_id = get_the_ID();
$args = [
'posts_per_page' => '6',
'post__not_in' => [$current_page_id]
];
$the_query = new WP_Query( $args ); ?>
<?php
// Start our WP Query
while ($the_query -> have_posts()) : $the_query -> the_post();
// Display the Post Title with Hyperlink
?>
<div class="slides match-height">
<a href="<?php the_permalink(); ?>" title="Read more of the blog post '<?php the_title_attribute(); ?>'">
<?php if (has_post_thumbnail()) : ?>
<div class="log-book-slider-image">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<div class="log-book-details-wrapper white-bg">
<h3 class="black log-title"><?php the_title(); ?></h3>
<div class="log-book-slider-excerpt ">
<p class="log-book-text"><?php sew_display_post_intro_block( $post->ID, 10 ); ?></p>
</div>
</div>
</a>
</div>
<?php
$count++;
endwhile;
wp_reset_postdata();
?>
</div>
Related
I'm having some problems trying to show the first image of a gallery product on woocommerce,
I´m working on a personal wordpress theme, with personal template, all this work is into an owl carousel to show every product on a different slide,
I´m new with Woocommerce, I´m using WP_Query to show the products of a category, inside I´m showing the_title, the_content, the_permalink of every product on a while, I want to show the first and only one image inside the gallery of every product but I don´t know how to reach to it:
<div class="owl-carousel owl-theme" id="carousel">
<?php
$params = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '-1',
'product_cat' => 'barras'
);
$wc_query = new WP_Query($params);
if ($wc_query->have_posts()) :
while ($wc_query->have_posts()) :
$wc_query->the_post();
?>
<div class="item">
<a href="<?php the_permalink();?>">
<img src="<?php echo get_template_directory_uri(); ?>/assets/img/prev.png" alt="">
<img src="<?php echo get_template_directory_uri(); ?>/assets/img/next.png" alt="">
<p class="titulo-producto-slider"><?php the_title(); ?></p>
</a>
<div class="espacio-10"></div>
<div class="descripcion-producto-slider">
<?php the_content(); ?>
</div>
<div class="ver-detalle">
<ul>
<li>
Ver detalles
</li>
</ul>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
else: ?>
<p><?php _e( 'No Products' );?></p>
<?php endif;
?>
</div>
Sorry if you don´t understand something, it is my first question on stack and I don´t speak English,
Thanks a lot,
Try this:
$product = new WC_Product( get_the_ID() );
$attachment_ids = $product->get_gallery_image_ids();
if ( is_array( $attachment_ids ) && !empty($attachment_ids) ) {
$first_image_url = wp_get_attachment_url( $attachment_ids[0] );
// ... then do whatever you need to do
} // No images found
else {
// #TODO
}
For more details: WP_Product class reference.
I've done my hour+ research to find any answer but cannot find a solution.
Anyways... I have a wordpress posts page and on individual posts I have a query to display the latest 4 posts at the bottom of each post page.
What I'd like to do is only show 4 posts that are older than the current post page a visitor is on. Currently it shows the latest 4 entries no matter which post you're viewing.
Thank you.
<ul>
<?php $the_query = new WP_Query( array (
'post__not_in' => array( $post->ID ),
'posts_per_page' => 4,
'ignore_sticky_posts' => 1
) ); ?>
<div class="row">
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<li class="col-xs-12 col-md-3">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute();
?>">
<?php the_post_thumbnail( 'post-thumbnail', ['class' => 'img-responsive
responsive--full'] ); ?>
</a>
<?php endif; ?>
<p><?php the_title(); ?></p>
</li>
<?php
endwhile;
wp_reset_postdata();
?>
</div>
</ul>
You can use get_previous_post() to get the previous post.
use this in a loop (assuming $post variable is updated for every post) to get previous 4 posts.
Having a little issue with some PHP code, my skills in PHP aren't the strongest and I'm a little stuck at the moment.
I am working on a site which uses posts for a news page.
We are building a blg page, and I want to use posts as well, to make it simpler for the user, however I cant seem to get the page to use just one category, its pulling every single post no matter what the category.
Here is my code for the page template.
<div class="container">
<div>
<br /><?php echo pageWidgets($post->ID,'main',400); ?>
</div>
<?php /*?> <div class="contentTop2 newspad">
<?php
$args = array( 'numberposts' => 1,'category_name'=> "blog", 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<?php $exclude_first_post = $post->ID; ?>
<div class="postNews">
<?php if ( has_post_thumbnail() ) { /* loads the post's featured thumbnail, requires Wordpress 3.0+
echo '<div class="featured-thumbnail">'; the_post_thumbnail(); echo '</div>';
} ?>
<div class="newsRightPost">
<div class="newsTitleTop"><?php the_title(); ?></div>
<div class="newsDateTop">Posted <?php the_date(); ?></div>
<div class="newsExcerptTop"><?php the_excerpt(); ?></div>
<div class="readMoreText">Read More >></div>
</div>
<div class="clearBoth"> </div>
</div>
<?php endforeach; ?>
<div class="clearBoth"> </div>
</div>
<?php */?>
I thought 'category_name'=> "blog" would sort it out for me, however it doesn't seem to work at all.
I get the blog category, but I also get every other category.
Can anyone point me in the right direction?
Thanks guys!
I already read several answer in SE network and WP forum and several log. but I am still in the dark.
At first, I have to say, my consequence is different.
I created a Page Template Blog (blog.php) and added a wp_query on that page to show normal blog posts in that post because my homepage is a static homepage.
Then I simply created a blank page named Blog with permalink /blog
Next, I added wppagenavi on that ge after elsewhile
It didn't work so I read couple of SOF answer and added
<?php wp_pagenavi( array( 'query' => $the_query ) ); ?>.
It shows page navigation but not working. I clicked 2,3,4... button and it return same page though url changes to /blog/page/2
So, how to make page navi work?
My snippet
<?php $the_query = new WP_Query( 'showposts=8' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="content-box bg-wrap"><?php
if ( has_post_thumbnail() ) { ?>
<div class="img"><a href="<?php the_permalink(); ?>">
<?php
the_post_thumbnail( 'blog_thumb' );
?>
</a></div>
<?php
}
?>
<div class="post-info p20px">
<h2><?php the_title(); ?></h2>
<div class="post-meta"><span class="theauthor"><i class="fa fa-user"></i> <?php echo get_the_author(); ?></span> <span class="thetime"><i class="fa fa-clock-o"></i> <?php the_date(); ?></span> <span class="thecategory"><i class="fa fa-tags"></i><?php the_category(', '); ?></span> <span class="thecomments"><i class="fa fa-comments"></i><?php echo $write_comments; ?></span> </div>
<br/>
<?php echo substr(get_the_excerpt(), 0,600); ?> Read this post »
</div>
</div>
<?php endwhile;?>
<?php wp_pagenavi( array( 'query' => $the_query ) ); ?>
Do not bother other functions and variables or html. I just need to make pagenavi work.
I got pagenavi code from here
I found the problem and the solution as well.
I have to add '&paged='.$paged on $wp_query() and it enables navigation on that scenario.
So, the first line
<?php $the_query = new WP_Query( 'showposts=8' ); ?>
have to be replaced with
<?php $the_query = new WP_Query( 'showposts=8' . '&paged='.$paged); ?>
That's all.
See a live preview (the project I applied this): Here
So I did my due diligence looking on here for an answer since this has come up a few times in the Treehouse forum, but found nothing related. I also tried to find a related topic on the acf site but that didn't give me the right info either. Hopefully you guys can help me out.
At the end of a tutorial on Treehouse, the instructions explain to add a few custom fields using ACF. He explains how to pull all of those in the code except on one crucial one.
We are supposed to create a checkbox field with the Field Label as Display on Homepage Slider and Field Name (or slug) as display_on_homepage. The idea is that we check this on each custom post entry that we want to display on the homepage slider, if you hadn't already guessed that.
Here's the code for the slider as it stands now.
<?php get_header('header.php'); ?>
</div>
<div id="featured" class="clearfix flexslider">
<ul class="slides">
<?php
$args = array(
'post_type' => 'work'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li style="background-color:<?php the_field( 'background_color' ); ?>;">
<div class="container">
<div class="grid_7">
<img src="<?php the_field( 'homepage_slider_image' ); ?>" alt="<?php the_title(); ?> Project Screenshot">
</div>
<div id="featured-info" class="grid_5 omega">
<h6>Featured Work</h6>
<h3><?php the_title(); ?></h3>
<p><?php the_field( 'description' ); ?></p>
<p><a class="btn blue" style="background-color: <?php the_field( 'button_color' ); ?>" href="<?php the_permalink(); ?>">View Project →</a></p>
</div>
</div>
</li>
<?php endwhile; endif; ?>
</ul>
</div>
I'm sure I need to pull some conditions in the args or even establish a different rule in my acf plugin, but I'm at a loss as to where to start with that can of worms. Thanks in advance for any help or advice. I'll be sure to forward this answer to the forum if I can get any assistance.
You'll want to add in an argument to your query for your custom field (meta field): http://codex.wordpress.org/Class_Reference/WP_Query
$args = array(
'post_type' => 'work',
'meta_key' =>'display_on_homepage',
'meta_value'=>'true'
);
The first answer is almost there, just missing a couple of commas :)
$args = array(
'post_type' => 'work',
'meta_key' =>'display_on_homepage',
'meta_value'=>'true'
);