【问题标题】:Tag list filter for posts帖子的标签列表过滤器
【发布时间】:2016-01-13 13:03:21
【问题描述】:

我正在尝试添加基于标签的过滤器,但我不确定如何继续我的帖子列表中显示的标签及其锚标签。

<?php
   $tags = get_tags( array('orderby' => 'count', 'order' => 'DESC') );
   foreach ( (array) $tags as $tag ) {
   echo '<a href="' . get_tag_link ($tag->term_id) . '" rel="tag">' . $tag->name . ' (' . $tag->count . ') </a>';
 }
 ?></p>

我正在使用以下循环来获取我的帖子

<?php
$myposts = get_posts('numberposts=-1&offset=$debut');
foreach ($myposts as $post):
  setup_postdata($post);
?>

所以我的问题

1 如何调整我的 get_posts 以根据标签选择应用过滤器。使用ajax会很容易吗

2 生成标签链接列表以便执行上述操作的最佳方法是什么。

编辑 我想要做的是确保我只是从 tag/tag-3 获取 url slug 我如何得到这个。

编辑 好的,我已经做得更远了,但它仍然只显示我的标签页面中的所有帖子,即使单个标签标题不是空白的,所以什么给了?。

<div class="post-list" style="width:80%;float:left">
            <?php

            $tag = single_tag_title('', false);
   echo '<h2>Tag: '.$tag.'</h2>';
  $args = array(
        'taxonomy' => $tag,
        'terms' => $tag,
 );


        $myposts = get_posts($args);
        foreach ($myposts as $post):
            setup_postdata($post);
        ?>
             <div id="dateInfo" style="float:right;">
<?php the_date('Y.m.j'); ?>&nbsp;|&nbsp;
<?php comments_number( '0 hozzászólás', '1 hozzászólás', '% hozzászólás' ); ?>.
</div>


             <div id="title_wrapper">
    <h2><?php the_title(); ?></h2>

</div>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    试试这个把你的分类和术语名称放在这里

    $args = array(
        'tax_query' => array(
            array(
                'taxonomy' => 'genre',
                'field' => 'slug',
                'terms' => 'jazz'
            )
        )
    );
    $postslist = get_posts( $args );
    

    如果您希望通过 ajax 调用完成此操作,则必须在 &lt;a&gt; 标签上运行 onclick 函数并在 ajax 响应中调用上述代码 这将返回带有标签过滤帖子的您..

    需要通过onclick函数传递taxonomyterms

    【讨论】:

    • 当标签链接被点击时,我如何从 url 中检索标签 slug ?目前它会到这里kvalixhu.digitalthinkersni.co.uk/tag/tag-3,但仍然显示所有标签。
    • 您能否提供一个示例,以便我可以标记为答案,感谢通过 onlcick 传递值并在该区域拦截它们
    • 这不会过滤标签这是taxonmoys
    【解决方案2】:

    好的,既然你有&lt;a&gt;标签列表,修改为:

    获取您的分类名称和术语 ID 已经在这里。这将是 传入onclick函数。

    <?php
       $tags = get_tags( array('orderby' => 'count', 'order' => 'DESC') );
       foreach ( (array) $tags as $tag ) {
        $term_ID = $tag->term_id;
        $taxonomy_name = 'GET TAXONOMY NAME HERE';
    
       echo '<a onclick="show_filter_tags('.$term_ID.','.$taxonomy_name.')" href="' . get_tag_link ($tag->term_id) . '" rel="tag">' . $tag->name . ' (' . $tag->count . ') </a>';
     }
     ?>
    

    &lt;a&gt;标签中定义onclick函数

    <script type="text/javascript">
        function show_filter_tags(term_id,taxonomy_name){
    
            var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
              $.post(
              ajaxurl, 
              {
                  'action'  :   'get_tagged_posts',
                  'term_id' :   term_id,
                  'taxonomy_name' : taxonomy_name,
              }, 
              function(output){
                console.log(output)
              });
          }
    </script>
    

    现在这里是需要编写查询的函数,它将根据标签过滤帖子。将此放在活动主题文件夹中的functions.php文件中

    add_action('wp_ajax_get_tagged_posts', 'get_tagged_posts');
    add_action('wp_ajax_nopriv_get_tagged_posts', 'get_tagged_posts');
    function get_tagged_posts() {
        $args = array(
            'tax_query' => array(
                array(
                    'taxonomy' => $_REQUEST['taxonomy_name'],
                    'field' => 'term_id',
                    'terms' => $_REQUEST['term_id'],
                    'operator' => 'IN',
                )
            )
        );
        $postslists = get_posts( $args );
        foreach ($postslists as $postslist) {
            # code...
        }
    }
    

    【讨论】:

    • 我应该很清楚,我没有撤销帖子的特定分类我试图显示基于此过滤器的所有帖子
    • 请在我的 oringal 问题中查看编辑我只需要从 url 获取过滤器我并没有真正为 mo 的 ajax 所困扰
    • Prakash 你能看到我上面的编辑吗?我只需要在帖子中获取 slug url
    猜你喜欢
    • 2019-02-15
    • 2010-11-04
    • 1970-01-01
    • 2015-10-14
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    相关资源
    最近更新 更多