Pergunta

I'm building a shortcode to display certain posts in a page. So far it looks like this:

function bloghome( $atts ) {
    extract( shortcode_atts( array(
        'id' => 17      // Add the *default category id
    ), $atts ) );

    $posts = get_posts( array(
        'posts_per_page' => -1,
        'post_status'    => 'publish',
        'cat'       => $id,
    ) );
    $return = '';
    $return .= '<div class="row page-template-press bloghome">';
    foreach ( $posts as $post ) {
        $link = get_permalink( $post->ID);
        $title =  get_the_title($post->ID);
        $img =  get_the_post_thumbnail( $post->ID, 'medium' );
        $snippet = get_post_meta($post->ID, 'Snippet', true);
        $return .= '<article class="col-lg-'.$width.' col-md-'.$width.' col-sm-'.$width.'"><a href="'.$link.'"><header><div class="page-title-post">'.$title.'</div></header><div class="entry-content">'.$img.'<p class="snippet">'.$snippet.'</p></div></a></article>';
    } 

$return .= '</div>';
return $return;
}
add_shortcode( 'bloghome', 'bloghome' );  

I want to add a conditional though, to define column width if the post is sticky.

I've tried the following but it throws an error:

foreach ( $posts as $post ) {
    if ( is_sticky() ) {
        $width = '12'
    } else {
        $width = '4'
    };
    $link = get_permalink( $post->ID);
    $title =  get_the_title($post->ID);
    $img =  get_the_post_thumbnail( $post->ID, 'medium' );
    $snippet = get_post_meta($post->ID, 'Snippet', true);
    $return .= '<article class="col-lg-'.$width.' col-md-'.$width.' col-sm-'.$width.'"><a href="'.$link.'"><header><div class="page-title-post">'.$title.'</div></header><div class="entry-content">'.$img.'<p class="snippet">'.$snippet.'</p></div></a></article>';
} 

It says: Parse error: syntax error, unexpected '}' in ...

What am I doing wrong?

Foi útil?

Solução

it may be easier than you thought. You're coding is missing endline apostrophes:

foreach ( $posts as $post ) {
    if ( is_sticky() ) {
        $width = '12'
    } else {
        $width = '4'
    };

should be

foreach ( $posts as $post ) {
    if ( is_sticky() ) {
        $width = '12';
    } else {
        $width = '4';
    };
Licenciado em: CC-BY-SA com atribuição
Não afiliado a wordpress.stackexchange
scroll top