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>
Related
I'm trying to display 6 posts from a specific custom post type in Wordpress. Everything is working, except for when I remove the line "$wp_query = new WP_Query();". When missing that line, 20 posts gets displayed in alphabetical order and I have no idea why. wp_reset_postdata() or wp_reset_query() don't seem to do anything.
<?php $wp_query = new WP_Query( ); ?>
<div class="blog-footer row margin-top">
<?php
global $post;
$args = array( 'post_type' => 'blog', 'posts_per_page' => 6 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<div class="col-md-2 col-sm-4">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('thumbnail'); ?>
<h4><?php the_title(); ?></h4>
</a>
</div>
<?php endforeach;
wp_reset_postdata();?>
</div>
Happy to get any inputs on why this behaves the way it does. Appreciate it!
global $post is a global variable mostly used by wordpress itself and is used to get contents/ attributes of current post or page mostly..
Read details here
https://codex.wordpress.org/Function_Reference/$post
try below code.
<div class="blog-footer row margin-top">
<?php
wp_reset_query() ;
$args = array( 'post_type' => 'blog', 'posts_per_page' => 6 );
$myposts = get_posts( $args );
foreach ( $myposts as $b_post ) { ?>
<div class="col-md-2 col-sm-4">
<a href="<?php $b_post->post_name; ?>">
<?php
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $b_post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0] ?>"/>
<h4><?php $b_post->post_title; ?></h4>
</a>
</div>
<?php }
wp_reset_postdata();?>
</div>
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.
[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>';
I am having an issue getting the $do_not_duplicate working properly I have several title duplicating on my blog and i need it to stop. Here is what i have so far:
<?php if (is_single()): ?>
<section>
<h3>Related Posts</h3>
<?php
global $post;
$cats = wp_get_post_categories($post->ID);
$do_not_duplicate[] = $post->ID;
if ( count ( $cats ) > 0):
$args = array( 'numberposts' => 3, 'category' => implode($cats, ","), 'exclude' => $post->ID, 'post__not_in' => $do_not_duplicate );
$related_posts = get_posts( $args );
if (count($related_posts)): ?>
<ul>
<?php foreach ($related_posts as $post) : setup_postdata($post); ?>
<li><a href="<?php the_permalink() ?>"><?php while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; if ( get_the_title() ) the_title(); else the_ID(); ?><?php
endwhile;
wp_reset_query(); ?>
</a></li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>No related posts found.</p>
<?php endif; ?>
<?php else: ?>
<p>No related posts found.</p>
<?php endif; ?>
</section>
<?php endif; ?>
you have a while (have_posts() ) within a foreach and this produces the duplication. You may change your loop to something like this:
<?php
global $post;
$cats = wp_get_post_categories($post->ID);
$do_not_duplicate[] = $post->ID;
if ( count ( $cats ) > 0):
$args2 = array( 'numberposts' => 3, 'category' => implode($cats, ","), 'exclude' => $post->ID, 'post__not_in' => $do_not_duplicate );
$related_posts = get_posts( $args2 );
if (count($related_posts)):
?>
<ul>
<?php foreach ($related_posts as $post) : setup_postdata($post); ?>
<li><a href="<?php the_permalink() ?>" ><?php $do_not_duplicate[] = $post->ID; if ( get_the_title() ) the_title(); else the_ID(); ?></a></li>
<?php endforeach; ?>
</ul>
<?php wp_reset_query(); ?>
<?php endif;endif; ?>
At the end of the loop, the var $do_not_duplicate save the id of the post and all the id of the relative posts.
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/