WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Show posts in a widget (4 posts)

  1. tastymouse
    Member
    Posted 1 year ago #

    I want to show post of one category in a widget. I use the Executable PHP widget to enter the code.
    The code I use is:

    <?php query_posts('cat=16&showposts=3'); ?>
    <?php while (have_posts()) : the_post(); ?>
    
    <ul>
    <li>
              <?php the_title(); ?>
              <?php the_content(); ?>
           </li>
    </ul>
    <?php endwhile; ?>

    [please remember to mark any posted code - see http://codex.wordpress.org/Forum_Welcome#Posting_Code ]

    This works BUT other widgets disappear. What am I doing wrong?

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    try and use WP_Query() and reset the query at the end;

    example:

    <?php $cat_query = new WP_Query('cat=16&posts_per_page=3'); ?>
    <?php while ($cat_query->have_posts()) : $cat_query->the_post(); ?>
    
    <ul>
    <li>
              <?php the_title(); ?>
              <?php the_content(); ?>
           </li>
    </ul>
    <?php endwhile; wp_reset_postdata(); ?>

    if this does not work, you might need to post a link to your site to illustrate the output of your code.

  3. tastymouse
    Member
    Posted 1 year ago #

    yes, that does it!

    Thanks a lot, Tom

  4. ak.singla
    Member
    Posted 1 year ago #

    You may also want to use in-built recent posts widget from the default Jetpack Plugin.
    It can be customized to fit needs.

    --I have read somewhere that using executable php on pages - compromises security.--

Topic Closed

This topic has been closed to new replies.

About this Topic