Grow your CSS skills. Land your dream job.

Display specific posts on pages in WordPress

  • # June 7, 2012 at 1:27 pm

    So I’m having a problem. In the website every product is a post, but when we add new products we want something like a newsletter, mostly like a post so in the sidebar of the home page you can see the new products or events of the month. I’m using pages because I don’t want to re-post a product on every new newsletter so I junt wanna display the posts inside the page. In the products page I separate every product by category and sub-category but since I want to group specific post to publish them on the sidebar I think that pages was the best way to do it.

    Right now I’m using this code:

    < ?php
    $productos = new WP_Query(array(
    'post__in'=> array(81, 83),
    'orderby'=>'title',
    'order'=>'ASC'
    )
    ); if ($productos->have_posts()) : while ($productos->have_posts()) : $productos->the_post();
    ?>

    It display the posts with the id of 81 and 83, I would like to show post by slug using ‘name’ as the codex says because is going to take some time to be checking the ids of the new post, insted of using the name of every new product but It dosen’t work in array or I’m doing something wrong.

    Now I will love to make something like this work

    $names = get_post_meta($post->ID, "names", $single = true);

    $productos = new WP_Query(array(
    'name'=> array($names),
    'orderby'=>'title',
    'order'=>'ASC'
    )
    );

    So every time I publish a new page I just write the slugs of the posts that I want to include in the page in a custom field, as you can see I’m not very good with php but I trying to learn and I search a lot for something that could work before asking in here.

    I try the ggis inline post plugin and although it works I need the id for every post I want to include and I will need to edit the plugin because I want a different order in the output of the post thats why I don’t like to depend to much on plugins.

    I hope someone can help me with this and sorry for my bad english (not my first language)

    # June 7, 2012 at 2:44 pm

    Would this be helpful ?

    http://bit.ly/cr8tR

    http://bit.ly/MjzTO8

    # June 7, 2012 at 2:55 pm

    Thank you the code that Michael post is great.

    < ?php
    $post_slug = 'online-coupons';
    $args=array(
    'name' => $post_slug,
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 1,
    'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    echo 'List of Posts';
    while ($my_query->have_posts()) : $my_query->the_post(); ?>

    " rel="bookmark" title="Permanent Link to < ?php the_title_attribute(); ?>">< ?php the_title(); ?>


    < ?php
    endwhile;
    }
    wp_reset_query(); // Restore global post data stomped by the_post().
    ?>

    But I still have the problem that I want to display 2 or more posts per page so I need something like

    'name' =>array($post_slug),

    But it looks like you can’t do an array on the name argument.

    # June 7, 2012 at 11:24 pm

    I find I solution using Shortcodes.
    So I put this on my functions.php page

    function productos($atts, $content = null) {
    extract(shortcode_atts(array(
    "slug" => '',
    "query" => ''
    ), $atts));
    global $wp_query,$post;
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query(array(
    'name'=> $slug,
    ));
    if(!empty($slug)){
    $query .= '&name='.$slug;
    }
    if(!empty($query)){
    $query .= $query;
    }
    $wp_query->query($query);
    ob_start();
    ?>
    < ?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    " rel="bookmark">< ?php the_title(); ?>


    < ?php the_content() ?>

    < ?php endwhile; ?>

    < ?php $wp_query = null; $wp_query = $temp;
    $content = ob_get_contents();
    ob_end_clean();
    return $content;
    }
    add_shortcode("producto", "productos");

    And in my page template I just write [producto slug="MY-SLUG"] and that way I can display multiple post just with the slugs. Hope someone find this useful.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".