duanlei2458 2014-09-29 16:46
浏览 60
已采纳

显示当前帖子类型的所有帖子的标题列表

I have a custom post type called press.

What I want to achieve is generate a list of the titles (with it's corresponding link to it) of all press custom post types.

I tried with this code with no luck, any ideas?

    <?php function all_posts_custom_posts( $query ) {
            $post_type =  $query->query_vars['post_type'];

            if ( 'press' == $post_type ){
                    $query->query_vars['posts_per_page'] = -1;
                    return;
            }
    } 
    add_action('pre_get_posts', 'all_posts_custom_posts',1); ?>

And also highlight the current post from the list by adding a class to it.

  • 写回答

1条回答 默认 最新

  • dongshai1944 2014-09-29 16:52
    关注

    I had a similar issue sometime ago. The following code example will display how I resolved my issue.

    <?php
    $type = 'products';
    $args=array(
      'post_type' => $type,
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)