Display posts only from one category (function modification) - php

I would like to display post from only one category. How should I change this function?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('post_type' => 'post','paged' => $paged);
query_posts($args);
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part( 'post-format/content', get_post_format() );
endwhile;
else:
get_template_part( 'post-format/content', 'none' );
endif;
?>

<?php
/*
Template Name: Boats'n'Hoes
*/
?>
<?php get_header(); ?>
<div id="main">
<div id="content" class="narrowcolumn">
<?php
query_posts('cat=1');
while (have_posts()) : the_post();
the_content();
endwhile;
?>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
reference :- https://wordpress.org/support/topic/display-the-posts-of-one-category-in-a-page-solved

Something I created for a client was a template that lists posts from a category that has the same name as the page.
So, create a Page called "funnies" if you want to list all posts in the "technology" category.
Oh, and the original content of the Page is displayed too, if you need an introductory text.
<?php /*
Template Name: ListPostsInCategoryThatHasSameNameAsPage
*/ ?>
<?php get_header(); ?>
<div id="content">
<div id="main">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?>
<?php endwhile; else: endif; ?>
</div>
</div>
<?php get_footer(); ?>

Pass this with $args
'category_name' => 'cat-name',

Related

How to make a second loop in single.php work?

-simple blog
-twenty twelve child theme
I need: a second loop in single.php that shows the selected post and all the other posts below.
What I have so far in single.php (results in a blank page) :
<?php get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
<?php endif; ?>
<?php wp_reset_postdata(); // reset the post data so we can run another query ?>
<?php get_sidebar(); ?>
<?php
// The Second Query
$the_query = new WP_Query();
// The Loop
if ( $the_query->have_posts() ):
while ( $the_query->have_posts() ):
$the_query->the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); // Restore original Post ?>
</div><!-- #content -->
</div><!-- #primary -->
This should do the trick:
<?php get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_postdata(); // reset the post data so we can run another query ?>
<?php
$args_second = array(
'posts_per_page' => -1,
);
// The Second Query
$second_query = new WP_Query( $args_second );
// The Loop
if ( $second_query->have_posts() ):
while ( $second_query->have_posts() ):
$second_query->the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); // Restore original Post ?>
</div><!-- #content -->
</div><!-- #primary -->
Notes:
You need to properly show the title and content using the_title() and the_content() inside the single loop.
To show other posts, you need to query them, you'll quickly understand by looking at the code above.
I'll leave the styling for you.
It is tested and working.

query_post and have_posts issue

I have 20 posts and i need to display text after every 5 posts. So i have asked the query in stackoverflow and i have get the solution. I have tried the query in my site. My code is,
<?php
query_posts( array(orderby=>post_date, order=>desc) );
$p = 1;
while ( have_posts() ) : the_post();
?>
<div class="post">
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
</div>
<?php ($p%5 == 0) ? ($p/5): ""; ?>
<?php if($p==5):
echo hai;
elseif($p==10):
echo fine;
endif;
?>
<?php
$p++;
?>
<?php
endwhile;
?>
It works fine when echoeing "Hai" and "Fine" after 5 posts.
BUt while replacing the code in the below format. The posts are not fetching correctly. Please anyone help me. I need to add my own category id 3 after 5th post and category id 4 after 10th post
<?php
query_posts( array(orderby=>post_date, order=>desc) );
$p = 1;
while ( have_posts() ) : the_post();
?>
<div class="post">
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
</div>
<?php ($p%5 == 0) ? ($p/5): ""; ?>
<?php if($p==5):
if (have_posts()) :
query_posts( array(cat=>3, orderby=>post_date, order=>desc) );
while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif;
elseif($p==10):
echo fine;
endif;
?>
<?php
$p++;
?>
<?php
endwhile;
?>
inplace of echo fine; i need to add the same code used above in place of echo hai;
Use wp_reset_query().
wp_reset_query() restores the $wp_query.
<?php
query_posts( array(orderby=>post_date, order=>desc) );
$p = 1;
while ( have_posts() ) : the_post();
?>
<div class="post">
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
</div>
<?php ($p%5 == 0) ? ($p/5): ""; ?>
<?php if($p==5):
if (have_posts()) :
wp_reset_query();
query_posts( array(cat=>3, orderby=>post_date, order=>desc) );
while (have_posts()) : the_post(); ?>
<?php the_title();
?>
<?php endwhile; ?>
<?php endif;
elseif($p==10):
wp_reset_query();
if (have_posts()) :
wp_reset_query();
query_posts( array(cat=>3, orderby=>post_date, order=>desc) );
while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif;
endif;
$p++
?>
<?php
endwhile;
?>

Display Post image on the Custom Page Template with Pagination

I have this code for my page template.
This is what i have right now: http://www.ohright.com/emoticons/
How do I change it so that it can display all my post images at 20 images per-page and with a pagination below?
<?php
/*
Template Name: emoticons-page
*/
get_header(); ?>
<div id="content">
<div id="main">
<?php query_posts('cat=44'.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?>
<?php endwhile; else: endif; ?>
</div>
</div>
<?php get_footer(); ?>
<?php
/*
Template Name: emoticons-page
*/
get_header(); ?>
<div id="content">
<div id="main">
<?php
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$posts_per_page = 20;
query_posts('cat=44&post_status=publish&paged=' . $paged . '&posts_per_page=' . $posts_per_page);?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?>
<?php endwhile;
posts_nav_link();
wp_reset_query();
endif; ?>
</div>

Call top posts in different divs

I want to have my latest post show up with all info(title, author, thumbnail, and content) but only the title of the second most recent post in another div. Here is my code. It renders the divs correctly but the 'title' in the second div is the 'title' of the latest post still.
<div id="blog-pane">
<div id="blog-post">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-title">
<?php the_title(); ?>
</div>
<div id="post-author">
<?php the_author(); ?>
<?php the_date(); ?>
<?php the_time(); ?>
</div>
<div id="post-image">
<?php the_post_thumbnail(); ?>
</div>
<div id="post-text">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php rewind_posts(); ?>
</div>
<div id="post-link-1">
<?php
query_posts( 'p' );
while ( have_posts() ) : the_post();
the_title();
endwhile;
wp_reset_query();
?>
</div>
</div>
</div>
<?php get_footer(); ?>
you can try to introduce some skip logic to have it skip the first post,
<div id="post-link-1">
<?php
query_posts( 'p' );
$count = 0;
while ( have_posts() ) {
if ($count++ == 0) {
the_post();
continue;
}
the_post();
the_title();
}
wp_reset_query();
?>
</div>

Wordpress: category collection with "&orderby=date&order=asc" but order refuses to ascend

Here is some code I am trying to use from this topic :
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else: endif; ?>
<?php query_posts('category_name='.get_the_title().
'&order=ASC&orderby=date&posts_per_page=10'.'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?>
<?php endwhile; else: endif; ?>
By the time the post loads, is it too late to use "orderby"? Is that why it stubbornly stays "newest post first"?
Try orderby first and order second.
&orderby=date&order=ASC

Categories