如何在点击按钮时获得与特定类别名称相关的所有帖子?

时间:2017-10-14 作者:Ishan Patel

我试图实现的是在第一页上显示所有帖子类别标题的缩略图。但当用户单击特定类别时,我想显示该特定类别的所有帖子。我正在使用自定义帖子类型和自定义字段插件,所以我也创建了不同的自定义帖子和字段,但我仍然坚持如何配置the permalink 函数或创建自定义查询,可以帮助我实现这一点。

我们将非常感谢您的帮助。非常感谢。

1 个回复
最合适的回答,由SO网友:Shital Marakana 整理而成

How to get category wise all post in WordPress?

在分类法中添加以下代码-“yourtaxonomyname”。php

例如:文件名:taxonomy事件类别。php&;帖子类型:事件(&P);分类名称:事件类别

<?php
$category_object        = get_queried_object();
$category_taxonomy      = $category_object->taxonomy;
$category_term_id       = $category_object->term_id;
$category_name          = $category_object->name;
$category_description   = $category_object->description;
$category_slug          = $category_object->slug;

$post_type = \'events\';
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$post_args=array(
    \'type\'                     => $post_type,
    \'post_status\'              => \'publish\',
    \'posts_per_page\'           => 15,
    \'paged\'                    => $paged, 
    \'caller_get_posts\'         => -1,
    \'child_of\'                 => 0,
    \'parent\'                   => 0,
    \'orderby\'                  => \'name\', 
    \'order\'                    => \'ASC\',
    \'hide_empty\'               => 0,
    \'hierarchical\'             => 1,
    \'exclude\'                  => \'\',
    \'include\'                  => \'\',
    \'number\'                   => \'\',
    \'tax_query\'                => array(
                                        array(
                                            \'taxonomy\' => $category_taxonomy,
                                            \'field\' => \'id\',
                                            \'terms\' => $category_term_id
                                        )
                                    ),
    \'pad_counts\'               => false, 
);
$post_my_query = null;
$post_my_query = new WP_Query($post_args);

if( $post_my_query->have_posts() ) :
?>

    <ul>
        <?php
            while ($post_my_query->have_posts()) : $post_my_query->the_post(); 
            ?>
            <li>
                <a href="<?php echo get_permalink($post->ID);?>">
                <?php
                    if ( has_post_thumbnail() ) {
                        //get_the_post_thumbnail( $post->ID, array( 100, 100) ); 
                       ?>
                        <?php echo  get_the_post_thumbnail($post->ID,\'medium\'); //thumbnail,medium,large,full,array(100,100)?>
                       <?php
                    }
                ?>
                    <p><?php echo get_the_title( $post->ID );?></p>
                </a>
            </li>
            <?php
           endwhile;
       ?>
    </ul>

<?php
else :      
    echo \'<p class="red-error">No Post Found!</p>\'; 
endif;

wp_reset_query($post_my_query);
?>
<?php //if(function_exists(\'wp_pagenavi\')) { wp_pagenavi( array( \'query\' => $post_my_query ) ); }else{ echo \'<p class="red-error">No Event Post Found!</p>\'; }
?>

结束

相关推荐

Change Taxonomy Permalinks

我有自定义帖子,我创建了一个显示所有自定义帖子的页面。示例:www.example.com/archive-page我想知道是否可以更改与此自定义帖子相关的类别和标签的永久链接。现在我有:www.example.com/my-custom-post-type-cats/my-category-1www.example.com/my-custom-post-type-tags/my-tag-1</我想要这样的东西:www.example.com/archive-page?category=1www.e