• @PhilipHoy

    (@venomphil)


    Hi WordPress Community,

    So I have a Custom Query running with Ajax but cannot figure out how to get the do_shortcode to run.

    The function is recognized, if you run it with no argument you get an ‘expecting argument 1’ error, the shortcode itself isn’t recognized.

    Template.php

    <script>
    				var newspage = 1;
    				var $ = jQuery;
    				function browsenews(x) {
    					newspage = x;
    					$.post('/newsloader', { derp: x}, function(result) {
    						$('.newswrapper').html(result);
    					});
    				}
    			</script>
    			<!-- START news -->
    			<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    			<div id="content" class="news">
    				<div id="inner-content" class="wrap cf">
    					<main id="main" class="m-all t-3of3 d-7of7 cf" role="main" itemscope itemprop="mainContentOfPage" itemtype="http://schema.org/Blog">
    						<article id="post-<?php the_ID(); ?>" <?php post_class( 'cf' ); ?> role="article" itemscope itemtype="http://schema.org/BlogPosting">
    
    							<header class="article-header">
    								<h1 class="page-title">news</h1>
    							</header>
    
    							<section class="entry-content cf" itemprop="articleBody">
    							<?php
    							$newsArgs = array(
    							'post_type' => 'post',
    							'cat' => '3',
    							'posts_per_page' => 1,
    							'order' => 'ASC' );
    							$philquery = new WP_Query($newsArgs);
    							echo '<div id="news-posts"><div class="newswrapper">';
    								while ($philquery->have_posts()) : $philquery->the_post();?>
    									<li>
    										<div class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
    										<div class="content"><?php echo the_content(); ?></div>
    										<div class="share"><?php echo do_shortcode('[addtoany]') ?></div>
    										<div class='navigation'></div>
    									</li>
    								<?php endwhile; ?>
    								<input value='Next' type='button' onclick='browsenews(2);'>
    							</div>
    
    							</div>
    							</section>
    
    						</article>
    					</main>
    				</div>
    			</div>
    			<?php endwhile; endif; ?>
    			<!-- END news -->

    newsloader.php

    $derp = intval($_POST['derp']);
    $newsArgs = array(
    	'post_type' => 'post',
    	'posts_per_page'=>1,
    	'paged'=>$derp,
    );
    $philquery = new WP_Query($newsArgs);
    $next = array(
    	'post_type' => 'post',
    	'posts_per_page'=>1,
    	'paged'=>$derp+1,
    );
    $nextq = new WP_Query($next);
    print_r(do_shortcode('[addtoany]'));
    while ($philquery->have_posts()) : $philquery->the_post();?>
    	<li>
    		<div class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
    		<div class="content"><?php the_content(); ?></div>
    		<div class="share"><?php echo do_shortcode('[addtoany]') ?></div>
    	</li>
    <?php endwhile;
    	if ($derp > 1) {
    		echo "<input value='Back' type='button' onclick='browsenews(".($derp-1).");'>";
    	}
    
    	if ($nextq->found_posts > 0) {
    		echo "<input value='Next' type='button' onclick='browsenews(".($derp+1).");'>";
    	}
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    What does the shortcode handler look like? It cannot echo out content. All content should be collected into a single variable that is returned from the handler and do_shortcode() to be echoed out at the appropriate location.

    Echoing content out from a shortcode handler leads to unpredictable placement and sometimes missing content.

    The same might apply to AJAX content. I’ve always echoed out AJAX content from a single collector variable. That of course works. I’m not so sure about mixed HTML and multiple PHP echoes in an AJAX handler context. It may be OK, or a source of your problem.

Viewing 1 replies (of 1 total)
  • The topic ‘Custom Query, Ajax Pagination and do_shortcode’ is closed to new replies.