I have this PHP code that shows a related posts element created using advanced custom fields plugin. I want to create a shortcode inside functions.php with the code and then use the shortcode in a text element of a page builder. Would someone kindly assist me with the modified code to put inside functions.php? Thanks
<?php
$posts = get_field('related_posts', false, false);
$loop = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 3, 'post__in' => $posts, 'post_status' => 'publish', 'orderby' => 'post__in', 'order' => 'ASC' ));
if($loop->have_posts()) { ?>
<div class="rel-posts">
<?php while ($loop->have_posts()) : $loop->the_post(); ?>
<div class="related-post">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('td_218x150'); ?></a>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
</div>
<?php endwhile; ?>
</div>
<?php } wp_reset_query(); ?>
I have this PHP code that shows a related posts element created using advanced custom fields plugin. I want to create a shortcode inside functions.php with the code and then use the shortcode in a text element of a page builder. Would someone kindly assist me with the modified code to put inside functions.php? Thanks
<?php
$posts = get_field('related_posts', false, false);
$loop = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 3, 'post__in' => $posts, 'post_status' => 'publish', 'orderby' => 'post__in', 'order' => 'ASC' ));
if($loop->have_posts()) { ?>
<div class="rel-posts">
<?php while ($loop->have_posts()) : $loop->the_post(); ?>
<div class="related-post">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('td_218x150'); ?></a>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
</div>
<?php endwhile; ?>
</div>
<?php } wp_reset_query(); ?>
Share
Improve this question
asked Dec 29, 2018 at 11:24
user145284user145284
2 Answers
Reset to default 0If your code works fine then do this in function.php
<?php
add_shortcode( 'custom_name', 'footag_func' );
function footag_func( $atts ) {
$html= '';
$posts = get_field('related_posts', false, false);
$loop = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 3, 'post__in' => $posts, 'post_status' => 'publish', 'orderby' => 'post__in', 'order' => 'ASC' ));
if($loop->have_posts()) {
$html .= '<div class="rel-posts">';
while ($loop->have_posts()) : $loop->the_post();
$html .= '<div class="related-post">';
$html .='<a href="'.the_permalink().'">'.the_post_thumbnail('td_218x150').'</a>';
$html .= '<h3><a href="'.the_permalink().'">'.the_title().'</a></h3>';
$html .= '</div>';
endwhile;
$html .= '</div>';
} wp_reset_query();
}
return $html;
?>
your shortcode [custom_name]
add_shortcode('location_start_your_application_group', 'start_your_application_group');
function start_your_application_group() {
$start_your_application_group = '';
$start_your_application_group .= '<section class="start-your-application">';
if ( have_rows( 'start_your_application_group', 'option' ) ) :
while ( have_rows( 'start_your_application_group', 'option' ) ) : the_row();
$heading = get_sub_field( 'heading' );
$content = get_sub_field( 'content' );
if ( $heading !== '' ) {
$start_your_application_group .= '<h3 class="start-your-application__heading">' . $heading . '</h3>';
}
if ( $content !== '' ) {
$start_your_application_group .= '<div class="start-your-application__content">' . $content . '</div>';
}
$image = get_sub_field( 'image' );
if ( $image ) {
$start_your_application_group .= '<div class="start-your-application__image-container"><img class="start-your-application__image" src="' . $image['url'] .'" alt="' . $image['alt'] . '" /></div>';
}
endwhile;
endif;
$start_your_application_group .= '</section>';
return $start_your_application_group;
}