Support » Fixing WordPress » Need to add variable to function

  • Resolved ekajatik

    (@ekajatik)


    I need to do the following:

    <?php wp_list_bookmarks(‘categorize=0&category=X&before=<span>&after=</span>&show_images=1&show_description=1&orderby=url’); ?>

    I need to replace the X in category= with the content of a custom field, what is the syntax? I guess use:

    <?php the_field(‘links’); ?> to output the custom field value, but how do I put them together, every way I’ve tried so far gives errors?

    Thanks

Viewing 15 replies - 1 through 15 (of 33 total)
  • <?php
    $link = get_post_meta( get_the_ID(), 'links', true );
    wp_list_bookmarks(
      "categorize=0&category=$link&before=<span>&after=</span>&show_images=1&show_description=1&orderby=url"
    );
    ?>

    Assuming the custom field is a string, that you don’t want the bookmarks to show if the field wasn’t set, and that you’re in the loop, I’d try something like:

    <?php
    $custom_field = get_post_meta( $post->ID, 'links', true );
    if ( $custom_field ) {
        wp_list_bookmarks( 'categorize=0&category=' . $custom_field . '&before=<span>&after=</span>&show_images=1&show_description=1&orderby=url' );
    }
    ?>

    *not tested

    Edit:
    Dangit! That’s what I get for taking so long to post.

    Works great using this:

    <?php
    $link = get_post_meta( get_the_ID(), 'links', true );
    wp_list_bookmarks(
    "title_li=0&categorize=0&category_name=0&category=$link&show_images=0&show_description=1&after=<br />&orderby=url"
    );
    ?>

    One more question: How do I get a hyphen between the link and the description?

    (example here, see the links tab: http://www.heritagetraveller.com/north-america-world-heritage-site/yellowstone-national-park/)

    Oops, just realised that this will now show all links if the custom field is blank. How to make it default to some standard text if there is no custom field value?

    <?php
    $link = get_post_meta( get_the_ID(), 'links', true );
    if ( $link ) {
        wp_list_bookmarks( "title_li=0&categorize=0&category_name=0&category=$link&show_images=0&show_description=1&after=<br />&orderby=url" );
    } else {
        echo 'No bookmarks.';
    }
    ?>

    With the “between” argument you can add whatever string you want between the link and the description.

    <?php
    $link = get_post_meta( get_the_ID(), 'links', true )
    if ( $link ) {
    wp_list_bookmarks(
    "title_li=0&categorize=0&category_name=0&category=$link&show_images=0&show_description=1&after=<br />&between=  &orderby=url" );
    } else {
    echo '<em>No links have been submitted and approved yet.</em>';
    }
    ?>

    posts with no CF value showing the default text, fine. However, posts with CF value and links in that category are also showing the ‘else’ version. Example: http://www.heritagetraveller.com/europe-world-heritage-site/fortifications-vauban/ definitely has links in the category defined in the CF

    Just tested it in my dev site; works fine for me.

    Assuming what you posted is the exact code you’re using, make sure to add a semicolon on the second line:

    $link = get_post_meta( get_the_ID(), 'links', true );

    And just to triple check: Make sure that the post’s custom field name is exactly “links”, that the post’s custom field value is the ID number for the link category you want (ex: 3), and that you’re using this code in the loop.

    Hi Bagel

    All the above, check.
    Working? No.

    Have triple checked. The code block contains the semi colon (not sure how it disappeared in my post above), it is in the loop, the CF field name is definitely ‘links’, the Value is 366 which corresponds to links category 366 which contains one link with description.

    Tried:

    1. Removing this line:
    else { echo '<em>No links have been submitted and approved yet.</em>'; }
    still does not return the link, just has a blank space where the link should be.

    2 Key problem is that this does not work and returns all links regardless of category:

    <?php
    $link = get_post_meta( get_the_ID(), 'links', true );
    wp_list_bookmarks("title_li=0&categorize=0&category_name=0&category=$link&show_images=0&show_description=1&after=<br />&between=&nbsp;&nbsp;&orderby=url"
    );
    ?>

    so &category=$link is not working?

    Must be the $link that is the issue as the following outputs only the links in category 366:

    <?php wp_list_bookmarks('title_li=0&categorize=0&category_name=0&category=366&show_images=0&show_description=1&after=<br />&orderby=url'); ?>

    The problem seems to be that get_post_meta is retuning an empty string (it does this when it can’t find the key requested), which would result in exactly what you’re seeing. Everything looks good and is set correctly…bah.

    If you want to try a little troubleshooting, you could put this bit in:

    $test_id = get_the_ID();
    $test_keys = get_post_custom_keys( get_the_ID() );
    $test_value = get_post_meta( get_the_ID(), 'links', true );
    echo '<!--' . $test_id . '-->';
    echo '<!--' . $test_keys[0] . '-->';
    echo '<!--' . $test_value . '-->';

    Then you can check the page’s source code to see the values (after clearing any cache of course). Maybe the key is getting stored strangely? After all the troubleshooting someone will probably show up and find the problem in 5 seconds.

    Two suggestions,
    First the $link should be outside the “” and added with a period.
    "html text ".$link."more html text"

    Second use isset() to test the variable is not null!

    Untested:

    <?php
    $link = get_post_meta( get_the_ID(), 'links', true );
    if( isset($link) && $link && is_int($link) ) :
    	wp_list_bookmarks("title_li=0&categorize=0&category_name=0&category=".$link."&show_images=0&show_description=1&after=<br />&between==  &orderby=url");
    endif;
    ?>

    Untested ‘easy read’ Code:

    <?php
    $link = get_post_meta( get_the_ID(), 'links', true );
    if(isset($link) && $link ) :
    	$args=array(
    		'title_li' => 0,
    		'categorize' => 0,
    		'category_name' => 0,
    		'category' => $link,
    		'show_images' => 0,
    		'show_description' => 1,
    		'after' => '<br />',
    		'between' => '  ',
    		'orderby' => 'url'
    	);
    	wp_list_bookmarks( $args );
    endif;
    ?>

    HTH

    David

    The double quotes causes any variables to be processed:

    "text...$link...text"
    and
    'text' . $link . 'text'
    and
    "text" . $link . "text"

    will all have the same result.

    Also, get_post_meta (what $link is being set to) never returns null (it’s good to check if a variable is null before using it, but is a bit unnecessary here). If it can’t find anything it returns an empty string (which is why, if you get rid of the conditional, wp_list_bookmarks still works as if the category argument was set as an empty sting).

    Hi Big Bagel,

    Have a look at the replies from ekajatik there are no periods in the code!

    no .$link. or ..$link..

    So the $args = array() may be a better managable solution, rather than a long string.

    Also there is no test to see if the value is an integer and if the category exists.

    should also add is_int($link)

    Regards

    David

    Yes, using an array is more readable. I was using the periods in my first example as an ellipsis. Until his last post (which I believe was probably a just a mistake) he was using double quotes. There’s no problem there.

    You don’t need to use periods to concatenate a string with a variable if you use double quotes.

    All of these work:
    wp_list_bookmarks("category=$link");
    wp_list_bookmarks("category=" . $link);
    wp_list_bookmarks('category=' . $link);

    Using his current code, he would want a string rather than an int, which is exactly what get_post_meta with it’s last argument set to true will return every time. Checking that it is a string (or int) is not needed.

    You don’t need to use periods to concatenate a string with a variable if you use double quotes.

    @big Bagel:
    Cheers,
    I did not know that wp_list_bookmarks("category=$link"); would work!

    I have seen variables in database queries like {$link}, and I have always used “string “.$variable.” string”

    category => $link expects a string with an integer value or an array of integer strings, that is why I suggested the integer check, category ’12’ will works, category ‘news’ will not!

    If they put the category name into the custom field then nothing will be returned.

    Regards

    David

Viewing 15 replies - 1 through 15 (of 33 total)
  • The topic ‘Need to add variable to function’ is closed to new replies.