WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to get pretty permalink in custom loop (7 posts)

  1. ninjaboy
    Member
    Posted 6 years ago #

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

    <?php
    $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 ?>
    
    <ul>
    <?
    $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);
    }?>
    </ul>

    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:

    http://www.site.com/index.php/whats-new/

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

    http://www.site.com/index.php/whats-new/index.php?p=95

    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?

  2. WPITn2shape
    Member
    Posted 6 years ago #

    index.php?p=%s\

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

  3. WPITn2shape
    Member
    Posted 6 years ago #

    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>"

    with

    <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!

  4. ninjaboy
    Member
    Posted 6 years ago #

    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; ?>
  5. Kafkaesqui

    Posted 6 years ago #

    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... )

  6. ninjaboy
    Member
    Posted 6 years ago #

    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!).

  7. Peter Butler
    Member
    Posted 5 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic