Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello,
    I think you could create your own shortcode with a custom wp_query:

    $args = array( 'post_type' => 'advert'
                    'orderby' => 'date',
                    'posts_per_page' => $posts_number,
    	        'order'   => 'DESC' ) ;
    
    $query = new WP_Query( $args );

    and for the related ads, add the category parameter and test against the actual post’s category:

    $args = array( 'post_type' => 'advert'
                   'advert-category' => $actual_post_cat
                   'orderby' => 'date',
                   'posts_per_page' => $posts_number,
    	       'order'   => 'DESC' ) ;

    I hope this help.

    Thread Starter matpuscb

    (@matpuscb)

    Thanks beirdostudio.

    Not sure just how to use this for now. Is this the whole code or do I need something else to actually “print”/display it on frontend.

    You would need to define a shortcode and run the loop on that query, i could give you the whole code when i get home but you could try to do it yourself meanwhile.
    WP_Query
    Add_shortcode

    here is a working example, add this code to your functions.php file:

    function wa426_latest_ads($atts) {
    
        $args = array('post_type' => 'advert',
            'orderby' => 'date',
            'posts_per_page' => $atts['count'],
            'order' => 'DESC');
        $wa_query = new WP_Query($args);
    
        // The Loop
        if ($wa_query->have_posts()) {
            echo '<ul>';
            while ($wa_query->have_posts()) {
                $wa_query->the_post();
                echo '<li>' . get_the_title() . '</li>';
            }
            echo '</ul>';
        } else {
            // no posts found
        }
        /* Restore original Post Data */
        wp_reset_postdata();
    }
    
    add_shortcode('wa426_latest_ads', 'wa426_latest_ads');

    then, call the shortcode wherever you need it:

    [wa426_latest_ads count="3"]

    where count is the number of ads you want to show.

    for the related ads, you can make a custom single.php for the ads and add the query wherever you need it in that single page (under the post for example), don’t forget to add the 'advert-category' => $actual_post_cat to this last query so it will only get latest ads of the same category.

    you can get $actual_post_cat with :

    $actual_post_cat = get_the_terms ( $post, 'advert-category' );

    in case you don’t know how or where to make the single file, you need to make a copy of your theme’s single.php and call it single-advert.php

    Thread Starter matpuscb

    (@matpuscb)

    Amazing, that works like a charm, I tried to make it myself but I was unable, this code yours is working, thank you again.

    The only thing now I need to figure out is how to call the advert image.

    Plugin Author Greg Winiarski

    (@gwin)

    Hi, after $wa_query->the_post(); you should be able to get image URL using following code

    $image = adverts_get_main_image( get_the_ID() );
    if($image):
        echo '<img src="'.esc_attr($image).'" alt="" class="" />';
    endif;

    Great work with the shortcode @beirdostudio, this is really useful!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Similar/latest ads’ is closed to new replies.