Forums

weird problem trying to show random post (22 posts)

  1. sandrodz
    Member
    Posted 7 months ago #

    Hi, I'm trying to display random post in my sidebar, from the pull of posts taged with "featured". My script works just fine, one thing only, on the parent category of the post my featured post disappears only if I'm logged out. if I'm logged in as admin it displays featured post just fine!

    to see example check:
    other category:
    http://www.sandrophoto.com/category/car-photography/

    parent category:
    http://www.sandrophoto.com/category/photo-stories/

    any ideas what could be causing this? something to do with admin privileges? maybe my theme?

  2. esmi
    Member
    Posted 7 months ago #

    Assuming that it's not something really obvious (such as a post marked private that won't display), dropping a copy of your random script code in the pastebin might help.

  3. sandrodz
    Member
    Posted 7 months ago #

    well I'm talking about category not displaying my script. on all other pages it works fine... check links I provided, and look at the sidebar on the left bottom...

    what is pastebin?

  4. sandrodz
    Member
    Posted 7 months ago #

    decided to write it in a clear way. I have script that fetches random script taged as 'featured'.

    It displays fine everywhere, except the parent category. On parent category it displays only if I;m logged in.

  5. esmi
    Member
    Posted 7 months ago #

    I did check the links but I couldn't figure out where to look for the "missing" image the first time.

    The pastebin is a repository for code snippets. You paste your code into there and then drop the resulting url back here. Saves cluttering up posts with long stretches of code and often means that the code itself is much easier to read/troubleshoot.

    It does sound like there's some sort of logic problem mixed up with the user's login status - which is why I think we need to see the code you're using.

  6. sandrodz
    Member
    Posted 7 months ago #

    Thank you so much for looking into this! and fast replys... here is the code: http://wordpress.pastebin.ca/1399005

    It's nothing fancy or complicated, escapes my mind why it wouldn't work on parent category, or tag/featured/ page.

  7. esmi
    Member
    Posted 7 months ago #

    As you say, there's nothing in that script that should cause it to fail. So I can only guess that it might be where you're calling it that's the problem. Have you tried echoing out in various places in the script?

    See http://wordpress.pastebin.ca/1400014

    It might help to pin down where it's failing (I'm assuming it's the sql) but the various messages should only be visible if you check the page source.

  8. sandrodz
    Member
    Posted 7 months ago #

    okay checked with ur code :) got very strange result...

    On all pages where featured post is visible I get:

    <div style="display:none;">SELECT * FROM wp_term_relationships WHERE term_taxonomy_id = 354 ORDER BY rand() LIMIT 1</div><div style="display:none;">We have data!</div>Array
    (
        [object_id] => 12
        [term_taxonomy_id] => 354
    )
    <pre style="display:none;">1</pre>
    		 <h3>Featured Post</h3> <h2><a href="http://www.sandrophoto.com/2007/10/05/budapest-hungary/" rel="bookmark" title="Featured Post: Budapest, Hungary">Budapest, Hungary</a></h2> <p>I spent 2 years of my life in Budapest, I just wish I did more landscape photography like this… It’s an amazing city with beautiful architecture and pretty scenery.</p>
    
    <p>The first photo is a view from my dormitory room, around 6:00 AM - night of heavy drinking and no sleep, and the scene set a good mood for the hangover day <img src='http://www.sandrophoto.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
    <p><a href="http://www.sandrophoto.com/wp-content/gallery/landscapes/SuperNova.jpg" title="Budapest sky at 6:00AM. Insomnia was bothering me whole year, so I've seen some of the most beautiful paintings of nature :)" rel="lightbox[singlepic10]" ><img class="ngg-singlepic" src="http://www.sandrophoto.com/wp-content/plugins/nextgen-gallery/nggshow.php?pid=10&amp;width=336&amp;height=&amp;mode=" alt="Budapest, Hungary" title="Budapest, Hungary" /></a> <a href="http://www.sandrophoto.com/2007/10/05/budapest-hungary/#more-12" class="more-link">more photos »</a></p>

    on the page where it fails:

    <div style="display:none;">SELECT * FROM wp_term_relationships WHERE term_taxonomy_id = 354 ORDER BY rand() LIMIT 1</div><div style="display:none;">We have data!</div>Array
    (
        [object_id] => 38
        [term_taxonomy_id] => 354
    )
    <pre style="display:none;">1</pre>
  9. sandrodz
    Member
    Posted 7 months ago #

    so basically what doesn't work is foreach? and query post? :S very confusing...

    any suggestions?

  10. esmi
    Member
    Posted 7 months ago #

    We've proven that there's data ready to be used for the display, so the first part of the script is fine (you could remove all those debug lines now).

    One thing bothers me - why are you using both query_posts and setup_postdata with a foreach loop?

    Can't you use

    query_posts('p='.$row['object_id']);
    global $more;
    $more = 0;
    while (have_posts()) : the_post();?>
    	<h3>Featured Post</h3>
    	<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Featured Post: <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    	<?php the_content('more photos »'); ?>
    <?php endwhile;?>
  11. sandrodz
    Member
    Posted 7 months ago #

    Okay, took ur advice and simplified second part of the code... I'm not a coding guru, in Fact this code I created reading several wordpress support files, and example pages :) so basically I'm doing everything blind...

    New code: http://wordpress.pastebin.ca/1401720

    Problem is still there, when I'm logged in, it displays image on the parent category of the post, when i log out featured post disappears.

    I'm wondering, problem is the parent category only. If I add some new post to my featured tags, that category is also not displaying featured post!.

    I'm running out of ideas :(

  12. sandrodz
    Member
    Posted 7 months ago #

    could it be because of my theme? anything lacking in functions.php?

  13. avijit_sadhu
    Member
    Posted 7 months ago #

    Even i need to do something like this but not exactly like it. I am looking to make the posts change randomly after fixed intervals of time. Any idea how i could do that. I am getting the posts using the query_posts function and setting its attribute <?php query_posts('cat=6&amp;showposts=1') ?> of showposts to 1. But how do i keep changing the posts on my sidebar??? Even if its javascript which is the solution please lend me some more help.....

  14. esmi
    Member
    Posted 7 months ago #

    could it be because of my theme? anything lacking in functions.php?

    Perhaps it's where you're calling this in your template file. Are you using category.php? Can you drop a copy of that into the pastebin?

  15. esmi
    Member
    Posted 7 months ago #

    @avijit_sadhu: Please start a new topic.

  16. sandrodz
    Member
    Posted 7 months ago #

    yes, I use category.php... here is the content: http://pastebin.com/m733dbe9d

    doesn't give me any clues :) do u see something there?

    btw, wordpress.pastebin.ca was giving me error, internal server problem, and I found com site :S

    thanks for looking into my issue, I appreciate this allot...

  17. esmi
    Member
    Posted 7 months ago #

    Nothing strange going on in there either. Can you drop a copy of your header and sidebar into the pastebin?

  18. sandrodz
    Member
    Posted 7 months ago #

    oh sorry I had connection problems so... here we go,

    Sidebar: http://wordpress.pastebin.ca/1406317
    header: http://wordpress.pastebin.ca/1406325

    :S

  19. esmi
    Member
    Posted 6 months ago #

    Ok - I'm officially stumped. I can't see anything wrong in the code snippets that you've posted. The only thing left to try is deactivating all plugins in case one is interfering.

  20. sandrodz
    Member
    Posted 6 months ago #

    :) like me... it's very very interesting / frustrating... I will try ur suggestion and report back.

  21. gengivas
    Member
    Posted 4 months ago #

    Hi, sandrodz!

    I had the same problem (not really the same). I wanted a single random post before my regular posts.

    The problem is that the random post could be seing only when i was logged in, and the post wasn´t a random post (it always displayed the same post)

    I reactivated all my plugins, and deleted the ones I was not using. It didnt work... Then I tried this solution from esmi (I just deleted the tag selection).

    This worked for me! I hope this can help you... Here is the code I´m using:

    <?php
    $taggedposts = get_posts('category_name=Humor&showposts=1&orderby=rand');
    foreach($taggedposts as $post) :
        setup_postdata($post);?>
        [ do stuff ]
    <?php endforeach; ?>
  22. PaulWarren
    Member
    Posted 2 months ago #

    I had a similar issue where I needed to include the content of custom fields from a random posts in a specific category in my sidebar. In case it helps anyone, here is my hodgepodge of code. -- I was rather proud of myself :-)

    <?php query_posts('category_name=bonus_benefits&showposts=1&orderby=rand'); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
              <?php // check for thumbnail
    $thumb = get_post_meta($post->ID, 'Sidebar Thumbnail', $single = true);
    // check for thumbnail class
    $sidebar_text = get_post_meta($post->ID, 'Sidebar Text', $single = true);
    $thumb_class = get_post_meta($post->ID, 'Thumbnail Class', $single = true);
    // check for thumbnail alt text
    $thumb_alt = get_post_meta($post->ID, 'Thumbnail Alt', $single = true); ?><?php // if there's a thumbnail
    if($thumb !== '') { ?>
    	<p>
    	<a href="<?php the_permalink(); ?>"><img src="<?php echo $thumb; ?>"
    	class="<?php if($thumb_class !== '') { echo $thumb_class; } else { echo "sidebar-thumbnail"; } ?>"
    	alt="<?php if($thumb_alt !== '') { echo $thumb_alt; } else { echo the_title(); } ?>"
    /></a></p><p><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    <?php echo $sidebar_text; ?>
    	</p>
    <?php } // end if statement
    
    // if there's not a thumbnail
    else { echo ''; } ?>      
    
    <?php endwhile; ?>
    <?php endif; ?>
     <?php wp_reset_query(); ?>

Reply

You must log in to post.

About this Topic