WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Put Slug into Variable (3 posts)

  1. codecottage
    Member
    Posted 6 years ago #

    I'd like to use the information in my slugs in one of my templates. I'd like to parse it and use it in some headings.

    What do I need to do to put it into a variable? I've got a nice query working and it's pulling back the proper data but how do I get at the slug data? I'm new to php/mySQL so it's probably a matter of some simple syntax. I got this nice syntax from this site and modified it.

    <?php
    $querystr ="
      SELECT $wpdb->posts.* FROM $wpdb->posts
      LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
      WHERE $wpdb->postmeta.meta_key = 'calendar'
      AND $wpdb->postmeta.meta_value = 'yes'
      ORDER BY $wpdb->posts.menu_order ASC";
    
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    
     if ($pageposts):
        foreach ($pageposts as $post):
      		setup_postdata($post); ?>
        	<strong><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
        	<?php the_title(); ?></a></strong><br />
      		<?php the_content(__('(more...)')); ?>
      <?php endforeach; ?>
      <?php else : ?>
          <h2> Not Found</h2>
    <?php endif; ?>

    ---------------------
    TIA!

    Codecottage

  2. Kafkaesqui

    Posted 6 years ago #

    In the $post object the 'slug' would be available through:

    $post->post_name

    post_name is also the name of the column that holds the slug in the posts table.

  3. codecottage
    Member
    Posted 6 years ago #

    Thank you, Kafkaesqui! It worked great!

    Codecottage

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags