WordPress.org

Support

Support » How-To and Troubleshooting » if empty check on wp_list_bookmarks

if empty check on wp_list_bookmarks

  • Hey guys. I’m doing some complex PHP coding and am trying to check a link category to see if it is empty or not. If it is empty (there are no links in it) have the code output some text and if it isn’t empty (there are links in it) have the code display the links.

    I have the display the links part down but I need a way to check to see if the links are there first. As it is now, it just won’t display anything if the category is empty but I want it to display some text. Without the if else statement though it would just display the text all the time.

    Any ideas? I can post some of the code here but it is using several variables and would take some more explaining if needed.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi,
    If you are returning the link or links in an array(‘url’,’title’) from a custom function ‘get_my_links’, in a template page:

    <?php $links = get_my_links($category); ?>
    <?php if($links) : ?>
    	<?php foreach $links as $link : ?>
    		<li><a href="<?php echo $link[0]"; title="<?php echo $link[1]; "><?php echo $link[1]; ?></a></li>
    	<?php endforeach; ?>
    <?php else: ?>
       'Sorry no links for this category!'
    <?php endif; ?>

    Alternative returned from a function in functions.php

    function get_cat_links( $category ) {
       $links = get_my_links($category);
       if($links) {
         foreach $links as $link {
    		echo '<li><a href="' .$link[0] .'" title="' .$link[1] .'">' .$link[1] .'></a></li>';
    		}
    	} else {
    		echo 'Sorry no links for this category!';
    	}
    }

    Called in the template:

    <?php get_cat_links('the_category'); ?>

    HTH

    David

    possibly:

    <?php if( !wp_list_bookmarks('category=whatever&echo=0') ) : ?>
    some text
    <?php else :
    wp_list_bookmarks('category=whatever');
    endif; ?>

    (keep and integrate whatever paramters you might already have in the function)

    http://codex.wordpress.org/Function_Reference/wp_list_bookmarks

    edit: thank you @david – error corrected in the above code 😉

    I really should read the titles twice I missed the ‘wp_list_bookmarks’

    As alchymyth said, except the first line is missing a not !
    if not get the links the some text

    <?php if( ! wp_list_bookmarks('category=whatever&echo=0') ) : ?>
       some text

    David

    Those should work. I’m not at home and can’t implement anything yet but will give them a try when I can. Thanks guys.

    From a glance though I can see that this may not work due to some syntax issue I will have.

    My category_name is generated through a variable which works for me. So my syntax for the wp_list_bookmarks would be something like this: <?php if( ! wp_list_bookmarks(‘category_name=’.$userestimates&echo=0) ) : ?>
    <p>You don’t have any estimates yet.</p>

    The way I have to do the variable would cause an unexpected T_ECHO where it says &echo because it will appear outside the quotes.

    I would use it inside the quotes but the variable does not work that way.

    hopefully just a syntax problem:

    <?php if( ! wp_list_bookmarks('category_name='.$userestimates.'&echo=0) ) : ?>
    <p>You don't have any estimates yet.</p>

    OK trying to run this code:

    <?php
    if( ! wp_list_bookmarks('category_name='.$userestimates.'&echo=0) ) {
    echo "<p>You don't have any estimates yet.</p>";
    }

    returns an error: “Parse error: syntax error, unexpected T_STRING in CODE on line 3 Errors parsing CODE”

    This is being run both on the live site and a syntax checker.

    my mistake when correcting the syntax – a missing ‘ before the closing bracket:

    try again:

    <?php
    if( ! wp_list_bookmarks('category_name='.$userestimates.'&echo=0') ) {
    echo "<p>You don't have any estimates yet.</p>";
    }

    That did the trick! Sorry it took so long to respond, I was away. Simple syntax mistake lol. Thanks a ton. Now I just got to add the else statement to display the links when there are links.

    Small update. It’s all working fine. Thanks again for the help. I hope to one day know enough PHP tricks to get stuff done on my own lol. Until then, glad there are people out there who are smarter than me. 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘if empty check on wp_list_bookmarks’ is closed to new replies.
Skip to toolbar