Skip to main content
added 679 characters in body
Source Link
Segia
  • 69
  • 1
  • 7
<!-- Add glossary list in content -->
<?php foreach ( $groups as $group ): ?>
    <ul>
    <div class="glossary-title-link" id ="section-<?php echo str_replace(' ', '-', strtolower($group['name'])) ?>">
    <h4 id ="<?php echo $group['name'] ?>"><?php echo $group['name'] ?></h4>
    </div>
    <?php foreach ( $group['tags'] as $tag ): ?>
        <li class="glossary-list">

        <!-- If there are any posts for the tag, link to the tag archivepage -->
<li class="glossary-list">    
    <?php 

            if ( $tag['count'] > 0 ) {
                echo '<a class="glossary-link" id="term-';
                echo str_replace(' ', '-', strtolower($tag['name']));
                echo '" href="';
                echo $tag['link'];
                echo '">';
                echo $tag['name'];
                echo '</a></br>'; 


            } else {
                echo '<div class="glossary-link" id="term-';
                echo str_replace(' ', '-', strtolower($tag['name']));
                echo '">';
                echo $tag['name'];
                echo '</div>';

            }
        ?>
        <?php echo $tag['description'] ?></li>
                    
    <?php endforeach; ?>
    </ul>
<?php endforeach; ?>
<!-- If there are any posts for the tag, link to the tag archive -->
<li class="glossary-list">    
 <?php
    if ( $tag['count'] > 0 ) {
        echo '<a href="';
        echo $tag['link'];
        echo '">';
        echo $tag['name'];
        echo '</a></br>';
    } else {
        echo $tag['name'];
    }
?>
<?php echo $tag['description'] ?></li>
<!-- Add glossary list in content -->
<?php foreach ( $groups as $group ): ?>
    <ul>
    <div class="glossary-title-link" id ="section-<?php echo str_replace(' ', '-', strtolower($group['name'])) ?>">
    <h4 id ="<?php echo $group['name'] ?>"><?php echo $group['name'] ?></h4>
    </div>
    <?php foreach ( $group['tags'] as $tag ): ?>
        <li class="glossary-list">

        <!-- If there are any posts for the tag, link to the tag page -->
        <?php 

            if ( $tag['count'] > 0 ) {
                echo '<a class="glossary-link" id="term-';
                echo str_replace(' ', '-', strtolower($tag['name']));
                echo '" href="';
                echo $tag['link'];
                echo '">';
                echo $tag['name'];
                echo '</a></br>'; 


            } else {
                echo '<div class="glossary-link" id="term-';
                echo str_replace(' ', '-', strtolower($tag['name']));
                echo '">';
                echo $tag['name'];
                echo '</div>';

            }
        ?>
        <?php echo $tag['description'] ?></li>
                    
    <?php endforeach; ?>
    </ul>
<?php endforeach; ?>
Source Link
Segia
  • 69
  • 1
  • 7

Link directly to the first post in an archive

on my WordPress site, I'm developing a glossary list of novel writing terms, using tags and tags descriptions to draw definitions from. Currently, when any tag has more than 0 posts associated with it, it will link to that tag's archive page, allowing readers to further read up on the topic.

What I would like to achieve, is to have any tag that has exactly 1 post associated with it, to link directly to that post rather than the tag archive. I've been looking around for a solution, but can't seem to find any way to achieve that.

Any help would be greatly appreciated!

For reference, this is the current code:

<!-- If there are any posts for the tag, link to the tag archive -->
<li class="glossary-list">    
<?php
    if ( $tag['count'] > 0 ) {
        echo '<a href="';
        echo $tag['link'];
        echo '">';
        echo $tag['name'];
        echo '</a></br>';
    } else {
        echo $tag['name'];
    }
?>
<?php echo $tag['description'] ?></li>

Which leads to this, where 'cliffhangers' is linked and 'denouement' is not: