0

I have a custom function that someone else here help me build that shows custom post types on my front page. I want to add the tags/tag-links of each post to it, but whenever I reference get_the_tags() it just says Array.

Here is my code:

function custom_portfolio_cases($atts = null, $content = null, $tag = null) {

$odd = TRUE;
$out = '';

$args = array( 
    'numberposts' => '-1', 
    'post_status' => 'publish', 
    'post_type' => 'portfolio_cases',
);

$recent = wp_get_recent_posts( $args );

if ( $recent ) {

        $out .= '<div id="portfolio-cases" class="row">';

        $posttags = get_the_tags($item['ID']);

        foreach ( $recent as $item ) {


            if ( $odd == TRUE ) {
                $out .= '<div class="case_img col-sm-12 col-md-6"><img class="img-fluid" src="';
                $out .= get_field('laptop_image',$item['ID']);
                $out .= '" /></div><div class="case_text col-sm-12 col-md-6"><h5>';
                $out .= get_the_title($item['ID']);
                $out .= '</h5><p class="case_tags">';
                if($posttags) {
                    foreach($posttags as $tag) {
                        $out .= $tag->name . ' ';
                    }
                }
                $out .= '</p><p>';
                $out .= get_field('project_text',$item['ID']);
                $out .= '</p>';
                $out .= '<a class="btn btn-front understrap-read-more-link" href="'. get_permalink( $item['ID'] ) . '">VIEW WORK</a></p>';
                $out .= '</div>';
                $odd = FALSE;
            } else {
                $out .= '<div class="case_img col-sm-12 col-md-6 push-md-6"><img class="img-fluid" src="';
                $out .= get_field('laptop_image',$item['ID']);
                $out .= '" /></div><div class="case_text col-sm-12 col-md-6 pull-md-6"><h5>';
                $out .= get_the_title($item['ID']);
                $out .= '</h5><p class="case_tags">';
                if($posttags) {
                    foreach($posttags as $tag) {
                        $out .= $tag->name . ' ';
                    }
                }
                $out .= '</p><p>';
                $out .= get_field('project_text',$item['ID']);
                $out .= '</p>';
                $out .= '<a class="btn btn-front understrap-read-more-link" href="'. get_permalink( $item['ID'] ) . '">VIEW WORK</a></p>';
                $out .= '</div>';
                $odd = TRUE;
            }
        }

        $out .= '</div>';
    }

    if ( $tag ) {
        return $out;
    } else {
        echo $out;
    }

}

add_shortcode( 'recentposts', 'custom_recent_posts' );
0

Try changing order:

foreach ( $recent as $item ) {
    $posttags = get_the_tags($item['ID']);
  • reversing the order makes the entries/posts disappear entirely. Where as the original just prevents the tags from showing up. – Xero1 Jun 16 '17 at 18:48
  • Original version doesn't make sense, because you are trying to use the $item variable before it is assigned within the loop - and that's why you get the error about the array. The rest of the code looks reasonable and generates code when tested, although if your callback function in add_shortcode is called custom_recent_posts your function should be have the same name and not custom_portfolio_cases. – Picard Jun 16 '17 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.