WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display an include from a Custom Field Value (9 posts)

  1. jonoc73
    Member
    Posted 5 years ago #

    Hello,

    Following on from my previous post here I need some more help.

    I would like to use the following code to show certain guides if a checkbox is selected. When this script runs with only one guide selected it displays 3 times. I realise this was originally for words like 'John, Mary, James' so I would be grateful if someone could let me know what bits I need to change. Thanks Jono

    <?php
    	$gt_guides=get_post_meta($post->ID, "gt_guides", false);
    	$out='';
    	$between =', ';
    	if ($gt_guides) {
    	  foreach($gt_guides as $guide) {
    		if ('Phil' == $guide )
    		{ include(TEMPLATEPATH . '/includes/guides/guides-phil.php'); }
    		  elseif ('Rob' == $guide )
    		{ include(TEMPLATEPATH . '/includes/guides/guides-rob.php'); }
    		elseif ('Nic' == $guide )
    		{ include(TEMPLATEPATH . '/includes/guides/guides-nic.php'); }
    		elseif ('Jef' == $guide )
    		{ include(TEMPLATEPATH . '/includes/guides/guides-jef.php'); }
    		elseif ('Ben' == $guide )
    		{ include(TEMPLATEPATH . '/includes/guides/guides-ben.php'); }
    		elseif ('Brend' == $guide )
    		{ include(TEMPLATEPATH . '/includes/guides/guides-brend.php'); }
    		elseif ('Crys' == $guide )
    		{ include(TEMPLATEPATH . '/includes/guides/guides-crys.php'); }
    		elseif ('Kev' == $guide )
    		{ include(TEMPLATEPATH . '/includes/guides/guides-kev.php'); }
    		elseif ('Peter' == $guide )
    		{ include(TEMPLATEPATH . '/includes/guides/guides-peter.php'); }
    		elseif ('Shane' == $guide )
    		{ include(TEMPLATEPATH . '/includes/guides/guides-shane.php'); }
    		elseif ('Dan' == $guide )
    		{ include(TEMPLATEPATH . '/includes/guides/guides-dan.php'); }
    		elseif ('Lynne' == $guide )
    		{ include(TEMPLATEPATH . '/includes/guides/guides-lynn.php'); }
    		elseif ('Alex' == $guide )
    		{ include(TEMPLATEPATH . '/includes/guides/guides-alex.php'); }
    		  else {$out .= $guide . $between;}
    	  } echo substr($out,0,-2);} ?>
    <?php }	?>
  2. stvwlf
    Member
    Posted 5 years ago #

    Hi

    This code get_post_meta($post->ID, "gt_guides", false);
    creates an array that has one element for each time a custom field with the key value of "gt_guides" is defined for the post in question.

    And this code

    $gt_guides=get_post_meta($post->ID, "gt_guides", false);
    	$out='';
    	$between =', ';
    	if ($gt_guides) {
    	  foreach($gt_guides as $guide) {

    runs the loop of code once for each element in the get_post_meta array. So if gt_guides is defined 3 times in one post, the code runs 3 times.

    You are not checking the value of the custom field - you are just processing once for each instance of the key.

  3. jonoc73
    Member
    Posted 5 years ago #

    What would I take out if I didn't want a comma'ed list? I just wanted a list of the included templates?

    Thanks J

  4. stvwlf
    Member
    Posted 5 years ago #

    Hi, I'm sorry, I don't understand what you are asking for.

    Give an example of typical custom fields you will have on a post - their keys and values, listing all of them from one post - keys and values

    And then, what do you want to do with them. If there are 3 for example and you only want one to be used, how do you determine which to use.

  5. Mark / t31os
    Moderator
    Posted 5 years ago #

    jonoc, if you only need to check the field for one value then get_post_meta should be set to true, and you don't need the foreach loop.

    If that's the case, then something like this may get you started..

    <?php
    	$gt_guides=get_post_meta($post->ID, "gt_guides", true);
    	if ($gt_guides) {
    		switch($gt_guides) {
    			case 'Phil':
    			case 'Ro':
    			case 'Nic':
    			case 'Jef':
    			case 'Ben':
    			case 'Brend':
    			case 'Crys':
    			case 'Kev':
    			case 'Peter':
    			case 'Shane':
    			case 'Dan':
    			case 'Lynne':
    			case 'Alex':
    				$flname = 'guides-'.strtolower($gt_guides).'.php';
    				if(file_exists(TEMPLATEPATH . '/includes/guides/'.$flname.'')) {
    					include_once(TEMPLATEPATH . '/includes/guides/'.$flname.'');
    				}
    				else {
    					echo 'Failed to include (TEMPLATEPATH . \'/includes/guides/'.$flname;
    				}
    			break;
    			default:
    				// Something to do when the field has a value but not matched to the cases above
    			break;
    		}
    	}
    	else {
    		// If you hit this 'ELSE', then the custom field (post meta field) does not exist for this post/page
    		echo 'No value in custom field gt_guides';
    	}
    ?>

    Still stvwlf is right, a little clarification would be most helpful... :)

  6. jonoc73
    Member
    Posted 5 years ago #

    Sorry guys, yes I should have written more to explain!

    At the moment the above script was written to display guide names if they were filled in in a custom field. They would display like: John, Kev, Peter

    Instead of just their name I wanted to display a div containing things like a small images, email and phone details - this was via the TEMPLATEPATH line.

    However I am still using the same code as if they were a list of names including the 'comma' between them.

    So what would I take out of the above code?

    Thanks for your help once more.

    Jono.

  7. stvwlf
    Member
    Posted 5 years ago #

    Please answer the questions I asked before...

    Give an example of typical custom fields you will have on a post - their keys and values, listing all of them from one post - keys and values

    And then, what do you want to do with them. If there are 3 for example and you only want one to be used, how do you determine which to use.

  8. jonoc73
    Member
    Posted 5 years ago #

    Ok, I have a custom fields key of gt_guides and there are a list of checkboxes for the staff giving values of Phil Rob Nic and Jef etc. Each has their own php file with a div and some contact info like this:

    <div id="guidewrap">
    <div class="guide"><a href="http://www.website.com/guides/phil"><img src="/images/guides/phil.png"/></a></div>
    <div class="details"><a href="http://www.website.com/guides/phil">Phil</a></div>
    <div class="contact">Email: <a href="mailto:phil@website.com" title="Email">phil@website.com</a></div>

    I want to display the div containing their contact details if their name is selected in the checkbox. If their name isn't checked then I don't want anything to show.

    The above code does word but I was wanting to know what I should remove from it as it was originally written to echo a list of names like Phil, Rob, Nic instead of including a php file.

    Thanks Jono.

  9. stvwlf
    Member
    Posted 5 years ago #

    Hi -
    when you say

    The above code does work
    I am assuming you are referring to your original code that started this thread, and not the code that t31os_ suggested - let me know if that is not what you mean.

    You said in the first post that

    When this script runs with only one guide selected it displays 3 times.

    The reason it displays three times is because

    I have a custom fields key of gt_guides and there are a list of checkboxes for the staff giving values of Phil Rob Nic and Jef etc.

    I assume from this that more than one custom field with the same KEY is being created for each post. Your original code calls get_post_meta, which creates an array that contains one element for every OCCURRENCE of the key gt_guides. Your code is not checking the VALUE assigned to an occurrence of that key, it is executing once per occurrence. So it is running whether or not the box is checked. You need to include a test that checks the value, and only run the code when the box is checked.

    There is a function you call, <?php get_post_custom_values($key, $post_id); ?>
    ( see http://codex.wordpress.org/Function_Reference/get_post_custom_values ) to loop through and check the values assigned.

    I think this addresses what you are after.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.