Conditional WP_Query with PHP
-
Hey,
I am trying to conditionally perform a WP_Query depending on what page I am on. This is necessary because I want to pull a series of tags depending on what page I am on.
eg. The ‘Politics and Debates’ page will have posts from both the tags ‘Politics’ and ‘Debates’. My thoughts are to use this code in my page.php file:
$page = get_the_title( $page_id ); echo $page; if ($page = 'Gender & Equality') { $tag_query = new WP_Query ( 'tag=Gender,Equality,Sexuality' ); } elseif ($page ='Science & Research') { $tag_query = new WP_Query ( 'tag=Science,Research' ); } elseif ($page ='Politics & Debates') { $tag_query = new WP_Query ( 'tag=Politics,Debates' ); } elseif ($page ='Science & Environment') { $tag_query = new WP_Query ( 'tag=Science,Environment' ); } elseif ($page ='Scientific Research') { $tag_query = new WP_Query ( 'tag=Research' ); } elseif ($page ='Local Events') { $tag_query = new WP_Query ( 'tag=Events' ); } else { echo 'No posts found'; } while ( $tag_query -> have_posts() ) { $tag_query -> the_post(); echo '<div class="masonry-item"><p>' . get_the_title() . '</p></div>'; };
However, when I view any of my pages only posts from this query are loaded.
$tag_query = new WP_Query ( 'tag=Gender,Equality,Sexuality' );
Any suggestions would be appreciated.
- The topic ‘Conditional WP_Query with PHP’ is closed to new replies.