WordPress.org

Ready to get started?Download WordPress

Forums

How to show posts by one specific tag, using new WP_query (4 posts)

  1. jumust
    Member
    Posted 2 years ago #

    I wanna show the last posts (2-3 posts with title, content) that have a specific tag = 'mytag'
    so I tried to put this code in a php widget:

    <?php
                $query = new WP_Query();
                $query->query('tag=mytag');
            ?>
     <?php echo get_the_title(); ?>
    <?php the_content('Read more...'); ?>

    But it gets the most recent post that also doesn't have any tag...Anyone know why?

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

  3. jumust
    Member
    Posted 2 years ago #

    I put the "usage" code and changed the $arg with mytag. The title of the right post is showing up but suddenly the background changed color!?

    <?php
    
    // The Query
    $the_query = new WP_Query( 'tag=casa' );
    
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
    	echo '<li>';
    	the_title();
    	echo '</li>';
    endwhile;
    
    // Reset Post Data
    wp_reset_postdata();
    
    ?>
  4. jumust
    Member
    Posted 2 years ago #

    Ok it was a HTML/CSS issue, I replaced the
    <li> with <p> and it's fine. Now I want to add some content beyond the title so I started with the excerpt.

    <?php
    
    // The Query
    $the_query = new WP_Query( 'tag=casa' );
    
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
    	echo '<p>';
    	the_title();
            the_excerpt();
    	echo '</p>';
    endwhile;
    
    // Reset Post Data
    wp_reset_postdata();
    
    ?>

    I need thumbnail but not sure how to get it. I know how to add everything if I have them outside the php function.
    Like I was trying to do in my firt post above. Is it possible? it will make things much easier as I'm not expert in php
    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic