lundi 20 avril 2015

Include custom post type in Wordpress loop

Sorry, I think I read every post about this but cant get it to work.

I have a Wordpress custom post typ called "external" and I would like to show both the post from "external" aswell as my normal standard posts at the same time on my homepage.

This is my loop:

<?php 
get_header(); 

if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }?>


<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(''); ?>>
    <?php the_content(); ?>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>    

How can I include the "external" posts in this code?

The theme then uses blocks to display content on the startpage, here is block8.php with a post query.

function block_eight_ajax_query($atts='') {
    $args = array (
$is_ajax = 0;
if($atts==''){
    $is_ajax=1;
    $atts=$_GET;
    if($atts['global_query']){
        unset($atts['no_found_rows']);
        unset($atts['suppress_filters']);
        unset($atts['cache_results']);
        unset($atts['update_post_term_cache']);
        unset($atts['update_post_meta_cache']);
        unset($atts['nopaging']);
    }
}
$atts['is_ajax'] = $is_ajax;
$query = null;
$query = new WP_Query( $atts );
$html = '';
if ( $query->have_posts() ) {
    ob_start();
    $i = 1;
    while ( $query->have_posts() ):
        $query->the_post();

        $atts['video'] = rd_field( 'abomb_post_video' );
        $atts['extern'] = rd_field( 'extern' );
        $atts['audio'] = rd_field( 'abomb_post_audio' );
        $atts['gallery'] = rd_field( 'abomb_post_gallery' );
        $atts['counter'] = $i;
        block_grid_content($atts);
        if ($atts['column'] == 'grid-12') { $divide = 1; }
        else if ($atts['column'] == 'grid-3') { $divide = 4; }
        else if ($atts['column'] == 'grid-4') { $divide = 3; }
        else { $divide = 2; }
        if ($i%$divide==0 && $divide!=1) {
            echo '<div class="clear"></div>';
        }
        $i++;
    endwhile;
    if ($atts['nav'] == 'numbered') {
        echo '<div class="grid-12">';
            rd_pagination($query->max_num_pages);
        echo '</div>';
    }
    wp_reset_postdata();
    $html = ob_get_clean();
    if($is_ajax==1){
        echo $html;
        exit();
    }
    return $html;
}
else{
    if($is_ajax==1){
        echo '-11';
        exit();
    }
    return '';
}
}

Update: I added this code to functions:

add_action( 'pre_get_posts', 'add_my_post_types_to_query' );


function add_my_post_types_to_query( $query ) {
if ( $query->is_home() && $query->is_main_query() )
    $query->set( 'post_type', array( 'post', 'extern' ) );
return $query;
}

Now I can see the posts in for exampel the search result but I need the Query in Block8.php to fetch them aswell.

I Thanks!

Aucun commentaire:

Enregistrer un commentaire