Why it gives random post ID - php

I am building a WordPress theme. I am having some trouble on commenting section. When showing comments it gets randomly post ID.
I have put the same code in 2 different places in the first place it TOP but by at BOTTOM its not working. Can anyone help me by telling why is this not working at bottom?!
Here is my single.php file
<?php get_template_part('/template-parts/standard-post-header'); ?>
<main role="main">
<!-- section -->
<section>
<div class="container background-color">
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if (has_post_video( isset($post_id) ) != null) {
// Featured VIDEO -->
get_template_part('/template-parts/featured-video-post');
// END Featured VIDEO -->
} else { ?>
<!-- Featured Image -->
<?php $header_type = get_post_meta(get_the_id(), 'meta-box-dropdown', true); ?>
<?php if ($header_type == 'Slider') {
// SLIDER Header
get_template_part('/template-parts/featured-slider-post');
?>
<?php } else {
// SLIDER Header
get_template_part('/template-parts/featured-normal-post');
} ?>
<!-- END Featured Image -->
<?php } ?>
<div class="container container-post-color">
<div class="content">
<?php the_content(); ?>
<?php edit_post_link(); ?>
</div>
</div>
<?php
global $post;
echo $post->ID;
?>
<ol class="commentlist">
<?php
//THIS WORKS!!!
$comments = get_comments(array(
'post_id' => $post->ID,
'status' => 'approve'
));
wp_list_comments(array(
'per_page' => 10,
'reverse_top_level' => false
), $comments);
?>
</ol>
<!-- Post Navigation -->
<div class="container container-post-color top-space" style="padding-left: 0px; padding-right: 0px;">
<div id="left-side"><?php previous_post_link(); ?></div>
<div id="right-side"><?php next_post_link(); ?></div>
<?php echo wp_link_pages(); ?>
</div>
<!-- Tags -->
<div class="tags-area">
<?php echo the_tags(); ?>
</div>
<!-- Related Articles -->
<?php get_template_part('/template-parts/related-articles'); ?>
<!-- Coments Part -->
<?php //get_template_part('/template-parts/comments'); ?>
<?php
global $post;
echo $post->ID;
?>
<ol class="commentlist">
<?php
//THIS DOES NOT WORKS!!! WHY?!
$comments = get_comments(array(
'post_id' => $post->ID,
'status' => 'approve'
));
wp_list_comments(array(
'per_page' => 10,
'reverse_top_level' => false
), $comments);
?>
</ol>
</article>
<!-- /article -->
</div>
<!-- END of Container-Fluid -->
<?php endwhile; ?>
<?php else: ?>
<!-- article -->
<article>
<div class="container background-color">
<h1 class="black mastheading-post"><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h1>
</div>
</article>
<!-- /article -->
<?php endif; ?>
</section>
<!-- /section -->
</main>
<!-- INSTAGRAM -->
<?php get_template_part('/template-parts/instagram'); ?>
<?php get_footer(); ?>
related-articles.php
<div class="container container-post-color" style="padding-left: 0px; padding-right: 0px;">
<div class="random">
<ul>
<?php $posts = get_posts('orderby=rand&numberposts=4'); foreach($posts as $post) { ?>
<div class="col-md-3 padding-zero">
<li>
<div class="random-thumb">
<?php the_post_thumbnail('full'); ?>
</div>
<div class="random-title">
<h1 class="black mastheading"><?php the_title(); ?></h1>
</div>
<div class="black iltalica"><?php echo excerpt(25); ?></div>
<div class="category">
<div class="random-category">
<h5><?php echo the_category();?></h5>
</div>
</div>
</li>
</div>
<?php } ?>
</ul>
</div>
</div>

So first off, since you are in the loop, everywhere you've used global $post; ... $post->ID you should be able to use get_the_ID() instead.
Second, I strongly suspect the problem is your template part /template-parts/related-articles probably messes-up the main loop. I suggest you look at that file and see if it's itself looping on a selection of posts - chances are it is not doing it cleanly, in a way that can be re-used inside the main loop.
You can add that file's code to your question if you need help figuring it out.
UPDATE
Ok, so indeed, you need to reset the loop data after the related-articles loop:
...
<?php
}
wp_reset_postdata(); // <----- add this after your loop
?>
</ul>
...
Hope this helps!

Related

Wordpress loop doesn't work

I created my own template in WordPress, but the loop entries does not work. I would like to entries work on one of the subpages. I also added entries.
This is my code of my subpage. Please help me. I don't know what is wrong. I added a picture under the code.
<?php include 'header.php'; ?>
<main class="subpage-blog">
<div class="subpage-banner">
<div class="container">
<h3>BLOG SIDEBAR</h3>
<div class="breadcrumbs">
</div>
</div>
</div>
<aside class="side-menu col-md-4">
<div class="search">
<h4>Search blog</h4>
<input type="text" value="Search">
</div>
<!-- .search -->
<div class="categories">
<h4>Blog Categories</h4>
<ul class="categories-blog-ul">
<li>Inspirtation</li>
<li>Work</li>
<li>Tech</li>
<li>Creative</li>
</ul>
</div>
<!--.categories-->
<div class="recent-posts">
<h4>Recents posts</h4>
<ul>
</ul>
</div>
<!-- .recent-posts-->
<div class="tags-spot">
<h4>Tags</h4>
<div class="tag"></div>
</div>
<!-- .tags-spot-->
</aside>
<!-- .side-menu-->
<article class="content">
<div class="container">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="news-box">
<div class="news-list-content">
<a href="">
<h3><?php the_title(); ?></h3>
<?php the_content('czytaj dalej'); ?>
</a>
</div>
<!-- .news-list-content-->
<div class="image-box-news">
<img src="<?=get_template_directory_uri(); ?>/images/ikona-wpisu.png" alt="" />
</div>
</div>
<!-- .news-box-->
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
<!-- .container-->
</article>
<!-- .content-->
</main>
<?php include 'footer.php'; ?>
The problem is that:
if (have_posts()) : while (have_posts()) : the_post();
is using the current page's have_posts query.. That means that it probably will just show whatever that current page template's content would be.
Instead, you'll want to create an entirely new query object and call these functions on it like so:
<?php
$the_query = new WP_Query( array('posts_per_page' => 10 ) ); //Create our new custom query
if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div class="news-box">
<div class="news-list-content">
<h3><?php the_title(); ?></h3>
<?php the_content('czytaj dalej'); ?>
</div>
<!-- all your other markup goes here.... -->
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<?php wp_reset_postdata(); //Restore the original postdata for this page, if needed later ?>

Change order by date on category wordpress

I need help with a product category page in Wordpress. Original code was made by someone else, and I'm fairly new to php. I may need help with looking for a way to make the product sort by date published. right now, here is the code that I have.
category-books.php
<?php
/**
* Template Name: Store Template
* #package WordPress
*/
get_header(); ?>
<div id="page-title">
<!-- 960 Container Start -->
<div class="container">
<div class="sixteen columns">
<h1>
<?php single_cat_title( '', true ); ?>
</h1>
</div>
</div>
<!-- 960 Container End -->
</div>
<?php
get_template_part( 'content', 'product4' ); ?>
<div class="blogsidebar" style="float:left !important">
<?php dynamic_sidebar('sidebar-1'); ?>
</div>
<?php get_footer(); ?>
content-product4.php
<?php function bac_wp_strip_header_tags_only( $excerpt ) {
$regex = '#(<h([1-6])[^>]*>)#';
$excerpt = preg_replace($regex,'', $excerpt);
return $excerpt;
}
add_filter( 'the_content', 'bac_wp_strip_header_tags_only', 0); ?>
<div class="container">
<?php $sidebar_side = get_post_meta($post->ID, 'incr_sidebar_layout', true);
if($sidebar_side == "left-sidebar") {
get_sidebar();
} ?>
<!-- Blog Posts ================================================== -->
<div class="twelve columns" style="float:right !important;">
<div class="product-container">
<?php $posts = query_posts($query_string . 'orderby=date&order=DESC&cat=238'); ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Post -->
<div <?php post_class('post'); ?> id="product-post-2Col" > <a class="post_title" href="<?php the_permalink() ?>">
<div style="margin-bottom:10px;width: 130px;height: 180px !important; border: 2px solid #000 !important;padding: 3px;float: left;margin: 0 15px 15px 0;overlay:hidden;">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail( 'medium', array( 'class' => 'store-featured-image' ) );
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/product-image.png" alt="<?php the_title(); ?>" />
<?php } ?>
</div>
<div style="min-height:40px !important;width:50% !important;float:left;">
<h4>
<?php the_title(); ?>
</h4>
</a> </div>
<?php?>
<div class="product-description-2col">
<?php the_excerpt()?>
<a style="margin-bottom: 8px;" class="post_title" href="<?php the_permalink() ?>">Learn More</a><br/>
<?php ?>
<a href="<?php the_field('buy_now_1')?>">
<div id="buybtn">Buy Now</div>
</a>
<?php ?>
</div>
<?php ?>
<p> </p>
<p> </p>
</div>
<!-- Post -->
<?php endwhile; // End the loop. Whew. ?>
</div>
<div style="text-align:center;">
<?php posts_nav_link( ' ยท ', 'previous page', 'next page' ); ?>
</div>
</div>
<!-- eof eleven column -->
is there a way to add something to functions.php? I've added this line but still not working:
<?php $posts = query_posts($query_string . 'orderby=date&order=DESC&cat=238'); ?>
<?php while (have_posts()) : the_post(); ?>
Thanks for the help.
Instead of using functions.php for this, create a child theme and edit a copy of the file you want to change there.
https://codex.wordpress.org/Child_Themes

Working with external loop in two different situations in same site (WP)

In my theme I have a blog page and a homepage (without blog loop). Now I would like to add some blog posts on my homepage (like most recent).
I am using the blog page as a template for the homepage ( I copied and renamed the original), wich works good. But I can't make any changes to it without changing the original blog page (the loop is called in an external php).
I Have no idea were to start.
This is the piece on the homepage calling the blog loop:
<?php if(have_posts()) : the_post(); ?>
<div id="main">
<div class="central-wrapper clearfix">
<div id="center" class="fullwidth">
<div id="content">
<?php if($gallery_select && $gallery_position == 1) : ?>
<?php codeus_gallery($gallery_select, $gallery_size, $gallery_style); ?>
<?php endif; ?>
<div class="inner">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="pagination"><div class="page-links-title">' . __( 'Pages:', 'codeus' ) . '</div>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
<div class="bloghome">
<?php codeus_blog_list(); ?>
</div>
<?php if($quickfinder_position == 2) : ?>
<?php codeus_quickfinder($quickfinder_select); ?>
<?php endif; ?>
<?php if($portfolio_position == 2) : ?>
<?php codeus_portfolio(implode(',',$portfolio_select), $portfolio_size, $portfolio_count, $portfolio_filter, $portfolio_title); ?>
<?php endif; ?>
<?php if($gallery_select && $gallery_position == 3) : ?>
<?php codeus_gallery($gallery_select, $gallery_size, $gallery_style); ?>
<?php endif; ?>
</div>
</div><!-- #content -->
</div><!-- #center -->
</div><!-- .central-wrapper -->
</div><!-- #main -->
<?php endif; ?>
</div><!-- wrap end -->
And this is the piece in the external php called blog.php
<?php if($blog_loop->have_posts()) : ?>
<div class="blog_list">
<ul class="styled">
<?php while ( $blog_loop->have_posts() ) : $blog_loop->the_post(); ?>
<li class="clearfix">
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php $image_url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'codeus_post_list_image'); ?>
<div class="comment-info">
<?php echo get_the_date('d'); ?>
<div class="date-month"><?php echo get_the_date('M'); ?></div>
</div>
<div class="post-info">
<h3><?php the_title(); ?></h3>
<?php if($image_url[0]) : ?>
<div class="post-image">
<div class="image wrap-box shadow middle">
<div class="shadow-left"></div><div class="shadow-right"> </div>
<img src=" <?php echo $image_url[0]; ?>" alt="<?php the_title(); ?>" />
</div>
</div>
<?php endif; ?>
<div class="text clearfix"><?php the_excerpt(); ?></div>
<?php codeus_author_info(get_the_ID()); ?>
</div>
</div>
</li>
<?php $post = $portfolio_posttemp; wp_reset_postdata(); ?>
<?php endwhile; ?>
</ul>
<?php codeus_pagination($page_num, $pages_count); ?>
</div>
<?php endif; ?>
Remember, I don't want to make changes in the original blog.php, because the normal blog page needs to work as it is now. I would like to make the blog work on the homepage also, but just with a maximum of 3 posts showing, perhaps of a certain category.
Help would be so welcome!!
I think I found a solution, I edited the homepage template like this:
<!-- wrap start --><div class="content-wrap">
<?php if(have_posts()) : the_post(); ?>
<div id="main">
<div class="central-wrapper clearfix">
<div id="center" class="fullwidth">
<div id="content">
<div class="inner">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="pagination"><div class="page-links-title">' . __( 'Pages:', 'codeus' ) . '</div>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
<div class="bloghome">
<?php
$page_num = (get_query_var('paged')) ? get_query_var('paged') : 1;
$items_per_page = 2;
$blog_loop = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => $items_per_page,
'paged' => $page_num
));
$pages_count = ceil($blog_loop->found_posts/$items_per_page);
global $post;
$portfolio_posttemp = $post;
?>
<?php if($blog_loop->have_posts()) : ?>
<div class="blog_list">
<ul class="styled">
<?php while ( $blog_loop->have_posts() ) : $blog_loop->the_post(); ?>
<li class="clearfix">
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php $image_url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'codeus_post_list_image'); ?>
<div class="comment-info">
<?php echo get_the_date('d'); ?>
<div class="date-month"><?php echo get_the_date('M'); ?></div>
</div>
<div class="post-info">
<h3><?php the_title(); ?></h3>
<?php if($image_url[0]) : ?>
<div class="post-image">
<div class="image wrap-box shadow middle">
<div class="shadow-left"></div><div class="shadow-right"></div>
<img src="<?php echo $image_url[0]; ?>" alt="<?php the_title(); ?>" />
</div>
</div>
<?php endif; ?>
<div class="text clearfix"><?php the_excerpt(); ?></div>
<?php codeus_author_info(get_the_ID()); ?>
</div>
</div>
</li>
<?php $post = $portfolio_posttemp; wp_reset_postdata(); ?>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
</div>
</div>
</div><!-- #content -->
</div><!-- #center -->
</div><!-- .central-wrapper -->
</div><!-- #main -->
<?php endif; ?>
</div><!-- wrap end -->

Wordpress - Why calling the_post_thumbnail() AFTER get_sidebar() doesn't show anything on my template

I'm actually trying to display the post thumbnail inside the loop just before the content by using the_post_thumbnail(); function.
It works like a charm in anywhere in my page before I call the function
<?php get_sidebar(); ?>
After that impossible to show the post thumbnail.
I've tried with
<?php the_post_thumbnail(); ?>
and also
<?php echo get_the_post_thumbnail(); ?> but nothing happens.
Here is my whole code :
<?php
/**
* The Template for displaying all single posts
*
* #package WordPress
*/
get_header(); ?>
<div id="pageHeader" >
<div id="pageHeader-inner"> <span class="shadow"></span><img src="<?php bloginfo('template_url'); ?>/images/header_01.jpg" /></div>
</div>
<div class="container" id="pageTitle">
<h1><?php the_title(); ?></h1>
</div>
<!--Begin of content-->
<div class="grey-bg">
<div class="container">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="row">
<div class="col-sm-3 sidebar">
<!-- Sub Nav -->
<?php if ( is_page() ) { ?>
<?php
if($post->post_parent)
$children = wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0'); else
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if ($children) { ?>
<div class="sidebar-module sub-menu">
<ul>
<?php echo $children; ?>
</ul>
</div>
<?php } } ?>
<!--Works perfectly till here -->
<?php get_sidebar(); ?>
<!--Broken till here-->
</div> <!-- /.sidebar -->
<div class="col-sm-9">
<div class="content-main">
<div class="content white-bg left-stroke <?php echo $post->post_name; ?>">
<?php if ( has_post_thumbnail() ) the_post_thumbnail(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else : ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1>Not Found</h1>
</div>
<?php endif; ?>
</div> <!-- /.content -->
</div><!-- /.content-main -->
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container -->
</div>
<?php get_footer(); ?>
Thank's a lot for your responses.
You are trying to use function the_post_thumbnail() outside WordPress loop. To use this function outside loop you have to specify "post ID". Documentation for this function http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
For example to get thumbnail for post ID 4 you have to use function with parameter 4 like the_post_thumbnail(4) or store ID in variable.

"Previous Posts" is Not Working

For some reason, when you click "Previous Posts" on my wordpress blog, the page URL changes, but the first ten posts are displayed again.
You can see the issue here: http://onedirectionconnection.com/ and then page two http://onedirectionconnection.com/page/2/#sthash.0SQiq9AP.dpbs (that's another thing - I'm not sure why that code is being added at the end of the following page's URL)...
Anyway, here is the code I'm using for my front page template saved in a file called front-page.php
<?php
/*
Template Name: Splash Page
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<div class="section-container">
<h1 class="section-title">Latest News</h1>
</div>
<?php $my_query = new WP_Query('showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<h1 class="entry-title bottommargin big">
<?php the_title(); ?>
</h1>
<div class="entry-content">
<?php the_content(); ?>
</div>
<div class="row">
<div class="col-md-12">
<footer class="row no-margin">
<div class="col-md-3 meta-entry">
Author: <br>
<?php the_author_link(); ?>
</div>
<div class="col-md-3 meta-entry">
Posted On:<br>
<?php the_time('F j, Y'); ?>
</div>
<div class="col-md-3 meta-entry">
Categorized:<br>
<?php echo get_the_category_list(', '); ?>
</div>
<div class="col-md-3 meta-entry-right">
Discussion:<br>
<?php comments_number(); ?>
</div>
</footer>
</div>
</div>
<?php endwhile; ?>
<div class="section-container">
<h1 class="section-title">More News</h1>
</div>
<?php
$custom_query = new WP_Query(array(
'posts_per_page' => 10,
'offset' => 1,
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
));
?>
<?php while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
<div class="row topmargin">
<div class="col-md-3 no-padding center">
<?php the_post_thumbnail('thumbnail', array('class' => 'img-thumbnail img-responsive')); ?>
</div>
<div class="col-md-9">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_excerpt(); ?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'professional1d' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
<?php
// If comments are open or we have at least one comment, load up the comment template
if ( comments_open() || '0' != get_comments_number() )
comments_template();
?>
</div>
</div>
<footer class="row no-margin">
<div class="col-md-3 meta-entry">
Author: <br>
<?php the_author_link(); ?>
</div>
<div class="col-md-3 meta-entry">
Posted On:<br>
<?php the_time('F j, Y'); ?>
</div>
<div class="col-md-3 meta-entry">
Categorized:<br>
<?php echo get_the_category_list(', '); ?>
</div>
<div class="col-md-3 meta-entry-right">
Discussion:<br>
<?php comments_number(); ?>
</div>
</footer>
<?php endwhile; // end of the loop. ?>
<div class="center">
<?php posts_nav_link(); ?>
</div>
</main><!-- #main -->
</div><!-- #primary -->
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Everything seems pretty standard so I really have no clue what the issue is. If anyone could help me out with this issue, it would be greatly appreciated!
After comparing two - three post/pages of your blog. i think, it add the # tag at the end of url. this happen only after loading the entire page. which means it is added by one of your plugin. the plugin may be for "load fresh content from the server instead of the local browser cache"
so first deactivate the plugin you installed for this kind of features

Categories