• Hi there,
    I’m trying to get a list of posts of a certain category on a page. On that page is a shortcode like [get_list].
    The markup should look like this:

    <ul>
    <li>
      <a href="#"id="">'.the_title().'</a>
      <a href="#"id="">'.the_title().'</a>
      ...
    </li>
    </ul>

    I tried this:

    function get_offenestellen( $atts ) {
    if (is_page()) {
      $cat=12;
      $posts = get_posts ("cat=$cat&showposts=5");
      if ($posts) {
        foreach ($posts as $post):
          setup_postdata($post); ?>
          <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <?php endforeach;
      }
    }
    return;
    }
    add_shortcode( 'offene_stellen', 'get_offenestellen' );

    But all I get is like links to the page I’m currently on (the one with the shortcode on).

    What’s my mistake? Can you help me?

Viewing 1 replies (of 1 total)
  • shortcodes need to return the results;

    your code is returning nothing with this line:

    return;

    review https://codex.wordpress.org/Shortcode_API

    try:

    function get_offenestellen( $atts ) {
    if (is_page()) {
      $cat=12;
      $posts = get_posts ("cat=$cat&showposts=5");
      if ($posts) {
        $list = '';
        foreach ($posts as $post):
          setup_postdata($post); ?>
          $list .= '<a href="' . get_the_permalink( $post->ID ) . '" rel="bookmark" title="Permanent Link to ' . the_title_attribute( 'echo=0' ) . '">' . the_title('','',false) . '</a>';
        endforeach;
      }
    }
    wp_reset_postdata();
    return $list;
    }
    add_shortcode( 'offene_stellen', 'get_offenestellen' );

    (not tested for typing mistakes)
    the above does not take care of your desired html structure;
    if you have problems integrating that yourself, please post again.

Viewing 1 replies (of 1 total)
  • The topic ‘Get a list of posts on a static page’ is closed to new replies.