• Resolved Ponken

    (@ponken)


    Hi there,

    I’m trying to create a new Posts/index page that lists all posts as usual, except that it highlights some of them if they have certain tags. I tried using has_tag until I realised that only checks the tags of the current page, i.e. of the index page itself, and not of the posts. Does anyone know how to check if the posts that are being listed have particular tags?

    Here’s a privately published page using the page template in question, currently only displaying data for itself instead of for all posts:
    http://whobackwhen.com/test-index-page/

    Below is what I have at the moment. Any help would be greatly appreciated.

    Thanks

    Ponken

    <?php if (have_posts()) : ?>
    	<?php while (have_posts()) : the_post(); ?>
    	<?php if(has_tag('tag-1')) { ?>
    			<div class="something">
    	<?php } elseif(has_tag('tag-2')) { ?>
    			<div class="something_else">
    	<?php } else { ?>
    			<div class="fallback">
    	<?php } ?>
    <?php endwhile; ?>
    …
    <?php else : ?>
    …
    <?php endif; ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    The has_tag is a Loop function, so your code above should work fine. I think where you’re going wrong probably has something to do with your query.

    Is this a page template? How are you getting the posts to display here? What does your query look like?

    Thread Starter Ponken

    (@ponken)

    Thanks for the quick reply!

    I use the exact same code minus the internal conditional on the current Posts page, and it works just fine. I.e.:

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <div class="something">
    <?php endwhile; ?>

    <?php else : ?>

    <?php endif; ?>

    Sorry if I’m using the wrong terminology btw. To clarify, it’s currently a page template, but the plan is to replace index.php with it. It’s not meant to have content of its own, but rather to list all posts, newest to oldest. My current Posts/index page is here: http://whobackwhen.com/doctor-who-podcast/

    Cheers!

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    The basic Loop will be the same whether you’re displaying a Page or Posts. So if you have it in a Page Template, then it’s going to show the Page’s content. If you put the same code in the index.php, then it’s going to display the main Posts.

    See, the URL actually determines what content loads and then is displayed by default. If you want to display Posts on a Page Template, then you need to modify the Query or change the Static Front Page settings.

    Thread Starter Ponken

    (@ponken)

    Wow – I copied the page template into index.php, and it works splendidly.

    Thanks so much, Otto! Have a rocking day!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Tag Conditionals in index loop?’ is closed to new replies.