Forums

Setting up sidebar list of entries with the same tag (13 posts)

  1. twgrundy
    Member
    Posted 3 weeks ago #

    Hi all...

    I'm out of my depth somewhat, having spent 8 hours today adapting the Polaroid theme in a trial-and-error effort to move my traditional website from here: http://globalcitizen.co.uk/ to the new WP version here: http://globalcitizen.co.uk/wp

    As you can see, there are a number of sections on the sidebar. I have the 'recent entries' section working via a plugin, and my archive is working via some code involving "?php list_cats". Currently, the other sections are simply link categories.

    What I'm hoping to do is list all my entries tagged with the word "travel" under the travel sidebar section, and all my entries tagged with the word "adventure" under the adventure section etc..etc.. Is there some code or a plug-in which will fetch entries with a specific tag?

    Many thanks in advance!
    Tom.

  2. Rhand
    Member
    Posted 3 weeks ago #

    This WP Codex page describes something I think you can use link to a page that loads all entries tagged with a certain tag: http://codex.wordpress.org/Function_Reference/get_tag_link

  3. twgrundy
    Member
    Posted 3 weeks ago #

    Wonderful thanks!! That seems to be what I'm looking for.

    I wonder if anyone can guide me as to how I can insert this into my sidebar?

    The other sections have a piece of code like this:
    <?php get_links(4, '<li>', '</li>','', false, 'rand', false,false); ?>
    in order to show a category of links...

    I imagine I would need a variation of "get_tag_link" to make what I'm trying to do work... However, simply replacing 'get_links' with 'get_tag_link' didn't work - and I'm unsure as to where I should specify the tags I wish to bring down.

    Many thanks for your patience! :-)
    T

  4. MichaelH
    moderator
    Posted 3 weeks ago #

    You could put this code in a sidebar Template or in a widget using Otto's PHP Code Widget:

    <?php
    //display 5 posts for category IDs 1 and 3
    foreach( array(1, 3,) as $cat )  {
      $category=get_category($cat);
      $args=array(
        'cat' => $category->cat_ID,
        'showposts'=>5,
        'caller_get_posts'=>1
       );
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo '<h2>Posts from '. $category->name . '</h2>';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
         <?php
        endwhile;
      } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //foreach
    ?>
  5. MichaelH
    moderator
    Posted 3 weeks ago #

    Oops, forgot you are looking for tags...will have to get back to you on that.

    Also for links use the template tag, wp_list_bookmarks().

    Both get_links and list_cats are deprecated.

  6. twgrundy
    Member
    Posted 3 weeks ago #

    Holy crap - I think this is beyond my copy-past-hopeforthebest approach to code. I'm somehow hoping for a few lines I can paste in the existing sidebar.php which came with the Polaroid theme...

    I've thought of an elaborate workaround using pages and simply creating lists of links, but it feels messy.

    Any further tips for this n00b most appreciated...
    T =o)

  7. MichaelH
    moderator
    Posted 3 weeks ago #

    Post this in a sidebar or PHP code widget

    <?php
    //display 5 posts for post_tag IDs 5 and 12
    $taxonomy = 'post_tag';
    $taxargs ='include=5,12';
    $terms = get_terms( $taxonomy, $taxargs );
    foreach( $terms as $term )  {
      $args=array(
        'tag__in' => array($term->term_id),
        'showposts'=> 5,
        'caller_get_posts'=>1
       );
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo '<h2>Posts from '. $term->name . '</h2>';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
         <?php
        endwhile;
      } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //foreach
    ?>
  8. twgrundy
    Member
    Posted 3 weeks ago #

    Thanks Michael! Would you be able to tell me please how I specify the tag whose entries I'm hoping to list. Also, will this code list ALL of the posts containing the said tag?
    Many many thanks!! T

  9. MichaelH
    moderator
    Posted 3 weeks ago #

    Change this to be the tags you want. To find tag id hover over tag in Name column of Posts->Post Tags and look at bottom of browser to see the ID.
    For tag ids 17,15,3

    $taxargs ='include=5,12';

    to

    $taxargs ='include=17,15,3';

    To list ALL posts, change

    'showposts'=> 5,

    to

    'showposts'=> -1,
  10. twgrundy
    Member
    Posted 3 weeks ago #

    BRILLIANT - cheers... Will give this a whirl in the morning, as it's bedtime here in Hong Kong.
    Thanks for your help!
    T =:O)

  11. twgrundy
    Member
    Posted 3 weeks ago #

    Took me a good half hour to work out the difference - or lack of - between tags and categories on Wordpress, but it's worked great in the end, thanks!

    All I need to do now is unify the style, as I seem to have lost the formatting by pasting in that code... http://globalcitizen.co.uk/wp Currently, the other sidebar sections are stylised using what I believe to be this code:

    <ul class="comments">
    <?php get_links(4, '<li>', '</li>','', false, 'rand', false,false); ?>

    I'm just not sure which part of it I need to transplant into the new code you gave - and where it should be pasted - in order to maintain the font/size/padding.

    Cheers once more for all your help... T

  12. twgrundy
    Member
    Posted 3 weeks ago #

    or do I need to like it to the style sheet somehow?
    Thanks... T

  13. twgrundy
    Member
    Posted 3 weeks ago #

    (bump!)

Reply

You must log in to post.

About this Topic