0

I've written a function to display a custom sidebar, which is outputted as a sidebar. On some pages, I have a Custom Field called 'show_sidebar', which I can assign as 'false' on pages which I do not want to display the Sidebar.

On the pages, I have some more shortcodes set up to initiate a loop for specific post categories. I need to show the sidebar on each page (through page.php in theme files), with 4 posts printed, each of the same category of the main posts (they are all the same category).

Problem is, to get the Custom Field, I need to use the global $post; method to get the Page ID and not the Post ID (none of the posts will have the Custom Field set as there is no point). This all works fine and I can get the value of Custom Field just fine. However, when I echo the shortcode, I need the ID of the posts on the page (generated through the loop shortcode I wrote).

Here is my code:

function showSidebar() {
    global $post; // so I get the Page ID, not the Post IDs
    $customField = get_post_meta($post->ID, 'show_sidebar', true);
    if ($customField == 'true' || empty($customField)) {
        $showSidebar = true;
    } elseif ($customField = 'false') {
        $showSidebar = false;
    } // get value of Custom Field 'show_sidebar' and check if it was true or false
    $postCat = get_the_category($post->ID); // here is where I need to use the Post ID, not the Page ID
    if ($showSidebar) {
        echo do_shortcode('[sidebar category="'. $postCat[0]->name .'" no_of_posts="4" title="Latest '. $postCat[0]->name .'"]');
    } // shortcode is initiated
}

So my question is, how can I use the Page ID in one part of the function, and Post ID in another part?

5
  • I'm a little confused on what you're asking exactly. If you Need to differentiate between Pages and Posts you can use the $post->post_type - one will be a page, the other a post. Another method would be to use conditionals such as is_home() or is_single(). Again not 100% sure what you're looking for so hope that helps.
    – Howdy_McGee
    Commented Feb 10, 2014 at 19:35
  • Where and how are you calling this function? I don't see a problem here, as you simply want to access two very different things. Please clarify (and maybe rephrase) what you want to do/achieve.
    – tfrommen
    Commented Feb 10, 2014 at 19:41
  • There are multiple ways to achieve this, you could use a global variable or a static variable, more information about the static approach plus information about the OOP class based one on that answer. Or, if it fits, you can just setup a query variable for that. Another possibility are sessions or cookies if you need that. Commented Feb 10, 2014 at 20:27
  • hey, thanks for the comments :) Obviously I didn't explain it very clearly, sorry. I am either calling this function through PHP (using the do_shortcode() method), or on a page using the shortcode itself, but for the question, this way doesn't matter. Effectively, I need to be able to access the Custom Field of the current page, and the Category/ies of the posts that are on the page which are generated by a shortcode ( [loop] ). Hope that makes it clearer. It's kinda difficult to explain :/
    – Tom Oakley
    Commented Feb 10, 2014 at 21:22
  • Actually all you need to know, you can find inside my first comment. Good luck! Commented Feb 10, 2014 at 22:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.