Display all the posts from category - php

I have a category.php with script inside:
<?php $categories = get_the_category(); ?>
<?php $category_id = $categories[0]->cat_ID; ?>
<?php query_posts('cat='.$category_id); ?>
<?php while (have_posts()) : ;?>
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<div class="article">
<div class="article_image" style="background-image: url('<?php echo $image[0]; ?>')">
<i class="fa article_favourite"><?php wpfp_link() ?></i>
</div>
<h3><?php the_title(); ?></h3>
<div class="article_subheading_wrapper">
<p>
<!--Cut the long text -->
<?php
$string = substr(get_field("subheading"), 0, 100);
echo $string."… ";
?>
</p>
</div>
<div class="bottom">
<ul>
<li><p id="views"><img id="eye" src="<?php bloginfo('template_directory'); ?>/icons/watched_icon.svg"> <span><?php if(function_exists('the_views')) { the_views(); } ?></span></p></li>
<li><p id="likes"><i class="fa"></i><?php if(function_exists('like_counter_p')) { like_counter_p(''); }
?></p></li>
<li><p id="comments"><i class="fa fa-comment"></i> <?php comments_number( '0', '1', '%' ); ?></p></li>
</ul>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
It supposed to be displaying all the posts that belong to this category. I does, but it displays only recent one and infinite number of times.
What do I do wrong?

PLEASE PLEASE PLEASE, don't use query_posts, it is evil. Rather use WP_Query

Try either of this,
get_posts('cat=3&posts_per_page=-1');
or
query_posts( array ( 'cat' => cat_id, 'posts_per_page' => -1 ) );
Hope this helps you

Related

how to add pagination in child posts list

I created a custom post type called Programs, Each program contains episodes
Episodes will be Child Post
Some programs contain more than a hundred episodes, so I want to add pagination to my child posts list
I've had a basic attempt at this using $paged = (get_query_var('page')) ? get_query_var('page') : 1; and the paged argument in the WP_Query, but my next_posts_link doesn't work. my code to show child post
<?php
$args = array(
'post_type' => 'programs',
'posts_per_page' => 60,
'post_parent' => $post->ID,
'order' => 'ASC',
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<div class="penci-border-arrow penci-homepage-title penci-magazine-title style-5 pcalign-right pciconp-right pcicon-right" style="border-color: #3E68B0">
<h3 class="inner-arrow" style="background:#3E68B0;border-color: #3E68B0">
الحلقات </h3>
</div>
<ul class="penci-wrapper-data penci-grid">
<?php while ( $parent->have_posts() ) : $parent->the_post();
$featured_img_url = get_the_post_thumbnail_url(get_the_ID());
$episode_number = get_field('episode_number', get_the_ID());
?>
<li class="list-post pclist-layout">
<article id="post-<?php the_ID(); ?>" class="item hentry">
<div class="thumbnail child-thumbnail">
<a class="penci-image-holder penci-lazy" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" style="background-image: url(<?php echo $featured_img_url?>);">
</a>
</div>
<div class="content-list-right content-list-center child-content">
<div class="header-list-style">
<div class="episode-details">
<span class=""> <a class="url fn n" href="<?php the_permalink(); ?>"><?php echo $episode_number ?></a></span>
<span style="margin:0 20px"><a>|</a></span>
<span class="featc-date"><time class="entry-date published" datetime="2021-10-31T13:26:29+00:00">2021-10-31</time>
</span>
<span class='program-viewcn'><?php pvc_stats_update( get_the_ID(), 1 ); ?></span>
</div>
<h2 class="grid-title entry-title">
<?php the_title(); ?>
</h2>
</div>
<div class="item-content entry-content">
<p>
<?php the_excerpt();?>
</p>
</div>
</div>
</article>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>

How to place pagination in WordPress? I am trying similar code but it doesn't work

I am trying to paginate archived post in WordPress when I click "next button" but it doesn't work. Please guide me to do it in a simple way. My code is given below:
<div class="grid-wrapper ">
<?php $args = array(
'posts_per_page' => 3,
'paged' => $paged
);
$the_query = new WP_Query($args);
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="inner-blog">
<div class="row">
<div class="col-lg-4 col-md-2">
<div class="post-thumbnail">
<img class="img-fluid" src="<?php the_post_thumbnail_url(); ?>">
</div>
</div>
<div class="col-lg-8 col-md-9">
<h2 class="post-title"><?php the_title();?></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php wp_reset_postdata(); ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
<ul class="pagination pull-right">
<li><?php echo get_next_posts_link( 'Next Page', $the_query->max_num_pages ); ?> »</li>
<li><?php echo get_previous_posts_link( 'Previous Page' ); ?></li>
</ul>
</div>
</div>

Infinite Scroll wordpress not work

I'm trying to use the Infinite Scroll plugin for Wordpress. I set it up and it doesn't work.
Screen of settings:
Template file
<div id="posts-wrapper" class="row main">
<?php $counter = 1; ?>
<?php if ( have_posts() ){ while ( have_posts() ){ the_post(); ?>
<?php $url = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' ); ?>
<div class="single-post <?php if( $counter == 5 ) { echo "col-xs-12 col-sm-12 col-md-8 col-lg-8"; } else { echo "col-xs-12 col-sm-6 col-md-4 col-lg-4";} ?>">
<div class="thumbnail">
<div class="category-wrapper"><?php the_category(', ');?></div>
<a class="href-overlay" href="<?php the_permalink(); ?>"><div class="wrapper">
<div class="<?php if( has_term( 'hot', 'hot', $post->ID ) ){ echo "inside inside-hot"; } else { echo "inside";} ?>"><div class="image-cover" style="background-image: url(<?php echo $url[0]; ?>)"></div></div>
</div></a>
<div class="post-wrapper">
<p class="post-created-by">Napisane przez <strong><span class="special"><?php echo get_the_author(); ?></span></strong> | <i class="glyphicon glyphicon-comment"></i> <?php comments_number( '0' , '1' , '%' ); ?></p>
<h2 class="post-title"><?php the_title(); ?></h2>
</div>
</div>
</div>
<?php $counter++ ; ?>
<?php }else{ ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php } ?>
</div>
<div id="paginacja">
<?php next_posts_link('wczytaj więcej »')?>
</div>
What am I doing wrong?

Wordpress: How to put 2 different featured posts on one page

hi I have a problem to put featured post without pagination and featured post with pagination in one page:
Here is the code the first one I use this:
<?php
$sticky = get_option('sticky_posts');
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 5);
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
if (have_posts()) : while (have_posts()) : the_post();
$postImage = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' );
?>
<div class="slider" style="background:url(<?php echo $postImage[0];?>) no-repeat center center">
<div class="slider-shadowbox"></div>
<a class="slider-hyperlink" href="<?php the_permalink(); ?>">
<div class="slider-title">
<div class="featured-post-author">
<?php echo get_avatar( get_the_author_meta('user_email'), $size = '100'); ?>
<?php the_author_posts_link(); ?>
</div>
<span class="slider-tagline"><?php the_title()?></span>
<em class="slider-author">By <?php the_author_posts_link(); ?></em>
<em class="slider-date"><i class="fa fa-clock-o"></i> <?php the_time('F j, Y'); ?></em>
</div>
</a>
</div>
<?php
endwhile;
endif;
?>
/and the second one I use this to create featured post with pagination, but somehow the pagination shows the numbers but the hyperlink for each number doesn't work properly./
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<div class="featured-post">
<div class="zitem">
<?php the_post_thumbnail( array(360, 240));?>
</div>
<span class="featured-post-summary">
<h3><?php the_title(); ?></h3>
<?php echo get_excerpt(); ?>
</span>
<div class="featured-post-author-wrap">
<?php echo get_avatar( get_the_author_meta('user_email'), $size = '35'); ?>
<span class="featured-post-author">By <?php the_author_posts_link(); ?></span>
<span class="featured-post-date"><?php the_time('F j, Y'); ?><span>
</div>
</div>
<?php endwhile; ?>
<div class="featured-post-pagination pagination">
<div class="pagination-outer"><?php my_pagination(); ?></div>
</div>
<?php else : ?>
<strong>Sorry no post available.</strong>
<?php endif; ?>

WordPress Blog, only use archive for current page?

I am building a WP site, which will have a News and Blog.
They will be in separate pages, One for news and one for Blog, which will be separated by categories.
So for example, I have this code on 'News', which stops the loop getting posts:
<?php
$uncat = get_cat_ID('uncategorised');
$uncat2 = get_cat_ID('blog');
$args = array(
'posts_per_page' => 3,
'category__not_in' => array($uncat, $uncat2)
);
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-12 col-sm-12 col-xs-12" style="padding-left:0; padding-right:0;">
<a style="color:#333; text-decoration:none;" href="<?php echo get_permalink(); ?>">
<div class="postsize">
<div class="leftfloat" style="float: left; padding-right:20px;">
<?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'faqposts')); ?>
</div>
<div class="contentfaq">
<h4><?php the_title(); ?></h3>
<span class="entry-date-blue"><strong><?php echo get_the_date('d/m/y'); ?></strong></span>
<?php $trimexcerpt = get_the_excerpt();
$shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 10, $more = '… <br/> Read More ...' );
echo '<a style="color:#333; text-decoration:none;" href="' . get_permalink() . '"><p>' . $shortexcerpt . '</p></a>';
?>
</div>
</div>
</div>
</a>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
This works fine, but I also have 'Archives' on the right hand side, which filters by date posted. The issue is, this will get posts from News AND blog, which defeats the idea of splitting them up.
Is there a way to split these up, so if the user clicks 'March 2015' on the archive, it will only get the posts from this month from NEWS?
Here is my current code for Archive.php
<?php if (have_posts()) : ?>
<!-- First, the loop checks whether any posts were discovered with the have_posts() function. -->
<!-- If there were any posts, a PHP while loop is started. A while loop will continue to execute as long as the condition in the parenthesis is logically true. So, as long as the function have_posts() returns a true value, the while loop will keep looping (repeating). -->
<?php while (have_posts()) : the_post(); ?>
<div class="col-md-12 col-sm-12 col-xs-12">
<a href="<?php echo get_permalink(); ?>">
<div class="postsize">
<div style="float: left; padding-right:20px;">
<?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'faqposts')); ?>
</div>
<h5 class="captext"><?php the_title(); ?></h5>
<span class="entry-date-orange"><?php echo get_the_date(); ?></span>
<?php
foreach((get_the_category()) as $category) {
echo ' | ' . $category->cat_name;
}
?>
<p style="margin-top:10px";><?php the_excerpt(); ?></p>
</div>
</a>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Try this , it will echo list
<h2>Archives by Month:</h2>
<ul>
<?php wp_get_archives('type=monthly'); ?>
</ul>
<h2>Archives by Subject:</h2>
<ul>
<?php wp_list_categories(); ?>
</ul>
full example code
<?php
/*
Template Name: Archives
*/
get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php get_search_form(); ?>
<h2>Archives by Month:</h2>
<ul>
<?php wp_get_archives('type=monthly'); ?>
</ul>
<h2>Archives by Subject:</h2>
<ul>
<?php wp_list_categories(); ?>
</ul>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Categories