• I’ve created a sidebar widget to display a menu of services/prices that are taken from a custom post type I set up.

    What I am trying to do is this: When you are on a page that features the widget, all values from a particular custom post ID will display in the widget depending on the parent page ID. But I haven’t found a way to do this.

    Right now, by using query_posts, I am able to load a single custom post into the widget but I’m not able to control which post is loaded; only the last post entered will display even if I try to force it with a static post_id (see below).

    <?php wp_reset_postdata(); ?>
    
    <?php $args = array(
    'post_type' => 'hireprices',
     'post_id' => '1012',
    'posts_per_page' => 1 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); ?>

    …Widget code example:

    <div class="widget-key"><?php echo post_custom('widgetProductname', true); ?></div>
    <div class="widget-value"><?php echo post_custom('widgetPrice', true); ?></div>

    Any insight, links, examples appreciated.

Viewing 1 replies (of 1 total)
  • Thread Starter tfagency

    (@tfagency)

    For anyone interested, I worked out a solution to the above. I now have a conditional statement that checks ids of five different pages, matches custom post data with them and displays it in the sidebar.

    The conditional could be more elegant, but it works for me:

    <?php
    
    if ( is_page( 'service-one' ) ) {
            $pricedata = 01;
        } elseif ( is_page( 'service-two' ) ) {
            $pricedata = 02;
        } elseif ( is_page( 'service-three' ) ) {
            $pricedata = 03;
        } elseif ( is_page( 'service-four' ) ) {
            $pricedata = 04;
        } elseif ( is_page( 'service-five' ) ) {
            $pricedata = 05; }
    
    $pricemenu_loop = new WP_Query(array( 'post_type' => 'servicemenu', 'posts_per_page' => 1, 'p' => $pricedata));
    		while ( $pricemenu_loop->have_posts() ) : $pricemenu_loop->the_post(); ?>

    Styled the output in the sidebar with post slugs in a couple of floating divs:

    <h2>Service Menu</strong></h2>
    
    <div class="floatleft">
    <ul>
    <li><?php echo post_custom('item1', true); ?></li>
    <li><?php echo post_custom('item2', true); ?></li>
    </ul>
    </div>
    
    <div class="floatright">
    <ul>
    <li><?php echo post_custom('price1', true); ?></li>
    <li><?php echo post_custom('price2', true); ?></li>
    </ul>
    </div>

    …etc.

    Hope this helps somebody. Pretty simple stuff but it was quite a search for me to finding the right formula.

    If anyone knows a better way to code the above hackery, have at it 🙂

Viewing 1 replies (of 1 total)
  • The topic ‘Load custom post data into sidebar by parent page id’ is closed to new replies.