• Resolved benjoschen

    (@benjoschen)


    I have been trying to find syntax to express:
    If tag = pagename, then do some stuff.

    I have named my pages so that each pagename should have a corresponding tag that is the same. For example, john-smith.php has a pagename of john-smith and I have created a tag with the john-smith id. When john-smith.php is the active page, I want to display all posts tagged “john-smith.” Since I am working with a template, I can’t use specific names. It seems like I need to get the news for the active page by comparing the active pagename to the tag.

    Thanks for any help you can provide.

Viewing 5 replies - 1 through 5 (of 5 total)
  • https://codex.www.ads-software.com/Template_Hierarchy#Tag_display

    You can create a template page .. with your example of john-smith as being the tag, you can create a file

    tag-john-smith.php and put a loop in there and it will display all of the posts that have that tag in it

    Thread Starter benjoschen

    (@benjoschen)

    Thanks. I’m not sure I understand what you are proposing.
    I have created a page template for employee profiles called profile.php. Each employee will have a profile page utilizing the profile.php template.
    I would like to display on each employee’s profile all news posts that are tagged with their name. The tag slugs/id and the pagenames are identical. For example, the tag slug for John Smith is john-smith. The pagename for John Smith is also john-smith. Presumably, I could create an if statement that is something like: if (tag == pagename){display the news;}. I know how to make the news display, I just don’t know how to express tag == pagename in my if statement.

    Should I be changing the permalinks for each employee’s profile to /employees/tag-firstname-lastname?

    How are you retrieving the $pagename ? how are you getting the value that fills that variable?

    Thread Starter benjoschen

    (@benjoschen)

    I just had a breakthrough.
    if ($tag-slug == $pagename) {do some stuff;} works. Now I am just working through displaying the posts with $args=array() and WP_Query.
    Almost there!

    Thanks for taking the time to try to help me. I really appreciate it.

    Thread Starter benjoschen

    (@benjoschen)

    Below is the final markup, if you or anyone else is curious. Seems to work perfectly:

    <?php
    $args=array(
    'category_name'=>'news',
    'tag'=>$pagename,
    'showposts'=>3,
    'caller_get_posts'=>1
    );
    
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() )
    {
    while ($my_query->have_posts()) : $my_query->the_post();
    ?>
    
    <li><a>"><?php the_title(); ?></a>
    <p><?php the_excerpt($length, $more); ?></p>
    </li>
    <?php
    endwhile;
    }
    //if ($my_query)
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘tag = pagename’ is closed to new replies.