Parse error: syntax error, unexpected 'endforeach' (T_ENDFOREACH) [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I'm trying to create a page for WordPress "category.php" from the code in this tutorial: http://scratch99.com/wordpress/wp-seo/siloing/
My current theme (Sociallyviral by Themeforest) there is no page "category.php," so I created one and added the code below:
Author's Note:
"The code should look something like the below. Note, you can’t just use this code exactly – it needs to fit in with your theme so that the classes of wrapping divs, etc match the rest of your theme and the style is picked up."
// work out category id, then see if there are any subcategories
$main_cat_ID = get_query_var( 'cat' );
$subcats = get_categories( array( 'parent' => $main_cat_ID, 'hide_empty' => 0 ) );
// if it is page 1
if ( $paged < 2 ) :
// show the category description
echo category_description();
// if there are subcategories, loop through and show them
if ( count( $categories ) > 0 ) :
foreach ( $subcats as $category ) : ?>
<div class="subcategory">
<h2><a class="subcategory-link" href="<?php echo get_category_link( $category->term_id ); ?>">
<?php echo $category->name; ?></a></h2>
<?php echo wpautop( $category->description ); ?>
</div>
<?php
endforeach;
endif;
// get sticky posts and show them if they exist
$args = array( 'category__in' => $main_cat_ID, 'include' => get_option( 'sticky_posts' ) );
$myposts = get_posts( $args );
if ( count( $myposts ) > 0 ) :
foreach ( $myposts as $value ) : ?>
<div class="sticky">
<h3>
<a href="<?php echo get_permalink( $value->ID ); ?>" rel="bookmark"
title="<?php echo $value->post_title; ?>"><?php echo $value->post_title; ?></a>
</h3>
<div class="entry-content">
<?php
// if there is an excerpt, use it, otherwise roll our own (get_the_excerpt won't work outside the loop)
if ( $value->post_excerpt ) :
echo wpautop( $value->post_excerpt );
else :
$sjc_excerpt = explode( '<!--more-->', $value->post_content );
if ( count( $sjc_excerpt ) >= 2 ) :
echo wpautop( strip_tags( $sjc_excerpt[0] ) );
else :
echo wpautop( implode ( ' ', array_slice( explode( ' ', strip_tags( $sjc_excerpt[0] ) ), 0, 45 ) ) . ' ...' );
endif;
endif; ?>
</div>
</div>
<?php
endforeach;
endif;
// pages 2 and onwards don't worry about cat desc or stickies, but link to main cat page
else : ?>
<p>For an introduction to this topic and our latest articles, please see the main
<?php single_cat_title(); ?> page.</p>
<?php
endif;
// if we have any posts in the main loop (no stickies there)
if ( have_posts() ) : ?>
<ul class="other-posts">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php
// you need to replace this with whatever your theme uses!
sjc_content_nav( 'nav-below' );
// if no posts, and also no stickies and no categories, go with no results
elseif ( count( $myposts ) == 0 && count( $subcats ) == 0 ) :
get_template_part( 'no-results', 'archive' );
endif;
If necessary, below my theme file "archive.php"
<?php $mts_options = get_option(MTS_THEME_NAME); ?>
<?php get_header(); ?>
<div id="page">
<div id="content_box">
<h1 class="postsby">
<?php if (is_category()) { ?>
<span><?php single_cat_title(); ?><?php _e(" Archive", "mythemeshop"); ?></span>
<?php } elseif (is_tag()) { ?>
<span><?php single_tag_title(); ?><?php _e(" Archive", "mythemeshop"); ?></span>
<?php } elseif (is_author()) { ?>
<span><?php $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); echo $curauth->nickname; _e(" Archive", "mythemeshop"); ?></span>
<?php } elseif (is_day()) { ?>
<span><?php _e("Daily Archive:", "mythemeshop"); ?></span> <?php the_time('l, F j, Y'); ?>
<?php } elseif (is_month()) { ?>
<span><?php _e("Monthly Archive:", "mythemeshop"); ?>:</span> <?php the_time('F Y'); ?>
<?php } elseif (is_year()) { ?>
<span><?php _e("Yearly Archive:", "mythemeshop"); ?>:</span> <?php the_time('Y'); ?>
<?php } ?>
</h1>
<?php $j = 1; if (have_posts()) : while (have_posts()) : the_post(); $featured = get_post_meta($post->ID, 'mts_featured', true); ?>
<article class="latestPost excerpt<?php echo (($j+2) % 3 == 0) ? ' first' : ''; ?><?php echo ($j % 3 == 0) ? ' last' : ''; ?>" itemscope itemtype="http://schema.org/BlogPosting">
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="nofollow" id="featured-thumbnail">
<?php echo '<div class="featured-thumbnail">'; the_post_thumbnail('featured',array('title' => '')); echo '</div>'; ?>
<?php if (function_exists('wp_review_show_total')) wp_review_show_total(true, 'latestPost-review-wrapper'); ?>
<?php if ($featured) : ?><div class="post-label"><i class="fa fa-star"></i> <span><?php _e('Featured','mythemeshop'); ?></span></div><?php endif; ?>
</a>
<header>
<h2 class="title front-view-title"><?php the_title(); ?></h2>
<?php if ( ! empty( $mts_options["mts_home_headline_meta"] ) ) { ?>
<div class="post-info">
<?php if( ! empty( $mts_options["mts_home_headline_meta_info"]['date']) ) { ?>
<span class="thetime updated"><i class="fa fa-calendar"></i> <span itemprop="datePublished"><?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . __(' ago','mythemeshop'); ?></span></span>
<?php } ?>
<?php if( ! empty( $mts_options["mts_home_headline_meta_info"]['category']) ) { ?>
<span class="thecategory"><i class="fa fa-tags"></i> <?php mts_the_category(', ') ?></span>
<?php } ?>
</div>
<?php } ?>
</header>
</article><!--.post excerpt-->
<?php $j++; endwhile; endif; ?>
<!--Start Pagination-->
<?php if (isset($mts_options['mts_pagenavigation_type']) && $mts_options['mts_pagenavigation_type'] == '1' ) { ?>
<?php mts_pagination(); ?>
<?php } else { ?>
<div class="pagination pagination-previous-next">
<ul>
<li class="nav-previous"><?php next_posts_link( '<i class="fa fa-angle-left"></i> '. __( 'Previous', 'mythemeshop' ) ); ?></li>
<li class="nav-next"><?php previous_posts_link( __( 'Next', 'mythemeshop' ).' <i class="fa fa-angle-right"></i>' ); ?></li>
</ul>
</div>
<?php } ?>
<!--End Pagination-->
</div>
<?php get_footer(); ?>
I get this error when i try to open the category page:
"Parse error: syntax error, unexpected 'endforeach' (T_ENDFOREACH) in /wp-content/themes/sociallyviral/category.php on line 20"
This syntax error, are solved!
Thank you all!
Best regards!

You are missing the <?php Tag at the beginning of your file.
Without it, the first bit of code, the parser will parse the following code as php:
echo get_category_link( $category->term_id );
echo $category->name;
echo wpautop( $category->description );
endforeach;
endif;
as you can see, the first thing to reach (beside the three echo's) is endforeach;and this is where your parser throws an error.
The authors note said the following: "...it needs to fit in with your theme so that the classes of wrapping divs, etc match the rest of your theme and the style is picked up."
This means, that you need to provide the <div>'s in your code with class and style addributes, according to your and your templates needs. In detail, you are meant to apply the classes used by your template, to apply the desired layout. You should have some form of reference of the classes your template uses (probably something like this). Where you can have a look at which classes to use.
This is what the reference of bootstrap looks like - just to give you some idea of what you are after.

Put a php tag at the beginning of your code
<?php // show the category description
echo category_description();
// if there are subcategories, loop through and show them
if ( count( $categories ) > 0 ) :
foreach ( $subcats as $category ) : ?>
<div class="subcategory">
<h2><a class="subcategory-link" href="<?php echo get_category_link( $category->term_id ); ?>">
<?php echo $category->name; ?></a></h2>
<?php echo wpautop( $category->description ); ?>
</div>
<?php
endforeach;
endif;
// get sticky posts and show them if they exist
$args = array( 'category__in' => $main_cat_ID, 'include' => get_option( 'sticky_posts' ) );
$myposts = get_posts( $args );
if ( count( $myposts ) > 0 ) :
foreach ( $myposts as $value ) : ?>
<div class="sticky">
<h3>
<a href="<?php echo get_permalink( $value->ID ); ?>" rel="bookmark"
title="<?php echo $value->post_title; ?>"><?php echo $value->post_title; ?></a>
</h3>
<div class="entry-content">
<?php
// if there is an excerpt, use it, otherwise roll our own (get_the_excerpt won't work outside the loop)
if ( $value->post_excerpt ) :
echo wpautop( $value->post_excerpt );
else :
$sjc_excerpt = explode( '<!--more-->', $value->post_content );
if ( count( $sjc_excerpt ) >= 2 ) :
echo wpautop( strip_tags( $sjc_excerpt[0] ) );
else :
echo wpautop( implode ( ' ', array_slice( explode( ' ', strip_tags( $sjc_excerpt[0] ) ), 0, 45 ) ) . ' ...' );
endif;
endif; ?>
</div>
</div>
<?php
endforeach;
endif;
// pages 2 and onwards don't worry about cat desc or stickies, but link to main cat page
else : ?>
<p>For an introduction to this topic and our latest articles, please see the main
<?php single_cat_title(); ?> page.</p>
<ul class="other-posts">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php
// you need to replace this with whatever your theme uses!
sjc_content_nav( 'nav-below' );
// if no posts, and also no stickies and no categories, go with no results
elseif ( count( $myposts ) == 0 && count( $subcats ) == 0 ) :
get_template_part( 'no-results', 'archive' );
endif;
your problem will solved.

Related

How to display a specific category in wordpress post by id or other

I am trying to create a related post section at the bottom of each post. I am using YARPP plugin to generate the related posts. I want customise the a template to show, a thumbnail(Done), title(Done) and a specific category(Issue).
My issue is its displaying all assigned categories, I only want to display one child category. I have a speaker category with child categories of the names of the speakers.
Let me know if you need more information. Thanks
<h3>Related Posts</h3>
<?php if ( have_posts() ) : ?>
<div>
<?php
while ( have_posts() ) : the_post(); ?>
<div>
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail( $dimensions['size'], array( 'data-pin-nopin' => 'true' ) ); ?></a>
<a href="<?php the_permalink(); ?>" rel="bookmark norewrite" title="<?php the_title_attribute(); ?>">
</div>
<div>
<?php the_title(); ?>
<!-- How to add specific category -->
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if ( ! empty( $categories ) ) {
foreach( $categories as $category ) {
$term_id = '328';
$output .= '|| ' . esc_html( $category->name ) . '' . $separator;
}
echo trim( $output, $separator );
}
?>
<!-- How to add specific category END -->
</div>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>No related photos.</p>
<?php endif; ?>
<?php $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 6, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) { setup_postdata($post); ?>
<div class="post">
<div class="post-title">
<h6 class="title"><?php the_title() ?></h6>
</div>
<div class="post-img" style="background-image: url(<?php the_post_thumbnail_url(); ?>)"></div>
</div>
<?php } wp_reset_postdata(); ?>

How to create an image slide with php?

I am using a plugin on WordPress and I need to customise the php. This is the page: https://levels-ventures.com/buying/ and instead of one image displaying I would like this to be a slider.
This is the code I have:
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="es-property-inner">
<div class="es-property-thumbnail">
<div class="es-thumbnail">
<a href="<?php the_permalink(); ?>">
<?php if ( ! empty( $es_property->gallery ) ) : ?>
<?php es_the_post_thumbnail( 'es-image-size-archive' ); ?>
<?php elseif ( $image = es_get_default_thumbnail( 'es-image-size-archive' ) ) : ?>
<?php echo $image; ?>
<?php else: ?>
<div class="es-thumbnail-none">
<?php if ( ! $es_property->get_labels_list() ) : ?>
<?php _e( 'No image', 'es-plugin' ); ?>
<?php endif; ?>
</div>
<?php endif; ?>
<?php if ( $es_settings->show_labels ) : ?>
<ul class="es-property-label-wrap">
<?php foreach ( $es_property->get_labels_list() as $label ) : $value = $es_property->{$label->slug}; ?>
<?php if ( ! empty( $value ) ) : ?>
<li class="es-property-label es-property-label-<?php echo $label->slug; ?>"
style="color:<?php echo es_get_the_label_color( $label->term_id ); ?>"><?php _e( $label->name, 'es-plugin' ) ; ?></li><br>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php if ( ! empty( $es_property->gallery ) && is_array( $es_property->gallery ) ) : ?>
<div class="es-thumbnail-bottom"><?php echo count( $es_property->gallery ); ?></div>
<?php endif; ?>
</a>
</div>
</div>
Does anything know how to change this so instead of displaying one image it displayes a slider of images?
Thanks for your help!!
Use Bootstrap's Carousel. Very simple to implement with slider. Please refer to their official website Carousel

How to make post loop in two columns / Wordpress

I've been trying to split posts for a long time. I tried a variety of variations, but every time I post a single post or copy me all double.
I mean multiple posts.
If anyone has an idea how to fix it I will be very grateful to him.
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php do_action( 'esteem_before_post_content' ); ?>
<?php
if( has_post_thumbnail() ) {
$image = '';
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'blog-large');
$title_attribute = the_title_attribute( 'echo=0' );
$image .= '<figure class="post-featured-image">';
$image .= '<a href="' . get_permalink() . '" title="'.the_title_attribute( 'echo=0' ).'">';
$image .= get_the_post_thumbnail( $post->ID, 'blog-large', array( 'title' => esc_attr( $title_attribute ), 'alt' => esc_attr( $title_attribute ) ) ).'</a>';
$image .= '<div class="mask">
<div class="image-icon-wrap">
<i class="icon-search"></i>
<i class="icon-link"></i>
</div>
</div>';
$image .= '</figure>';
echo $image;
}
?>
<div class="blog-content">
<header class="entry-header">
<h2 class="entry-title">
<?php the_title(); ?>
</h2><!-- .entry-title -->
</header>
<?php esteem_entry_meta(); ?>
<div class="entry-content clearfix">
<?php the_excerpt(); ?>
</div><!-- .entry-content -->
</div>
<?php do_action( 'esteem_after_post_content' ); ?>
</article>
Post loop
<div id="primary">
<div id="content" class="clearfix">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php get_template_part( 'navigation', 'none' ); ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'none' ); ?>
<?php endif; ?>
</div><!-- #content -->
</div><!-- #primary -->
If anyone has an idea how to fix it I will be very grateful to him.
Since you are already calling post_class() function, you need to pass alternate class to that function itself and make sure to do this only on your custom page or selected blog page. The code will essentially be something like:
global $current_class;
$current_class = 'odd';
function alternating_post_class ( $classes ) {
global $current_class;
if( is_page(<PAGE_ID_HERE>) || is_home() ):
$classes[] = $current_class;
$current_class = ($current_class == 'odd') ? 'even' : 'odd';
endif;
return $classes;
}
add_filter ('post_class', 'alternating_post_class');
This should resolve the problem by giving you odd/even class which you can use to style your two columns.
Cheers Mate!!!

Wordpress: Next/Previous post link are not working

Please let me know why Next and Previous Post links are not working on the following code? I am trying to display only one post at a time. I have tried to check on different post but not able to find something similar to what I have. Please guide...
<?php
$args = array( 'numberposts' => 1 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post);
?>
<h2><?php the_title(); ?></h2>
<div>
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
</div>
<?php the_content(); ?>
<?php endforeach; ?>
<?php comments_template(); ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('PREVIOUS POSTS'); ?></div>
<div class="next"><?php previous_posts_link('NEXT POSTS'); ?></div>
</nav>
I think I had the same problem as you, simply adding this will make the link go to previous_post_link or next_post_link
<?php previous_post_link( '%link','Previous' ) ?>
<?php next_post_link( '%link','Next' ) ?>
Best of luck
According to the get_posts documentation, you should use the following method to display prev/next links:
<?php
$postlist = get_posts( 'orderby=menu_order&sort_order=asc' );
$posts = array();
foreach ( $postlist as $post ) {
$posts[] += $post->ID;
}
$current = array_search( get_the_ID(), $posts );
$prevID = $posts[$current-1];
$nextID = $posts[$current+1];
?>
<div class="navigation">
<?php if ( !empty( $prevID ) ): ?>
<div class="alignleft">
<a href="<?php echo get_permalink( $prevID ); ?>"
title="<?php echo get_the_title( $prevID ); ?>">Previous</a>
</div>
<?php endif;
if ( !empty( $nextID ) ): ?>
<div class="alignright">
<a href="<?php echo get_permalink( $nextID ); ?>"
title="<?php echo get_the_title( $nextID ); ?>">Next</a>
</div>
<?php endif; ?>
</div><!-- .navigation -->

Wordpress Blog page only showing one post

Im having an issue with a theme i've been customizing. My blog post only shows ( 1 ) post and i cant seem to fix the issue in Admin > Reading > Blog pages show at most. The Value only stays at "1" even after save. The code i have here is in the Loop.php
<?php ?>
<article class="primary-content">
<?php $firstClass='first-post' ; ?>
<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( ! have_posts() ) : ?>
<article role="main" class="the-content">
<h1><?php _e( '404 - I'm sorry but the page can't be found' ); ?></h1>
<p>Please try searching again or head back to the homepage.</p>
</article>
<?php endif; ?>
<?php ?>
<?php if (is_home()): ?>
<h1>
<?php if ( is_day() ) : ?><?php printf( __( '<span>Daily Archive</span> %s' ), get_the_date() ); ?>
<?php elseif ( is_month() ) : ?><?php printf( __( '<span>Monthly Archive</span> %s' ), get_the_date('F Y') ); ?>
<?php elseif ( is_year() ) : ?><?php printf( __( '<span>Yearly Archive</span> %s' ), get_the_date('Y') ); ?>
<?php elseif ( is_category() ) : ?><?php echo single_cat_title(); ?>
<?php elseif ( is_search() ) : ?><?php printf( __( 'Search Results for: %s' ), '<span>' . get_search_query() . '</span>' ); ?>
<?php elseif ( is_home() ) : ?>Blog<?php else : ?>
<?php endif; ?>
</h1>
<?php endif; ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php /* How to display standard posts and search results */ ?>
<article class="article-archive <?php echo $firstClass; ?>" id="post-<?php the_ID(); ?>">
<?php $firstClass="" ; ?>
<?php ?>
<?php if (is_front_page()) { ?>
<div class="home-summary">
<?php } else { ?>
<div class="entry-summary">
<?php } ?>
<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( '%s' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
<?php the_post_thumbnail( 'flozo-thumb');?>
</a>
<?php if (is_front_page()) { ?>
<h1><?php the_title(); ?></h1>
<?php } else { ?>
<h2><?php the_title(); ?></h2>
<?php } ?>
<?php the_excerpt(); ?>
<?php if ( is_home() ) : ?>
<p class="entry-meta">
<time datetime="<?php the_time('l, F jS, Y') ?>" pubdate>
<?php the_time( 'l jS F Y') ?>
</time>
</p>
<?php endif; ?>
</div>
</article>
<?php /*?>
<?php comments_template( '', true ); ?>
<?php */?>
<?php endwhile; // End the loop. Whew. ?>
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div class="navigation">
<div class="nav-previous">
<?php next_posts_link( __( 'Older posts' ) ); ?>
</div>
<div class="nav-next">
<?php previous_posts_link( __( 'Newer posts' ) ); ?>
</div>
</div>
<!-- #nav-below -->
<?php endif; ?>
</article>
Any and all help with be greatly appreciated! Sorry if this was posted already.
L

Categories