帖子的标签列表过滤器

时间:2016-01-13 13:03:21

标签: php wordpress

我正在尝试添加基于标签的过滤器,但我不知道如何继续我的标签显示在我的帖子列表中,带有锚标签。

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

2 个答案:

答案 0 :(得分:0)

尝试将此分类和术语名称放在此处

  clientHandler.InitiateSocket(myServerIpv4Address, serverPortNo);
  if (clientHandler.IsConnected()) {
      //Do something
  } else {
      //Do something, note the error message below
    logBox.WriteTimedLog("Connecting attempt through TCP/IP is failed! Timeout Occurs!\n", Color.Red);
  }

如果您希望通过ajax调用完成此操作,则必须在//Receive callback const int MAX_RECEIVE_ATTEMPT = 10; int receiveAttempt = 0; private void receiveCallback(IAsyncResult result) { System.Net.Sockets.Socket socket = null; try { socket = (System.Net.Sockets.Socket)result.AsyncState; if (socket.Connected) { int received = socket.EndReceive(result); if (received > 0) { receiveAttempt = 0; byte[] data = new byte[received]; Buffer.BlockCopy(buffer, 0, data, 0, data.Length); //something else socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(receiveCallback), socket); } else if (receiveAttempt < MAX_RECEIVE_ATTEMPT) { ++receiveAttempt; socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(receiveCallback), socket); } else { //completely fails! if (ErrorMessageHandler != null) //Note the error message ErrorMessageHandler("receiveCallback is failed!\n"); receiveAttempt = 0; this.Close(); } } } catch (Exception e) { //Something else } } 标记上运行onclick函数并在ajax响应中调用上述代码 这将返回标签过滤后的帖子..

  

需要通过onclick功能传递分类条款

答案 1 :(得分:0)

好的,因为您有<a>标签列表,请将其修改为:

  

获取分类名称和术语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>';
 }
 ?>

<a>标记

中定义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...
    }
}