Support » Alpha/Beta/RC » Tags for pages

  • I am testing out WP 3.0 for my new site and I am running into an issue. On the production site(WPMU 2.8.6a), I am using a plugin that allows me to apply tags to a page. I then use a query that finds all posts with the same tag as the page and lists them out on the page. You can see an example at the bottom of this page. In testing the 3.0 beta, the plugin I used before does not work in 3.0. Is that because I am supposed to be using a custom post type to create the editing page to allow tags or is there a “better” way I should be drawing the list of tagged posts for a particular page?


Viewing 6 replies - 1 through 6 (of 6 total)
  • What i’d suggest, since you’re using 3.0, is to register a new taxonomy called something similar to tags, but register to both pages and posts, so you could add tags to both the page and the posts..

    You’d then have the page template fetch the page tags, then grab all posts that have those tags.

    I’ll give you an example, just gimme 10 mins or so to test some code..

    Actually i’ve found a better way, just add post tag support to pages… 🙂

    Bear with me a few more minutes and i’ll have an example.. 🙂

    Firstly, register the post tag taxonomy for pages like so..

    add_action( 'init', 'register_tags_for_pages', 0 );
    function register_tags_for_pages() {
    	register_taxonomy_for_object_type( 'post_tag', 'page' );

    Then update your page template to something along these lines…

    Template Name: Show posts with page's tags
    // Intercept the $post var now and get tags
    $page_tags = wp_get_post_terms( $post->ID , 'post_tag', array( 'fields' => 'ids' ) );
    // If $page_tags is not empty, we have tags
    if( !empty( $page_tags ) )
    	// Now we have tags, we re-define the main query to look for posts
    		// Array of parameters for query
    			// Sets up paging
    			'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
    			// Gets only posts that have one of the tags found in the page
    			'tag__in' => $page_tags,
    			// Make sure we fetch posts
    			'post_type' => 'post',
    			// And that they're published
    			'post_status' => 'publish',
    			// Set the posts per page (you can remove or adjust this as appropriate
    			'posts_per_page' => 10
    // Most basic of loop examples, adjust this as necessary
    if( have_posts() ) :
    	while( have_posts() ) : the_post();
    		echo '<br />';

    Works here, so let me know if you have any problems… 😉

    Thanks! This worked. It’s funny that the plugin I was using to do this was just updated to work in 3.0. Now I don’t know if your way or the old way is better. I guess it’s the same thing. The one difference is in my code. I just have to detect if the tagged page is a subpage of the parent and then there is no need to remember to select the proper page template.

    <?php if ($post->post_parent=="24")?>
    	<?php  //for use in the loop, list 60 post titles related to first tag on current post
      $backup = $post;  // backup the current object
      $tags = wp_get_post_tags($post->ID);
      $tagIDs = array();
      if ($tags) {
        $tagcount = count($tags);
        for ($i = 0; $i < $tagcount; $i++) {
          $tagIDs[$i] = $tags[$i]->term_id;
          'tag__in' => $tagIDs,
          'post__not_in' => array($post->ID),
    	<h1 class="entry-title"> Player posts </h1>
    	$my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <li><?php the_time('m-d-Y'); ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?> </a></li>
          <?php endwhile;
        } else { ?>
          <li>No related posts found.</li>
        <?php }
      $post = $backup;  // copy it back
      wp_reset_query(); // to use the original query again

    Any opinions?
    Thanks for the help.

    Use whatever suits you best.. 🙂

    NOTE: showposts is deprecated in favour of posts_per_page (anything you pass to showposts will get moved to posts_per_page anyway).

    The code i supplied is a little more refined and faster then what you have, but unless you’re running an extremely busy site i very much doubt you’ll notice any difference.

    NOTE: What i’ve supplied pretty much does what you have above.

    The one difference is in my code. I just have to detect if the tagged page is a subpage of the parent and then there is no need to remember to select the proper page template.

    Not sure i quite follow what you mean. Do subpages inherit the parent’s page template?

    Thanks for the note about showposts. I’ll be updating that code 🙂

    I only have 2 templates. One for showing page comments and the default page template. I have one parent page for the roster(post_parent=24) and all player’s pages are sub pages of the roster. And since all the new pages use the default template, I don’t have to change anything.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Tags for pages’ is closed to new replies.