• Resolved Takkemike

    (@takkemike)


    Hi there,

    Because I’m not a fan of plugins, I’m trying to make a breadcrumb with only php. I’m running into an if / elseif error that has costed a few hours of puzzling already.
    The breadcrumbs looks something like this:

    <?php
    if( $post->ID ==  1 ) { 	// Page1 ?>
    	<div id="page1"><a href="/page1">page1</a> &raquo Article: <?=$post->single_post_title()?></div>
    	<? } else { ?> Page1 <? }
    elseif ( $post->ID == 2 ) { 	// Page2 ?>
    	<div id="page2"><a href="/page2">Page2</a> &raquo <?=$post->post_title()?></div>
    	<? } else { ?>Page2<? }
    elseif ( $post->ID == 3 ) { 	// Page3 ?>
    	<div id="page3">Page3</div>
            <? }
    elseif ( $post->ID == 4 ) { 	// Page4 ?>
    	<div id="page4">Page4</div>
    	<? }
    else { ?>
    	<?=$post->single_post_title()?>
    	<? }
    ?>

    [Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    An parser error is found in the line of the first elseif. What I assume because the if is already closed by the else, that’s supposed to be merely an alternative, if the single_post _title isn’t found.
    Any suggestions (no plug ins, please) are more than welcome.
    Thanks in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Try something like:

    <?php
    if( $post->ID < 5 ) :?>
    $my_div_id = 'page' . $post->ID;
    $my_page_title = 'Page ' . $post->ID;
    $my_page_slug = esc_url( home_url( '/' ) ) . $post->ID;>
    
    <div id="<?php echo $my_div_id;?>"><a href="<?php echo $my_page_slug;?>"><?php echo $my_page_title;?></a> &raquo Article: <>pphp echo $post->single_post_title();?></div>
    <?php else:
    echo $post->single_post_title();
    endif;
    ?>

    1. Don’t use relative urls in WordPress. They are unpredictable at best.
    2. Don’t use the shortened version of <?php

    Thread Starter Takkemike

    (@takkemike)

    Wow, that’s a pretty drastic change! Thank you very much.
    Also for the tips.

    I’ll look into this code and post any further steps/obstacles/solutions for assistance.

    Thread Starter Takkemike

    (@takkemike)

    Pretty soon after posting this topic, the following code got it working. (It may be pretty messy, but it’s purely to be of help to anyone who has a similar problem). Esmi’s code is probably far cleaner, but unfortunately not in this case, because of the many sideways.

    <?php
    if($post->ID ==  x): { ?>
        	<div class="myawesomeclass">
    					<?php if( $post->post_type == 'post_type1' ) { ?>
    						<a href="#"><div class="myawesomeclass"></div></a>  &raquo Link: <?=$post->post_title?>
    						<? } ?>
    				</div>
    
      	<? } elseif($post->ID ==  x):  { ?>
    
      	 	<div class="bloghoofd">
    
    					<?php if( $post->category_name == 'category_slug' ) { ?>
    						<a href="/##"><div class="myawesomeclass"></div></a> &raquo Link: <?=$post->post_title?>
    					<? } ?>
    				</div>
    
      	<? } elseif($post->ID ==  x):  {?>
    	   <div class="myawesomeclass"></div>
    
    	<? } elseif($post->ID ==  x):  {?>
    	   <div id=class="myawesomeclass"></div>
    
    	<? } elseif($post->ID == x):  {?>
    	   <div class="myawesomeclass">
    	   				<a href="/###"><div class="myawesomeclass"> Link: </div></a>
    	   				</div>
    	<? } elseif( in_category( 'blog' ) ): { ?>
    	   <a href="/####">
    	   				<div class="myawesomeclass"> Link: </div></a> 
    
    	<? } elseif($post->ID > x):  {?>
    	   <div class="myawesomeclass">
    					<?php if( $post->post_type == 'post_type2' ) { ?>
    						<a href="/#####"><div class="myawesomeclass"></div></a> &raquo Link: <?=$post->post_title?>
    						<? } ?>
    		</div>	
    
    	<? } elseif($post->ID ==  2):  {
    	   echo ('');
    
    	 } else: {
    	    echo the_title(); ?>
    		<?}
    endif;
    ?>

    Maybe it contains pieces for anyone.
    Hope it helps ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘breadcrumb not working’ is closed to new replies.