Highlight (Active) menu post title in a category.php - php

This is the website i´m working on: http://canal.es/wordpress/
The problem I have is that, if you go, to "Pasteleria dulce" (which is a category.php file) it shows a left sidebar with the active title of the category, it shows a post on the center and it shows below the image a list of the post of the same category.
I haven´t find a solution to highlight the post title that opens automatically when I enter to the category. This is the code i´m using on the category.php file:
<!-- Highlight menu from current category -->
<?php
if (is_category()) {
$this_category = get_category($cat);
}
if($this_category->category_parent)
$this_category = wp_list_categories('orderby=id&title_li=&child_of='.$this_category->category_parent."&echo=0"); else
$this_category = wp_list_categories('orderby=id&title_li=&child_of='.$this_category->cat_ID."&echo=0");
if ($this_category) { ?>
<ul class="sub-menu">
<?php echo $this_category; ?>
</ul>
<?php } ?>
<div class="producto_column">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="producto_image">
<img src="<?php the_field('imagen_producto'); ?>" alt=""/>
</div>
<div class="share_item">
<a class="minimal" id="premium">
<img src="<?php bloginfo('template_directory') ?>/images/share.png" alt="share">
</a>
<a class="maillink" href="http://canal.local/?page_id=220">
<img src="<?php bloginfo('template_directory') ?>/images/email.png" alt="email">
</a>
</div>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<!-- Post list from current category -->
<ul id="submenu_productos" class="clearfix">
<?php
$IDOutsideLoop = $post->ID;
while( have_posts() ) {
the_post();
foreach( ( get_the_category() ) as $category )
$my_query = new WP_Query('category_name=' . $category->category_nicename . '&orderby=title&order=asc&showposts=100');
if( $my_query ) {
while ( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<li<?php print ( is_single() && $IDOutsideLoop == $post->ID ) ? ' class="test"' : ''; ?>>
<?php the_title(); ?> |
</li>
<?php
}
}
}
?>
</ul>
How can I make active the current post title that appears when I open the category page?

Try to use this solution:
<ul>
<?php
$lastposts = get_posts('numberposts=5&orderby=rand&cat=-52');
foreach($lastposts as $post) :
setup_postdata($post); ?>
<li<?php if ( $post->ID == $wp_query->post->ID ) { echo ' class="current"'; } else {} ?>>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>

Related

Change page template to show Custom Post Type in Wordpress

I have this Page Template, developed by someone else a long long time ago, which shows all my Posts in a grid based on the category they're in.
Now, I want to have such a page for my Custom Post Type but I can't get it to work. I tried replacing the
<?php if (have_posts()) : while (have_posts()) : the_post();?>
with
<?php $args = array('post_type'=>array('shopitems')); query_posts($args); if (
have_posts() ) : while ( have_posts() ) : the_post();?>`
which seems to be the go-to answer everywhere I look. but for me, it turns out empty.
Below the original Page Template, I want to change this so it loads the 'shopitems' Custom Post Type.
Any help or nod in the right direction is greatly appreciated!
<?php get_header(); ?>
<div class="content-sidebar-wrap">
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<?php $category_list= get_the_content(); ?>
<?php endwhile; endif; ?>
<div id="content" class="post-category page">
<?php $category_name=explode(',', $category_list);
$catIDs="";
foreach ($category_name as $cat_name) {
$catIDs.= get_cat_ID(trim($cat_name)).",";
}
$catIDs=trim($catIDs,",");
$i=0;
?>
<?php $thePosts= query_posts('cat='.$catIDs.'&post_status=publish&posts_per_page=-1'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if (has_post_thumbnail( $post->ID ) ) { ?>
<?php // $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) ); ?>
<?php if($i%4==0) { ?>
<div class="clear"></div>
<?php } $i++; ?>
<div class="thumb">
<a href="<?php the_permalink(); ?>">
<img alt="" src="<?php echo $image[0]; ?>" class="thumb-img">
<span class="thumb-title">
<?php
$second_name = get_field('second_name');
if( $second_name && !empty($second_name))
echo $second_name;
else
the_title();
?>
</span>
</a>
</div>
<?php } endwhile; else: endif; ?>
</div>
</div>
<?php get_footer(); ?>
Just you need to modify your query like below.
<?php
$args = array(
'post_type'=>'shopitems'
);
$query = new WP_Query($args);
if ($query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
//do your code here
endwhile;
else:
// No posts found
endif;
?>`

ACF Repeatable Content Widget - WordPress

I've created a Field group called 'Team Members' and set the field type as repeatable with three sub fields; Image, Position and Name.
The rule for this group is to show within a custom widget I've created. When I drag the widget in the Appearance > Widgets area of WordPress I can see it is indeed there and can add members / images to my widget.
Where I've hit a brick wall is trying to output this data on the front end through my widget template.
This is my code:
<?php if( have_rows('team_members') ): ?>
<ul class="slides">
<?php while( have_rows('team_members') ): the_row();
// vars
$image = get_sub_field('image');
$content = get_sub_field('name');
$link = get_sub_field('position');
?>
<li class="slide">
<?php if( $link ): ?>
<a href="<?php echo $link; ?>">
<?php endif; ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
<?php if( $link ): ?>
</a>
<?php endif; ?>
<?php echo $content; ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Where am I going wrong?
To get values from your custom widget via ACF the syntax is:
if ( have_rows( 'team_members', 'widget_' . $widget_id ) ) :
while have_rows( 'team_members', 'widget_' . $widget_id ) ) : the_row();
// get sub fields ...
endwhile;
endif;

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

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.

WP posts_per_page or showposts?

New to PHP. Below is the code to show posts and pagination. I'm trying to get 10 posts per page to show and am confused on what code to write to do this. I tried changing Reading Settings to 10 blog posts, but when I save it, it overwrites back to one. So I figured the setting is being overwritten in php somewhere. I'm looking to overwrite that here. Please help.
I tried adding:
but not only does 10 posts show, so does a second category list below the posts.
<?php get_template_part('templates/page', 'header'); ?>
<?php if (!have_posts()) : ?>
<div class="alert">
<?php _e('Sorry, no results were found.', 'roots'); ?>
</div>
<?php get_search_form(); ?>
<?php endif; ?>
<?php $i = 0; ?>
<?php while (have_posts()) : the_post(); $i++; ?>
<article class="<?php $allClasses = get_post_class(); foreach ($allClasses as $class) { echo $class . " "; } if($i&1) { echo 'odd';} else {echo 'even';}; ?> block clearfix">
<?php get_template_part('templates/content-category', get_post_format()); ?>
</article>
<?php endwhile; ?>
<?php if ($wp_query->max_num_pages > 1) : ?>
<nav class="post-nav">
<ul class="pager">
<li class="previous"><?php next_posts_link(__('← Older posts', 'roots')); ?></li>
<li class="next"><?php previous_posts_link(__('Newer posts →', 'roots')); ?></li>
</ul>
</nav>
<?php endif; ?>
try adding this to your functions.php
function trance_posts_per_page( $query ) {
if (! is_main_query())
return;
$query->set( 'posts_per_page', 20 );
}
add_action( 'pre_get_posts', 'trance_posts_per_page' );
check the plugin WP-PageNavi if this doesn't help you
Got to work by removing this:
<?php $i = 0; ?>
<?php while (have_posts()) : the_post(); $i++; ?>
<article class="<?php $allClasses = get_post_class(); foreach ($allClasses as $class) { echo $class . " "; } if($i&1) { echo 'odd';} else {echo 'even';}; ?> block clearfix">
<?php get_template_part('templates/content-category', get_post_format()); ?>
</article>
<?php endwhile; ?>
and replacing it with this:
<?php query_posts( $query_string . '&posts_per_page=-10' );?>
<?php while (have_posts()) : the_post(); ?>
<article class="block clearfix">
<?php get_template_part('templates/contentcategory',
get_post_format()); ?>
</article>
<br />
<?php endwhile; ?>

Finding and fixing the missing div tags - WordPress custom template

I am having a problem with the page http://www.believeinstjohn.com/store/
What I'm ultimately trying to accomplish is moving the sidebar to its proper place (right sidebar column). It is showing three missing </div>tags, it is a WordPress site and the page is using a custom template. I cannot figure out where the missing tags are. The template is pulling resources from the rest of the site, and the other pages are just fine. Two of the divs (container & container-2) are located in the header.php, but closing them out there messes up the whole site. I must have missed something in the template, but cannot see it. Here is the PHP for the template
<?php
/**
* Template Name: Products
*
* Selectable from a dropdown menu on the edit page screen.
* #package WordPress
* #subpackage Adventure_Journal
*/
$themeOpts = get_option('ctx-adventurejournal-options');
get_header();
?>
<div class="content" <?php ctx_aj_getlayout(); ?>>
<div id="col-main" style="<?php echo ctx_aj_customwidth('content'); ?>">
<div id="main-content" <?php //ctx_aj_crinkled_paper(); ?>>
<?php
global $wp_query, $post;
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$tax = $wp_query->query_vars['product-category'];
query_posts( array ('post_type' => 'products', 'paged' => $paged, 'product-category' => $tax ) );
$loop_counter = 1;
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li class="product <?php if ( $loop_counter == 2 ) { echo "product-last"; } ?>">
<div class="product_wrap">
<?php
$title = get_the_title().' ';
$chars_title = strlen($title);
$title = substr($title,0,28);
$title = substr($title,0,strrpos($title,' '));
if ($chars_title > 28) { $title = $title."..."; }
$excerpt = get_the_excerpt();
if (strlen($excerpt) > 150) {
$excerpt = substr($excerpt,0,strpos($excerpt,' ',80)); } ;
$excerpt = $excerpt.' ...';
if (has_post_thumbnail()) {
echo '<a href="'. get_permalink() .'" class="product-thumb" title="'. the_title_attribute('echo=0') .'">';
the_post_thumbnail( 'Product Thumb', array('title'=>$post_title) );
echo '</a>';
}
?>
<h3 class="entry-title product-title"><?php echo $title; ?></h3>
<div class="product_content">
<?php echo apply_filters('the_excerpt',$excerpt); ?>
</div>
<div class="product_details">
<a class="button" href="<?php the_permalink(); ?>">More Info</a>
</div>
</div>
</li>
<?php if ( $loop_counter == 2 ) {
$loop_counter = 1;
echo '<li class="clear"></li>';
} else {
$loop_counter++;
} ?>
<?php endwhile; else: endif; ?>
<div class="clear"></div>
<?php get_sidebar('store'); ?>
<?php get_footer(); ?>
Where do you close the following tags ?
<div class="content" <?php ctx_aj_getlayout(); ?>>
<div id="col-main" style="<?php echo ctx_aj_customwidth('content'); ?>">
<div id="main-content" <?php //ctx_aj_crinkled_paper(); ?>>

Categories