Forum Replies Created

Viewing 1 replies (of 1 total)
  • I would like to second this thank you. This solution has also worked perfectly for me, so far. Now I’m wondering if I can make this more efficient…

    Using page name variable to show a category of posts

    I also dynamic sidebar that changes depending on the page. I’m creating categories so that there is a category name that corresponds with each page slug, and then using “if is_page” to show the posts in the related category.

    For example:
    page slug = about
    post category name = about

    Since I have over 20 pages, I would rather not have 20+ if then statements. I would rather figure out a way to have one if-then statement that places the current page slug into both the the IF IS_PAGE, part of the statement and the new WP_Query(“category_name=about”).

    I am new to php and database programming as a whole, but it seems like there would be a logical way to do this by creating a variable and putting it in both parts of the query. I’ve seen a few posts on the forum that are along the same lines, but I can’t figure it out.

    Basically, automate the if then statement below, so that I only write it once:

    if (is_HOME()) {
    
     $my_query = new WP_Query("category_name=HOME");
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID;
     the_title();
      the_content('More...');
       endwhile;
    }   elseif (is_page()) {
    
            if (is_page('ABOUT')) {
    
     $my_query = new WP_Query("category_name=ABOUT");
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID;
    the_title('<h3>', '</h3>');
      the_content('Read more about this client & others&raquo;');
       endwhile;

    etc…

    Thanks in advance for your help!

Viewing 1 replies (of 1 total)