• Resolved beyondthelamppost

    (@beyondthelamppost)


    Hi 🙂

    My site has a blog and a travel guide, so I have created a custom post type called ‘entries’ to keep the guide entries separate from the blog posts. Both post types are using the same core WP categories and tags. On the tag page, however, standard WP coding only brings up the posts. I would like to load both the entries and the posts but not all mixed together. I would like to have all the entries listed first, with a heading saying Guide Entries, with the blog posts underneath following a Blog Posts heading.

    I have managed to include the entries when post_type is called using the following code in functions.php:

    function add_custom_types_to_tag_archives( $query ) {
    if( is_tag() && empty( $query->query_vars[‘suppress_filters’] ) ) {
    $post_types = array( ‘post’, ‘entries’ );
    $query->set( ‘post_type’, $post_types );
    return $query;
    }
    }
    add_filter( ‘pre_get_posts’, ‘add_custom_types_to_tag_archives’ );

    And then have managed to use the code below to get the tag.php to load only the entries.

    <?php $query = new WP_Query( ‘post_type=entries’ ); ?>
    <?php if ( $query->have_posts() ) : ?>
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
    <?php x_get_view( $stack, ‘content’, get_post_format() ); ?>
    <?php endwhile; ?>
    <?php wp_reset_postdata();?>

    That is all working fine, and I feel like I’m really close to getting it to work, but am left with 3 questions.

    #1. How can I repeat that second piece of code to only return the posts for the second part of the process? I tried just pasting it below the first code and changing the post_type to post, but that just broke the site (I’m new to PHP so don’t know how to do things and am basically copying and pasting from other support forums).

    #2. What do I need to include in the tag page to have the titles come up between the two sets of results?

    And #3. How can I limit the first piece of code to only apply to processes in the content area? I have widgets in the footer that also use post_type and they are also being affected. I’m thinking some kind of if statement relating to the div class maybe, as that is how the theme loads the page.

    Thanks for your help!!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi @beyondthelamppost,
    can you show your tag template code.

    • This reply was modified 9 years, 3 months ago by Supercoder.

    Hi @beyondthelamppost,
    you can try this code on your tag page template and please don’t forget to change test code and class from this code.
    Let me know if it not work for you.

    
    <div class='your_tag_container'>
    	<div class='your_entries_container'>
    		<h3>Entries</h3>
    		<?php 
    		$query = new WP_Query( ‘post_type=entries’, 'tag' => 'cooking'); 
    		if ( $query->have_posts() ):
    			while ( $query->have_posts() ) : $query->the_post();
    				// here your enties content will come
    			endwhile; 
    			wp_reset_postdata();
    		endif;
    		?>
    	</div>
    	<div class='your_post_container'>
    		<h3>Blog Post</h3>
    		<?php 
    		$query = new WP_Query( ‘post_type=post,'tag' => 'cooking'); 
    		if ( $query->have_posts() ):
    			while ( $query->have_posts() ) : $query->the_post();
    				// here your post content will come
    			endwhile; 
    			wp_reset_postdata();
    		endif;
    		?>
    	</div>
    </div>
    
    Thread Starter beyondthelamppost

    (@beyondthelamppost)

    Hi,

    Here is the tag code:

    <?php

    // =============================================================================
    // VIEWS/INTEGRITY/WP-INDEX.PHP
    // —————————————————————————–
    // Index page output for Integrity.
    // =============================================================================

    ?>

    <?php get_header(); ?>

    <div class=”x-container max width offset”>
    <div class=”<?php x_main_content_class(); ?>” role=”main”>

    <?php x_get_view( ‘global’, ‘_tag’ ); ?>

    </div>

    <?php get_sidebar(); ?>

    </div>

    <?php get_footer(); ?>

    And this is the _tag code (that I am editing):

    <?php

    // =============================================================================
    // VIEWS/GLOBAL/_TAG.PHP
    // —————————————————————————–
    // Includes the tag output.
    // =============================================================================

    $stack = x_get_stack();

    if ( is_home() ) :
    $style = x_get_option( ‘x_blog_style’ );
    $cols = x_get_option( ‘x_blog_masonry_columns’ );
    $condition = is_home() && $style == ‘masonry’;
    elseif ( is_archive() ) :
    $style = x_get_option( ‘x_archive_style’ );
    $cols = x_get_option( ‘x_archive_masonry_columns’ );
    $condition = is_archive() && $style == ‘masonry’;
    elseif ( is_search() ) :
    $condition = false;
    endif;

    ?>

    <?php if ( $condition ) : ?>

    <?php x_get_view( ‘global’, ‘_script’, ‘isotope-index’ ); ?>

    <div id=”x-iso-container” class=”x-iso-container x-iso-container-posts cols-<?php echo $cols; ?>”>

    <?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <?php if ( $stack != ‘ethos’ ) : ?>
    <?php x_get_view( $stack, ‘content’, get_post_format() ); ?>
    <?php else : ?>
    <?php x_ethos_entry_cover( ‘main-content’ ); ?>
    <?php endif; ?>
    <?php endwhile; ?>
    <?php else : ?>
    <?php x_get_view( ‘global’, ‘_content-none’ ); ?>
    <?php endif; ?>

    </div>

    <?php else : ?>

    <?php $query = new WP_Query( ‘post_type=entries’ ); ?>
    <?php if ( $query->have_posts() ) : ?>
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
    <?php x_get_view( $stack, ‘content’, get_post_format() ); ?>
    <?php endwhile; ?>
    <?php wp_reset_postdata();?>

    <?php else : ?>
    <?php x_get_view( ‘global’, ‘_content-none’ ); ?>
    <?php endif; ?>

    <?php endif; ?>

    <?php pagenavi(); ?>

    I tried using the code you provided, but it’s breaking the site below the header. I’m wondering if it’s because of the div containers and the structure of the theme?

    @beyondthelamppost, entire code is wrong for your tag template.
    The very first thing

      You First decide what actual view you want and how it will look like
      If you want two things(post and entries) to be separated on tag archive. did you want pagination for both?
      Even though it seems simple but still your theme code is in very bad condition and I wish to try it to solve. I will have to check it on your site.
    • This reply was modified 9 years, 3 months ago by 1naveengiri.
    • This reply was modified 9 years, 3 months ago by 1naveengiri.
    Thread Starter beyondthelamppost

    (@beyondthelamppost)

    @1naveengiri the only thing that I have touched inside that template is adding the query to:

    <?php $query = new WP_Query( ‘post_type=entries’ ); ?>
    <?php if ( $query->have_posts() ) : ?>
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
    <?php x_get_view( $stack, ‘content’, get_post_format() ); ?>
    <?php endwhile; ?>
    <?php wp_reset_postdata();?>

    The rest came from the theme itself. I have messaged you on Skype. I’d appreciate your help.

    yes @beyondthelamppost,
    the above code will show all post from entries post type.

    but you want here post from entries have tag foo.

    one more thing the query which is already there is for all post with tag foo.
    it will show you all post that have foo tag.

    better you should customise it for both by different and appropriate query.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Using custom post types in tag archive pages’ is closed to new replies.