554 posts
  • Bought between 10 and 49 items
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Football Contest Participant/Runner-up
  • Has been a member for 4-5 years
  • Referred between 1 and 9 users
  • United Kingdom
Gareth_Gillman says

I am having a slight issue with WP’s custom fields

I am using custom fields to display a sidebar for each page, and using the following code:

<?php global $wp_query;
   $postid = $wp_query?>post->ID;
    $sidebar = get_post_meta($postid, 'Sidebar', true);
    echo $sidebar;
    wp_reset_query();
?>

This code displays the actual custom field but I want to display the code that is within the file, which is stored in the custom field e.g.

If I enter “full” into the custom post type, it will look for and display “sidebar-full.php”.

Thanks in advance

7 posts
  • Exclusive Author
  • Has been a member for 3-4 years
  • United States
harrisonstatham says

Hey I thought I would try and help you out!

Im not exactly sure if I understand what your trying to do, but I’ll give it a shot!

Are you trying to display a custom sidebar for pages vs posts? Or display a custom sidebar for individual posts / pages.

“If I enter “full” into the custom post type, it will look for and display “sidebar-full.php”.”

If the quote above is what your trying to achieve now, maybe you should take a look at this article: http://codex.wordpress.org/Function_Reference/get_template_part

Not sure if I was any help, but I thought I would give it a go! Good luck with what your trying to do!

Harrison

554 posts
  • Bought between 10 and 49 items
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Football Contest Participant/Runner-up
  • Has been a member for 4-5 years
  • Referred between 1 and 9 users
  • United Kingdom
Gareth_Gillman says

That doesn’t work, but thanks for the idea, it’s a bit more complicated as I need to incorporate the custom fields.

Anyone able to lend a hand getting this sorted?

219 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 1 and 9 users
  • Sold between 1 000 and 5 000 dollars
  • United Kingdom
NuclearThemes says
Gareth_Gillman said
I am having a slight issue with WP’s custom fields

I am using custom fields to display a sidebar for each page, and using the following code:

<?php global $wp_query;
   $postid = $wp_query?>post->ID;
    $sidebar = get_post_meta($postid, 'Sidebar', true);
    echo $sidebar;
    wp_reset_query();
?>

This code displays the actual custom field but I want to display the code that is within the file, which is stored in the custom field e.g.

If I enter “full” into the custom post type, it will look for and display “sidebar-full.php”.

Thanks in advance
I have an idea where you are going with this and also what the problem is so can you clear something up for me before I answer? Are you adding all the HTML into the posts custom field under sidebar, entering either true or false to display or not, or entering the name of the sidebar you want to show?
554 posts
  • Bought between 10 and 49 items
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Football Contest Participant/Runner-up
  • Has been a member for 4-5 years
  • Referred between 1 and 9 users
  • United Kingdom
Gareth_Gillman says

All the html code is in the sidebar file e.g. sidebar-full.php

In the custom field, the client will enter sidebar-full or just full (depending on complexity) and it will show the the sidebar-full.php as the sidebar for that page.

814 posts
  • Author had a Free File of the Month
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 1 and 9 items
  • Referred between 1 and 9 users
  • Serbia
  • Has been a member for 5-6 years
rvision_ says

Simple include doesn’t do the trick?

554 posts
  • Bought between 10 and 49 items
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Football Contest Participant/Runner-up
  • Has been a member for 4-5 years
  • Referred between 1 and 9 users
  • United Kingdom
Gareth_Gillman says

Unfortunately not as each page will have a separate sidebar, and for an include I would need a large amount of templates for each page.

741 posts
  • Has been a member for 5-6 years
  • Sold between 50 000 and 100 000 dollars
  • Exclusive Author
  • Most Wanted Bounty Winner
  • Bought between 50 and 99 items
  • Poland
  • Referred between 10 and 49 users
Orbital_Themes says

Not sure if I get what you’re trying to do, but right now (with your code) you are only getting the custom field value and echoing it.

If you, however, want WordPress to look for sidebar-full.php file when the user enters full into the custom field, and then include the sidebar-full.php file you’d need to do something like that:

<?php $sidebar = 'sidebar-' . $sidebar . '.php';
    get_template_part( $sidebar );
?>
554 posts
  • Bought between 10 and 49 items
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Football Contest Participant/Runner-up
  • Has been a member for 4-5 years
  • Referred between 1 and 9 users
  • United Kingdom
Gareth_Gillman says
pogoking said
Not sure if I get what you’re trying to do, but right now (with your code) you are only getting the custom field value and echoing it.

If you, however, want WordPress to look for sidebar-full.php file when the user enters full into the custom field, and then include the sidebar-full.php file you’d need to do something like that:

&lt;?php $sidebar = 'sidebar-' . $sidebar . '.php';
    get_template_part( $sidebar );
?&gt;

I need to grab the input from the custom field, then use that to display the sidebar with the same name, my code is now:

<?php global $wp_query;
   $postid = $wp_query?>post->ID;
    $sidebar = get_post_meta($postid, 'sidebar', true);
    wp_reset_query();
    $sidebar2 = 'sidebar-' . $sidebar . '.php';
    get_template_part( $sidebar2 );
    wp_reset_query();
  ?>

Any ideas?

219 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 1 and 9 users
  • Sold between 1 000 and 5 000 dollars
  • United Kingdom
NuclearThemes says
Gareth_Gillman said
pogoking said
Not sure if I get what you’re trying to do, but right now (with your code) you are only getting the custom field value and echoing it.

If you, however, want WordPress to look for sidebar-full.php file when the user enters full into the custom field, and then include the sidebar-full.php file you’d need to do something like that:

&lt;?php $sidebar = 'sidebar-' . $sidebar . '.php';
    get_template_part( $sidebar );
?&gt;

I need to grab the input from the custom field, then use that to display the sidebar with the same name, my code is now:

&lt;?php global $wp_query;
   $postid = $wp_query?&gt;post->ID;
    $sidebar = get_post_meta($postid, 'sidebar', true);
    wp_reset_query();
    $sidebar2 = 'sidebar-' . $sidebar . '.php';
    get_template_part( $sidebar2 );
    wp_reset_query();
  ?&gt;
Any ideas?
I’d go for <?php $sidebar = get_post_meta($post?>ID, 'Sidebar', true); if ($sidebar != '') { get_sidebar("$sidebar"); } ?>

I haven’t tested that but the basic theory is there. You may need to double check. That will work if someone types full, it will pull sidebar-full.php and so forth

by
by
by
by
by
by