php - How can I change this loop so that it displays posts from the current tag

423

How can I change this loop so that it displays posts from the current tag. Now it displays all posts. I need this to make the page tag.php

<?php
$current_page = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$args = array(
    'posts_per_page' => 7, 
    'paged'          => $current_page 
    );
query_posts($args);

$wp_query->is_archive = true;
$wp_query->is_home = false;

while(have_posts()): the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <div class="post_headline">
     <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
 </div>
 <?php
 endwhile;
 if (function_exists('custom_pagination')) {
    custom_pagination($query->max_num_pages,"",$paged);
}
?>
946

Answer

Solution:

Hi we need to pass tag name to the query post argument list, so I just modified the code, please try below code and let me know if any issues,

<?php
$current_page = (get_query_var('paged')) ? get_query_var('paged') : 1; 
 $tag = get_queried_object();
$args = array(
    'posts_per_page' => 7, 
    'tag' => $tag->slug,
    'paged'          => $current_page
    );
query_posts($args);

$wp_query->is_archive = true;
$wp_query->is_home = false;

while(have_posts()): the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <div class="post_headline">
     <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
 </div>
 <?php
 endwhile;
 if (function_exists('custom_pagination')) {
    custom_pagination($query->max_num_pages,"",$paged);
}
?>

People are also looking for solutions to the problem: php - How Do I grab the User Id from database to display the profile for one single user when they are logged in?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.