WordPress.org

Forums

Get recent post permalinks by category (5 posts)

  1. barneywimbush
    Member
    Posted 1 year ago #

    Hi,

    So I've created a custom page template for my "Topics" Page.

    What I want to do is add in some PHP to the custom page template my Topics page uses, to retrieve permalinks for the 3 most recent posts from a chosen category.

    E.g.
    (From post category 1)
    --> Permalink for post 1
    --> Permalink for post 2
    --> Permalink for post 3

    Can anyone advise on doing this? I've seen many solutions but wasn't able to get any working. I'd rather avoid using a plugin if possible.

    Thanks in advance!

  2. Kakoma
    Inactive
    Posted 1 year ago #

    <ul>
    <?php
    	$category_posts = new WP_Query('cat=x&showposts=y');
    while ($category_posts ->have_posts()) : $category_posts->the_post();?>
    <li>
     <a class="yourclass" href="<?php the_permalink(); ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?> </a></li>
     <?php endwhile; ?>
    </ul>

    Replace x with the category ID and y with the number of links you'd like to retrieve

  3. barneywimbush
    Member
    Posted 1 year ago #

    Is there any particular place I need to put this, as pasting it into my .php file doesn't seem to work.

    Thanks :)

  4. barneywimbush
    Member
    Posted 1 year ago #

    Ok I've added this in, but it doesn't seem to work when I change "cat=x" to cat=1 or cat=categoryname

    How can I select only topics from a certain category?

    Thanks for all the help so far! :)

  5. Kakoma
    Inactive
    Posted 1 year ago #

    Add the code to the custom page template you created for your Topics page

Topic Closed

This topic has been closed to new replies.

About this Topic