无法将包装器div插入index.php

时间:2015-04-13 作者:Jensen010

在我的php索引文件中插入包装器div时遇到了一个问题,我们正在使用该文件作为网站的首页。

现在看起来是这样的:Ugly!

它需要如下所示:Much better!

我知道这是一个CSS问题,我能想到的解决方法是在主页上的这6个div周围插入一个包装div,但我不知道该将代码放在哪里,如下所示:

<?php get_header(); ?>
<?php global $woo_options; ?>
<?php if ( $woo_options[\'woo_featured_disable\'] <> "true" ) include( TEMPLATEPATH . \'/includes/featured.php\'); ?>
<?php

$args = array(
\'post_type\' => \'infobox\',
\'order\' => \'DESC\', // DESC for newer first.
\'orderby\' => \'date\',
\'posts_per_page\' => 6 // For a 3x2 grid.
);

$latest = new WP_Query( $args ); // You now have an object you can use in \'The Loop\'.
if ( $latest->have_posts() ) {
while ($latest->have_posts()) : $latest->the_post(); ?>

                <div class="bskhp_t">
        <a href="<?php echo get_post_meta($post->ID, \'mini_readmore\', true); ?>">
            <img src="<?php echo get_post_meta($post->ID, \'mini\', true); ?>" alt="" class="home-icon">
        </a>
        <div class="bskhp_f">
            <a href="<?php echo get_post_meta($post->ID, \'mini_readmore\', true); ?>">
                <h3><?php the_title(); ?></h3>
            </a>
            <p class="mini-p"><?php echo get_post_meta($post->ID, \'mini_excerpt\', true); ?></p>
            <a href="<?php echo get_post_meta($post->ID, \'mini_readmore\', true); ?>">
                <span>Read more</span>
            </a>
        </div>
        </div>

<?php endwhile;
}
$v = array();

$upload_dir = wp_upload_dir();

$v[] = "<div class=\\"bskhp_vpv\\"><a href=\'https://www.youtube.com/watch?v=x6HQ4MFqMg0\' target=\'_blank\' style=\'color:#445567;font-family:arial;font-size:12px;font-weight:500\'><img style=\'padding:4px;border:1px solid #000000; width:180px; height:90px\' src=\'".$upload_dir["baseurl"]."/2015/04/video-capture-1.png\' width=\'180\' height=\'90\' vwidth=\'180\' vheight=\'90\' /><div style=\'color:#000000;font-family:arial;font-size:12px; padding:4px 0\'>Mental Health, Homelessness, and Stability in Housing.</div><p style=\'width:200px\'></p></a></div>";
$v[] = "<div class=\\"bskhp_vpv\\"><a href=\'https://www.youtube.com/watch?v=gnMt4_7Xc9M\' target=\'_blank\' style=\'color:#445567;font-family:arial;font-size:12px;font-weight:500\'><img style=\'padding:4px;border:1px solid #000000; width:180px; height:90px\' src=\'".$upload_dir["baseurl"]."/2015/04/video-capture-2.png\' width=\'180\' height=\'90\' vwidth=\'180\' vheight=\'90\' /><div style=\'color:#000000;font-family:arial;font-size:12px; padding:4px 0\'>2014 Key to Hope luncheon keynote address by Charles Gibson</div><p style=\'width:200px\'></p></a></div>";
$v[] = "<div class=\\"bskhp_vpv\\"><a href=\'https://www.youtube.com/watch?v=GKCJTTJq4KM\' target=\'_blank\' style=\'color:#445567;font-family:arial;font-size:12px;font-weight:500\'><img style=\'padding:4px;border:1px solid #000000; width:180px; height:90px\' src=\'".$upload_dir["baseurl"]."/2015/04/video-capture-3.png\' width=\'180\' height=\'90\' vwidth=\'180\' vheight=\'90\' /><div style=\'color:#000000;font-family:arial;font-size:12px; padding:4px 0\'>Plymouth Housing Group 2013 Documentary</div><p style=\'width:200px\'></p></a></div>";
$p = array();

$p[] = "<div><a href=\'https://www.plymouthhousing.org/about-us/publications/\'><img src=\'".$upload_dir["baseurl"]."/2015/04/publications-1.png\' height=\'254\' width=\'190\' /><div class=\\"bskhp_pcap\\">ANNUAL REPORTS</div></div>";
$p[] = "<div><a href=\'https://www.plymouthhousing.org/about-us/publications/\'><img src=\'".$upload_dir["baseurl"]."/2015/04/publications-2.png\' height=\'254\' width=\'190\' /><div class=\\"bskhp_pcap\\">FACT SHEET &amp; MAPS</div></div>";
$p[] = "<div><a href=\'https://www.plymouthhousing.org/about-us/publications/\'><img src=\'".$upload_dir["baseurl"]."/2015/04/publications-3.png\' height=\'254\' width=\'190\' /><div class=\\"bskhp_pcap\\">NEWSLETTER</div></div>";

$mv = array();

$mv[] = "<h3 class=\'bskhp_h3\'>Our Vision</h3>Housing is just the beginning...the first step to building hope and transforming lives.  We envision a day when every person has a home and a better quality of life.<p class=\\"bskhp_rm\\"><a href=\'https://www.plymouthhousing.org/about-us/mission-history/\'>READ MORE</a></p>";
$mv[] = "<h3 class=\'bskhp_h3\'>Our Mission</h3>Plymouth Housing Group works to eliminate homelessness and address its causes by preserving, developing and operating safe, quality, supportive housing and by providing homeless adults with opportunities to stabilize and improve their lives.<p class=\\"bskhp_rm\\"><a href=\'https://www.plymouthhousing.org/about-us/mission-history/\'>READ MORE</a></p>";

?>
早些时候,有人亲切地帮助我编写了这段代码,但这是漫长的一天,我的头一直在痛:)有人知道一个简单的解决方法吗?

3 个回复
最合适的回答,由SO网友:Tim Malone 整理而成

如果我理解正确,您希望修改代码,使其顶部如下图所示:

<div class="new_wrapper">
<?php

$args = array(
下面是:

<?php endwhile;
} ?>
</div>
<?php
$v = array();
听起来您可能在添加<div></div> 内部<?php ?> 代码,这可能会导致错误(如果关闭错误显示,则可能会导致空白页)。

基本上,您在这里所做的是在主Wordpress循环周围插入一个包装器-包括if (have_posts())while (have_posts()) 块。将包装保持在这两个块之外意味着1)当没有帖子时,包装仍将被打印出来(因为它在if 块)和2)包装将只打印一次,即不会像在while

希望这能解决问题,并帮助你理解背后的逻辑——如果不是,如果我误解了这个问题,请道歉!

SO网友:Emir Dupovac

你应该在之前打开divargs<?php 然后马上关上endwhile, 如果我答对了问题。

SO网友:Tom J Nowell

现在您有:

if ( $latest->have_posts() ) {
while ($latest->have_posts()) : $latest->the_post(); ?>
 ... inside the loop
<?php endwhile;
}
在伪码中为:

if we have posts
    foreach post
        show the post
    endforeach
endif
因此,您需要这样做:

if we have posts
    open wrapper div
    foreach post
        show the post
    endforeach
    close wrapper div
endif

结束

相关推荐

Iterate through ID's in loop

我已经基于category创建了一个自定义循环,现在我想运行一个函数,将当前帖子的特定ID作为参数进行迭代。我有。。$secondary_loop = new WP_Query(array( \'category_name\' => get_the_title(), \'posts_per_page\' => 5 )); while ( $secondary_loop->have_posts() ) : $secondary_loop->the_post();&#x