admin管理员组

文章数量:1388903

I'm adding tags to posts. Clicking a tag takes you to an overview listing of all posts for that tag. I created this listing by adding template "tag.php", which includes:

$tag = get_queried_object();
$currentSlug = $tag->slug ;
$args = array(
    'tag'                   =>  $currentSlug,
    'post_type'             =>  array( 'post' ),
    'posts_per_page'        =>  '6',
);
$recentBlog = new WP_Query( $args ); 

This works and produces a listing for a slug like /tag/subject01. So far so good. Next thing that I want is to add some content blocks to the tag listing page, like a title and intro text. This should be a normal wordpress page that can be edited by content authors. So I created a new page with slug /tag/. For this page, I can't select the tag.php template. So I copied template tag.php to page-tag.php, and the new page /tag/ is using template page-tag.php. The problem that I have is that the new page will only show a list of all posts that have been tagged, regardless of which tag it is. When I go to tag/subject01, the page /tag/ is not showing with my additional content; it just shows the list from the template tag.php. So my question is, how can I make the listing page show the content for tag/subject01, tag/subject02, etc? Thanks!

  • 03 26 2020 - Adding page php:

=== PAGE TEMPLATE ===

<?php
/**
 *  Theme:      MyTheme
 *  Template:           page-tag.php
 *  Template name:      Tag
 *  Description:    Tag post overview template
 */

get_header();

?>

<main id="main" role="main">
        <?php 
            // Insert tag post overview 
            get_template_part('includes/tag-overview-result'); 
        ?>
</main>

<?php
get_footer();
?>

<?php
/**
 *  Theme:      MyTheme
 *  Template:           page-tag.php
 *  Template name:      Tag
 *  Description:    Tag post overview template
 */

get_header();

?>

<main id="main" role="main">
        <?php 
            // Insert tag post overview 
            get_template_part('includes/tag-overview-result'); 
        ?>
</main>

<?php
get_footer();
?>

=== INCLUDE FILE ===

<?php
/**
 *  Theme:      MyTheme
 *  Template:       tag-overview-result.php
 *  Description:    Tag post Ajax results
 */


$tag = get_queried_object();
$currentSlug = $tag->slug ;
$args = array(
    'tag'               =>  $currentSlug,
    'post_type'             =>  array( 'post' ),
    'posts_per_page'        =>  '9'
);

$recentBlog = new WP_Query( $args ); 
?>
<?php if ( $recentBlog->have_posts() ) { ?>
    <?php while ( $recentBlog->have_posts() ) { $recentBlog->the_post(); ?>
        <div class="post__inner-content">

            <?php the_excerpt(); ?>         
            <a class="button blue-border post-button" href="<?php the_permalink(); ?>" title="<?php _e('Learn more','control'); ?>"><?php _e('Learn more','control'); ?></a>        
    <?php } ?>
<?php } wp_reset_postdata(); ?>

I'm adding tags to posts. Clicking a tag takes you to an overview listing of all posts for that tag. I created this listing by adding template "tag.php", which includes:

$tag = get_queried_object();
$currentSlug = $tag->slug ;
$args = array(
    'tag'                   =>  $currentSlug,
    'post_type'             =>  array( 'post' ),
    'posts_per_page'        =>  '6',
);
$recentBlog = new WP_Query( $args ); 

This works and produces a listing for a slug like /tag/subject01. So far so good. Next thing that I want is to add some content blocks to the tag listing page, like a title and intro text. This should be a normal wordpress page that can be edited by content authors. So I created a new page with slug /tag/. For this page, I can't select the tag.php template. So I copied template tag.php to page-tag.php, and the new page /tag/ is using template page-tag.php. The problem that I have is that the new page will only show a list of all posts that have been tagged, regardless of which tag it is. When I go to tag/subject01, the page /tag/ is not showing with my additional content; it just shows the list from the template tag.php. So my question is, how can I make the listing page show the content for tag/subject01, tag/subject02, etc? Thanks!

  • 03 26 2020 - Adding page php:

=== PAGE TEMPLATE ===

<?php
/**
 *  Theme:      MyTheme
 *  Template:           page-tag.php
 *  Template name:      Tag
 *  Description:    Tag post overview template
 */

get_header();

?>

<main id="main" role="main">
        <?php 
            // Insert tag post overview 
            get_template_part('includes/tag-overview-result'); 
        ?>
</main>

<?php
get_footer();
?>

<?php
/**
 *  Theme:      MyTheme
 *  Template:           page-tag.php
 *  Template name:      Tag
 *  Description:    Tag post overview template
 */

get_header();

?>

<main id="main" role="main">
        <?php 
            // Insert tag post overview 
            get_template_part('includes/tag-overview-result'); 
        ?>
</main>

<?php
get_footer();
?>

=== INCLUDE FILE ===

<?php
/**
 *  Theme:      MyTheme
 *  Template:       tag-overview-result.php
 *  Description:    Tag post Ajax results
 */


$tag = get_queried_object();
$currentSlug = $tag->slug ;
$args = array(
    'tag'               =>  $currentSlug,
    'post_type'             =>  array( 'post' ),
    'posts_per_page'        =>  '9'
);

$recentBlog = new WP_Query( $args ); 
?>
<?php if ( $recentBlog->have_posts() ) { ?>
    <?php while ( $recentBlog->have_posts() ) { $recentBlog->the_post(); ?>
        <div class="post__inner-content">

            <?php the_excerpt(); ?>         
            <a class="button blue-border post-button" href="<?php the_permalink(); ?>" title="<?php _e('Learn more','control'); ?>"><?php _e('Learn more','control'); ?></a>        
    <?php } ?>
<?php } wp_reset_postdata(); ?>
Share Improve this question edited Mar 26, 2020 at 14:05 ams asked Mar 25, 2020 at 21:42 amsams 112 bronze badges 5
  • Can you paster the full page of the page and more clear what you trying to do? – BenB Commented Mar 26, 2020 at 1:12
  • Hi @BenB , thanks for your reply. I added the full php above. I am trying to do this: - create a tag landing page with that lists all posts for a specific tag. For example; /tag/dogs - this needs to be a page with additional copy that can be edited - so in Wordpress, I want the "edit page" button to show The php above will fetch all tagged posts, but not just the posts tagged as 'dog'. – ams Commented Mar 26, 2020 at 14:11
  • Why not use the tad description field or add extra fields to the tad and then those fields? – BenB Commented Mar 26, 2020 at 21:42
  • @BenB that works for me, thank you. I'm using the tag name and tag description in my template now. – ams Commented Mar 27, 2020 at 15:30
  • happy it helped. I update a full answer in case someone will run into similar issue – BenB Commented Mar 27, 2020 at 23:06
Add a comment  | 

1 Answer 1

Reset to default 0

You can use the function tag_description to get the tag description in the tag page instead of creating a post to store the tag description

Link to docs: https://developer.wordpress/reference/functions/tag_description/

本文标签: templatesHow to make tag post listing page working