WordPress.org

Ready to get started?Download WordPress

Forums

Putting php code inside php code (11 posts)

  1. Sazuka
    Member
    Posted 3 years ago #

    This is my base code and i want to enter another php code into it.
    I am getting errors all the time because im doing something wrong

    <? if (is_single('2466')){
       query_posts($query_string . '&cat=72');
    
    //THE CODE SHOULD BE HERE.
    }

    -----------------------
    The code that should be put inside is this:

    <?php if(have_posts()) : while(have_posts()) : the_post('exclude=3'); ?>
    
    	<div class="entry2">
    	<div class="post" id="post-<?php the_ID(); ?>">
    
    	<b><a href="<?php the_permalink() ?>" rel="bookmark" title="Read the rest of <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    	</b>
    	<span class="content">
    	<?php the_excerpt(); ?>
    	</span>
    	<div class="clearfloat"></div>
    <?php edit_post_link('edit', '', ''); ?>
    	<div class="allinfos">
    
    	</div>
    
    	</div>
    	</div>
    	<?php endwhile; ?>
    
    	<div style="padding:10px;"><!-- page navi -->
    	<?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>
    	<!-- page navi end --></div>
    
    	<?php else : ?>
    
    	<h2 class="center">Not Found</h2>
    	<p class="center">Sorry, but you are looking for something that isn't here.</p>
    	<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    
    <?php endif; ?>
    
    </div>
  2. m7csat
    Member
    Posted 3 years ago #

    What kind of errors are you getting? I suspect it has to do with the <?php if(have_posts()) : while(have_posts()) : the_post('exclude=3'); ?> and the endwhile, else: and endif; that happens later in the code.

  3. Sazuka
    Member
    Posted 3 years ago #

    Parse error: syntax error, unexpected '<'

    I think im doing a newbie mistake...This is how I put the code inside:

    <? if (is_single('2466')){
       query_posts($query_string . '&cat=72');
    
    <div id="twitter">
    <h2>Latest Articles</h2>
    <ul id="twitter_update_list"></ul>
    <br/>
    
    <?php if(have_posts()) : while(have_posts()) : the_post('exclude=3'); ?>
    
    	<div class="entry2">
    	<div class="post" id="post-<?php the_ID(); ?>">
    
    	<b><a href="<?php the_permalink() ?>" rel="bookmark" title="Read the rest of <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    	</b>
    	<span class="content">
    	<?php the_excerpt(); ?>
    	</span>
    	<div class="clearfloat"></div>
    <?php edit_post_link('edit', '', ''); ?>
    	<div class="allinfos">
    
    	</div>
    
    	</div>
    	</div>
    	<?php endwhile; ?>
    
    	<div style="padding:10px;"><!-- page navi -->
    	<?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>
    	<!-- page navi end --></div>
    
    	<?php else : ?>
    
    	<h2 class="center">Not Found</h2>
    	<p class="center">Sorry, but you are looking for something that isn't here.</p>
    	<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    
    <?php endif; ?>
    
    </div>
    
    }
  4. chinmoy
    Member
    Posted 3 years ago #

    Try this

    <?php
     global $query_string;
    if (is_single('2466')){
       query_posts($query_string . '&cat=72&category__not_in=3');
       if(have_posts()) : while(have_posts()) : the_post(); ?>
    
    	<div class="entry2">
    	<div class="post" id="post-<?php the_ID(); ?>">
    
    	<b><a href="<?php the_permalink() ?>" rel="bookmark" title="Read the rest of <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    	</b>
    	<span class="content">
    	<?php the_excerpt(); ?>
    	</span>
    	<div class="clearfloat"></div>
    <?php edit_post_link('edit', '', ''); ?>
    	<div class="allinfos">
    
    	</div>
    
    	</div>
    	</div>
    	<?php endwhile; ?>
    
    	<div style="padding:10px;"><!-- page navi -->
    	<?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>
    	<!-- page navi end --></div>
    
    	<?php else : ?>
    
    	<h2 class="center">Not Found</h2>
    	<p class="center">Sorry, but you are looking for something that isn't here.</p>
    	<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    
    <?php endif; } ?>
    
    </div>
  5. chinmoy
    Member
    Posted 3 years ago #

    Try this

    <?php
     global $query_string;
    if (is_single('2466')){
       query_posts($query_string . '&cat=72&category__not_in=3');
       if(have_posts()) : while(have_posts()) : the_post(); ?>
    
    	<div class="entry2">
    	<div class="post" id="post-<?php the_ID(); ?>">
    
    	<b><a href="<?php the_permalink() ?>" rel="bookmark" title="Read the rest of <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    	</b>
    	<span class="content">
    	<?php the_excerpt(); ?>
    	</span>
    	<div class="clearfloat"></div>
    <?php edit_post_link('edit', '', ''); ?>
    	<div class="allinfos">
    
    	</div>
    
    	</div>
    	</div>
    	<?php endwhile; ?>
    
    	<div style="padding:10px;"><!-- page navi -->
    	<?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>
    	<!-- page navi end --></div>
    
    	<?php else : ?>
    
    	<h2 class="center">Not Found</h2>
    	<p class="center">Sorry, but you are looking for something that isn't here.</p>
    	<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    
    <?php endif; } ?>
    
    </div>
  6. Sazuka
    Member
    Posted 3 years ago #

    thx but i recieve:

    Warning: implode() [function.implode]: Invalid arguments passed in /var/customers/webs/side/wp-includes/query.php  on line 1907
  7. chinmoy
    Member
    Posted 3 years ago #

    <?php
     global $query_string;
    if (is_single('2466')){
       query_posts($query_string . '&cat=72&post__not_in=3');
       if(have_posts()) : while(have_posts()) : the_post(); ?>
    
    	<div class="entry2">
    	<div class="post" id="post-<?php the_ID(); ?>">
    
    	<b><a href="<?php the_permalink() ?>" rel="bookmark" title="Read the rest of <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    	</b>
    	<span class="content">
    	<?php the_excerpt(); ?>
    	</span>
    	<div class="clearfloat"></div>
    <?php edit_post_link('edit', '', ''); ?>
    	<div class="allinfos">
    
    	</div>
    
    	</div>
    	</div>
    	<?php endwhile; ?>
    
    	<div style="padding:10px;"><!-- page navi -->
    	<?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>
    	<!-- page navi end --></div>
    
    	<?php else : ?>
    
    	<h2 class="center">Not Found</h2>
    	<p class="center">Sorry, but you are looking for something that isn't here.</p>
    	<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    
    <?php endif; } ?>
    
    </div>

    I replace something. Try this code now.

  8. chinmoy
    Member
    Posted 3 years ago #

    IF solve, mark it as resolved.

  9. Sazuka
    Member
    Posted 3 years ago #

    thx, actually the one before ur last post worked but only if u remove the global $query_string;

    I dont know why, but if u remove it no more errors.. kinda wierd

  10. Sazuka
    Member
    Posted 3 years ago #

    Another enhanced question to my problem.

    My code shows the latest posts of category X in my sidebar.
    Now I wanted to add a second box showing category Y posts under it.

    So i put it in between my (is_single('2466')){ code.

    Original code:

    <?php
    if (is_single('2466')){
       query_posts($query_string . '&cat=1&category__not_in=3');
       if(have_posts()) : while(have_posts()) : the_post(); ?>
    <br><div id="twitter">
    <h2>Latest Articles</h2>
    <ul id="twitter_update_list"></ul>
    <br/>
    
    	<div class="entry2">
    	<div class="post" id="post-<?php the_ID(); ?>">
    
    	<b><a href="<?php the_permalink() ?>" rel="bookmark" title="Read the rest of <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    	</b>
    	<span class="content">
    	<?php the_excerpt(); ?>
    	</span>
    	<div class="clearfloat"></div>
    <?php edit_post_link('edit', '', ''); ?>
    	<div class="allinfos">
    
    	</div>
    
    	</div>
    	</div>
    	<?php endwhile; ?>
    
    	<div style="padding:10px;"><!-- page navi -->
    	<?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>
    	<!-- page navi end --></div>
    
    	<?php else : ?>
    
    	<h2 class="center">Not Found</h2>
    	<p class="center">Sorry, but you are looking for something that isn't here.</p>
    	<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    </div>
    <?php endif; } ?>

    --------------------------------
    Now I add the second box and recieve this error Parse error: syntax error, unexpected T_ENDWHILE in /var/customer/webs/side/wp-content/themes/Dig/sidebar.php on line 63

    Here is how I did it:

    <?php
    if (is_single('2466')){
       query_posts($query_string . '&cat=1&category__not_in=3');
       if(have_posts()) : while(have_posts()) : the_post(); ?>
    <br><div id="twitter">
    <h2>Latest Articles</h2>
    <ul id="twitter_update_list"></ul>
    <br/>
    
    	<div class="entry2">
    	<div class="post" id="post-<?php the_ID(); ?>">
    
    	<b><a href="<?php the_permalink() ?>" rel="bookmark" title="Read the rest of <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    	</b>
    	<span class="content">
    	<?php the_excerpt(); ?>
    	</span>
    	<div class="clearfloat"></div>
    <?php edit_post_link('edit', '', ''); ?>
    	<div class="allinfos">
    
    	</div>
    
    	</div>
    	</div>
    	<?php endwhile; ?>
    
    	<div style="padding:10px;"><!-- page navi -->
    	<?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>
    	<!-- page navi end --></div>
    
    	<?php else : ?>
    
    	<h2 class="center">Not Found</h2>
    	<p class="center">Sorry, but you are looking for something that isn't here.</p>
    	<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    </div>
    
    query_posts($query_string . '&cat=1&category__not_in=3');
       if(have_posts()) : while(have_posts()) : the_post(); ?>
    <div id="twitter">
    <h2>Latest Articles</h2>
    <ul id="twitter_update_list"></ul>
    <br/>
    
    	<div class="entry2">
    	<div class="post" id="post-<?php the_ID(); ?>">
    
    	<b><a href="<?php the_permalink() ?>" rel="bookmark" title="Read the rest of <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    	</b>
    	<span class="content">
    	<?php the_excerpt(); ?>
    	</span>
    	<div class="clearfloat"></div>
    <?php edit_post_link('edit', '', ''); ?>
    	<div class="allinfos">
    
    	</div>
    
    	</div>
    	</div>
    	<?php endwhile; ?>
    
    	<div style="padding:10px;"><!-- page navi -->
    	<?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>
    	<!-- page navi end --></div>
    
    	<?php else : ?>
    
    	<h2 class="center">Not Found</h2>
    	<p class="center">Sorry, but you are looking for something that isn't here.</p>
    	<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    </div>
    
    <?php endif; } ?>
  11. chinmoy
    Member
    Posted 3 years ago #

    <?php
    if (is_single('2466')){
       query_posts($query_string . '&cat=1&category__not_in=3');
       if(have_posts()) : while(have_posts()) : the_post(); ?>
    <br><div id="twitter">
    <h2>Latest Articles</h2>
    <ul id="twitter_update_list"></ul>
    <br/>
    
    	<div class="entry2">
    	<div class="post" id="post-<?php the_ID(); ?>">
    
    	<b><a href="<?php the_permalink() ?>" rel="bookmark" title="Read the rest of <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    	</b>
    	<span class="content">
    	<?php the_excerpt(); ?>
    	</span>
    	<div class="clearfloat"></div>
    <?php edit_post_link('edit', '', ''); ?>
    	<div class="allinfos">
    
    	</div>
    
    	</div>
    	</div>
    	<?php endwhile; ?>
    
    	<div style="padding:10px;"><!-- page navi -->
    	<?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>
    	<!-- page navi end --></div>
    
    	<?php else : ?>
    
    	<h2 class="center">Not Found</h2>
    	<p class="center">Sorry, but you are looking for something that isn't here.</p>
    	<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    </div>
    <?php endif;
    wp_reset_query();
    query_posts($query_string . '&cat=1&category__not_in=3');
       if(have_posts()) : while(have_posts()) : the_post(); ?>
    <div id="twitter">
    <h2>Latest Articles</h2>
    <ul id="twitter_update_list"></ul>
    <br/>
    
    	<div class="entry2">
    	<div class="post" id="post-<?php the_ID(); ?>">
    
    	<b><a href="<?php the_permalink() ?>" rel="bookmark" title="Read the rest of <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    	</b>
    	<span class="content">
    	<?php the_excerpt(); ?>
    	</span>
    	<div class="clearfloat"></div>
    <?php edit_post_link('edit', '', ''); ?>
    	<div class="allinfos">
    
    	</div>
    
    	</div>
    	</div>
    	<?php endwhile; ?>
    
    	<div style="padding:10px;"><!-- page navi -->
    	<?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>
    	<!-- page navi end --></div>
    
    	<?php else : ?>
    
    	<h2 class="center">Not Found</h2>
    	<p class="center">Sorry, but you are looking for something that isn't here.</p>
    	<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    </div>
    
    <?php endif; } ?>

    Try it once.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.