[SOLVED]
I'm trying to integrate Flex Slider (flexslider.woothemes.com) with WordPress. I've included all JS/CSS files needed.
Then, I added this code to show the featured images of a certain category - the famous WP_Query with $args set to a certain category. Then using echo the_post_thumbnail(); I'm showing the posts' images. It's working fine. I just need to make the images linked to post URLs (imgs are hrefed). Please provide help and thanks in advance.
<?php
$args = array(
'post_type' => 'post',
'category_name' => 'one',
'posts_per_page' => 5
);
// The Query
$the_query = new WP_Query( $args );
// Check if the Query returns any posts
if ( $the_query->have_posts() ) {
// Start the Slider ?>
<div class="flexslider">
<ul class="slides">
<?php
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<?php }
// The Slide's Image
echo the_post_thumbnail();
</li>
<?php endwhile; ?>
</ul><!-- .slides -->
</div><!-- .flexslider -->
<?php
// Reset Post Data
wp_reset_postdata();
?>
New code that's working:
<?php
$args = array(
'post_type' => 'post',
'category_name' => 'one',
'posts_per_page' => 5
);
// The Query
$the_query = new WP_Query( $args );
// Check if the Query returns any posts
if ( $the_query->have_posts() ) {
// Start the Slider
?>
<div class="flexslider">
<ul class="slides">
<?php
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php echo the_post_thumbnail(); ?>
</a>
</li>
<?php endwhile; ?>
</ul><!-- .slides -->
</div><!-- .flexslider -->
<?php }
// Reset Post Data
wp_reset_postdata();
?>
If I understand what you're asking, I think the answer is
echo '<a href="'. the_permalink() .'">';
echo the_post_thumbnail();
echo '</a>';
Related
I want to display the 3 most recent posts at the bottom of my single-blog page, but without the current posts.
my code:
<div class="blog__posts">
<h2><?php esc_html_e('andere blogberichten', 'dfib-theme'); ?></h2>
<?php esc_html_e('alle blogberichten', 'dfib-theme'); ?>
<div class="blog__posts--wrapper">
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="blog__single">
<div class="blog__single--img" style="background-image: url(<?php echo get_the_post_thumbnail_url();?>);"></div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
lees meer
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
</div>
</div>
It displays 3 posts, but when I visit the most recent post, the same post is displayed at the bottom again. Is there a way to exclude the current one every time?
I just found the solution on this website: https://pineco.de/snippets/exclude-current-post-from-wp_query/
I just added this piece to my query:
'post__not_in' => array(get_the_ID())
You have to exclude the current post with the post__not_in.
Add post__not_in in WP_Query array like below.
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
'post__not_in' => array( get_the_ID() )
));
?>
How do i get the permalink from cubeportofolio, and the title/thumbnail from posts? With the post_type syntax from wordpress.
here is my code:
<?php
$posts = get_posts(array(
'posts_per_page' => 1,
'post_type' => 'cubeportfolio' ));
if( $posts ): ?>
<?php foreach( $posts as $post ): setup_postdata( $post ) ?>
<li>
<a href="<?php the_permalink(); ?>">
until here the code is oke.
So i am getting the good permalink but my title is wrong, i havent even tried to get the thumbnail.
<?php
$args = array(
'post_type'=> 'post',
'order' => 'date'
);
echo get_the_title($recent)."<br/>";
wp_reset_postdata();
?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I have read some things in the documentation but i still cant figure it out.
i want an ul so people can click through the posts but the problem is, i am making my blogs in an plugin. thats the wrong title and thumbnail, So i want the permalink of cubeportfolio(this is an post_type). and I want the title from posts(this is an post_type)
Hole code:
$posts = get_posts(array(
'post_type' => 'cubeportfolio',
'order' => 'date' ));
if( $posts ): ?>
<?php foreach( $posts as $post ): setup_postdata( $post ) ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php wp_reset_postdata(); ?>
<?php
$args = array(
'post_type'=> 'post',
'order' => 'date'
);
echo get_the_title($recent)."<br/>";
wp_reset_postdata();
?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Any help would be appreciated,
Id love to help you out but I would need to understand what it is you are trying to do. From the above code you are pulling in 1 "cubeportfolio" and then doing another query to pull in "posts" and then output their titles. If you are trying to create a unordered list of links to the "cubeportfolio" there is not need for the second query.
<?php $args = array(
'post_type' => 'cubeportfolio',
'posts_per_page' => 1,
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ) { ?>
<ul>
<?php while ( $the_query->have_posts() ) { $the_query->the_post();
<li>
<?php the_title(); ?>
</li>
<?php } wp_reset_postdata(); ?>
</ul>
<?php endif; ?>
This will only pull in 1 of the "cubeportfolio" items because the query is set to 1 post_per_page. Hope this helps, if not please help me understand your plan and I can update the code to help you out.
Why the following code would cause my breadcrumb trail to show the same post link in the breadcrumbs no matter what page you are on in the site?
I have been stuck on this issue for ages now and just cannot figure it out...
See what I mean here.
<!-- Loops through Stories and returns images -->
<ul style="width: 1520px">
<?php
global $post;
$category_id = get_cat_ID('stories');
$args = array( 'numberposts' => 9, 'offset'=> 0, 'category'=> $category_id );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<!-- stories -->
<li>
<figure>
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(array(64,64)); // Declare pixel size you need inside the array ?></a>
<?php endif; ?>
</figure>
<!-- /post thumbnail -->
<h3><?php the_title(); ?></h3>
</li>
<?php endforeach; ?>
</ul>
<!-- /Loops through Stories and returns images -->
Try to reset the postdata, like in this example from the Codex:
<ul>
<?php
global $post;
$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :
setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php endforeach;
wp_reset_postdata(); ?>
</ul>
I have the current loop working to display posts, but I can't seem to get the title or content to populate. What am I missing?
<?php
// LOOP ARGUMENTS
$args = array( 'post_type' => 'cbd_slider', 'posts_per_page' => -1 ); // -1 Shows ALL Posts
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
// CUSTOM CONTENT
$slideLink = get_post_meta($post->ID,"slideLink",true);
$thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail_name');
$imgURL = (isset($thumb[0]) ? $thumb[0] : get_template_directory_uri() . "/images/placeholder.jpg");
?>
<li class="clearfix"><!-- Start of featured slide -->
<a href="<?php echo $slideLink ?>">
<img src='<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo urlencode($imgURL); ?>&h=330&w=496' alt='featuredimage' /></a>
<div class="description">
<h2>TITLE GOES HERE</h2>
<p>POST CONTENT GOES HERE</p>
more
</div>
</li><!-- End of featured slide --><?php /* END WHILE AND RESET QUERY */ endwhile; wp_reset_query(); ?>
Have you tried the_title? Substituting <?php the_title(); ?> for your TITLE GOES HERE and <?php the_content(); ?> for CONENT GOES HERE?
I have one page (not an article) with n subpages.
In the main page, I need to show max 3 titles of the subpages and insert a pagination for the other.
How can I do that?
This is my simple code now:
<?php
$parent_id = 14; //main page id
$pages = get_pages( array( 'sort_column' => 'menu_order', 'numberposts' => 3, 'child_of' => $parent_id ) );
foreach ( $pages as $page ) : ?>
<div class="item">
<div class="item-title">
<h2><?php echo $page->post_title; ?></h2>
</div>
</div>
<?php endforeach; ?>
Thanks.
I solved by myself, the solution is to use wp_query() to create a new loop insted of using get_pages().
Here the new code for page title and contentwith pagination by Preeti Dua from Avigma Technology:
<?php
// Pagination variable
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// The Query
$the_query = new WP_Query( array( 'post_parent' => 782, 'post_type' => 'page', 'paged' => $paged) );
// The Loop
if($the_query->have_posts()) : while($the_query->have_posts()) : $the_query->the_post();
global $post;
$thePostID = $post->ID; /* this variabled is used if you need to get custom fields of the subpage */
?>
<div id="side-post-title">
<?php the_title(); ?>
</div>
<div id="side-post-excerpt">
<?php the_excerpt(); ?>
<a href="<?php echo get_permalink( $page->ID ); ?>"> <div id="read-more">
<img src="/wp-content/uploads/2012/10/read-more-btn.png"/></div> </a>
</div>
<?php endwhile; endif; ?>
<nav class="navigation">
<div style="float:left;"> <?php next_posts_link('Show older', $the_query->max_num_pages) ?></div>
<div style="float:right;"> <?php previous_posts_link('Show newer') ?></div>
</nav>
not sure,
but try following plugin
http://wordpress.org/extend/plugins/wp-pagenavi/
If you'd like to add subPage title, description or even thumbnail in pagination button you can use the ACP free Wordpress plugin: http://wordpress.org/plugins/advanced-content-pagination/