最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Category Page Displaying all Pages

programmeradmin5浏览0评论

My page here: /

is displaying all pages on the site, not just the ones that are associated with this 'Selma' category. Which piece am i missing? Should I be using WP_query instead? I'd like to have this dynamic so that all of category pages show the correct pages associated with their cats.

Putting this in my archive.php code:

<ul>

   <?php
        $cat = get_query_var('cat');
        $PozCat = get_category ($this_category);
        $PozCat->id;
        query_posts('&post_type=page”.'.$PozCat->id);
        if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <li><a href="<?php the_permalink();?>"><?php the_title(); ?></a></li>
        <p> <?php the_excerpt()?> </p>
    <?php endwhile; endif; ?>
</ul>

My page here: https://blackfreedom.proquest/category/civil-rights-and-black-power-movements/activism/selma-demonstrations-and-marches/

is displaying all pages on the site, not just the ones that are associated with this 'Selma' category. Which piece am i missing? Should I be using WP_query instead? I'd like to have this dynamic so that all of category pages show the correct pages associated with their cats.

Putting this in my archive.php code:

<ul>

   <?php
        $cat = get_query_var('cat');
        $PozCat = get_category ($this_category);
        $PozCat->id;
        query_posts('&post_type=page”.'.$PozCat->id);
        if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <li><a href="<?php the_permalink();?>"><?php the_title(); ?></a></li>
        <p> <?php the_excerpt()?> </p>
    <?php endwhile; endif; ?>
</ul>
Share Improve this question edited Oct 2, 2020 at 14:42 Joe Kalucki asked Oct 2, 2020 at 14:10 Joe KaluckiJoe Kalucki 11 bronze badge 2
  • $this_category is not defined; try working with: $PozCat = get_category ($cat); – Michael Commented Oct 3, 2020 at 1:58
  • Interesting. It's now showing the pages, but only ones that are not associated with the current category. – Joe Kalucki Commented Oct 3, 2020 at 22:22
Add a comment  | 

1 Answer 1

Reset to default 0

Resolved using this:

<div>
            <ul>
            
            <?php 
            $current_category = get_queried_object(); ////getting current category
$args = array(
        'post_type' => 'page',// your post type,
        'orderby' => 'menu_order',
        'order' => 'DESC',
        'cat' => $current_category->cat_ID // current category ID
);
$the_query = new WP_Query($args);
if($the_query->have_posts()):
   while($the_query->have_posts()): $the_query->the_post();
    echo '<h2><a href="'.get_the_permalink().'">'.get_the_title().'</a></h2>';
    "<p>".the_excerpt()."</p>";
endwhile;
endif;
            ?>
            </ul>
                </div>
发布评论

评论列表(0)

  1. 暂无评论