743 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+3 more
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
  • Has been part of the Envato Community for over 3 years
  • Sells items exclusively on Envato Market
  • Located in 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

743 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+3 more
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
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+2 more
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?
743 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+3 more
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.

815 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Contributed a free file of the month
+3 more
rvision_ says

Simple include doesn’t do the trick?

743 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+3 more
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
  • Had an item featured on Envato Market
  • Won a Most Wanted contest
  • Has been part of the Envato Community for over 5 years
  • Has sold $40,000+ on Envato Market
+4 more
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 );
?>
743 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+3 more
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
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+2 more
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