admin管理员组文章数量:1024663
I create a custom post type and wrote a shortcode to display the posts on the front end of my website, but the code only displays one (1) post, and the most recent one. See front-end page here -> . The CPT is the "jobs board".
Here is my code. Any advice for cleaning this code up and making sure all of my posts display on the front end? Thank you!
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="" /><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="" target="_blank" />Click here</a>.' );
wp_reset_query();
return html_entity_decode($out);
}
add_shortcode( 'locgviewjobs', 'locg_view_jobs');
I create a custom post type and wrote a shortcode to display the posts on the front end of my website, but the code only displays one (1) post, and the most recent one. See front-end page here -> https://www.800goldlaw/careers. The CPT is the "jobs board".
Here is my code. Any advice for cleaning this code up and making sure all of my posts display on the front end? Thank you!
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');
Share
Improve this question
asked Apr 8, 2019 at 13:28
tommycopelandtommycopeland
33 bronze badges
2 Answers
Reset to default 1Try adding 'posts_per_page' => -1,
to your default atts. This will make the query return all of the posts instead of limiting it. Also you're overriding $out
each loop instead of adding to it. Change $out = '<div class
to $out .= '<div class
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');
I create a custom post type and wrote a shortcode to display the posts on the front end of my website, but the code only displays one (1) post, and the most recent one. See front-end page here -> . The CPT is the "jobs board".
Here is my code. Any advice for cleaning this code up and making sure all of my posts display on the front end? Thank you!
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="" /><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="" target="_blank" />Click here</a>.' );
wp_reset_query();
return html_entity_decode($out);
}
add_shortcode( 'locgviewjobs', 'locg_view_jobs');
I create a custom post type and wrote a shortcode to display the posts on the front end of my website, but the code only displays one (1) post, and the most recent one. See front-end page here -> https://www.800goldlaw/careers. The CPT is the "jobs board".
Here is my code. Any advice for cleaning this code up and making sure all of my posts display on the front end? Thank you!
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');
Share
Improve this question
asked Apr 8, 2019 at 13:28
tommycopelandtommycopeland
33 bronze badges
2 Answers
Reset to default 1Try adding 'posts_per_page' => -1,
to your default atts. This will make the query return all of the posts instead of limiting it. Also you're overriding $out
each loop instead of adding to it. Change $out = '<div class
to $out .= '<div class
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');
本文标签: wp queryWPquery only displays one of my custom post type entries
版权声明:本文标题:wp query - WP_query only displays one of my custom post type entries 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745614651a2159213.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论