【问题标题】:Show all posts for wordpress category not working显示 wordpress 类别的所有帖子不起作用
【发布时间】:2014-06-22 01:58:58
【问题描述】:

我在 wordpress 中设计了一个自定义主题,我正在尝试添加一个指向主页的链接,该链接会将用户带到特定类别的帖子...例如

第 1 类的帖子 来自第 1 类的帖子 来自第 1 类的帖子 (查看第 1 类的所有帖子)

第 2 类的帖子 来自第 2 类的帖子 来自第 2 类的帖子 (查看第 2 类的所有帖子)

但由于某种原因,此链接不起作用,所有链接都将它们带到“特色”类别(特色类别 ID 为 261)

这是我用来从类别中提取帖子列表的代码:

<?php query_posts( 'cat=5&posts_per_page=4' ); ?>
<?php get_template_part('news_page_loop'); ?>

这里是 news_page_loop 页面的内容。下面几行有查看所有帖子链接的代码:

<div class="single_post">
    <?php if(have_posts()) : ?><?php while(have_posts())  : the_post(); ?>
    <div class="cat-page-thumb-title">
        <a href="<?php the_permalink(); ?>">
            <?php the_post_thumbnail('home-cat-image', array('class' => 'cat-thumb')); ?>
        </a> 
        <p class="catpage-post-title">
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </p>
        <p class="post-meta">
            <?php comments_popup_link('0 Reviews', '1 Review', '% Reviews'); ?>
        </p>
    </div><!--cat page thumb and title-->
    <?php endwhile; ?>
    <?php endif; ?>
</div><!--single post-->

<div class="page_navi_wp">
    <div class="view-more-cat"><?php
        $category = get_the_category();
        if($category[0]){
            echo '<a href="'.get_category_link($category[0]->term_id ).'">View More Posts</a>';
        }
    ?></div>
</div><!--page navi wp-->

我做错了什么?请帮忙。 供参考,网址为:http://tour.khujbo.com/

谢谢

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    这是我的 Categoty.php 自定义主题的代码

    我不使用任何查询。

    <?php get_header(); ?>
    <div class="row">
    <div class="col-xs-12 col-sm-7 col-md-8 post">
    <h3><?php _e('Category:','My-Theme');?> <a href="<?php echo get_category_link( $category_id ); ?>" title="<?php echo single_cat_title(); ?>"><?php echo single_cat_title(); ?></a></h3>
    <?php if ( have_posts() ) : ?>
    <?php
    // The Loop
    while ( have_posts() ) : the_post(); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <div class="post-header-line">
    <?php My_Theme_posted_on(); ?>
    </div>
    <div class="post-content">
    <?php the_excerpt(); ?>
    </div>
    <div class="post-footer-line">
    <?php _e('Posted in  ','My-Theme');?><?php the_category(', ');?>
    </div>
    <?php endwhile; // End Loop
    My_Theme_numeric_posts_nav();
    else: ?>
    <p class="bg-primary"><?php _e('Sorry, no posts matched your criteria.','My-Theme');?></p>
    
    <?php endif; ?>
    </div>
    <div class="col-xs-12 col-sm-5 col-md-4">
    <?php get_sidebar(); ?>
    </div>
    </div>
    <div class="clear"></div>
    <?php get_sidebar('footer'); ?>
    <div class="clear"></div>
    <?php get_footer(); ?>
    

    页面导航代码

    function my_theme_numeric_posts_nav() {
    
        if( is_singular() )
            return;
    
        global $wp_query;
    
        /** Stop execution if there's only 1 page */
        if( $wp_query->max_num_pages <= 1 )
            return;
    
        $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
        $max   = intval( $wp_query->max_num_pages );
    
        /** Add current page to the array */
        if ( $paged >= 1 )
            $links[] = $paged;
    
        /** Add the pages around the current page to the array */
        if ( $paged >= 3 ) {
            $links[] = $paged - 1;
            $links[] = $paged - 2;
        }
    
        if ( ( $paged + 2 ) <= $max ) {
            $links[] = $paged + 2;
            $links[] = $paged + 1;
        }
    
        echo '<div><ul class="pagination">' . "\n";
    
        /** Previous Post Link */
        if ( get_previous_posts_link() )
            printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
    
        /** Link to first page, plus ellipses if necessary */
        if ( ! in_array( 1, $links ) ) {
            $class = 1 == $paged ? ' class="active"' : '';
    
            printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
    
            if ( ! in_array( 2, $links ) )
                echo '<li><span>...</span></li>';
        }
    
        /** Link to current page, plus 2 pages in either direction if necessary */
        sort( $links );
        foreach ( (array) $links as $link ) {
            $class = $paged == $link ? ' class="active"' : '';
            printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
        }
    
        /** Link to last page, plus ellipses if necessary */
        if ( ! in_array( $max, $links ) ) {
            if ( ! in_array( $max - 1, $links ) )
                echo '<li><span>...</span></li>' . "\n";
    
            $class = $paged == $max ? ' class="active"' : '';
            printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
        }
    
        /** Next Post Link */
        if ( get_next_posts_link() )
            printf( '<li>%s</li>' . "\n", get_next_posts_link() );
    
        echo '</ul></div>' . "\n";
    
    }
    

    【讨论】:

    • 感谢您的意见。但我的 category.php 工作正常。我想要的是在主页上创建一个链接,将用户带到包含该类别帖子的页面。谢谢
    • 不要将此行用于类别&lt;?php _e('Posted in ','My-Theme');?&gt;&lt;?php the_category(', ');?&gt;
    • 恐怕这并没有解决问题。目前我只是使用&lt;a href="http://tour.khujbo.com/category/how-to-get-there/"&gt;See All Posts on This Category&lt;/a&gt;这个链接和&lt;a href="http://tour.khujbo.com/category/places-to-visit/"&gt;See All Posts on This Category&lt;/a&gt;这个链接来显示帖子。我在主页上使用这个链接。 link
    • 您没有使用页面导航,所以您无法查看更多帖子
    • 编辑 category.php 页面并添加这两行。 &lt;?php next_posts_link('Older Posts'); ?&gt; &lt;?php previous_posts_link('Newer Posts'); ?&gt; 我编辑了我的答案并添加了页面导航代码。将该代码添加到functions.php而不是在任何地方使用。
    【解决方案2】:

    在news_page_loop文件中,endif语句后,提一下,

    <?php wp_reset_query(); ?>
    

    恢复原始主查询的语句。

    上面的文件中不需要写“查看更多帖子”部分代码。

    您可以使用以下代码,而不是在主页上,

    <?php query_posts( 'cat=5&posts_per_page=4' ); ?>
    <?php get_template_part('news_page_loop'); ?>
    <?php  $category_link = get_category_link(5); ?>
    <a href="<?php echo esc_url( $category_link ); ?>" title="Category Name">View More Posts</a>
    

    生成的“new_page_loop.php”将包含以下代码,

    <div class="single_post">
        <?php if(have_posts()) : ?><?php while(have_posts())  : the_post(); ?>
        <div class="cat-page-thumb-title">
            <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail('home-cat-image', array('class' => 'cat-thumb')); ?>
            </a> 
            <p class="catpage-post-title">
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </p>
            <p class="post-meta">
                <?php comments_popup_link('0 Reviews', '1 Review', '% Reviews'); ?>
            </p>
        </div><!--cat page thumb and title-->
        <?php endwhile; ?>
        <?php endif; ?>
    <?php wp_reset_query(); ?>
    </div><!--single post-->
    

    参考链接:http://codex.wordpress.org/Function_Reference/get_category_link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多