• Hi. I want to show 4 posts that tagged with “test”.
    I used this code:

    <ul>
    
    <?php
    
    // The Query
    $the_query = new WP_Query( 'showposts=4&tag=test' );
    
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
    	echo '<li>';
    	the_title();
    	echo '</li>';
    endwhile;
    
    // Reset Post Data
    wp_reset_postdata();
    
    ?>
    
    <?php while (have_posts()) : the_post(); ?>
    <li><a href="<?php echo get_permalink(); ?>" target="_blank" rel="nofollow" alt="<?php echo mb_substr(the_title('', '', FALSE), 0, 55); ?> " title=" <?php echo mb_substr(the_title('', '', FALSE), 0, 55); ?>" >
    					<?php echo mb_substr(the_title('', '', FALSE), 0, 55); ?></a><span><?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?></span></li>
    <?php endwhile;?><?php wp_reset_query(); ?>
    		        </ul>

    But it shows all posts that 4 first posts, has not any link and style!
    What I do?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Sorry, but I do not understand the problem. Can you explain more, and give a link to a page where the problem can be seen?

    Thread Starter poyagh

    (@poyagh)

    I want to show 4 posts that tagged with “test” by their links.
    I used they codes.
    What is the problems of my code?
    If it is minus, please give me right codes.

    You have 2 loops in the code. Do you only want one loop? Please post a link to a page where the problem can be seen.

    Thread Starter poyagh

    (@poyagh)

    yes I want one loop, but I dont know how…!
    I haven’t an online adress.
    It shows all posts tagged with “test” that 4 first posts havent any link. (but other have their links)

    The first loop shows 4 posts tagged with ‘test’. The second loop shows a different set of posts with links and other information.

    Do you want to keep the second loop?

    Sorry for all the questions, I just want to be sure I know exactly what you want. Please try this code to replace what you showed:

    <ul>
    
    <?php
    global $wp_query;
    query_posts(
       array_merge(
          $wp_query->query,
          array(
             'tag' => 'test',
             'posts_per_page' => 4,
          )
       )
    );
    while (have_posts()) :
       the_post(); ?>
       <li>
          <a href="<?php echo get_permalink(); ?>" target="_blank" rel="nofollow" alt="<?php echo mb_substr(the_title('', '', FALSE), 0, 55); ?> " title=" <?php echo mb_substr(the_title('', '', FALSE), 0, 55); ?>" >
             <?php echo mb_substr(the_title('', '', FALSE), 0, 55); ?>
          </a>
          <span><?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?></span>
       </li>
    <?php endwhile;?>
    <?php wp_reset_query(); ?>
    </ul>
    Thread Starter poyagh

    (@poyagh)

    Thank you
    It worked

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to show posts tagged by …’ is closed to new replies.