WordPress.org

Ready to get started?Download WordPress

Forums

Show list of articles when not on home page (2 posts)

  1. f1f
    Member
    Posted 5 years ago #

    I'm trying to create a bit of code to show a list of the latest articles in the sidebar when the user is viewing a page other than the home page.

    I found the code to determine if a user is viewing the home page in the codex:

    <?php
    if ( is_home() ) {
    	// This is a homepage
    } else {
    	// This is not a homepage
    }
    ?>

    So I tried adding the code to show a list of 10 articles from a given category as follows:

    <?php
    if ( is_home() ) {
    	// This is a homepage
    } else {
    <?php $my_query = new WP_Query('cat=17&showposts=10'); ?>
    <ul class="inbrieflist">
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    <?php
    if( $post->post_excerpt ) {
    echo ' - ';
        the_excerpt_rss(); comments_number(' (0)', ' (1)', ' (%)');
    } else {
    comments_number(' (0)', ' (1)', ' (%)');
    }
    ?>
    </li>
    <?php endwhile; ?>
    </ul>
    }
    ?>

    But that gave me the following error:

    Parse error: syntax error, unexpected '<' in sidebar2.php on line 27

    I've tried making a few changes to the code but with my limited knowledge of PHP I'm struggling. Can anyone solve it for me or point me in the direction of some resources that will help?

  2. MichaelH
    Member
    Posted 5 years ago #

    Didn't really check your logic, but you forgot that last little <?php tag:

    .
    .
    .
    <?php endwhile; ?>
    </ul>
    <?php }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic