Wordpress按特定类别创建博客帖子

时间:2015-08-12 21:22:44

标签: php mysql wordpress

我正在博客上工作,目前当我加载初始博客页面时,所有帖子都会正确显示。但是,每当我使用侧边栏导航转到另一个页面时,我的查询就会变得不正确,我无法显示任何帖子。

这是我的博客索引页面:

<div class="single-page-bg">
    <div class="row">
        <div class="large-12 columns">
            <div class="main-copy-sidebar">
                <?php get_sidebar('news'); ?>
            </div>
            <div class="main-copy-container">
            <h1 class="news-header">NEWS</h1>
            <div>
                <?php
                    if ( have_posts() ) : 
                        while ( have_posts() ) : the_post();
                            echo "have posts";
                            get_template_part( 'content', get_post_format() );
                        endwhile;
                    else :
                        echo "no posts";
                        get_template_part( 'content', 'none' );
                    endif;
                ?>
                </div>
            </div><!-- /.main-copy-container -->
    </div>
</div>

这是我的侧边栏导航:

<a class="sidebar-trigger" data-action="sidebar-trigger"><span class="label">RELATED CONTENT</span><span class="arrow white-arrow-down"></span>    </a>
<div class="sidebar-accordion"  data-active="0">
  <div class="sidebar-content">
        <div class="page-sidebar">

            <div class="sidebar-header">
                <h3>CATEGORIES</h3>
            </div>
            <div class="sidebar-body">
                <?php 

                $cats = get_categories(); 
                foreach($cats as $cat){
                $category_id = get_cat_ID( $cat->slug );
                $category_link = get_category_link ($category_id);
                echo "<a href=\"".$category_link."\" class=\"btn btn-subnav-item btn-narrow-container\">".$cat->name."</a>";
                }
                ?>
            </div>
        </div>
    </div>
</div><!-- /.page-sidebar -->
<div class="sidebar-buttons"></div><!-- /.sidebar-buttons -->

这是我加载内容的地方:

<article class="main-copy news-article">
        <?php 
            $uri = $_SERVER["REQUEST_URI"]; // Get the URI
            $uri_array = split("/",$uri); // Split the URI via /'s
            $uri_url = $uri_array[2]; // might need to change this to [1]
            $uri_category = $uri_array[3]; // Get the category to sort by
        ?>

        <?php
        // The Query
            $query = new WP_Query( array(
                'taxonomy' => '$uri_category',
                'order' => 'DESC'
            ));

            if ( $query->have_posts() ) {
                echo "true";
            while ( $query->have_posts() ) {
                $query->the_post();?>
                <?php the_excerpt(); ?>
            <?php }
        } else {
            // no posts found
        }
        ?>

        <?php if ($uri_url = "category" && $uri_category != NULL) { 
            //Testing my uri request
        } ?>

    <?php
        if ( is_single() ) :
            the_title( '<h2 class="entry-title">', '</h2>' );
        else :
            the_title( '<h2 class="entry-title outside"><a href="' . convert_reno_link(esc_url( get_permalink() )) . '" rel="bookmark">', '</a></h2>' );
        endif;
        ?>
        <div class="meta">
            <p class="meta-date"><time><?php the_date('F j, Y'); ?></time></p>
        </div>
    <?php 



        // the_content(); 
        $image_id = get_field('featured_image');
      $xsmall_breakpoint = wp_get_attachment_image_src( $image_id, 'xsmall_breakpoint' );
      $args = array(
        'images' => array(
          array(
            'src' =>$xsmall_breakpoint[0],
         )
        ),
        'fallback_src' => $xsmall_breakpoint[0],
        'class' => $class
      );

        if ( is_single() ) {
          writePicturefill($args);
            the_content();
        } else {

            if($image_id){
            ?>
            <div class="article-image">
                <?php
                  writePicturefill($args);
                ?>
            </div><div class="article-body">
            <?php } ?>
            <?php
                $excerpt = get_the_excerpt();
                echo $excerpt;
                echo "&nbsp;<a href=\"".convert_reno_link(esc_url( get_permalink() ))."\">Learn more</a>";
            if($image_id){ ?></div><?php }
        }
    ?>  
</article><!-- /.main-copy -->

就像我说的那样,初始博客页面加载在我的索引文件中,其中有帖子,内容页面正常工作。但是,一旦我点击侧边栏导航,它就会转到新页面,我相信查询已更改,并且不会返回任何帖子。我的侧边栏导航基本上是可用类别的列表。从第三位代码中删除的最重要部分是我尝试使用查询本身的最高位。下面是我如何使用返回值输出内容。

tl; dr:如何让这些类别页面返回包含帖子的查询?

对JudeRosario的回应。这是我的内容与wp_reset_query()。

<?php
        // The Query
            $query = new WP_Query( array(
                'taxonomy' => '$uri_category',
                'order' => 'DESC'
            ));

            if ( $query->have_posts() ) {
                // echo "true";
                while ( $query->have_posts() ) {
                    $query->the_post();?>
                    <!-- <?php the_excerpt(); ?> -->
                <?php }
            } else {
                // no posts found
            }
            wp_reset_query();
        ?>

这里是我的侧边栏,附带一个wp_reset_query():

<div class="sidebar-body">
                <?php 

                $cats = get_categories(); 
                foreach($cats as $cat){
                $category_id = get_cat_ID( $cat->slug );
                $category_link = get_category_link ($category_id);
                echo "<a href=\"".$category_link."\" class=\"btn btn-subnav-item btn-narrow-container\">".$cat->name."</a>";
                }
                wp_reset_query();
                ?>
          </div>

我现在在我的类别页面上显示一个帖子。但是,它是每个页面上的一个帖子,并且每页都是相同的帖子。

0 个答案:

没有答案
相关问题