• Resolved jeobrussels

    (@jeobrussels)


    I am looking for a plugin / widget that allows me to

    1) randomly display
    2) only the content(!) of
    3) 1 post from
    4) a specified category.

    None of the plugins I looked at, offered all four options.
    Thanks for your ideas!

Viewing 11 replies - 1 through 11 (of 11 total)
  • Consider downloading and installing Otto’s PHP Code Widget, then something like this code in one of those widgets:

    <?php
    $cat_id = get_cat_ID('mycategory');
    $args=array(
      'cat' => $cat_id,
      'orderby'=> 'rand',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <?php
        the_content();
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    Thread Starter jeobrussels

    (@jeobrussels)

    The widget works, but apprently the category call is ignored. I get one random post but from from all categories, although I changed “mycategory” to the appropriate ID (number).

    mycategory should be replaced by your category name (e.g. events) not the number.

    Thread Starter jeobrussels

    (@jeobrussels)

    If I put in the Category-Name, absolutely nothing is returned. There are 8 posts in this category, so it is not due to the Cat being empty ??

    For my Events category, this is working for me:

    <?php
    $cat_id = get_cat_ID('events');
    $args=array(
      'cat' => $cat_id,
      'orderby'=> 'rand',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <?php
        the_content();
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    You’ll need to paste your whole template in a pastebin and report the link back here–maybe someone can spot the problem.

    Thread Starter jeobrussels

    (@jeobrussels)

    The whole template??
    It’s a TwentyTen child.

    Thread Starter jeobrussels

    (@jeobrussels)

    OK, this is really odd. It works for other categories, but not for the “Quotes” one…

    Find the category id for that category and change

    'cat' => $cat_id,

    to

    'cat' => x,

    where x is your category id.

    By whole template I meant like your sidebar.php or index.php.

    Thread Starter jeobrussels

    (@jeobrussels)

    sidebar.php and index.php are unchanged TwentyTen files.

    Thread Starter jeobrussels

    (@jeobrussels)

    Find the category id for that category and change

    'cat' => $cat_id,

    to

    'cat' => x,

    where x is your category id.

    Yep, this works fine!!!
    Thanks a million for your patience ??

    Good.

    Ignore comments about templates, as I forgot you were using the code in a widget.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Random post – display content only’ is closed to new replies.