Support » Fixing WordPress » Show posts in a widget

  • Resolved tastymouse

    (@tastymouse)


    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?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Michael

    (@alchymyth)

    Forum Moderator

    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.

    yes, that does it!

    Thanks a lot, Tom

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

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Show posts in a widget’ is closed to new replies.