[Support request] Limit display rule to Category AND Tag Together

Home Forums Support [Support request] Limit display rule to Category AND Tag Together

Home Forums Support Limit display rule to Category AND Tag Together

Viewing 15 posts - 16 through 30 (of 40 total)
  • Author
    Posts
  • #1052174
    Heather

    As a side question – I noticed there is a display rule from Pages, is there a way to use this display rule but for a custom URL?

    #1052503
    Tom
    Lead Developer
    Lead Developer

    So this is your code for the Back to Blog:

    <div class="back-to-blog">
    <?php
    if ( !has_tag( array('case-study', 'webinar') ) ) {
        echo '<a href="/blog">Back To Blog</a>';
    }
    ?>
    </div>

    What’s your code for “Back to Case Studies”?

    The Display Rules are only for pages/archives that WP is aware of.

    #1052830
    Heather
    <div class="back-to-case-study">
    <?php
    if ( has_tag( 'case-study' ) ) {
        // The current post has the tag "case-study";
        echo '<a href="http://dev.indellient.com/case-studies/">Back to Case Studies</a>';
    }
    ?>
    </div>
    #1053396
    Tom
    Lead Developer
    Lead Developer

    What if you do this?:

    <?php
    $tag = isset( $_GET['tag'] ) ? true : false;
    
    if ( $tag ) : ?>
        <div class="back-to-case-study">
            <a href="http://dev.indellient.com/case-studies/">Back to Case Studies</a>
        </div>
    <?php else : ?>
        <div class="back-to-blog">
            <a href="/blog">Back To Blog</a>
        </div>
    <?php endif; ?>
    #1053977
    Heather

    That works! Thank you so much!

    The only other thing is that I have an additional page where I need this to happen, so I have a case-study page and a webinar page. I essentially need the same thing so that the webinar pages have “back-to-webinar”. What is the rule I would add in here to allow the 3 links to show up in their respective places (case-study, webinar, blog). Is it an ifelse statement in between?

    #1054391
    Tom
    Lead Developer
    Lead Developer

    Can you link me to the webinar page possibly?

    Let me know 🙂

    #1054940
    Heather

    http://dev.indellient.com/webinar/

    It is built in the same way as the case study page. So that page is a tag archive, and the categories have a url like: http://dev.indellient.com/blog/category/analytics/?tag=webinar

    if you go to the categories now, it says back to case studies because of the filter, but it needs to say back to webinar.

    #1055067
    Tom
    Lead Developer
    Lead Developer

    Let’s try this:

    <?php
    $tag = isset( $_GET['tag'] ) ? true : false;
    
    if ( $tag ) : ?>
        <?php if ( 'webinar' === $_GET['tag'] ) : ?>
            <div class="back-to-webinar">
                <a href="http://dev.indellient.com/webinar/">Back to Webinars</a>
            </div>
        <?php else : ?>
            <div class="back-to-case-study">
                <a href="http://dev.indellient.com/case-studies/">Back to Case Studies</a>
            </div>
        <?php endif; ?>
    <?php else : ?>
        <div class="back-to-blog">
            <a href="/blog">Back To Blog</a>
        </div>
    <?php endif; ?>
    #1055151
    Heather

    This worked wonderfully! Thank you so much, everything works so much smoother now.

    #1055264
    Heather

    Will something like this also work for displaying posts in these areas. So since I have a dedicated page for posts with a tag of case-study and webinar, I do not want them to display on my general blog archive page.

    This is fine, until you try to look at a category, and all the posts are there. For example:

    /blog contains only posts that do not have a tag of case-study or webinar

    /blog/category/devops will show ALL posts with a category of devops, regardless of what tags they have.

    Since I already have an archive page for case-study tagged posts with a category of devops (see here), I do not want any of these posts to show up within the general archive page.

    So is there a way to use this same logic but for displaying posts? Or only displaying posts if the url has the tag=case-study in it?

    #1055463
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    That would be different code. Are you only trying to exclude posts with those two tags? And they would only be excluded if you’re not viewing those specific tag archives?

    #1056274
    Heather

    Well I have this code in my functions.php

    //Exclude case study, event, and webinar posts from appearing in blog
    
    	function exclude_posts( $query ) { 
    
    		if ( $query->is_home() ) {  
    	
    			$query->set( 'tag__not_in', array( 182, 180, 166 ) ); 
    	
    		}
    	} 
    	
    	add_action( 'pre_get_posts', 'exclude_posts' );

    Those are the tag ID’s for case-study, webinar, and news. So if you go here you can see that these posts have these filtered on every page, but if you were to go to the archive page for Document Workflow Automation, the first few posts are case-studies that also appear on the Case Studies tag archive page. These category archives should still remain filtered to just show posts, in the same way that the blog archive page is filtered.

    Is there a way to do this, maybe with some if / ifelse statements?

    #1056467
    Tom
    Lead Developer
    Lead Developer

    Hmm, maybe try this:

    add_action( 'pre_get_posts', function( $query ) {
        if ( $query->is_archive( 'webinar' ) || $query->is_archive( 'case-study' ) ) {
            return $query;
        }
    
        if ( $query->is_archive() ) {
            $query->set( 'tag__not_in', array( 182, 180, 166 ) );
        }
    } );
    #1057079
    Heather

    So that worked fine, however it ended up filtering those tags even on those specialized archive pages. So for something like the case-study tag archive, when you select a category (document workflow automation link in previous post), it now shows no posts and acts like no posts with that tag exist.
    Works great for the blog archive and the plain category archives, so blog/category/whatever – but it seems like it doesn’t register the query when the tag is involved, ie: all my filtered category posts for those tags are now empty across the website.

    #1057415
    Tom
    Lead Developer
    Lead Developer
Viewing 15 posts - 16 through 30 (of 40 total)
  • You must be logged in to reply to this topic.