• Resolved the_enn

    (@the_enn)


    I’m having a very odd problem on something that was working days ago.

    In my sidebar.php, the code looks at the post ID to determine which sidebar to load. This has been working for over a year.

    However, only on some pages, it keeps going to the default sidebar, because when the code looks for the post ID, the value coming back is 1. On some pages, the post ID is coming back with the right value. On others, the value is 1.

    Here’s the top of sidebar.php file:

    global $post;
    print_r($post); //debugging
    global $forumids;
    echo 'Post id = ' . $post->ID //debugging

    This displays the array for post with the ID of 1, then the ID itself of 1.

    The content of the page loads on the site, but the sidebar.php file is not recognizing the post ID.

    Any suggestions? I haven’t added any plugins or upgraded core in several months.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Hello!

    can you try $post_it = get_queried_object_id();

    to get the post?

    can I also recommend an alternative? there’s this plugin Page Widgets – it’s totally awesome – it let’s you override the widgets on a per-page basis. You set the sidebar as normal, but there’s an area on the page/post edit view with the same widget area you’d find on the Appearance->widgets interface. It lets you override the global widget configuration for SPECIFIC POSTS.

    Blows my mind.

    You should try it out!

    Thread Starter the_enn

    (@the_enn)

    This returned a value of 0.

    $post_it = get_queried_object_id();
    echo 'post_it='.$post_it.'<br>';

    On some pages it returns the correct post id from sidebar.php. And the templates all use the same sidebar.php file. I am really stumped on this one.

    Thanks for the plugin suggestion. I can’t implement now, but in the future it sounds worth looking at.

    can you check out the current query in the affected sidebar.php? check out the global $wp_query from the sidebar.php? maybe there’s a rogue query that’s resetting things?

    are only specific templates affected? can you post the code from those templates?

    Thread Starter the_enn

    (@the_enn)

    It looks like only certain templates are affected. The confusing thing is I confirmed they all use the same line of code to get the sidebar:

    get_sidebar();

    Here’s something I just found: I went to the dashboard, and on one of the offending pages, changed the template from the ‘bad’ one to the default template (that correctly loads the sidebar on other pages). This time, the post ID displayed in sidebar.php was the correct post ID and was not 1!

    Any ideas what would account for that? These templates haven’t changed in a while.
    Could it be something else throwing it off? I can post the template code in entirely, but it’s fairly long. Can do if it helps?

    can you post the offending template in the forum? There’s probably something going wrong there.

    Thread Starter the_enn

    (@the_enn)

    Thanks for helping Bryan! ??

    I’m noticing that (EDIT) all the templates are having the same problem with the exception of the Default. I even cut and paste the code from the working default template over the offending template, saved, refreshed, and still the same problem exists. Makes me wonder if something else is afoot.

    In any case, here’s code from one of the offending templates:

    <?php
    /*
     Template Name: Publications
    */
    
     get_header(); ?>
    
    	<div id="content_box" <?PHP if(get_field('nosidebar') && get_field('right_sidebar')=='') { echo ' class="novhairline"'; } ?>>
    
    		<div id="left_box"><div id="left_box2">
    
    			<div id="content" class="page">
    
    				<div id="content_inner"<?PHP if(get_field('nosidebar') && get_field('right_sidebar')=='') { echo ' class="nosidebar"'; } ?>>
    
    				<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <?php
    $thisid=get_the_ID();
    $photo=wp_get_attachment_image_src(get_post_thumbnail_id($pid[$i]), 'single-post-thumbnail');
    
     ?>
    					<h1><?php the_title(); ?></h1>
    					<div class="format_text">
    <?php
    $photolink=get_field('link_for_featured_image');
    //echo 'photolink is:'.$photolink;
    if($photo!='') {
    	if($photolink!='') {
                if(stripos($photolink,'ed.gov')===FALSE) {
                    echo '<a href="' . $photolink. '">';
                } else { ?>
                     <a onclick="_gaq.push(['_trackEvent','External','IES',this.href]);" href="<?php echo $photolink.'">';
                }
    
            }
    	echo '<img src="' . $photo[0] . '" class="wrapleftOutline" alt="photo">';
    	if($photolink!='') { echo '</a>'; }
    }
    the_content();
    
     ?>
    
                        </div>
    
    				</div>
    
    				<?php endwhile; endif; ?>
    
    			</div>
    
    			<?php include (TEMPLATEPATH . '/left_bar.php'); ?>
    
    		</div></div>
    
    		<?php 
    
    		if(get_field('right_sidebar')!='') {
                        echo '<div id="right_bar" class="sidebar"><ul class="sidebar_list"><li class="widget">';
                        echo str_replace('<h2>','<h2 class="widgettitle">',get_field('right_sidebar'));
                        echo '</li></ul></div>';
    		} else if(!get_field('nosidebar')) {
                            echo "single-publications.php, nosidebar=" .get_field('nosidebar') ;
                            get_sidebar();
                        } ?>
    		<?php //include (TEMPLATEPATH . '/searchform.php'); ?>
    
    	</div>
    <?php include (TEMPLATEPATH . '/sharetop.php'); ?>
    <?php get_footer(); ?>

    I’m kinda confused about what’s going on at

    echo "single-publications.php, nosidebar=" .get_field('nosidebar') ;

    can you simplify this template to try to eliminate potential issues?

    maybe try removing the code between <div class="format_text"> ?

    also, can you try removing <?php include (TEMPLATEPATH . '/left_bar.php'); ?> ?

    Thread Starter the_enn

    (@the_enn)

    Bryan, you solved it.

    This was the culprit:

    include (TEMPLATEPATH . '/left_bar.php');

    This file was calling a jquery library that doesn’t exist anymore. And for some reason, the result was the post ID in the sidebar.php file coming up as 1. I don’t understand why that would happen, but it’s good now. I removed the include and the sidebar is up!

    This was driving me crazy and I wouldn’t have found it without your help.

    THANK YOU!! Does this forum allow points I can send ya? ??

    an ancient jquery library! I’m happy to help! – If you make it to Wordcamp Chicago – high fives all around!!!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘$post->ID returning '1'’ is closed to new replies.