• I wanted to query posts in a page template that were associated with each specific page that loaded the template. Started by going down the path of trying to create page categories to match up with post categories. This seemed easier, creating a post category that matches a page title and querying that category. Played around for awhile, thought this might save someone some time so I’m sharing.

    <?php $titleCat = get_the_title();
    $pageCat = get_cat_ID($titleCat);
    $pagePosts = new WP_Query( array( 
        'cat' => $pageCat
      )); ?>
    
Viewing 1 replies (of 1 total)
  • Thread Starter djmiller78

    (@djmiller78)

    Aaaand, to get back to the page from the post:

    <?php 
        $catArray = get_the_category();
        $firstCat = $catArray[0];
        $catName = $firstCat->name; ?>
    <a href="<?php echo site_url(); echo "/"; echo $catName;?>"> <?php echo $catName; echo " page";?> 
    </a>
Viewing 1 replies (of 1 total)
  • The topic ‘query post category based on page title’ is closed to new replies.