PHP-我的数组在放入函数时返回空值,但在函数外部工作正常
好吧,让我看看能不能解释一下。我正在制作一份报纸的WordPress主题。主题从类别中提取帖子。首页显示多个类别,按“新闻框”组织。每篇文章应该只在头版出现一次,即使所说的文章分为两个或两个以上的类别 为了防止文章在头版重复,我创建了一个数组来跟踪各个帖子ID。当一篇文章第一次出现在头版时,它的ID会被添加到数组中。在循环遍历每个类别的帖子之前,代码首先检查数组以查看已经显示了哪些帖子 好的,现在还记得我之前说过的头版显示多个类别,这些类别被组织为“新闻框”吗?嗯,这些新闻框是使用PHP includes在首页上调用的。我有6个新闻框出现在头版,调用它们的代码完全相同。我不想重复同样的代码6次,所以我把所有包含的代码都放在一个函数中 这个函数可以工作,但唯一的问题是它把我前面提到的重复的posts代码搞砸了。这些帖子都在重复。对$do_not_dump变量运行var_dump将返回一个索引为空的数组。如果我不将代码放在函数中,那么一切都会完美工作,但是一旦我将它们放在函数中,就好像数组甚至都没有连接到POST 下面是数组的代码。这里讨论的关键变量包括$do_not_duplicate[]=$post->ID、$do_not_duplicate和'post_not_in'=>$do_not_duplicatePHP-我的数组在放入函数时返回空值,但在函数外部工作正常,php,function,arrays,Php,Function,Arrays,好吧,让我看看能不能解释一下。我正在制作一份报纸的WordPress主题。主题从类别中提取帖子。首页显示多个类别,按“新闻框”组织。每篇文章应该只在头版出现一次,即使所说的文章分为两个或两个以上的类别 为了防止文章在头版重复,我创建了一个数组来跟踪各个帖子ID。当一篇文章第一次出现在头版时,它的ID会被添加到数组中。在循环遍历每个类别的帖子之前,代码首先检查数组以查看已经显示了哪些帖子 好的,现在还记得我之前说过的头版显示多个类别,这些类别被组织为“新闻框”吗?嗯,这些新闻框是使用PHP inc
<?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; ?>
-
这是包含在头版的新闻框的代码。这是我试图放入函数中的代码,以避免在一个页面上重复6次
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);
}
函数cpress\u show\u templatebitsf($tbit\u num,$tbit\u option){
全球$tbit_路径;
全局$shortname;$settings=get_选项($shortname.“u选项”);
//显示模板位(通常是边栏)
对于($i=1;$i请注意,包含的文件只能使用您在函数中标记为global
的全局变量。您在谈论哪个数组变量?不止一个。这是一个让人痛苦的问题code@Sarfraz-我说的是$do_not_duplicate[]=$post->ID;所以$do\u not\u duplicate似乎是这里的罪魁祸首。谢谢你的回答!我尝试将全局$do\u not\u duplicate添加到我的函数中,但没有效果:(