Forums

Loop Queries Effecting Sidebar Queries (6 posts)

  1. tadramgo
    Member
    Posted 3 years ago #

    Hi, I'm pretty new to wordpress, so I'm not sure about all of it's nuances.

    I'm running a basic (no non-default plugins) setup as a CMS, writers website. I have two custom fields for each post. "Post Author" and "Featured Details". There are a few catagories, Poetry, Prose, Editorial etc.

    Basically my content and comments are displaying fine with The Loop. However I am using custom Archive Pages that use the

    foreach($posts as $post) :

    loop formatting, much like the main page, displaying page titles / author details within it as appropriate. However my sidebar uses

    if (get_post_meta($post->ID, "Poem Author", 1)) { etc }

    To create a menu with "Authors Other Works" (another foreach) loop depending on the meta data. This is fine on pages belonging to that author, however on these custom archive pages it means that you have a list of "Other Works" for whoever was last on the Archive list. Whoever was last accessed by the loop. I don't want this to happen. Is there anyway to separate loops from each other? Or do I need to find a different method of achieving this?

    I hope I'm making sense. Thanks for your help.

    Tom ~

  2. moshu
    Member
    Posted 3 years ago #

    What would you want on the custom archives page's (Page?) sidebar? Can't you make
    a) a conditional in it to show something else
    or
    b) another sidebar for the archives page?

  3. Kafkaesqui
    Moderator
    Posted 3 years ago #

    The issue is the use of $posts in your custom code, which is whacking the original posts object (i.e. $posts) WordPress is passing along.

    foreach($posts as $post) :

    Something is missing here, that is where $posts is assigned the actual post(s) data. Is there something like:

    $posts = get_posts('blah blah blah'):

    just above it? Whatever the case, rename $posts throughout your code to something else, like $arc_posts.

  4. tadramgo
    Member
    Posted 3 years ago #

    I've been trying to solve the problem by defining values in the index.php and then relying on them to indicate to the sidebar that I want to show menu items...

    eg in index.php

    if (condition) {
    $useMenu = 1;
    }

    in sidebar.php

    if ($useMenu=1;) {
    show sidebar
    }

    however for some reasons these variables aren't available from one page to another using the wordpress "get_sidebar()" function. After testing for a while this is what I've concluded... is there anyway to make variables global, or pass variables to the function?

    I know this is a bit of a hacky solution, but if it works...

    cheers,
    tom

  5. Kafkaesqui
    Moderator
    Posted 3 years ago #

    if (condition) {
    global $usemenu;
    $useMenu = 1;
    }

    And in sidebar.php:

    global $usemenu;
    if ($useMenu == 1) {
    show sidebar
    }

  6. tadramgo
    Member
    Posted 2 years ago #

    Globals! I completely forgot about them... isn't global variables usually turned off in php? Or does defining them as you did override the basic php settings?

    I got around all this by using include() rather than wp_sidebar etc...

Topic Closed

This topic has been closed to new replies.

About this Topic