WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Need to add variable to function (34 posts)

  1. ekajatik
    Member
    Posted 2 years ago #

    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

  2. kz
    Member
    Posted 2 years ago #

    <?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"
    );
    ?>
  3. Big Bagel
    Member
    Posted 2 years ago #

    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.

  4. ekajatik
    Member
    Posted 2 years ago #

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

  5. ekajatik
    Member
    Posted 2 years ago #

    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?

  6. Big Bagel
    Member
    Posted 2 years ago #

    <?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.

  7. ekajatik
    Member
    Posted 2 years ago #

    <?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

  8. Big Bagel
    Member
    Posted 2 years ago #

    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.

  9. ekajatik
    Member
    Posted 2 years ago #

    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?

  10. ekajatik
    Member
    Posted 2 years ago #

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

  11. Big Bagel
    Member
    Posted 2 years ago #

    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.

  12. Digital Raindrops
    Member
    Posted 2 years ago #

    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

  13. Big Bagel
    Member
    Posted 2 years ago #

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

  14. Digital Raindrops
    Member
    Posted 2 years ago #

    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

  15. Big Bagel
    Member
    Posted 2 years ago #

    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.

  16. Digital Raindrops
    Member
    Posted 2 years ago #

    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

  17. Big Bagel
    Member
    Posted 2 years ago #

    Yup, I prefer concatenating with periods myself ('text' . $variable . 'text') when it comes to variables, but using double quotes alone does work.

    All your comments are good standard programming practices, they're just a bit overkill here. (Assuming WordPress functions always work as they should. :) )

    If he were using an array of arguments like you suggested then it would in fact take an integer, but he's currently passing all the arguments as a big string. I've asked and he's using the proper values in his custom field; you can see what he has as the custom field in one of his previous post.

  18. Digital Raindrops
    Member
    Posted 2 years ago #

    Reading back, it does look like the custom field is not set if hardcoded '366' works, and the code is corect.

    Elimination:
    A new page will eliminate a page cache issue
    Create a new post for good measure
    Custom field name = links ('links' not 'link' case sensitive so lowercase)
    Custom field value = 366

    Publish and test!

    HTH

    David

  19. ekajatik
    Member
    Posted 2 years ago #

    There is no page cache issue... other small changes have been made to template pages and posts and the results are immediate as I flush the cache as necessary.

    Custom field name is definitely 'links'
    Custom field value is definitely 366.

    Also, I tried your troubleshooting code above and it produced absolutely nothing in the source code... I put it in below the list_bookmarks as follows:

    <?php $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 . '-->'; ?>

    Also, Digital Raindrops, I tried your 'easy read' code above, but has same result as other code, no links are displayed.

  20. Digital Raindrops
    Member
    Posted 2 years ago #

    That is strange as I have tested it with Twenty Eleven, single.php

    Here is my single.php code.
    http://pastebin.com/sVSddv8N

    I created a link category 'test' = 514

    Custom Field:
    I used the bottom box 'Enter New' to add the new custom field, then selected it again from the bottom dropdown and re-entered 514 and it appeared above the dropdown section see Add Custom Fields Image.

    View the post image and you will see the links below the comments section.

    Use the pastebin code and test it with twenty eleven.

    If you can not resolve it then add the code to pastebin.php it will be something silly in the end it often is, so many times I have had this same frustrating situation.

    HTH

    David

  21. ekajatik
    Member
    Posted 2 years ago #

    Looks great, just like my code etc, except mine has no output.

    So I am trying it a different way... since I cannot execute php in custom fields I am using the WP-Render-blogroll shortcode in the CF like so:

    [wp-blogroll catid=366 showdesc=1 notitle=1]

    Then using the following to do the shortcode properly:

    <?php echo apply_filters('the_content', get_post_meta($post->ID, 'links', true)); ?>

    But that's not working either! Nothing showing up. The shortcode definitely works on pages.

  22. Big Bagel
    Member
    Posted 2 years ago #

    Bah, all this should work. It seems for some reason that get_post_meta is returning an empty string even though the arguments look correct. Maybe W3 Total Cache's object and database caching weren't refreshed? All I can think to do is completely turn off all the caching, check (yet again) that everything is properly matching, and try once more.

    Those troubleshooting comments didn't show at all when you added them to the code? (not even three blank <!-- -->?) Cause then we could at least see what exactly get_post_meta is returning and exactly what WordPress thinks your custom field key is set to. If the php file they were in was processed at all they should have shown up. Perhaps the cache didn't properly rebuild itself?

  23. ekajatik
    Member
    Posted 2 years ago #

    Deactivated w3 Cache and CDN.
    Added testing code which returned the following:

    <!--test id--1316--><!--test keys--_jd_post_meta_fixed--><!--test value---->

    So the method is not getting the CF value.

    Just to be sure I also used the get_field for the link CF and this shows fine the right CF value.

  24. ekajatik
    Member
    Posted 2 years ago #

    exact code I used:

    <?php
    $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--' . $test_id . '-->'; echo '<!--test keys--' . $test_keys[0] . '-->'; echo '<!--test value--' . $test_value . '-->';	 												?>
    <?php the_field('links'); ?>
  25. Digital Raindrops
    Member
    Posted 2 years ago #

    It looks more like the meta data is not being saved, the hardcoded category works but the meta data is not returning the saved value.

    Create a couple of random Custom Fields and use var_dump(array) to see what is in there, if it comes back a NULL then there is somthing wrong with the save or get meta data function, or the get_the_ID() on your install, as it works for us are we looking in the wrong area.

    In single.php or content-single.php

    <?php
    global $post;
    $test_keys = get_post_custom_keys( $post->ID );
    var_dump( $test_keys );
    ?>

    If the php file they were in was processed at all they should have shown up.

    Good point, ekajatik post the file content to http://pastebin.php and paste back the link, so we can see the whole thing, it might be something else in the file that is breaking the code.

    HTH

    David

  26. ekajatik
    Member
    Posted 2 years ago #

    So... created a new CF, added variable, changed CF name in code and result? No working.

    Added var dump (most of the content onscreen is through CF's so loads of results, except for links or the new CF I just created 'links_category'... both missing.

    I have a number of unused CF's which I created originally so I changed the name in the code to the unused one and added the link category variable and yes, it works!

    So I then changed the name of this unused CF and name of the slug, to 'web_links' and guess what... not working again.

    BTW, am using Advanced Custom Fields 2.0.5

  27. ekajatik
    Member
    Posted 2 years ago #

    I installed 'Easy Content Types' plugin for managing post types, installed last week, and now realise that this has CF functionality also (they call it metaboxes), could there be a conflict? Tried deactivating but no change.

  28. Digital Raindrops
    Member
    Posted 2 years ago #

    Do you call Advanced Custom Fields the same way?

    As it works when testing for me and Big Bagel it looks like a conflict, if you have a local host enviroment then test without the plugins on a clean database, load and activate the plugins until it breaks.

    If you are on windows there is a handy application InstantWP that will create a full test environment in a folder or on a usb stick, great for testing etc:, wrote about it here with a link in the post.

    HTH

    David

  29. Big Bagel
    Member
    Posted 2 years ago #

    Yeah, it's probably a conflict (I'd say it's the Advanced Custom Fields plugin). Are you adding this new custom field through that plugin or the "normal" way? In your original post you mentioned the_field('links'), which seems to be one of the way this plugin provides access to custom fields it creates. If you added the custom field through the plugin and it's set as "text" then you could probably replace:

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

    with

    $link = get_field('links');

  30. ekajatik
    Member
    Posted 2 years ago #

    $link = get_field('links');

    works. I may have to live with the conflict to be honest as ACF does not store CF values in the normal way but in its own data tables (just discovered), so extremely wary of fiddling with it.

    Will point the ACF dev to this thread and see how it goes.

    Lastly, how do I get the if/then statement to show default text if value missing, in this format:

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

    Thank you big bagel and Digital Raindrops, for your time and attention. Much appreciated :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.