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


Display title list of all post of current post type

我有一个名为press的自定义帖子类型。

我想要实现的是生成所有press自定义帖子类型的标题列表(以及相应的链接)。

我尝试了这个代码,但没有成功,有什么想法吗?

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

并通过在列表中添加class来突出显示当前帖子。

我之前也遇到过类似的问题。下面的代码示例将显示我是如何解决问题的。

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