【问题标题】:Query Posts dont work in Wordpress shortcode查询帖子在 Wordpress 短代码中不起作用
【发布时间】:2012-09-01 21:49:12
【问题描述】:

现在,我已经尝试了很多简码 sn-ps 在我的帖子(不是页面)中显示 3 个最近的帖子作为简码。

喜欢:

function posts_shortcode() {
$the_query = new WP_Query(array('posts_per_page' => 3));

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;

// Reset Post Data
wp_reset_postdata();

}

add_shortcode('posts', 'posts_shortcode'); 

function posts_shortcode() { // From Smashingmagazine
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 3));
   if (have_posts()) :
      while (have_posts()) : the_post();
         $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
      endwhile;
   endif;
   wp_reset_query();
   return $return_string;
}

function register_shortcodes(){
   add_shortcode('posts', 'posts_shortcode'); 
}

甚至

function wptuts_recentpost($atts, $content=null){ // From WP Tuts+

    $getpost = get_posts( array('number' => 1) );

    $getpost = $getpost[0];

    $return = $getpost->post_title . "<br />" . $getpost->post_excerpt . "…";

    $return .= "<br /><a href='" . get_permalink($getpost->ID) . "'><em>read more →</em></a>";

    return $return;

}
add_shortcode('newestpost', 'wptuts_recentpost');

所有这些似乎都不起作用。我怀疑它与循环或其他东西有关。如果我删除循环并且只做类似的事情

return 'hey there';

在函数()中,那么它工作得很好。

有什么想法吗?

编辑: 使用以下短代码:

function posts_shortcode() { // From Smashingmagazine
query_posts($args);
   if (have_posts()) :
      while (have_posts()) : the_post();
        $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
      endwhile;
   endif;
   wp_reset_query();
   return $return_string;

}

add_shortcode('posts', 'posts_shortcode');

我设法得到了一些结果,但它现在显示带有指向我的首页的链接的文本主页。首先,我不明白为什么......其次,当我用'posts_per_page = 5'替换$args时,它又消失了。我真的认为由于某种原因循环有问题..

【问题讨论】:

    标签: wordpress shortcode


    【解决方案1】:

    不要使用第一个。你不应该在简码函数中回显任何内容。

    最后一个只会打印一篇文章。此外,我认为应该是 array('numberposts' => 1) ,而不是 array('number' => 1)。

    第二个应该可以工作,但是您将 add_shortcode 包装在一个函数中,因此您需要在某个时候调用它。例如 add_action( 'init', 'register_shortcodes' );或将 add_shortcode 直接放入 functions.php 中。

    【讨论】:

    • 嗨,Calle,我已经尝试了您的建议,但它们不起作用。你没有提到的第二个。但是,我确实删除了循环,然后只执行 $return_string = 'hello',这确实有效。但是每当我尝试循环浏览帖子时,它都是空白的。
    • 您编辑的代码是我想要的,假设 $args 是以前使用的相同数组。似乎也有错别字,尝试将 $return_string = '... 更改为 $return_string .= '...
    【解决方案2】:

    这对我来说很好用

    add_shortcode( 'objectx-pages-list', 'objectx_pages_list_func' );
    function objectx_pages_list_func( $atts ) {
    global $post;
    ob_start();
    extract( shortcode_atts( array('ids' => '1186'), $atts ) );
    $id_array = explode(',', $ids); 
    $pages_query = new WP_Query( array(
        'post_type' => 'page',
        'post__in' => $id_array,
        'order' => 'ASC',
        'orderby' => 'title',
    ) );
    if ( $pages_query->have_posts() ) { ?>
    <div class="carousel-wrapper">
    <div class="owl-carousel owl-theme carousel-1" id="carousel-rooms">
    <?php while ( $pages_query->have_posts() ) : $pages_query->the_post();
    $featured_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    ?>
    <div <?php post_class('item'); ?> id="post-<?php the_ID(); ?>">
        <div class="row">
            <div class="col-md-7">
                <div class="img-rooms">
                    <a href="<?php the_permalink(); ?>">
                    <img class="img-responsive wp-post-image" src="<?php echo $featured_image; ?>"></a>
                </div>
            </div>
        <div class="col-md-5">
            <div class="detail-rooms">
                <h2 class="title-room "><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <?php the_excerpt(); ?> 
            </div>
        </div>
        </div>
    </div>
    <?php endwhile; ?>
    </div>
    </div>
    <?php $myvariable_pages = ob_get_clean();
    wp_reset_postdata();
    return $myvariable_pages;
        }
    }
    

    简码

    [objectx-pages-list id="15,16,17"]
    

    【讨论】:

      猜你喜欢
      • 2015-05-10
      • 2022-06-12
      • 2014-06-04
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多