【问题标题】:can't view custom post type posts in WP home page无法在 WP 主页中查看自定义帖子类型的帖子
【发布时间】:2016-12-25 09:24:52
【问题描述】:

我正在尝试在 WordPress 中查看自定义帖子类型的帖子并坚持使用下面的代码。

$args = array(
    'type'      => 'theslider',
    'orderby'   => 'date',
    'order'     => 'DESC',
    'cat'       => 23
);

$lasts = new WP_Query( $args );

if( $lasts->have_posts() ):

    while( $lasts->have_posts() ): $lasts->the_post();

        the_title( sprintf('<a href="%s"><div class="pt10 title">', esc_url( get_permalink() ) ),'</div></a>' ); 

    endwhile;

endif;

wp_reset_postdata();

此代码适用于默认帖子类型。并且自定义帖子类型功能在管理面板中运行良好。

如何在 WordPress 主页上查看自定义帖子类型的帖子。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    知道了。在此处发布以供将来参考。

    <?php 
    $args = array(
        'post_type'=> 'theslider',
        'orderby'   => 'date',
        'order'     => 'DESC'
    );              
    
    $the_query = new WP_Query( $args );
    if($the_query->have_posts() ) : 
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
    
            the_title();
    
        endwhile; 
    
    else: 
    
        echo '<p>Nothing Here.</p>';
    
    endif; 
    
    wp_reset_postdata(); 
    
    ?>
    

    就是这样。它将获取并查看自定义帖子类型的帖子:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 2014-07-12
      • 2016-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多