WordPress.org

Ready to get started?Download WordPress

Forums

if empty check on wp_list_bookmarks (10 posts)

  1. EnlightenedShadow
    Member
    Posted 2 years ago #

    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.

  2. Digital Raindrops
    Member
    Posted 2 years ago #

    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

  3. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    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 ;-)

  4. Digital Raindrops
    Member
    Posted 2 years ago #

    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

  5. EnlightenedShadow
    Member
    Posted 2 years ago #

    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.

  6. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    hopefully just a syntax problem:

    <?php if( ! wp_list_bookmarks('category_name='.$userestimates.'&echo=0) ) : ?>
    <p>You don't have any estimates yet.</p>
  7. EnlightenedShadow
    Member
    Posted 2 years ago #

    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.

  8. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    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>";
    }
  9. EnlightenedShadow
    Member
    Posted 2 years ago #

    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.

  10. EnlightenedShadow
    Member
    Posted 2 years ago #

    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. :)

Topic Closed

This topic has been closed to new replies.

About this Topic