• I am trying to define a number, to control a jcarousel instance, so that when a single page loads, it will figure out where to load the jcarousel menu and load up set it to start there…

    the navigation in the grey box on this (or any single) page is what I’m aiming to control… anyhow…

    I’m invoking a custom loop, setting the count to zero, then attempting to increment that number, until the loop comes up to the current $post->ID. Then it should stop and be able to echo out that number. I’ll probably have to do some sort of expression to the number, to get the jcarousel instance to load up on the proper page… but if I can get something other than zero, then perhaps I’ve got a chance.

    <?php
    $currentPost = $post->ID; // set current post ID //OK?
    $currentCat = get_query_var('cat');// get the query variable for the current category//OK?
    $arguments = array(
    	'category__in' => array($currentCat), //set category parameter//OK?
    	'showposts' => -1, //retrieve all posts (in the above category)//OK?
    	);
    ?>
    
    <?php
    $pageCount_query = new WP_Query($arguments);
    while ($pageCount_query->have_posts()) : $pageCount_query->the_post();
    
    global $pageCount_query;
    $pageCount_query->in_the_loop = true; // This line is added so that the_tags('') will work outside the regular loop.
    
    static $positionCount = 0;
    
    if( $post->ID == $currentPost ) { break; }  // if the current post ID in the loop, is the post ID, then stop the loop
    else { ?>
    
    <?php $positionCount++;  } ?>
    
    <?php endwhile; ?>
    <?php //endif: ?>
    
    <?php echo $positionCount; ?>
    <?php if ($positionCount < 5) { // positionCount is less than 5, start on page one
    	echo 'start: 1';
    	} elseif ($positionCount >= 5) { // positionCount is 5, or less than 8, start on page two
    	echo 'start: 2';
    	// if position count is 1 - 4, start on page 1
    	// if position count is 5 - 8, start on page 2
    	// if position count is 9 - 12, start on page 3
    	// if position count is 13 - 16, start on page 4
    	// and so on
    }?>
Viewing 13 replies - 1 through 13 (of 13 total)
  • Thread Starter guerrilladigital

    (@guerrilladigital)

    Thanks… I’ve seen both of those before, but I don’t know how to make use of them, in this instance. I want to loop thru UP TO the current post ID. Then stop. Not retrieve all posts. If you know of any working examples of this in the wild, that would be a huge help. Thanks.

    Thread Starter guerrilladigital

    (@guerrilladigital)

    Here’s an earlier iteration, that seemed to almost work.. but didn’t:

    <?php
    $currentPost = $post->ID; // set current post ID
    $currentCat = get_query_var('cat');// get the query variable for the current category
    $arguments = array(
    	'category__in' => array($currentCat), //set category parameter
    	'showposts' => -1, //retrieve all posts (in the above category)
    	);
    ?>
    
    <?php
    $pageCount_query = new WP_Query($arguments);
    $positionCount = 0;
    while ($pageCount_query->have_posts()) : $pageCount_query->the_post();
    global $pageCount_query;
    $pageCount_query->in_the_loop = true; // This line is added so that the_tags('') will work outside the regular loop.
    $positionCount++;
    if( $post->ID == $currentPost ) break;  // if the current post ID in the loop, is the post ID, then stop the loop
    ?>
    
    <?php endwhile; ?>
    
    <?php echo $positionCount; ?>
    <?php if ($positionCount < 5) { // positionCount is less than 5, start on page one
    	echo 'start: 1';
    	} elseif ($positionCount >= 5) { // positionCount is 5, or less than 8, start on page two
    	echo 'start: 2';
    	// if position count is 1 - 4, start on page 1
    	// if position count is 5 - 8, start on page 2
    	// if position count is 9 - 12, start on page 3
    	// if position count is 13 - 16, start on page 4
    	// and so on
    }?>

    Thread Starter guerrilladigital

    (@guerrilladigital)

    I’ll bet, since it’s on a single page, most of the problem lies here:

    $currentCat = get_query_var(‘cat’);// get the query variable for the current category

    need to determine what category it’s in.. easy to do on a category page.. not so easy on a single page.

    bloop.

    need to determine what category it’s in.. easy to do on a category page.. not so easy on a single page.

    <?php
    if ( is_single() ) {
      $cats = wp_get_post_categories($post->ID);
      if ($cats) {
        $first_cat = $cats[0];
      }
    }
    ?>
    Thread Starter guerrilladigital

    (@guerrilladigital)

    Thanks Michael H. That’s a much better way of retrieving the category that I figured out.

    I am getting it to retrieve some information… I will have more to report tomorrow. Here’s the code, so far.

    <?php
    $cats = wp_get_post_categories($post->ID);
      if ($cats) {
        $portfolio_cat = $cats[0];
      }
    echo 'category' . $portfolio_cat; //works
    $arguments = array(
    	'cat' => $portfolio_cat, //set category to be queried
    	'showposts' => -1, //retrieve all posts
    	);
    ?>
    
    <?php $countPosts_query = new WP_Query($arguments); ?>
    
    <!--stuff above this line, works - as far as I know -- >
    
    <?php
    $positionCount = 1; // intialize counting variable
    $currentPost = $post->ID; // set current post ID
    ?>
    
    <?php while ($countPosts_query->have_posts()) : $countPosts_query->the_post(); ?>
      <!-- Do special_cat stuff... -->
    <?php
    global $countPosts_query;
    $countPosts_query->in_the_loop = true; // This line is added so that the_tags('') will work outside the regular loop.
    ?>
    <?php
    echo 'position:' . $positionCount; //echo out the position count
    $positionCount++; //increment counting variable
    if( $post->ID == $currentPost ) break;  // if the current post ID in the loop, is the post ID, then stop the loop
    ?>
    <?php endwhile; ?>

    Thread Starter guerrilladigital

    (@guerrilladigital)

    I think now it’s just counting all the posts.. and not stopping when it reaches the current post. If it’s on the 3rd of 8 posts in the lists… i want it to count up to three, and break.

    attempting to make it go by using this:
    $currentPost = $post->ID; // set current post ID

    along with:
    if( $post->ID == $currentPost ) break;

    Any thoughts?

    Thread Starter guerrilladigital

    (@guerrilladigital)

    It seems to be pulling the post ID from the very last one in the loop each time, instead of pulling the post ID from the current post.

    That line

    $currentPost = $post->ID; // set current post ID

    would need to be after

    <?php while ($countPosts_query->have_posts()) : $countPosts_query->the_post(); ?>

    Thread Starter guerrilladigital

    (@guerrilladigital)

    hmmmm… that seemed to help, but now it seems to be retaining the same post ID for each category instead of the current post ID. It also stops looping through.

    <?php
    $cats = wp_get_post_categories($post->ID);
      if ($cats) {
        $portfolio_cat = $cats[0];
      }
    echo 'category' . $portfolio_cat; // test echo. this works
    $arguments = array(
    	'cat' => $portfolio_cat, //set category to be queried
    	'showposts' => -1, //retrieve all posts
    	);
    ?>
    
    <?php $countPosts_query = new WP_Query($arguments); ?>
    
    <!--stuff above this line, works - as far as I know -- >
    
    <?php
    $positionCount = 1; // intialize counting variable
    //$currentPost = get_the_ID(); // set current post ID
    ?>
    
    <?php while ($countPosts_query->have_posts()) : $countPosts_query->the_post(); ?>
      <!-- Do special_cat stuff... -->
    <?php
    $currentPost = $post->ID; // set current post ID
    global $countPosts_query;
    $countPosts_query->in_the_loop = true; // This line is added so that the_tags('') will work outside the regular loop.
    echo '<br />position before increment: ' . $positionCount; //echo out the position count
    $positionCount++; //increment counting variable
    echo '<br />position after increment: ' . $positionCount; //echo out the position count
    //if( $currentPost == !$post->ID ) {$positionCount++;}
    if( $currentPost == $post->ID ) break; // if the current post ID in the loop, is the post ID, then stop the loop
    echo 'currentPost var: ' . $currentPost . '<br />';
    echo 'post ID: ' . $post->ID . '<br />';
    ?>
    
    <?php endwhile; ?>
    
    <?php
    echo '<br />after the loop:<br />';
    echo 'currentPost var: ' . $currentPost . '<br />';
    echo 'post ID: ' . $post->ID . '<br />';
    // SEEMS TO BE ECHOING OUT THE SAME POST ID FOR EACH CATEGORY, instead of the current one.
    ?>
    Thread Starter guerrilladigital

    (@guerrilladigital)

    I got it working… so far so good. Here’s a link.

    <?php
    $cats = wp_get_post_categories($post->ID);
      if ($cats) {
        $portfolio_cat = $cats[0];
      }
    echo 'category' . $portfolio_cat; // test echo. this works
    $arguments = array(
    	'cat' => $portfolio_cat, //set category to be queried
    	'showposts' => -1, //retrieve all posts
    	);
    ?>
    
    <?php
    $positionCount = 0; // intialize counting variable
    // set current post ID
    global $wp_query;
    $currentPost = $wp_query->post->ID;
    // for whatever reason, it works.
    echo '<br />currentpost var: ' . $currentPost;
    echo '<br /><br /><br /><br />';
    ?>
    
    <?php $countPosts_query = new WP_Query($arguments); ?>
    
    <?php while ($countPosts_query->have_posts()) : $countPosts_query->the_post(); ?>
    
    <?php
    global $countPosts_query;
    $countPosts_query->in_the_loop = true; // This line is added so that the_tags('') will work outside the regular loop.
    echo '<br />position before increment: ' . $positionCount; //echo out the position count
    $positionCount++; //increment counting variable
    echo '<br />position after increment: ' . $positionCount; //echo out the position count
    if( $currentPost == $post->ID ) break; // if the current post ID in the loop, is the post ID, then stop the loop
    echo '<br />currentPost var: ' . $currentPost . '<br />';
    echo '<br />post ID: ' . $post->ID . '<br />';
    ?>
    
    <?php endwhile; ?>
    
    <?php
    echo '<br />after the loop:<br />';
    echo 'currentPost var: ' . $currentPost . '<br />';
    echo 'post ID: ' . $post->ID . '<br />';
    
    ?>
    
    <!--stuff above this line, works - as far as I know. I need help figuring out the pagination -- >
    
    <?php
    
    // if $positionCount is 1 - 4, echo 'start: 1'
    // if $positionCount is 5 - 8, echo 'start: 5'
    // if $positionCount is 9 - 12, echo 'start: 9'
    // if $positionCount is 13 - 16, echo 'start: 13'
    // and so on
    
    // setup a range to loop
    $range_to_go_thru = range(1, $top_num);
    // split the range into groups of 4
    $chunks = array_chunk($newest, 4);
    // look thru each group
    foreach ($chunks as $key => $chunk)
    {
    // is it in this group?
    if (in_array($positionCount, $chunk))
    {
    // found you
    // the $key is 1 less than your 'page'
    $page = $key + 1;
    echo 'start: ' . $page;
    // stop looping
    break;
    }
    }
    ?>

    Thread Starter guerrilladigital

    (@guerrilladigital)

    I’m almost there.. I have the PHP script doing what I want it to… but…

    first, here’s the script:

    <?php
    $cats = wp_get_post_categories($post->ID);
      if ($cats) {
        $portfolio_cat = $cats[0];
      }
    //echo 'category' . $portfolio_cat; // test echo.
    $arguments = array(
    	'cat' => $portfolio_cat, //set category to be queried
    	'showposts' => -1, //retrieve all posts
    	);
    ?>
    
    <?php
    $positionCount = 0; // intialize counting variable
    // set current post ID
    global $wp_query;
    $currentPost = $wp_query->post->ID;
    // for whatever reason, it works.
    //echo '<br />currentpost var: ' . $currentPost; // test echo.
    //echo '<br /><br /><br /><br />'; // test echo.
    ?>
    
    <?php $countPosts_query = new WP_Query($arguments); ?>
    
    <?php while ($countPosts_query->have_posts()) : $countPosts_query->the_post(); ?>
    
    <?php
    global $countPosts_query;
    $countPosts_query->in_the_loop = true; // This line is added so that the_tags('') will work outside the regular loop.
    //echo '<br />position before increment: ' . $positionCount; //echo out the position count // test echo.
    $positionCount++; //increment counting variable
    //echo '<br />position after increment: ' . $positionCount; //echo out the position count // test echo.
    //echo '<br />currentPost var: ' . $currentPost . '<br />'; // test echo.
    //echo '<br />post ID: ' . $post->ID . '<br />'; // test echo.
    
    if( $currentPost == $post->ID ) { // if currentPost is equal to the Post ID then do this WORKS
    
    	$positionCountRange = range(1, $positionCount); //setup a range to loop, starts with 1 and ends with position count when we hit the current post ID to stop
    	$chunks = array_chunk($positionCountRange, 4); // split the range into chunks of 4 - this creates an array
    	//print_r(array_chunk($positionCountRange, 4)); // WORKS outputs 'Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) [1] => Array ( [0] => 5 [1] => 6 ) )'
    	// look thru each chunk
    	foreach ($chunks as $key => $chunk) // separate into individual chunks to test
    	{
    	// is it in this group?
    	if (in_array($positionCount, $chunk))
    	{
    	// found you
    	if ($key == 0) {
    		$navPositionStart = 1; // if the key is zero or one, set it to one so it'll echo the first page position
    	} else { //if the key is one or larger, use this offset to determine page start number
    		$navPositionStart = 1 + $key * 3 + $key * 1;
    	}
    	//echo '<br />key: ' $key; //echo the key to see what it is. I'm guessing it's always null
    	//echo '<br />start: ' . $navPositionStart; //output start number WORKS
    	echo 'start: ' . $navPositionStart; //output start number WORKS
    	// stop looping
    	break;
    	}
    	}
    }
    if( $currentPost == $post->ID ) break; // if the current post ID in the loop, is the post ID, then stop the loop WORKS
    
    ?>
    <?php endwhile; ?>
    
    <?php
    //echo '<br /><br /><br /><br />after the loop:<br />'; // test echo.
    //echo 'currentPost var: ' . $currentPost . '<br />'; // test echo.
    //echo 'post ID: ' . $post->ID . '<br />'; // test echo.
    ?>

    BUT… I am attempting to include this script into my header.php file, so I can put the page number right into the javascript… which is essential for this navigation I’m using to work. Can anyone help me get this to work? I’m 99% there (I think.) Unless, for some reason wordpress just doesn’t allow looping inside the header.

    <script type="text/javascript">
    
    	var $j = jQuery.noConflict();
    	$j(document).ready(function() {
    	    $j('#mycarousel').jcarousel({
    	        vertical: true,
    	        scroll: 4,
    			<?php include (TEMPLATEPATH . '/includes/loop_determines_nav_pagination.php'); ?>
    	    });
    	});
    
    	</script>
    Thread Starter guerrilladigital

    (@guerrilladigital)

    okay… got the code to work, but the callback was just janky. So it’s been ported to a plugin… but NOW, the plugin version doesn’t work. it seems as if the “IF” statement just fails. Does anyone know why this might happen? All of the variables seem to work… It’s also outputting numbers at the top of the page.. can’t figure out why. https://www.platypus-ad.com/new2009/our-work/advertising/bayfield/

    <?php
    /*
    Plugin Name: JQUERY Carousel
    Plugin URI: https://platypus-ad.com
    Description: A hack so we can get that silly number into the head of our pages!
    Author: Marty
    Version: .01
    Author URI: https://platypus-ad.com
    */ 
    
    function carousel_head_js() {
    
    global $wp_query;
    global $countPosts_query;
    
    $currentPost = $wp_query->post->ID;
    
    $category = get_the_category();
    $parent = $category[0]->category_parent;
    $cats = wp_get_post_categories($currentPost);
      if ($cats) {
        $portfolio_cat = $cats[0];
      }
    
    $arguments = array(
    	'cat' => $portfolio_cat,
    	'showposts' => -1,
    	);
    
    $countPosts_query = new WP_Query($arguments);
    $countPosts_query->in_the_loop = true;
    
    $positionCount = 0;
    $positionCount++;
    
    while ($countPosts_query->have_posts()) : $countPosts_query->the_post();
    //while (have_posts()) : the_post();
    	echo $positionCount;
    
    	if($currentPost == the_ID()) {
    		$positionCountRange = range(1, $positionCount);
    		$chunks = array_chunk($positionCountRange, 4);
    		foreach ($chunks as $key => $chunk) {
    			if (in_array($positionCount, $chunk)) {
    				if ($key == 0) {
    					$navPositionStart = 1;
    				} else {
    					$navPositionStart = 1 + $key * 3 + $key * 1;
    				}
    				echo $navPositionStart;
    				break;
    			}
    		}
    	}
    
    if($currentPost == the_ID()) break;
    
    endwhile;
    
    ?>
    	<script type="text/javascript">
    
    	var $j = jQuery.noConflict();
    	$j(document).ready(function() {
    	    $j('#mycarousel').jcarousel({
    	        vertical: true,
    	        scroll: 4,
    			start: 5
    	    });
    	});
    
    	</script>
    <?php
    }
    ?>
Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Trying to count posts UP TO a particular post ID’ is closed to new replies.