• 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 - 16 through 30 (of 33 total)
  • 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.

    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

    Thread Starter ekajatik

    (@ekajatik)

    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.

    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

    Thread Starter ekajatik

    (@ekajatik)

    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.

    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?

    Thread Starter ekajatik

    (@ekajatik)

    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.

    Thread Starter ekajatik

    (@ekajatik)

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

    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

    Thread Starter ekajatik

    (@ekajatik)

    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

    Thread Starter ekajatik

    (@ekajatik)

    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.

    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

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

    Thread Starter ekajatik

    (@ekajatik)

    $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 šŸ™‚

    <?php
    $link = get_field( 'links' );
    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 );
    } else {
        echo '<em>No links have been submitted and approved yet.</em>';
    }
    ?>

    If it’s already working for you then there’s no problem, but if anything has a problem with $link being a string you can always do:

    $link = intval( get_field( 'links' ) );

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