【问题标题】:Wordpress shortcode no worksWordpress 简码无效
【发布时间】:2013-03-30 21:26:01
【问题描述】:

我为 wordpress 创建了这个简码,但没有用

<?php
function theme_tfw_posts()
{
?>
<?php
    global $post;
    $args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :
        setup_postdata($post);
?>
        $a=<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>;
    <?php endforeach; ?>
<?php
    return $a;
}
?>
<?php
add_shortcode('tfw_posts','theme_tfw_posts');
?>

我认为问题出在标签或其他东西上,但这是我的第一个短代码,问候

【问题讨论】:

  • 更多细节会让你更快得到更好的答案。当你说它不起作用时,你是什么意思?究竟会发生什么,它与您预期会发生什么不同?

标签: php wordpress shortcode


【解决方案1】:

至少有一件事看起来是错误的,$a 在您返回时不会被定义。原因是您的行 $a=&lt;a href... 不在 PHP 代码块内。

每次执行foreach 循环时,$a 也会被覆盖。也许您想将每个链接附加到上一个链接?

这可能会更好(尽管我不完全确定您要做什么,所以可能不会):

<?php
function theme_tfw_posts()
{
    $args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
    $myposts = get_posts( $args );
    $a = '';
    foreach( $myposts as $post )
    {
        setup_postdata($post);
        $a .= '<a href="' . the_permalink() . '">' . the_title() . '</a>';
    }
    return $a;
}
add_shortcode('tfw_posts','theme_tfw_posts');
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多