• I’m displaying recent blog titles on a page, however some of the blog posts does not have content (title only), and here is what I want to accomplish. I want to remove hyperlink from the titles for the blogs that have no content, but still want to show hyperlink for the ones that have content.
    Need someone’s help as I’m no php expert. Thanks in advance.

    <a href="<?php the_permalink() ?>">   (Remove if no content)
    <?php the_title(); ?>
    </a>   (Remove if no content)
Viewing 6 replies - 1 through 6 (of 6 total)
  • Since you’re in The Loop, you could use something like this:

    <?php
        if ( strlen( get_the_content() ) )
            echo '<a href="<?php the_permalink() ?>">';
    
        the_title();
    
        if ( strlen( get_the_content() ) )
            echo '</a>';
    ?>
    Thread Starter sasori390

    (@sasori390)

    Thanks Doug, I tried your code. It does remove the hyperlink from the the blog titles that have no content like the way I want it, however I get the 404 page not found error when I click the titles that have content. Any clue?

    Sorry about that…try this updated code:

    <?php
        if ( strlen( get_the_content() ) ) {
            echo '<a href="';
            the_permalink();
            echo '">';
        }
    
        the_title();
    
        if ( strlen( get_the_content() ) )
            echo '</a>';
    ?>
    Thread Starter sasori390

    (@sasori390)

    Works like a charm! Thank you so much.

    Awesome, you’re quite welcome!

    Hello,

    I am trying to do something VERY similar to this, but I am pretty new to html and all. I have a plugin that feeds in a long set of web addresses into one list. I want to display these links as simple “register” where the user can click the register text and follow the link. I have used href to do this and it works. The problem is some of the entries do not have links. (you cant register yet) but the text “register” will appear no matter what. I was hoping to find a way where when there it no link to follow that the text would be automatically hidden. The plugin still uses the its own value, so it is not just blank.

    <a href="%event_custom_field{Registration}%">Register</a>

    The %event_custom_field{Registration}% sometimes is a link and sometimes is not.

    This is the page I am trying to do this on… https://www.scnca.net/event-list/

    Really hoping there is a way to do this!

    I appreciate any kind of help on this!

    Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Remove hyperlink from the post titles that have no content.’ is closed to new replies.