Forums

Pulling just category titles, author, and date (7 posts)

  1. thwart1
    Member
    Posted 3 months ago #

    Not a PHP guy at all, so hoping this is a really easy question.

    I've created 3 columns for each of my 3 categories. I'd like each column to list (as a hyperlink) the last 5 articles for each category.

    Below the article title is the timestamp: ie., Friday, January 20, 2012 at 4:11:34 PM

    All I've been able to do is pull in full articles without titles.

    Can anyone point me to the code for this?

  2. esmi
    Theme Diva & Forum Moderator
    Posted 3 months ago #

  3. thwart1
    Member
    Posted 3 months ago #

    Thanks. Still not fully getting it though - here's the code I'm using...

    <?php
    $args = array( 'numberposts' => 5, 'category' => , 'orderby' => 'rand' );
    $rand_posts = get_posts( $args );
    foreach( $rand_posts as $post ) : ?>
    	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>

    But I don't know how to make the category work. For example, one of my categories is "health food". How do I make this just show posts from "health food"?

  4. thwart1
    Member
    Posted 3 months ago #

    Sorry - THIS is the code I'm using now that needs help:

    <?php
    $args = array( 'numberposts' => 3, 'orderby' => 'new' );
    $rand_posts = get_posts( $args );
    foreach( $rand_posts as $post ) : ?>
    	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
  5. alchymyth
    The Sweeper
    Posted 3 months ago #

    there is an example in the codex which virtually matches your needs:
    please read http://codex.wordpress.org/Template_Tags/get_posts#Examples

    'new' is not one of the accepted 'orderby' parameters; try to use 'date' plus 'order' => 'ASC'

    http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    or use WP_Query() http://codex.wordpress.org/Class_Reference/WP_Query

  6. thwart1
    Member
    Posted 3 months ago #

    I was looking at that. The problem I have with the example... category' => 1 );
    is that I have no idea what number my category is. This example says 1 returns the wrong category.

    How do I find out?

  7. alchymyth
    The Sweeper
    Posted 3 months ago #

    when you know the category name, you can use:

    get_cat_ID()

    http://codex.wordpress.org/Function_Reference/get_cat_ID

    example:

    <ul>
    <?php
    $cat1_id = get_cat_ID('Categoryname1');
    $args = array( 'numberposts' => 5, 'category' => $cat1_id, 'order' => 'ASC' );
    $myposts = get_posts( $args );
    if( $myposts ) foreach( $myposts as $post ) :	setup_postdata($post); ?>
    	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>

Reply

You must log in to post.

About this Topic

Tags

No tags yet.