doupu9251 2016-12-25 09:24
浏览 15
已采纳

无法在WP主页中查看自定义帖子类型的帖子

I am trying to view custom post type posts in WordPress and stuck with the code below.

$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();

This code works with the default post type. And the Custom Post Type function works well in the admin panel.

How can I view custom post type posts on WordPress home page.

  • 写回答

1条回答 默认 最新

  • dqcuq4138 2016-12-25 09:50
    关注

    Got it. Posting here for future reference.

    <?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(); 
    
    ?>
    

    That's it. it will get and view the custom post type posts :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写