PHP - My array returns NULL values when placed in a function, but works fine outside of the function
Posted
by orbit82
on Stack Overflow
See other posts from Stack Overflow
or by orbit82
Published on 2010-05-21T05:23:47Z
Indexed on
2010/05/21
5:30 UTC
Read the original article
Hit count: 291
Okay, let me see if I can explain this. I am making a newspaper WordPress theme. The theme pulls posts from categories. The front page shows multiple categories, organized as "newsboxes". Each post should show up only ONCE on the front page, even if said post is in two or more categories.
To prevent posts from duplicating on the front page, I've created an array that keeps track of the individual post IDs. When a post FIRST shows up on the front page, its ID gets added to the array. Before looping through the posts for each category, the code first checks the array to see which posts have ALREADY been displayed.
OK, so now remember how I said earlier that the front page shows multiple categories organized as "newsboxes"? Well, these newsboxes are called onto the front page using PHP includes. I have 6 newsboxes appearing on the front page, and the code to call them is EXACTLY the same. I didn't want to repeat the same code 6 times, so I put all of the inclusion code into a function.
The function works, but the only problem is that it screws up the duplicate posts code I mentioned earlier. The posts all repeat. Running a var_dump on the $do_not_duplicate variable returns an array with null indices. Everything works PERFECTLY if I don't put the code inside a function, but once I do put them in a function it's like the arrays aren't even connecting with the posts.
Here is the code with the arrays. The key variables in question here include $do_not_duplicate[] = $post->ID, $do_not_duplicate and 'post__not_in' => $do_not_duplicate
<?php query_posts('cat='.$settings['cpress_top_story_category'].'&posts_per_page='.$settings['cpress_number_of_top_stories'].'');?>
<?php if (have_posts()) : ?>
<!--TOP STORY-->
<div id="topStory">
<?php while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_post_thumbnail('top-story-thumbnail'); ?></a>
<h2 class="extraLargeHeadline"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<div class="topStory_author"><?php cpress_show_post_author_byline(); ?></div>
<div <?php post_class('topStory_entry') ?> id="post-<?php the_ID(); ?>">
<?php if($settings['cpress_excerpt_or_content_top_story_newsbox'] == "content") {
the_content(); ?><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><span class="read_more"><?php echo $settings['cpress_more_text']; ?></span></a>
<?php } else {
the_excerpt(); ?><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><span class="read_more"><?php echo $settings['cpress_more_text']; ?></span></a>
<?php }?>
</div><!--/topStoryentry-->
<div class="topStory_meta"><?php cpress_show_post_meta(); ?></div>
<?php endwhile; wp_reset_query(); ?>
<?php if(!$settings['cpress_hide_top_story_more_stories']) { ?>
<!--More Top Stories--><div id="moreTopStories">
<?php $category_link = get_category_link(''.$settings['cpress_top_story_category'].''); ?>
<?php if (have_posts()) : ?>
<?php query_posts( array( 'cat' => ''.$settings['cpress_top_story_category'].'', 'posts_per_page' => ''.$settings['cpress_number_of_more_top_stories'].'', 'post__not_in' => $do_not_duplicate ) ); ?>
<h4 class="moreStories">
<?php if($settings['cpress_make_top_story_more_stories_link']) { ?>
<a href="<?php echo $category_link; ?>" title="<?php echo strip_tags($settings['cpress_top_story_more_stories_text']);?>"><?php echo strip_tags($settings['cpress_top_story_more_stories_text']);?></a><?php } else {
echo strip_tags($settings['cpress_top_story_more_stories_text']); } ?>
</h4>
<ul>
<?php while( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>
<li><h2 class="mediumHeadline"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php if(!$settings['cpress_hide_more_top_stories_excerpt']) { ?> <div <?php post_class('moreTopStory_postExcerpt') ?> id="post-<?php the_ID(); ?>"><?php if($settings['cpress_excerpt_or_content_top_story_newsbox'] == "content") {
the_content(); ?><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><span class="read_more"><?php echo $settings['cpress_more_text']; ?></span></a>
<?php } else {
the_excerpt(); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><span class="read_more"><?php echo $settings['cpress_more_text']; ?></span></a>
<?php }?> </div><?php } ?>
<div class="moreTopStory_postMeta"><?php cpress_show_post_meta(); ?></div>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
<?php endif;?>
</div><!--/moreTopStories-->
<?php } ?>
<?php echo(var_dump($do_not_duplicate)); ?>
</div><!--/TOP STORY-->
<?php endif; ?>
And here is the code that includes the newsboxes onto the front page. This is the code I'm trying to put into a function to avoid duplicating it 6 times on one page.
function cpress_show_templatebitsf($tbit_num, $tbit_option) {
global $tbit_path;
global $shortname; $settings = get_option($shortname.'_options');
//display the templatebits (usually these will be sidebars)
for ($i=1; $i<=$tbit_num; $i++) {
$tbit = strip_tags($settings[$tbit_option .$i]);
if($tbit !="") {
include_once(TEMPLATEPATH . $tbit_path. $tbit.'.php');
} //if
}//for loop
unset($tbit_option);
}
I hope this makes sense. It's kind of a complex thing to explain but I've tried many things to fix it and have had no luck. I'm stumped. I'm hoping it's just some little thing I'm overlooking because it seems like it shouldn't be such a problem.
© Stack Overflow or respective owner