【问题标题】:Add more results to Wordpress tag search向 Wordpress 标签搜索添加更多结果
【发布时间】:2015-01-09 22:15:41
【问题描述】:

我正在尝试在 Wordpress 中修改我的 tag.php。基本上,我将一般循环设置为默认每页五个帖子。当用户单击我的标签云中的标签时,我希望它按标题显示所有相关结果。这是我在 tag.php 中的内容:

<p>Tag: <?php single_tag_title(); ?></p>

<?php if (have_posts()) : while (have_posts ()) : the_post (); ?>
                    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent   Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>


                    <?php endwhile; endif; ?>

这很好用,但是它只返回默认数量 5。当我尝试在循环之前使用 ('posts_per_page' => 1000) 添加 wp_query 时,它会返回我所有的网站帖子,而不仅仅是适当的标签。如何添加更多结果?谢谢!

【问题讨论】:

    标签: wordpress tags


    【解决方案1】:

    非常感谢 SilverKenn 的回复,非常感谢。我能够使用它来解决它。

    <?php $args = array( 'post_type' => 'post', 'posts_per_page' => -1, 'tag'=> get_query_var('tag') );
      $loop = new WP_Query( $args );
      while ( $loop->have_posts() ) : $loop->the_post(); ?>
      <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    

    【讨论】:

      【解决方案2】:

      当您尝试通过 wordpress 自定义某些内容时,无需编辑 php 文件,您可以使用 functions.php 修改几乎所有内容,具体取决于您对主题的编码方式,

      查看 post_limits 过滤器http://codex.wordpress.org/Plugin_API/Filter_Reference/post_limits

      或 pre_get_posts http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

      您可以使用上述过滤器限制结果,

      例如pre_get_posts

      function cmk_custom_result( $wp_query ) {
          $post_type = $wp_query->query['post_type'];
          if ( $post_type == 'your-post-type' && is_tag() ) {
              $wp_query->set('posts_per_page', '25');
          }
      }
      add_filter('pre_get_posts', 'cmk_custom_result'); 
      

      post_limist

      function cmk_post_result_limits( $limit, $query ) {
          if ( !is_admin() && $query->is_main_query() && is_tag() ) {
              return 'LIMIT 0, 25';
          }
          return $limit;
      }
      add_filter( 'post_limits', 'cmk_post_result_limits', 10, 2 );
      

      【讨论】:

        【解决方案3】:

        使用 get_query_var 获取正确的标签,例如:'tag'=&gt; get_query_var('tag') 然后在您的帖子数组中使用 -1 为“posts_per_page”获取该标签的无限数量的帖子,否则将数量更改为您想要限制的任何内容输出。

                        <?php $args = array( 'post_type' => 'post', 'posts_per_page' => -1, 'tag'=> get_query_var('tag') );
                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post(); ?>
                        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent   Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        
        
                        <?php endwhile; ?>
        

        【讨论】:

          猜你喜欢
          • 2014-08-03
          • 2013-07-23
          • 2019-03-11
          • 1970-01-01
          • 2016-08-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多