I'm trying to display a taxonomy term for each post on this page by using
<?php foreach(get_the_terms($wp_query->post->ID, 'stock') as $term) echo $term->slug; ?>
I've used this before in body classes so i can style pages better but i'm not sure why it's not working here...
can anyone help please?
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<?php $catquery = new WP_Query( 'cat=10&posts_per_page=10' ); while($catquery->have_posts()) : $catquery->the_post(); ?>
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<div class="container">
<a data-toggle="collapse" class="collapsed" data-parent="#accordion" href="#collapse<?php echo $i; ?>" aria-expanded="true" aria-controls="collapseOne">
<h4 class="panel-title">
<?php the_title(); ?><div class="stock-level"> <?php foreach(get_the_terms($wp_query->post->ID, 'stock') as $term) echo $term->slug; ?></div>
</h4></a>
</div>
</div>
<div id="collapse<?php echo $i; ?>" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-7 col-md-7 col-lg-7 recycled-image">
<?php echo the_post_thumbnail(); ?>
</div>
<div class="col-xs-12 col-sm-5 col-md-5 col-lg-5">
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
</div>
</div>
</div>
</div>
</div>
</div>
<?php $i++; endwhile; ?>
If this is more of an archive page than a single page using $wp_query->post->ID will not return the ID of all posts in the archive.
You would have to do something like:
$args = array('YOUR POST ARGS');
$posts = get_posts($args);
foreach ($posts as $post ) {
setup_postdata( $post );
foreach(get_the_terms(get_the_ID(), 'stock') as $term){
echo $term->slug.'<br />';
}
}
Related
I have some code below, and the pagination seems to be failing after the second page. I'm sure that I've done something wrong on one of the loops.
Can someone sanity check my code below for me?
<?php get_header();?>
<!-- Carousel -->
<div class="container fullwidth">
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active" style="background:url(<?php echo esc_url( home_url( '/' ) ); ?>wp-content/uploads/2018/09/testphoto.jpg);">
<div class="container">
<div class="carousel-caption microstory">
<h1>Backstage Blog</h1>
</div>
</div>
</div><!-- End Item -->
</div><!-- End Carousel Inner -->
</div>
</div>
<!-- End Carousel -->
<!-- Posts -->
<div class="container paddingtop paddingbottom">
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// are we on page one?
if(1 == $paged) { ?>
<div class="row row-eq-height paddingbottom">
<?php $query = new WP_Query( 'post_type=post&order=DESC&orderby=date&posts_per_page=1' ); ?>
<?php if ( $query->have_posts() ) :?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col col-lg-6 col-md-6 col-sm-6 col-xs-12 postpaddingbottom">
<div class="col-lg-12 col-xs-12">
<div class="featuredimage"><?php the_post_thumbnail('full'); ?></div>
<div class="blog-column">
<div class="titledatemain">
<h4><?php the_title(); ?></h4>
<ul class="blog-detail">
<li><i class="fa fa-calendar"></i> <?php the_time('F jS, Y'); ?></li>
</ul>
<p><?php the_excerpt(); ?></p>
</div>
Read More
</div>
</div>
</div>
<?php endwhile;?>
<?php endif; ?>
<?php $query2 = new WP_Query( 'post_type=advert&order=DESC&orderby=date&posts_per_page=1' ); ?>
<?php if ( $query2->have_posts() ) :?>
<?php while ( $query2->have_posts() ) : $query2->the_post();
$advert_url = get_post_meta($post->ID, 'url', true);
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); ?>
<div class="col col-lg-6 col-md-6 col-sm-6 col-xs-12 postpaddingbottom">
<a href="<?php echo $advert_url ?>"><div class="col-lg-12 col-xs-12 advertbox" style="background:url(<?php echo $featured_img_url ?>);">
</div></a>
</div>
<?php endwhile; endif; ?>
</div>
<?php
$query = new WP_Query( 'post_type=post&order=DESC&orderby=date&posts_per_page=3&offset=1&paged='. $paged ); ?>
<?php if ( $query->have_posts() ) :?>
<div class="row row-eq-height paddingbottom">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col col-lg-4 col-md-4 col-sm-4 col-xs-12 postpaddingbottom">
<div class="col-lg-12 col-xs-12">
<div class="featuredimage blogimagesmall" style="background:url(<?php the_post_thumbnail_url('full'); ?>);"></div>
<div class="blog-colum">
<div class="titledate">
<h4><?php the_title(); ?></h4>
<ul class="blog-detail">
<li><i class="fa fa-calendar"></i> <?php the_time( 'F jS, Y' ); ?></li>
</ul>
</div>
</div>
</div>
</div>
<?php endwhile;?>
</div>
<?php endif; ?>
<?php } else { ?>
<?php
$query = new WP_Query( 'post_type=post&order=DESC&orderby=date&posts_per_page=12&offset=4&paged='. $paged ); ?>
<?php if ( $query->have_posts() ) :?>
<div class="row row-eq-height paddingbottom">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col col-lg-4 col-md-4 col-sm-4 col-xs-12 postpaddingbottom">
<div class="col-lg-12 col-xs-12">
<div class="featuredimage blogimagesmall" style="background:url(<?php the_post_thumbnail_url('full'); ?>);"></div>
<div class="blog-colum">
<div class="titledate">
<h4><?php the_title(); ?></h4>
<ul class="blog-detail">
<li><i class="fa fa-calendar"></i> <?php the_time( 'F jS, Y' ); ?></li>
</ul>
</div>
</div>
</div>
</div>
<?php endwhile;?>
</div>
<?php
wp_reset_postdata();
endif; ?>
<?php } ?>
<div class="row paddingbottom">
<div class="pagenav">
<div class="alignleft"><?php previous_posts_link('Newer Posts', $query- >max_num_pages) ?></div>
<div class="alignright"><?php next_posts_link('Older Posts', $query->max_num_pages) ?></div>
</div>
</div>
</div>
<!-- End Posts -->
<?php get_footer();?>
I'm sure that there is just a mistake in my loop. I have essentially got a different layout on the first page to the rest of the pages (as you will see).
It just seems that after page 2, when you go to the 3rd page, it just keeps displaying the 2nd page content.
Any help would be great.
Here is the updated code:
<?php /* Template Name: Blog Page */ ?>
<?php get_header();?>
<!-- Carousel -->
<div class="container fullwidth">
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active" style="background:url(<?php echo esc_url( home_url( '/' ) ); ?>wp-content/uploads/2018/09/testphoto.jpg);">
<div class="container">
<div class="carousel-caption microstory">
<h1>Backstage Blog</h1>
</div>
</div>
</div><!-- End Item -->
</div><!-- End Carousel Inner -->
</div>
</div>
<!-- End Carousel -->
<!-- Posts -->
<div class="container paddingtop paddingbottom">
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// are we on page one?
$query = new WP_Query( 'post_type=post&order=DESC&orderby=date&posts_per_page=12&paged='. $paged );
if(1 == $paged) { ?>
<?php if ( $query->have_posts() ) { ?>
<div class="row row-eq-height paddingbottom">
<?php $temp_query2 = $wp_query2; ?>
<?php $query3 = new WP_Query( 'post_type=post&order=DESC&orderby=date&posts_per_page=1' ); ?>
<?php while ( $query3->have_posts() ) : $query3->the_post(); ?>
<div class="col col-lg-6 col-md-6 col-sm-6 col-xs-12 postpaddingbottom">
<div class="col-lg-12 col-xs-12">
<div class="featuredimage"><?php the_post_thumbnail('full'); ?></div>
<div class="blog-column">
<div class="titledatemain">
<h4><?php the_title(); ?></h4>
<ul class="blog-detail">
<li><i class="fa fa-calendar"></i> <?php the_time('F jS, Y'); ?></li>
</ul>
<p><?php the_excerpt(); ?></p>
</div>
Read More
</div>
</div>
</div>
<?php endwhile; ?>
<?php $wp_query2 = $temp_query2; ?>
<?php $temp_query = $wp_query; ?>
<?php $query2 = new WP_Query( 'post_type=advert&order=DESC&orderby=date&posts_per_page=1' ); ?>
<?php while ( $query2->have_posts() ) : $query2->the_post();
$advert_url = get_post_meta($post->ID, 'url', true);
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); ?>
<div class="col col-lg-6 col-md-6 col-sm-6 col-xs-12 postpaddingbottom">
<a href="<?php echo $advert_url ?>"><div class="col-lg-12 col-xs-12 advertbox" style="background:url(<?php echo $featured_img_url ?>);">
</div></a>
</div>
<?php endwhile; ?>
<?php $wp_query = $temp_query; ?>
</div>
<div class="row row-eq-height paddingbottom">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col col-lg-4 col-md-4 col-sm-4 col-xs-12 postpaddingbottom">
<div class="col-lg-12 col-xs-12">
<div class="featuredimage blogimagesmall" style="background:url(<?php the_post_thumbnail_url('full'); ?>);"></div>
<div class="blog-colum">
<div class="titledate">
<h4><?php the_title(); ?></h4>
<ul class="blog-detail">
<li><i class="fa fa-calendar"></i> <?php the_time( 'F jS, Y' ); ?></li>
</ul>
</div>
</div>
</div>
</div>
<?php endwhile;
rewind_posts();
}?>
</div>
<?php } else { ?>
<div class="row row-eq-height paddingbottom">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col col-lg-4 col-md-4 col-sm-4 col-xs-12 postpaddingbottom">
<div class="col-lg-12 col-xs-12">
<div class="featuredimage blogimagesmall" style="background:url(<?php the_post_thumbnail_url('full'); ?>);"></div>
<div class="blog-colum">
<div class="titledate">
<h4><?php the_title(); ?></h4>
<ul class="blog-detail">
<li><i class="fa fa-calendar"></i> <?php the_time( 'F jS, Y' ); ?></li>
</ul>
</div>
</div>
</div>
</div>
<?php endwhile;?>
</div>
<?php wp_reset_postdata(); ?>
<?php } ?>
<div class="row paddingbottom">
<div class="pagenav">
<div class="alignleft"><?php previous_posts_link('Newer Posts', $query->max_num_pages) ?></div>
<div class="alignright"><?php next_posts_link('Older Posts', $query->max_num_pages) ?></div>
</div>
</div>
</div>
<!-- End Posts -->
<?php get_footer();?>
You should see i'm now running one loop which is nice.. but I have put the first post in its own loop within a loop to show the first post. The issue i'm having now is that I want to on the main loop and only on the first page, have an offset of 1. But I can't work out how to do this just on the first page :(
i am trying to fetch posts in bootstrap accordian like this
<div class="container">
<div class="panel-group" id="accordion">
<?php
if ( have_posts() ) :
// Start the Loop.
while ( have_posts() ) :the_post ();?>
<div class='col-md-12 col-xs-12'>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="false" class="collapsed"><?php echo the_title()?></a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse" aria-expanded="false" style="height: 0px;">
<div class="panel-body">
<p><?php echo the_content()?></p>
</div>
</div>
</div>
<?php
get_template_part( 'content', get_post_format() );
endwhile;
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
</div>
<?php // get_sidebar( 'content' ); ?>
</div>
now problem is that once post content is printed inside accordian and once again its getting printed outside accoridan
I'm building a page where i want to display posts from the category, 'Recycled Aggregates' in a Bootsrap Accordion. I've managed to display each post in a new Panel which is great but now i need to display a custom taxonomy on each of these which is called 'Stock levels'.
What i've got so far... (which doesn't output the value)
Can anyone shed any light to why this isn't working?
Kind Regards,
Shaun
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<?php foreach(get_the_terms($wp_query->post->ID, ‘stock-levels’) as $term);?>
<?php $catquery = new WP_Query( 'cat=10&posts_per_page=10' ); while($catquery->have_posts()) : $catquery->the_post(); ?>
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<div class="container">
<a data-toggle="collapse" class="collapsed" data-parent="#accordion" href="#collapse<?php echo $i; ?>" aria-expanded="true" aria-controls="collapseOne">
<h4 class="panel-title">
<?php the_title(); ?><div class="stock-level"><?php echo $term;?></div>
</h4></a>
</div>
</div>
<div id="collapse<?php echo $i; ?>" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-7 col-md-7 col-lg-7 recycled-image">
<?php echo the_post_thumbnail(); ?>
</div>
<div class="col-xs-12 col-sm-5 col-md-5 col-lg-5">
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
</div>
</div>
</div>
</div>
</div>
</div>
<?php $i++; endwhile; ?>
</div>
</div>
</div>
</div>
Although this is not the right way of doing it, but for a quick fix just add the parameters below to the wp_query, replace TAXONOMY_NAME By the name of the taxonomy and TAXONOMY_TERM by the term within that taxonomy
new WP_Query( 'cat=10&posts_per_page=10&TAXONOMY_NAME=TAXONOMY_TERM' )
EDIT:
Well then add $args array before the query and replace wp_query with the one below.
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'TAXONOMY_NAME',
),
),
);
$catquery = new WP_Query( $args );
I create a template for category menu. when click "category", based on category it reload the page shows the post in the category. i need to make the category menu active. How to do?
Category-menu.php
<div class="row text-center" >
<div class="col-md-12 col-xs-12 col-sm-12 col-lg-12">
<?php
$menu_item=wp_get_nav_menu_items( 'Category Menus', $args );
foreach ($menu_item as $menu) {
?>
<div class="col-lg-3 col-sm-12 col-xs-12 col-md-3 menu-padding">
<div class="blog-menu-items cat-menu-color">
<a href="<?php echo $menu->url; ?>">
<?php echo $menu->title; ?>
</a>
</div>
</div>
<?php } ?>
</div>
</div>
Category.php
<?php get_template_part( 'category', 'menus' ); ?>
<?php $cat_title = strtoupper(get_cat_name($category_id)); ?>
<div class="row" style="padding-top:5%;">
<div class="col-md-12 col-xs-12 col-sm-12 col-lg-12">
<a href="<?php echo home_url() ?>" class="back-to-blog">
<i class="icon-chevron-left"></i>
<?php echo __( 'Back to Home', 'tiny_theme') ?>
</a>
</div>
</div>
<div class="row popular-posts">
<div class="col-md-12 col-xs-12 col-sm-12 col-lg-12">
<div class="col-lg-8 col-sm-6 col-xs-6 col-md-8 popular-posts-header pull-left">
<h4>POSTS ON <?php echo $cat_title ?></h4>
</div>
<?php if ($query1->max_num_pages > 1) { ?>
<div class="col-lg-4 col-sm-6 col-xs-6 col-md-4 pull-right" id="popular-post-pager" >
<?php echo paginate_links( $pag_args1 );?>
</div>
<?php } ?>
</div>
<div class="col-md-12 col-xs-12 col-sm-12 col-lg-12">
<?php
if ($query1->have_posts()) {
while ($query1->have_posts()) {
$query1->the_post();
?>
<h4 style="padding-top:5px;">
<?php echo the_title(); ?>
</h4>
<p> <?php the_excerpt(10); ?></p>
<?php }
}
wp_reset_query();
?>
</div>
</div>
You can try to do a comparaison between the current category ID and the object_id which holds the ID of the object the link is in relation with, something like this
(not tested)
<div class="row text-center" >
<div class="col-md-12 col-xs-12 col-sm-12 col-lg-12">
<?php
global $post;
$menu_item=wp_get_nav_menu_items( 'Category Menus', $args );
foreach ($menu_item as $menu) {
$currentClass = '';
if($post->ID == $menu->object_id)
$currentClass = 'current-menu-item';
?>
<div class="col-lg-3 col-sm-12 col-xs-12 col-md-3 menu-padding">
<div class="blog-menu-items cat-menu-color">
<a class="<?php echo $currentClass; ?>" href="<?php echo $menu->url; ?>">
<?php echo $menu->title; ?>
</a>
</div>
</div>
<?php } ?>
</div>
</div>
I'm trying to create an accordion where the first one is open (with this CSS that means that the panel has 'in' added to the panel-collapse div).
So, I'm setting the variable $i to 1 and just echoing 'in' when it does equal 1, incrementing after the first. When this is executed, however, all looped posts have 'in' and so are open. Can anyone see what I'm doing wrong?
<?php
$i=1;
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<!-- individual panel -->
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#faqs" href="#<?php the_ID(); ?>">
<?php the_title(); ?>
</a>
</h4>
</div>
<div id="<?php the_ID(); ?>" class="panel-collapse collapse <?php if ($i=1) { echo 'in'; } ?>">
<div class="panel-body">
<?php the_field('answer'); ?>
</div>
</div>
</div>
<!-- /individual panel -->
<?php $i++; endwhile;
?>
<?php
$i=1;
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<!-- individual panel -->
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#faqs" href="#<?php the_ID(); ?>">
<?php the_title(); ?>
</a>
</h4>
</div>
<div id="<?php the_ID(); ?>" class="panel-collapse collapse <?php if ($i==1) { echo 'in'; } ?>">
<div class="panel-body">
<?php the_field('answer'); ?>
</div>
</div>
</div>
<!-- /individual panel -->
<?php $i++; endwhile;
?>
It should be $i==1 not $i=1 in your if condition
When you do $i=1 it means you are assigning value 1 to $i , so every time it is successfully satisfying condition.
Here is Comparison Operators :D