WordPress.org

Ready to get started?Download WordPress

Forums

Network Latest Posts
[resolved] Pulling image from custom field (23 posts)

  1. aaronbennett2097
    Member
    Posted 1 year ago #

    Great plugin, really really good, but am wondering if it's possible to have the thumbnail image pulled from a custom field rather than the featured image.

    Am also having a small problem getting the plugin to display the way I want - I need 4 posts in a row to be displayed across the width of a page, but justified (left post justified to left side of site, right post aligned to right side of the page. But hoping I can figure this out myself.

    Thanks for your help!

    http://wordpress.org/extend/plugins/network-latest-posts/

  2. Jose Luis SAYAGO
    Member
    Plugin Author

    Posted 1 year ago #

    Hello Aaron,

    Well I thought that's a good idea so I implemented it. I have just uploaded version 3.2 which includes two new parameters: thumbnail_custom and thumbnail_field this will allow you to pull thumbnails from custom fields.

    Here is an example how you could use them:

    [nlposts thumbnail=true
             thumbnail_custom=true
             thumbnail_field=my_custom_thumbnail_field
             thumbnail_wh=300x150
    ]

    Concerning the styling issues, just play with the CSS styles, you could use display_type=block which puts articles inside divs then play with div widths and floats. Here you can find a quick example I made to show someone who asked how to display content in columns: CSS Columns

    I've tested the new feature and it works, once you have tested it let me know what you think.

    Cheers.

  3. aaronbennett2097
    Member
    Posted 1 year ago #

    Hi Jose.

    You are AMAZING for acting on this so quickly. Seriously! Thank you!!

    Am going to try it out and if I have any problems I'll get straight back to you.

    Ten thumbs up!! :-)

  4. aaronbennett2097
    Member
    Posted 1 year ago #

    Hi Jose.

    Have updated the plugin and set the Thumbnail Custom Field to the name I have (hero_shot).

    At the moment all I'm getting is the question mark graphic because the image cannot be found, but going to persevere and see if I can figure it out.

    I set my custom field using the Advanced Custom Fields plugin by the way, incase that helps.

    The plugin is being used on my homepage of a multisite install, which is pulling the posts from a sub blog (if thats the right terminology).

    The excerpts are there, so its finding the posts, just not the image (yet).

    Thanks again!
    Aaron

  5. Jose Luis SAYAGO
    Member
    Plugin Author

    Posted 1 year ago #

    Hello Aaron,

    I forgot to mention, the value in that custom field should be the URL pointing to the image. Check if your custom fields are registering images that way, otherwise we will have to hack it ;)...

    Cheers.

  6. aaronbennett2097
    Member
    Posted 1 year ago #

    Hi Jose.

    Have checked and the custom field is set to return the value as a url, but still no joy.

    To check, I did add a featured image to the post and it still seems to be using that, even though I set the widget to use a custom field.

    Am gonna keep trying, am sure its something on my setup - which is sadly being developed locally so can't share the url.

    Any further thoughts would be really appreciated.
    Aaron

  7. Jose Luis SAYAGO
    Member
    Plugin Author

    Posted 1 year ago #

    Hi again Aaron,

    Well, here is how I get the data:

    $thumbnail_custom_field = get_post_meta($field->ID, $thumbnail_field, true);
    // Get custom thumbnail
    $thumb_html = "<img src='".$thumbnail_custom_field."' width='".$thumbnail_size[0]."' height='".$thumbnail_size[1]." />";

    So what it basically does is look inside the wp_x_postmeta table the field passed through the thumbnail_field parameter. Check if the hero_shot field is there.

    If you see the image missing, check the source code and see what's returning (maybe an empty url?).

    Let me know if there's something else I can help you with.

    Cheers.

  8. aaronbennett2097
    Member
    Posted 1 year ago #

    Hi Jose.

    I've checked the code and here is what it's returning.

    <img src="93" width="202" height="202 /><h3 class=" nlposts-block-title'="">

    The img src="93" is where I suspect it's failing.

    I've also tried 'hacking' your file to hard code the hero_shot field, but that didn't work either.

    To eliminate other errors I created a new custom field, uploaded a new image and changed the widget to point to the new custom field, but still no gravy.

    Any other ideas?

    Thanks
    Aaron

  9. Jose Luis SAYAGO
    Member
    Plugin Author

    Posted 1 year ago #

    Hello,

    It seems like it's returning an ID instead of a URL so I'm wondering where is that plugin storing data.

    I used the custom fields functionality provided by WordPress, when you add a new custom field it saves data to the _postmeta table.

    I guess I will have to check that plugin to see where it's storing values. If I find something I will let you know.

    Cheers.

  10. aaronbennett2097
    Member
    Posted 1 year ago #

    Hi Jose.

    So you suspect Advanced Custom Fields is saving the data other than the _postmeta table?

    Shall I try using the built in custom fields and see if that works?

    I really appreciate all your help on this - do let me know if theres anything I can try to narrow down the problem.
    Aaron

  11. Jose Luis SAYAGO
    Member
    Plugin Author

    Posted 1 year ago #

    Hi Aaron,

    Well I don't really know how to use that plugin but I found under settings you have a field called Return Value which presents 3 different choices, Image Object, Image URL and Image ID (check this image: http://postimage.org/image/ok30yw1tn/ )

    If what is being shown is <img src="93" width="202" height="202 /> it makes me think 93 must be the image ID so, I guess if you set the Return Value to image URL it should be ok (maybe?)...

    Cheers.

  12. aaronbennett2097
    Member
    Posted 1 year ago #

    Hi Jose.

    I already made sure I had image URL selected, but no joy.

    Interestingly, when I look at the returning code...

    <img src="93" width="202" height="202 /><h3 class=" nlposts-block-title'="">

    ...if I click on the "93" it gives me the full url of where this image is supposed to be.

    This is where it gets confusing...

    The full URL is to my main blog (call it BLOGA) whereas the image is actually saved in a post in BLOGB.

    Does that make sense? I think it's looking in the wrong blog for the image.

    Thanks
    Aaron

  13. Jose Luis SAYAGO
    Member
    Plugin Author

    Posted 1 year ago #

    Well, it's a shame I can't test the plugin because is paid :D... But I guess it must be using the _postmeta table, check if the hero_shot (or the name of your custom field) exists in that table and check its value.

    Check also if there are additional tables created by that plugin, if so then I think we will have to hack it. Try to find inside the _postmeta the url pointing to your image and check what's the name of the field.

  14. aaronbennett2097
    Member
    Posted 1 year ago #

    Hi Jose.

    It's def not a paid plugin (http://advancedcustomfields.com) or at least I've never paid for it.

    From what I have just read, Advanced Custom Fields uses it's own table in the database, so that may be why its not picking up in the _postmeta.

    http://support.advancedcustomfields.com/discussion/72/-very-important-information-about-the-new-v1-1-acf-update/p1

    I'm going to dig into my database and see where the hero_shot table is stored.

    Thanks again for all your time.
    Aaron

  15. Jose Luis SAYAGO
    Member
    Plugin Author

    Posted 1 year ago #

    Hello Aaron,

    Hehe well I installed that one from the WordPress repository and it ask for license codes to activate some functionalities :D....

    Anyway I've checked the documentation and it seems there's no way to pass post IDs to functions so you could try this:

    Comment out all occurrences for this line:

    $thumbnail_custom_field = get_post_meta($field->ID, $thumbnail_field, true);

    Then look for this one:

    $thumb_html = "<img src='".$thumbnail_custom_field."' width='".$thumbnail_size[0]."' height='".$thumbnail_size[1]." />";

    And replace all occurrences by this one:

    $thumb_html = "<img src='".the_field($thumbnail_field)."' width='".$thumbnail_size[0]."' height='".$thumbnail_size[1]." />";

    I did not test this (because I can't make the plugin work for me), but in theory "it should work".. Based on these examples: http://www.advancedcustomfields.com/docs/getting-started/code-examples/

    Let me know if it works..

  16. aaronbennett2097
    Member
    Posted 1 year ago #

    Have looked through my database (bit out of my knowledge / depth) and although I found the _postmeta, I couldnt see anything that refers to hero_shot.

    I must admit, I don't really know what I'm doing with the database!

    Will keep researching and testing.
    Thanks!!
    Aaron

  17. aaronbennett2097
    Member
    Posted 1 year ago #

    Hi Jose.

    Just so I understand you and do it correctly, you want me to make the changes in the network-latest-posts.php?

    thank you thank you thank you!!

  18. Jose Luis SAYAGO
    Member
    Plugin Author

    Posted 1 year ago #

    Oops my bad!...

    Yes sorry I forgot to mention that, open network-latest-posts.php then look for those lines.

    $thumbnail_custom_field = get_post_meta($field->ID, $thumbnail_field, true); Lines: 510 & 716

    $thumb_html = "<img src='".$thumbnail_custom_field."' width='".$thumbnail_size[0]."' height='".$thumbnail_size[1]." />"; Lines: 513 & 719

    By the way if the_field doesn't work, try with get_field instead.

    Cheers.

  19. randler
    Member
    Posted 11 months ago #

    Hello!

    I have changed my custom thumbnail to be the image that I have uploaded through a function in my custom theme. It uploads a profile image of the blogger who wrote the post. But it doesn't work.

    I have used this in another plugin that we have to change from because it's not being updated any longer. I know this is the right field name, but still no image.

    Michael

  20. Jose Luis SAYAGO
    Member
    Plugin Author

    Posted 11 months ago #

    Hello Michael,

    I'm wondering, where is your custom function saving data? as you can see solutions above work for default thumbnails and for thumbnails created using Advanced Custom Fields plugin.

    If this is not the case then you'd have to customize Network Latest Posts to pull data from the correct field. If you want me to help I'd need you to share the source code where you pick custom images and save it to the database (specifically which table).

    Looking forward to your feedback.

  21. randler
    Member
    Posted 11 months ago #

    Hello, thank you for your fast reply.

    It uses a javascript that uses WordPress own media-upload.php file.
    So I'm guessing the data is in the wp_posts table. Which is the reason it's not working I suppose.

    Due to the situation I'm in, working as a consultant and doing this for a client, I can't use the solution you provided for ACF. Since I can't hard code stuff and loose the things I changed when you update the plugin.

    Can I add a default thumbnail function and use that instead maybe? How Does that work?

  22. Jose Luis SAYAGO
    Member
    Plugin Author

    Posted 11 months ago #

    Hi, I think if you save custom thumbnails into wp_postmeta then there is no need to change anything.

    Please take a look at this: Retrieve a Custom Field Thumbnail Url.

    As you can see there, they suggest doing this to get custom thumbnail URLs:

    <?php echo get_post_meta( get_the_ID(), 'thumb', true ) ?>

    That's exactly the way NLP works, so, if for example your custom theme includes a field called "Post Thumbnail" then you just have to set the thumbnail_field like this:

    // Shortcodes
    [nlposts ..your parameters..
            thumbnail=true
            thumbnail_custom=true
            thumbnail_field="Post Thumbnail"
    ]

    In case you're embedding NLP into a custom template then:

    // Embedded
    $args = array(
          ...other params...
          'thumbnail' => 'true',
          'thumbnail_custom' => 'true',
          'thumbnail_field' => 'Post Thumbnail'
    );
    network_latest_posts( $args );

    In theory what will happen is, NLP will look into wp_postmeta for the custom thumbnail URL saved into that field.

    Hope this makes sense.

  23. randler
    Member
    Posted 11 months ago #

    Okay, so if I understand you correctly I should change the way my form, that I have in functions.php saves the metadata about the author image.

    This is how the function that adds the extra fields on the options page of WordPress looks:

    /Extrainfo bloggare
    $new_general_setting = new new_general_setting();
    
    class new_general_setting {
    	function new_general_setting() {
    		add_filter('admin_init', array(
    			&$this,
    			'register_fields'
    		));
    		add_action('admin_init', array(
    			&$this,
    			'load_scripts'
    		));
    	}
    
    	function register_fields() {
    		register_setting('general', 'profilbild', 'esc_attr');
    		add_settings_field('profil_bild', '<label for="profilbild">' . __('Profilbild', 'profilbild') . '</label>', array(
    			&$this,
    			'field_profilbild'
    		), 'general');
    		register_setting('general', 'toppbanner', 'esc_attr');
    		add_settings_field('topp_banner', '<label for="toppbanner">' . __('Toppbanner', 'toppbanner') . '</label>', array(
    			&$this,
    			'field_toppbanner'
    		), 'general');
    	}
    	function load_scripts() {
    		wp_enqueue_script('media-upload');
    		wp_enqueue_script('thickbox');
    		wp_enqueue_style('thickbox');
    	}
    	function field_profilbild() {
    		$value = get_option('profilbild', '');
    		echo '<input type="text" id="profilbild" name="profilbild" value="' . $value . '" /><input type="button" id="upload_pic_button1" class="upload-button" value="Lägg till" />';
    ?>
    <script type="text/javascript">
    	jQuery(document).ready(function() {
    
    	var uploadID = ''; /*setup the var*/
    
    	jQuery('.upload-button').click(function() {
       		uploadID = jQuery(this).prev('input'); /*grab the specific input*/
       		formfield = jQuery('.upload').attr('name');
        		tb_show('', 'media-upload.php?type=image&TB_iframe=true');
        		return false;
    	});
    
    	window.send_to_editor = function(html) {
       		imgurl = jQuery('img',html).attr('src');
        		uploadID.val(imgurl); /*assign the value to the input*/
        		tb_remove();
    	};
    });
    </script>
    <?php
    	}
    	function field_toppbanner() {
    		$value = get_option('toppbanner', '');
    		echo '<input type="text" id="toppbanner" name="toppbanner" value="' . $value . '" /><input type="button" id="upload_pic_button2" class="upload-button" value="Lägg till" />';
    	}
    }

    Should I somewhere in here specify where in the database the image information should be saved?

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic