WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] combining php tags, can it work? (3 posts)

  1. csleh
    Member
    Posted 6 years ago #

    Can an if is_page be combined with get_posts? This doesn't work, but shows what I'm attempting

    <?php
       if (is_page('my_page')) {
    <ul>
     <?php
     global $post;
     $myposts = get_posts('category=4');
     foreach($myposts as $post) :
     ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
     <?php endforeach; ?>
     </ul>
      }
    ?>

    No, I don't know php as you can see. Have played with options from the codex, forums and web, but nothing works yet.

  2. Marcel Brinkkemper
    Member
    Posted 6 years ago #

    You have to enclose php statements within php tags. So as soon as you come to an HTML element you should close the php with ?>
    After the HTML element you open a new php statement etcetera.
    Nesting php tags is not possible

    <?php
       if (is_page('my_page'))  { ?>
    
    <ul>
     <?php
     global $post;
     $myposts = get_posts('category=4');
     foreach($myposts as $post) :
     ?>
    
    <li><a>"><?php the_title(); ?></a></li>
     <?php endforeach; ?>
     </ul>
     <?php }
    ?>
  3. csleh
    Member
    Posted 6 years ago #

    Learning by trial and error works better with a guide. Thanks macbrink! Works great!

    *future reference - small typo in the code above after a tag --

    <?php
       if (is_page('my_page'))  { ?>
    <ul>
     <?php
     global $post;
     $myposts = get_posts('category=4');
     foreach($myposts as $post) :
     ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
     <?php endforeach; ?>
     </ul>
     <?php }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.