Wordpress get_post_meta issue

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

我是php新手,被指派在Wordpress主题中修复一些代码,但我什么都搞不懂。该代码用于自定义主题的主页,它应该提取最新的博客帖子,并将它们放在页面顶部的3x2网格中。

Here\'s 正在讨论的页面

这是索引中的代码。php文件(我相信这就是问题所在)

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

$t = array();

$t[2] = "A";
$t[5] = "B";

query_posts(\'post_type=infobox&order=ASC&posts_per_page=20&meta_value=false\');

if (have_posts()) :

        $a = array(0,1,3,4); $i = 0;

        while (have_posts()) : the_post();

        $m = "";

        $m .= "<div class=\\"bskhp_t\\" style=\'font-family:arial;font-size:12px\'>";
        $m .= "<a href=\\"".get_post_meta($post->ID, \'mini_readmore\', $single = true)."\\"><img src=\\"".get_post_meta($post->ID, \'mini\', $single = true)."\\" alt=\\"\\" class=\\"home-icon\\"></a>";
        $m .= "<div class=\\"bskhp_f\\">";
        $m .= "<h3><a href=\\"".get_post_meta($post->ID, \'mini_readmore\', $single = true)."\\">".get_the_title()."</a></h3>";
        $m .= "<p>".get_post_meta($post->ID, \'mini_excerpt\', true)."</p>";
        $m .= "<a style=\'font-family:arial;font-size:13px;text-transform: uppercase\' href=\\"".get_post_meta($post->ID, \'mini_readmore\', $single = true)."\\" class=\\"btn\\">";
        $m .= "<span>Read More</span></a>";
        $m .= "</div>";
        $m .= "</div>";



        $t[$a[$i++]] = $m;

        endwhile;

endif;

$t[2] = $t[0];
$t[3] = $t[0];
$t[4] = $t[0];
$t[5] = $t[0];
出于某种原因,这段代码不会提取最新的博客帖子并将其放入3x2网格,前两项除外,我不知道为什么。我相信问题出在get\\u post\\u元函数中,但我的知识太少,无法调试它。

有人能帮忙吗?我会非常客气:)

1 个回复
最合适的回答,由SO网友:Sebastien 整理而成

首先,您应该对模板文件进行一些改进。尝试以下操作:

<?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><?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;
}
我删除了CSS,因为您应该将其移动到另一个文件中。

然后,对于3x2网格,我建议您使用CSS。尝试:

.bskhp_t {
    display: inline-block;
    width: 30%;
}
如果的父容器.bskhp_t DIVs采用所有可用宽度。

我没有测试此代码,所以如果您有一些问题,请告诉我;)

结束