Support » Fixing WordPress » How to get pretty permalink in custom loop

  • Resolved ninjaboy


    I have used the following code to generate a custom loop on an ‘archive’ type page:

    $how_many=5; //How many posts do you want to show
    require_once("wp-config.php"); // Change this for your path to wp-config.php file ?>
    $news=$wpdb->get_results("SELECT <code>ID</code>,<code>post_title</code> FROM $wpdb->posts
    WHERE <code>post_type</code>=\"post\" AND <code>post_status</code>= \"publish\" ORDER BY post_date DESC LIMIT $how_many");
    foreach($news as $np){
    printf ("<li><a href=\"index.php?p=%s\">%s</a></li>", $np->ID,$np->post_title);

    I can see where the link is generated:

    printf ("<li><a href=\"index.php?p=%s\">%s</a></li>", $np->ID,$np->post_title);

    …but I don’t seem to be able to generate the pretty permalink for the link. After trying a few things I’m starting to pull my hair out!

    The site I’m working on is based on a windows box, and the prettiest permalink I can generate is:

    …but this bit of php shown above generates links like this:

    which I’m amazed actually works, but it does! What I really want to do is generate a proper pretty permalink – could someone please tell me how to do that?

Viewing 6 replies - 1 through 6 (of 6 total)
  • index.php?p=%s\

    That’s your problem. Is there an option where you got the code for writing that part differently?

    Call me crazy, but could it be this easy.. (I don’t know where you got the code, if you wrote it yourself, I see hope)

    Could you replace

    "<li><a href=\"index.php?p=%s\">%s</a></li>"


    <li><a href=\"' . <?php the_permalink() ?> . '\">%s</a></li>

    Forgot the code backtips first time around. Hope you didn’t see that mess. I may be a tad bit off, but you can figure it out!

    Nope, notice that it is inside a php code block so that doesn’t work. I didn’t write that code, and can’t remember where I got it from.

    I solved this by going bakc to the drawing board and looking at ‘The Loop’ a little closer, and using it like this (so much simpler!). It allows me to use the permalink:

    <?php $posts = get_posts( "numberposts=5&orderby=post_date" ); ?>
    <?php if( $posts ) : ?>
         <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
              <li><a href="<?php the_permalink() ?>" title="View <?php the_title(); ?>"><?php the_title(); ?></a></li>
       <?php endforeach; ?>
    <?php endif; ?>

    Just in case* anyone ‘wants’ to call up the permalink to a post in the (pretty) permalink structure your blog uses, using the type of code ninjaboy has in his original topic post, you can do this:

    <?php echo get_permalink($np->ID); ?>

    Or within that printf():

    printf ("<li><a href=\"%s\">%s</a></li>", get_permalink($np->ID),$np->post_title);

    ( *I believe in completeness… )

    Cheers Kafkaesqui, that will come in useful in the future – your ‘completeness’ as you put it is VERY much appreciated (as has been your support for WP over the last couple of years mate!).

    Thanks Kafkaesqui – I was pulling my hair out trying to use guid. Somehow I missed that get_permalink() even existed…

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to get pretty permalink in custom loop’ is closed to new replies.