WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] the_tags() in a Page Template

[Resolved] the_tags() in a Page Template

  • I made a page template. Inside the page template is a custom query to grab a random post. I make a page, I used that page template and everything is great apart from the_tags() template tag breaking. I imagine this is because it’s a page, which doesn’t use tags and not a “post” page.

    This is the custom loop code:

    <?php
    $rand_id = $wpdb->get_var(“SELECT ID FROM $wpdb->posts WHERE post_status = ‘publish’ AND post_type = ‘post’ ORDER BY RAND() LIMIT 1”);
    $my_query = new WP_Query(“p=$rand_id”);
    ?>

    The single random post grabbing is working fine, it’s just the the_tags() I need to work. Maybe there’s another way to query that’ll let me use the_tags().

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Seems that a custom query created through the WP_Query class is not seen as a post loop (which the_tags() tests for), even though the same would work with query_posts(). Possibly a bug.

    Anyway, try adding this right after your $my_query line:

    $wp_query->in_the_loop = true;

    Kafkaesqui, thanks! That line worked like a charm!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] the_tags() in a Page Template’ is closed to new replies.