0

I have displayed the post title within the anchor tag after which I've set the permalink. The permalink, however, is not working with the same title that came from the post. It's like getting the previous link that just clicked. It's going to the same address over and over again and not to exact post link.

    <a class="heading-link" href="<?php  the_permalink(); ?>">           
     <?php

            query_posts('cat=4&showposts=1');

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

         the_title();

         endwhile;
        endif;

        wp_reset_query();

             ?>    
   </a> 

4 Answers 4

0

Firstly, you should never use query_posts to construct custom queries. This is not just my emphasis, but the codex as well. The one big problem with query_posts is, it many circumstances, pagination fails

Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

You need to construct you custom query with WP_Query.

Take a look at the_title( $before, $after, $echo );. Here are the parameters

$before

  • (string) (optional) Text to place before the title.

  • Default: None

$after

  • (string) (optional) Text to place after the title.

  • Default: None

$echo

  • (Boolean) (optional) Display the title (TRUE) or return it for use in PHP (FALSE).
  • Default: TRUE

So you can simply just do something like this

the_title( '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a>' );

You query should then look like this with all the before said. Before I forget, showposts has long time been depreciated, it was replaced with posts_per_page. If you had debug set to true in wp-config, you should have received a depreciation notice in the front end that would have indicated this. Read here: Debugging Wordpress

<?php
$args = array(
   'cat' => 4,
   'posts_per_page' => 1
);
// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        the_title( '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a>' );
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
3
  • hey Pieter i need another help its abt creating image gallery ... would you like to help me.
    – Code Smith
    Jul 16, 2014 at 8:07
  • Ok, this is a completely new question different from the scope of this one. You should start a new question. A tip, be clear in what your problem is, add all relevent code and information, show your efforts, and explain why and what is failing in your effort. Thanks :-) Jul 16, 2014 at 8:13
  • ok thanx man :)
    – Code Smith
    Jul 16, 2014 at 8:18
1

First of all, don't use query_posts; use a WP_Query object instead.

The reason your links are not being displayed correctly is that the link (<a> tag) is not actually within the loop.

<?php while ( have_posts() ) : the_post(); ?>
    <a class="heading-link" href="<?php  the_permalink(); ?>">
        <?php the_title(); ?>
    </a>
<?php endwhile; ?>
-1

YOU want to display one post for category id 4 ? if yes .PLs use below code

<?php

    query_posts('cat=4&showposts=1');

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

        <a class="heading-link" href="<?php  the_permalink(); ?>">    <?php ?>  <?php the_title(); ?>  </a>

       <?php  endwhile;
        endif;
        wp_reset_query();

             ?>   
1
  • Do not use query_posts Jun 26, 2014 at 13:52
-1

If you are using get_posts then you can use get_the_permalink(get_the_ID())

            query_posts('cat=4&showposts=1');

         if (have_posts()) : 
           while (have_posts()) : the_post(); ?>
<a class="heading-link" href="<?php  echo get_the_permalink(get_the_ID(); ?>">   

       <?php  the_title(); ?>
</a>
       <?php  endwhile;
        endif;

        wp_reset_query();

             ?>    
3

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.