0
votes

Agh this is frustrating. I've been searching for hours on how to do this.

I am creating a WP template that pulls in queries of a category on the page.

My question is how can I make the category in the array dynamically load, whether by slug or whatever, based entirely on what the page title is?

The fancy piece is that it will be three sections on the page via an additional category that also need to be loaded in the template, but those are not dynamic.

So if I can break this down:

Section 1 - TitleCat + Section1Cat

Then

Section 2 - TitleCat + Section2Cat

Then

Section 3 - TitleCat + Section3Cat

That way during content creation, my team can simply click two categories, and the website will automatically put the post excerpts where it needs to go!

It needs to be dynamic so I don't need to build a template for each of the 31 categories the posts are divided into.

EDIT: This is how I was approaching it using ACF. That array can be simply switched out for categories if that's the way to go. Same concept.

<?php $pagecat = $post->post_title; //this copies the page title the page title ?>

<?php 

// args
$args = array(
  'posts_per_page' => 4,
  'meta_query' => array(
    'relation' => 'AND',
    array(
      'key' => 'page',
      'value' => $pagecat,
      'compare' => '='
    ),
    array(
      'key' => 'section',
      'value' => 'Second String',
      'compare' => '='
    )
  )
);

$the_query = new WP_Query( $args ); 

?>
1
what did you try ? show some code - Prince Singh

1 Answers

0
votes

On any custom / template page you can use this many times over, for example on home page you want to show samples of articles in many different categories:

Code Sample

<!-- Create query -->
<?php  query_posts( array ( 'category_name' => 'category-slug-goes-here', posts_per_page' => 1 ) ); ?>

      <?php // The Loop
      while ( have_posts() ) : the_post();?>

      // CODE THAT YOU WANT HERE 

      <?php endwhile; ?>

// IMPORTANT RESET QUERY 
<?php // Reset Query
 wp_reset_query(); ?>

Now you can do this OVER and OVER again as many times as you need on one template...

Now for displaying your SINGLE article for some category you can do this in a "Single.php" page use http://www.wordpress.org website for more references on this..

Here is the code for single.php page you need an IF statement and you can target your specific categories that you want to display a specific way...

<?php if(have_posts() && (in_category('category-name-here') || in_category('category-name-here-2') || in_category('category-name-here-3') )) : while(have_posts()) : the_post(); ?>

// NOW HERE use HTML to format the article for those categories. 

Now you said you have 30+ different categories I assume you want different displays..

Now you will use that same IF statement over and over again and change the HTML format code in the middle. So you will have in the end ALL page layouts inside 1 page and you will target them by category-name

Best of luck!