Jump to content

Question

Posted

Hey there guys.

Ok thing is I really suck at php and I'm trying to exlude a post, or possible a category, either one would work, from a random post widget I have.

If you check out the [url="http://www.paranormalmovies.org/"]site[/url] you can see the widget on the right side (random paranormal movies) is displaying my sticky welcome message at the top.

I know the category id and post id and have tried a few things to no avail.

Anyway any help would sure be welcome:

<?php
/*
Plugin Name: Simple Random Posts Widget
Plugin URI: [url="http://infobak.nl/simple-random-posts-widget-for-wordpress//"]http://infobak.nl/si...for-wordpress//[/url]
Version: 1.26
Description: Widget which displays random posts
Author: Jan Meeuwesen
Author URI: [url="http://infobak.nl/simple-random-posts-widget-for-wordpress/"]http://infobak.nl/si...-for-wordpress/[/url]
License: GPLv2
Copyright 2012 Jan Meeuwesen
*/
define("DefNoOfPosts", "5"); // default number of random posts to display
class SimpleRandomPostsWidget extends WP_Widget {
function SimpleRandomPostsWidget()
{
parent::WP_Widget( false, 'Simple Random Posts', array('description' => 'Random posts widget') );
}
function widget($args, $instance)
{
global $NewSimpleRandomPosts;
$title = empty( $instance['title'] ) ? 'Simple Random Posts' : $instance['title'];
echo $args['before_widget'];
echo $args['before_title'] . $title . $args['after_title'];
echo $NewSimpleRandomPosts->GetSimpleRandomPosts( empty( $instance['ShowPosts'] ) ? DefNoOfPosts : $instance['ShowPosts'] );
echo $args['after_widget'];
}
function update($new_instance)
{
return $new_instance;
}
function form($instance)
{
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php echo 'Title:'; ?></label>
<input type="text" name="<?php echo $this->get_field_name('title'); ?>" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" value="<?php echo esc_attr($instance['title']); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('ShowPosts'); ?>"><?php echo 'Number of entries:'; ?></label>
<input type="text" name="<?php echo $this->get_field_name('ShowPosts'); ?>" id="<?php echo $this->get_field_id('ShowPosts'); ?>" value="<?php if ( empty( $instance['ShowPosts'] ) ) { echo esc_attr(DefNoOfPosts); } else { echo esc_attr($instance['ShowPosts']); } ?>" size="3" />
</p>
<?php
}
}

class SimpleRandomPosts {
function GetSimpleRandomPosts($noofposts)
{
rewind_posts();
query_posts('orderby=rand&showposts='.$noofposts);
if (have_posts()) :
echo '<ul>';
while (have_posts()) : the_post();
echo '<div id="post-'.get_the_ID().'"><li><a href="'.get_permalink().'">'.get_the_title().'</a></li></div>';
endwhile;


echo '</ul>';
endif;
wp_reset_query();
}
}

$NewSimpleRandomPosts = new SimpleRandomPosts();
function SimpleRandomPosts_widgets_init()
{
register_widget('SimpleRandomPostsWidget');
}
add_action('widgets_init', 'SimpleRandomPosts_widgets_init');

?>

Share this post


Link to post
Share on other sites

8 answers to this question

  • 0

Posted

Use the minus "-" sign when assigning ID's in the WP_Query array.

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='Mr.XXIV' timestamp='1355426614' post='595389354']
Use the minus "-" sign when assigning ID's in the WP_Query array.
[/quote]

Hey there and thanks alot for the reply. I already tried that but could you show me how you would write it, I fear I might have gotten it wrong.

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='Mr.XXIV' timestamp='1355426614' post='595389354']
Use the minus "-" sign when assigning ID's in the WP_Query array.
[/quote]

Can you please at least tell me where I can find the query array. Sorry not saying I don't appreaciate the help.

Share this post


Link to post
Share on other sites
  • 0

Posted

Example

[code]<?php $query = new WP_Query( array( 'post_type' => 'post', 'post__not_in' => array( 2, 5, 12, 14, 20 ) ) ); ?>[/code]

Those ID's in the array of "post__not_in" are what can be generally excluded.

All of your custom querying options are on this page in the WordPress Codex.

http://codex.wordpress.org/Class_Reference/WP_Query

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='Mr.XXIV' timestamp='1355631214' post='595395028']
Example

[code]<?php $query = new WP_Query( array( 'post_type' => 'post', 'post__not_in' => array( 2, 5, 12, 14, 20 ) ) ); ?>[/code]

Those ID's in the array of "post__not_in" are what can be generally excluded.

All of your custom querying options are on this page in the WordPress Codex.

[url="http://codex.wordpress.org/Class_Reference/WP_Query"]http://codex.wordpre...erence/WP_Query[/url]
[/quote]

Thanks again mate for the help.

What is confusing me though is that does the plugin which the original code is from write into the query file? Do I have to go into the query.php file and make the changes in there or can i make them in the original plugin file?

Sorry man you got me a little lost here. Coding has never been my strong suit.

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='hooko22' timestamp='1355841720' post='595400704']
Thanks again mate for the help.

What is confusing me though is that does the plugin which the original code is from write into the query file? Do I have to go into the query.php file and make the changes in there or can i make them in the original plugin file?

Sorry man you got me a little lost here. Coding has never been my strong suit.
[/quote]
no the plugin is using wp_query (query.php)
just edit the code directly, edit the plugin.
Either from the files or admincp>plugins>edit

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='Guth' timestamp='1355844702' post='595400814']
no the plugin is using wp_query (query.php)
just edit the code directly, edit the plugin.
Either from the files or admincp>plugins>edit
[/quote]

The code I posted is from the plugin.

I also can't find any obvious place where I could stuff the exlude or that is the -.

You think I might have to include the line he mentioned before inside the code:

[color="#666600"]<?[/color][color="#000000"]php $query [/color][color="#666600"]=[/color] [color="#000088"]new[/color][color="#000000"] WP_Query[/color][color="#666600"]([/color][color="#000000"] array[/color][color="#666600"]([/color] [color="#008800"]'post_type'[/color] [color="#666600"]=>[/color] [color="#008800"]'post'[/color][color="#666600"],[/color] [color="#008800"]'post__not_in'[/color] [color="#666600"]=>[/color][color="#000000"] array[/color][color="#666600"]([/color] [color="#006666"]2[/color][color="#666600"],[/color] [color="#006666"]5[/color][color="#666600"],[/color] [color="#006666"]12[/color][color="#666600"],[/color] [color="#006666"]14[/color][color="#666600"],[/color] [color="#006666"]20[/color] [color="#666600"])[/color] [color="#666600"])[/color] [color="#666600"]);[/color] [color="#666600"]?>[/color]

Share this post


Link to post
Share on other sites
  • 0

Posted

Replace:
[CODE]query_posts('orderby=rand&showposts='.$noofposts);[/CODE]

with:
[CODE]query_posts(array('orderby' => 'rand', 'posts_per_page' => $noofposts, 'post_not_in' => array(2,5,12,14,20) ) );[/CODE]
1 person likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.