categories - Display a mixed list of posts and media

I have a list of articles on my website.Some of the articles are post types with the "Article" category and ot

I have a list of articles on my website.

Some of the articles are post types with the "Article" category and others are PDF files uploaded to the media library, also with the "Article" category. I added categories to the media library using these functions in functions.php:

// add categories for attachments
function add_categories_for_attachments() {
    register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'add_categories_for_attachments' );

// add tags for attachments
function add_tags_for_attachments() {
    register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init' , 'add_tags_for_attachments' );

thanks to this article.

I need to display a single category page which lists both types of posts (media and regular posts).

Is there a simple way to achieve it?

I have a list of articles on my website.

Some of the articles are post types with the "Article" category and others are PDF files uploaded to the media library, also with the "Article" category. I added categories to the media library using these functions in functions.php:

// add categories for attachments
function add_categories_for_attachments() {
    register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'add_categories_for_attachments' );

// add tags for attachments
function add_tags_for_attachments() {
    register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init' , 'add_tags_for_attachments' );

thanks to this article.

I need to display a single category page which lists both types of posts (media and regular posts).

Is there a simple way to achieve it?

Share Improve this question asked Jun 9, 2019 at 13:49 CaptainNemoCaptainNemo 1054 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Something like this should work :

$args = array ( 'post_type' => array( 'post', 'attachment'), 'category' => ARTICLE_CATID   );
$query = new WP_Query( $args );

Thanks to @bjornredemption, I used the following code snippet:

$args = array('category' => $wp_query->get_queried_object_id(), 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC', 'post_type' => array( 'post', 'attachment'),'post_status' => array( 'publish', 'inherit'));
$glossaryposts = get_posts( $args );

Furthermore the function have_posts() must be changed because the default one does not check for attachments.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745406236a4626326.html

相关推荐

  • categories - Display a mixed list of posts and media

    I have a list of articles on my website.Some of the articles are post types with the "Article" category and ot

    10小时前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信