dongzhe3573 2013-03-19 12:19
浏览 18

在归档中显示页面和帖子

I have many categories, but for clearity I will do an example with only two:

  • News that contains many posts
  • Products that contains many pages (I've added this feature in functions.php)

I want that archive.php displays:

  • the posts when the user choose the category News
  • the pages when the user choose the category Products

At the moment Wordpress by deafult shows only posts and not pages.

I know that this is possible by adding a simple function in functions.php but I can't find it.

EDIT: For another project with custom post type "Books" I used this code

function query_post_type($query) {
    $post_types = get_post_types();

    if ( is_category() || is_tag()) {

        $post_type = get_query_var('books');

        if ( $post_type ) {
            $post_type = $post_type;
        } else {
            $post_type = $post_types;
        }

        $query->set( 'post_type', $post_type ); 


        return $query;
    }
}
add_filter('pre_get_posts', 'query_post_type');

Is there something like that for my case?

  • 写回答

1条回答 默认 最新

  • dsjws44266 2013-03-19 12:25
    关注

    You can query for solely pages in the WP Query.

    PHP:

    $args = array(
      'post_type' => 'page'
    );
    
    $query = new WP_Query( $args );
    

    This will return all pages in your database, and zero posts. Vice versa, you could have 'post_type' => 'post' to query all posts, but that's the default behaviour of the WP Query.

    For more documentation on this, check out the WP Query reference in the Wordpress Codex.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度