WP_QUERY只显示我的一个自定义帖子类型条目

时间:2019-04-08 作者:tommycopeland

我创建了一个自定义帖子类型,并编写了一个短代码来显示我网站前端的帖子,但代码只显示一(1)篇帖子,以及最近的一篇。请参见此处的前端页->https://www.800goldlaw.com/careers. CPT是“就业委员会”。

这是我的密码。有什么建议可以清理这个代码并确保我的所有帖子都显示在前端吗?非常感谢。

function locg_view_jobs($atts) {
    $atts = shortcode_atts(
                array( //default values
                \'post_type\' => \'locg_jobs\',
                \'post_status\' => \'publish\',
                ), 
                $atts, \'locgviewjobs\' );

    global $post;

    $posts = new WP_query($atts);
    $output = \'\';
    $terms = get_the_terms( get_the_ID(), \'job_cat\'); foreach ($terms as $term) { $job_cat_links[]=$term->name; }
    $job_cat_go = join(",", $job_cat_links);

        if ($posts->have_posts())
            while ($posts->have_posts()):
                $posts->the_post();
                $out = \'<div class="job_post"><h4><a href=" \' . get_the_permalink() . \'" title="Apply Now" />\' . get_the_title() . \'</a></h4>
                        <div class="jobs_meta">
                        <span class="meta">Posted Date:</span> \' . get_the_date() . \'</span> | <span class="meta">Open Until:</span> \' . get_post_meta($post->ID, \'expiredate\', true) . \' | <span class="meta">Department:</span> \' . get_post_meta($post->ID, \'Department\', true) . \' </div>
                        <p>\' . get_the_excerpt() . \'</p>
                        <a class="red_link" target="_blank" href=" \' . get_the_permalink() . \'" /><button class="red" style="margin-right: 10px;"/>Learn More</button></a>
                        <a class="red_link" target="_blank" href="http://gld.la/2GciVF4" /><button class="red"/>Apply Now</button></a>
                        </div>\';

    endwhile;
    else return( \'Sorry, there are no jobs available at this time. Check back often! In the meantime, you can fill out a blank application for us to hold on file. <a href="http://gld.la/2GciVF4" target="_blank" />Click here</a>.\' );
    wp_reset_query();
    return html_entity_decode($out);
}
add_shortcode( \'locgviewjobs\', \'locg_view_jobs\');

2 个回复
SO网友:mrben522

尝试添加\'posts_per_page\' => -1, 默认收件人。这将使查询返回所有帖子,而不是限制它。而且您正在覆盖$out 每个循环,而不是添加到其中。改变$out = \'<div class$out .= \'<div class

SO网友:Qaisar Feroz

Try this

function locg_view_jobs($atts) {
    $atts = shortcode_atts(
                array( //default values
                \'post_type\' => \'locg_jobs\',
                \'post_status\' => \'publish\',
                ), 
                $atts, \'locgviewjobs\' );

    global $post;

    $posts = new WP_query($atts);
    $out = \'\';

    $terms = get_the_terms( get_the_ID(), \'job_cat\');

    foreach ($terms as $term) { 
         $job_cat_links[]=$term->name;
    }

    $job_cat_go = join(",", $job_cat_links);

        if ($posts->have_posts())
            while ($posts->have_posts()):
                $posts->the_post();
                $out .= \'<div class="job_post"><h4><a href=" \' . get_the_permalink() . \'" title="Apply Now" />\' . get_the_title() . \'</a></h4>
                        <div class="jobs_meta">
                        <span class="meta">Posted Date:</span> \' . get_the_date() . \'</span> | <span class="meta">Open Until:</span> \' . get_post_meta($post->ID, \'expiredate\', true) . \' | <span class="meta">Department:</span> \' . get_post_meta($post->ID, \'Department\', true) . \' </div>
                        <p>\' . get_the_excerpt() . \'</p>
                        <a class="red_link" target="_blank" href=" \' . get_the_permalink() . \'" /><button class="red" style="margin-right: 10px;"/>Learn More</button></a>
                        <a class="red_link" target="_blank" href="http://gld.la/2GciVF4" /><button class="red"/>Apply Now</button></a>
                        </div>\';

    endwhile;
    else return( \'Sorry, there are no jobs available at this time. Check back often! In the meantime, you can fill out a blank application for us to hold on file. <a href="http://gld.la/2GciVF4" target="_blank" />Click here</a>.\' );
    wp_reset_query();
    return html_entity_decode($out);
}
add_shortcode( \'locgviewjobs\', \'locg_view_jobs\');

相关推荐

redirect if shortcode exists

WordPress初学者。我试图检查用户请求的页面中是否存在短代码,如果存在,则在用户未登录时重定向。function redirect_to_home() { if (has_shortcode(get_the_content(), \'shortcode\')) { if(!is_admin() && !is_user_logged_in()) { //redirect exit(); }