首页 开发指南

1我要做得效果如上图所示,其中这个内容是放在列表面页里的,所以这里面就用到主循环嵌套循环,其中可以用WP_QERUY()来解决,不多说那么多了,直接贴代码:

<?php
if (have_posts() ) :
while (have_posts() ) : the_post(); $c++;
?>
<div>
<h1><?php the_title(); ?></h1>
<div>
<div>
<?php
$img=preg_replace("#<(?!img)[^>]*>#","",get_the_content());//去除输出中的除IMG以外的标签
$pattern="/<[img|IMG].*?src=['|"](.*?(?:[.gif|.jpg]))['|"].*?[/]?>/";
preg_match_all($pattern,$img,$match);
echo $match[0][0];
?>
<?php
$content=strip_tags(get_the_content());
echo mb_strimwidth($content,0,200,"...");
?>
</div>
<div>
<div>
<div id="scroll<?php echo $c; ?>">
<ul>
<?php
$artid=get_the_ID();
$arr=explode(",",get_post_meta($artid,"catid",true));
for($i=0; $i<count($arr); $i++){
?>
<?php 
$str="p=".$arr[$i]."";
$recentPosts=new WP_Query($str);
//$recentPosts->query();
?>
//红字部分是重点,这个地方是按指定文章分类的ID输出文章的,如果我们这按指定文章的分类来输出,就换成下面的代码:
<?php 
$recentPosts=new WP_Query("cat='$arr[$i]'&showpost=5");
?>
【关于这点,我真是很纳闷,如果输出指定ID的文章的时候,把代码写成$recentPosts=new WP_Query("p='$arr[$i]'");就不会生效,非要重新定义一个字符串变量才可以。】
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); $num++; ?>
<li>
<div>
<div>
<a href="<?php the_permalink(); ?>"><?php
$img=preg_replace("#<(?!img)[^>]*>#","",get_post_meta(get_the_id(), 'pro_pic', true));//去除输出中的除IMG以外的标签
$pattern="/<[img|IMG].*?src=['|"](.*?(?:[.gif|.jpg]))['|"].*?[/]?>/";
preg_match_all($pattern,$img,$match);
echo $match[0][0];
?>
</a>
</div>
<div><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></div>
</div>
</li>


<?php
endwhile;
wp_reset_query();
?>
<?php
}
?>
</ul>
</div>
</div>
<div id="btn<?php echo $c; ?>">
<span></span>
<span>
<ol>
<script>
var len = $('<?php echo "#scroll".$c;? ?> ul li').length;//计算列表总长度(个数)
var page = Math.ceil(len/5);
for(b=0;b<page;b++){
?? ?if(b==0){
?? ??? ?document.write("<li class='numcur'></li>");
?? ??? ?}
?? ??? ?else{
?? ??? ?document.write("<li></li>");?? ?
?? ??? ?}
}
</script>
</ol>
</span>
<span></span>
</div>
</div>
<div></div>
</div>
<div></div>
</div>
<?php
endwhile;
wp_reset_query();
echo "<div class='cl'></div><div class='page_navi'>";
company_par_pagenavi($range="8")."</div>";
endif;
?>

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

会员评论:(0)

声明:本站所有主题/文章除标明原创外,均来自网络转载,版权归原作者所有,如果有侵犯到您的权益,请联系本站删除,谢谢!
©www.sbmzenith.com 2013-2022 All Rights Reserved.
豫ICP备15009393号