我正在构建一个短代码来显示页面中的某些帖子。目前看起来是这样的:
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\' );
不过,我想添加一个条件,以便在帖子有粘性时定义列宽。
我尝试了以下操作,但出现了一个错误:
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>\';
}
上面写着:
Parse error: syntax error, unexpected \'}\' in ...
我做错了什么?