0

How do I get the code below to display just the current post that corresponds to the current URL and not all the post from the same category? Barakadam, It works but it brings all the posts in the category instead of the one that was clicked on.

<?php
 $category = end(get_the_category());
 $current_name = $category->cat_name;
?>
<?php 
   // assign the variable as current category
   $category_name = $current_name;

  // concatenate the query
  $args = 'category_name=' . $current_name;

  // run the query
  query_posts( $args );

  if ( have_posts() ) : while ( have_posts() ) : the_post() ; 

 // do something here

the_title();
the_content( __('Read the rest of this page »', 'template'));

endwhile; endif;

wp_reset_postdata();

?>
3
  • Not clear : if you only want the current post, why would you bother searching it by category? Something is not logical in your question : if you are on the URL of the post you are looking for, then you already have the post! You might just check that it has a certain category applied to it, but you already have it. And you can directly echo the_title, the_content, etc. without having to execute a query_posts...
    – barakadam
    Commented Dec 14, 2012 at 7:54
  • Barakadam, It works but it brings all the posts in the category instead of the one that was clicked on.
    – Dz.slick
    Commented Dec 14, 2012 at 8:22
  • I am starting to understand your confusion. Please answer me: where did you place this code in your theme's folder: index.php? page.php? single.php?
    – barakadam
    Commented Dec 14, 2012 at 12:34

2 Answers 2

1

What you probably want is this:

1) A main page that displays a list of all posts in a certain category.

2) A page showing the individual post in this list on which the user has clicked.

To create page number, you don't have to do anything. WordPress has an automatic scheme for URLs for you. Just use :

http://yourdomain.com/category/name-of-you-category

This page will display a list of all the posts in this category (provided you left the default category.php page untouched in your theme's folder).

Then, when a user clicks on a post in this list, he will be sent to the corresponding post page. This page is built by WordPress using your theme's single.php page. So if this is where you have inserted the code you are showing us, it is wrong and I understand you will get a list of all posts. Just replace your code with this one :

<?php get_header(); ?>
…
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>    
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
…
<?php get_sidebar(); ?>
…
<?php get_footer(); ?>

This will only display the content of the currently viewed post (designated by the URL and automatically retrieved by WordPress default query).

0

Not sure what you mean by

"just the current post that corresponds to the current URL"

but my first thought is to add 'posts_per_page' => 1 as an argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.