wordpress primary and secondary navigation - wordpress

The below is my landing page "Products". it will display 2 columns, in the left is the secondary navigation and the right is the content. i want that if someone click in the primary navigation "Products". In second navigation the first item will in bold and will display the content.
<?php
/*
Template Name: Product Navigation Page
*/
?>
<?php get_header(); ?>
<div class="container page-border-top">
<div id="product-navigation" class="col-md-3" style="border: 1px solid green;">
<div id="navbar" class="navbar-collapse collapse">
<?php
$args2 = array(
'menu' => 'product-menu',
'menu_class' => 'nav navbar-nav',
'theme_location' => 'product-menu',
'container' => 'false'
);
wp_nav_menu( $args2 );
?>
</div><!--/.navbar-collapse -->
</div>
<div class="col-md-9" style="border: 1px solid red;">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</div>
</div>
<?php get_footer(); ?>

If your landing page "Products" has no parent, and if all products child pages of "Products", then you can use this template file for the landing page and for all child pages (products):
<?php
/*
Template Name: Product Navigation Page
*/
$redirect = false;
$parent_id = wp_get_post_parent_id( $post->ID );
if ( ! $parent_id ) {
$redirect = true;
$parent_id = $post->ID;
}
// get all child's
$all_wp_pages = $wp_query->query(array(
'post_type' => 'page',
'order' => 'ASC',
'orderby' => 'menu_order',
'post_status' => 'publish'
));
$childs = get_page_children( $parent_id, $all_wp_pages );
if ( $redirect ) {
wp_redirect( get_permalink( $childs[0]->ID ) );
exit;
}
$current_page_id = $post->ID;
?>
<?php get_header(); ?>
<div class="container page-border-top">
<div id="product-navigation" class="col-md-3" style="border: 1px solid green;">
<div id="navbar" class="navbar-collapse collapse">
<ul class="menu">
<?php foreach ( $childs as $child ): ?>
<li class="<?php if ( $current_page_id == $child->ID ): ?>current-menu-item<?php endif; ?>"><?php echo $child->post_name; ?></li>
<?php endforeach; ?>
</ul>
</div><!--/.navbar-collapse -->
</div>
<div class="col-md-9" style="border: 1px solid red;">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</div>
</div>
<?php get_footer(); ?>
You dont't have to create a menu for the products, it just list all child pages, and you can sort the products by the Order field (Page Attributes).
Step by Step:
Create a page "Products" and select the template file above for it (Product Navigation Page)
Create some product pages as childs of "Products" and select also the template file above for it (Product Navigation Page).
The template file will automaticly create the desired secondary navigation on the left side. The current product has a css class current-menu-item, so you can style it in bold.
This part generates the second nav:
<ul class="menu">
<?php foreach ( $childs as $child ): ?>
<li class="<?php if ( $current_page_id == $child->ID ): ?>current-menu-item<?php endif; ?>"><?php echo $child->post_name; ?></li>
<?php endforeach; ?>
</ul>

Related

Show parent child taxonomy in nested tabs with its post

I want to show parent taxonomy in tab section and the under parent taxonomy i want to display child taxonomy in nested tab. On selection of child taxonomy i want to show its post. I am getting every time child taxonomy with with its post which i don't want.
Here is what i want:
TAB1(parent active) TAB2(parent)
ALL_TAB1(child active) SUB1_TAB1 SUB2_TAB1
(posts for all, sub1_tab1, sub2_tab1 and respectively whichever is active)
What i tried
<?php
/*
Template Name: Prac
*/
get_header();
// Get list of 'categories' for tabs -->
$args = array(
'hide_empty' => false,
'parent' => 0
);
$preferences = get_terms( 'preferences', $args );
?>
<div class="row">
<div class="col-md-12">
<?php
while ( have_posts() ) : the_post();
the_content();
endwhile; ?>
<ul class="nav nav-tabs" id="myTab" role="tablist">
<!-- Create the tabs -->
<?php
// Use counter so that 'active' class is only applied to first tab
$counter = 0;
foreach ($preferences as $preference) { ?>
<li class="nav-item">
<a class="nav-link <?= ($counter == 0) ? 'active' : '' ?>" id="<?php echo $preference->slug;?>-tab" data-toggle="tab" href="#<?php echo $preference->slug; ?>" role="tab" aria-controls="<?php echo $preference->slug;?>" aria-selected="<?= ($counter == 0) ? 'true' : 'false' ?>"><?php echo $preference->name; ?></a>
</li>
<?php $counter++; } ?>
</ul>
<div class="tab-content" id="nav-tabContent">
<!-- Get the content for each tab -->
<?php
$counter2 = 0;
foreach ($preferences as $preference) { ?>
<div role="tabpanel" class="tab-pane fade <?= ($counter2 == 0) ? 'show active' : '' ?>" id="<?php echo $preference->slug; ?>" aria-labelledby="<?php echo $preference->slug; ?>-tab">
<div class="row">
<?php
$args = array(
'post_type' => 'project',
'tax_query' => array(
array(
'taxonomy' => $preference->taxonomy,
'field' => $preference->slug,
'terms' => $preference->term_id
)
)
);
$loop = new WP_Query( $args );
?>
<div class="col-md-6" id="<?php echo $preference->slug . '-clauses' ?>">
<?php
?>
<?php
if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
//Do something if a specific array value exists within a post
$term_list = wp_get_post_terms($post->ID, $preference->taxonomy, array("fields" => "all"));
?>
<?php foreach($term_list as $term_single) {
$category_children = get_terms(array(
'parent' => $term_single->term_id,
'hide_empty' => false
) );
$category_children_count = count($category_children);
?>
<?php } ?>
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<?php echo $term_single->name; ?>
</li>
</ul>
<a class="col-md-6" href="<?php echo the_permalink();?>"><?php the_post_thumbnail();?><?php echo the_title(); ?></a>
<?php endwhile; endif; wp_reset_query(); ?>
</div>
</div> <!-- end row -->
</div> <!-- end tab-pane -->
<?php $counter2++; } ?>
</div> <!-- end tab-content -->
</div> <!-- end col -->
</div> <!-- end row -->
<!-- end content -->
<?php get_footer(); ?>
I am getting like this
TAB1 TAB2
SUB1_TAB1
post
SUB1_TAB1
post
SUB1_TAB2
Can anyone help ? Thanks.

how to add pagination in wordpress

I have posts in different custom taxonomies. I need to display posts for one taxonomy in a page. Now I'm getting the all the posts in all taxonomies in a single page. I have a foreach loop for taxonomy and inside the loop all the posts corresponding to that taxonomy is displaying through post method.I have never used pagination before. How can I add pagination in this case?
I am enclosing my code below
<div class="main">
<section class="brands-sec product-sec">
<div class="container">
<?php
$siteurl = home_url('/');
$tax = 'product'; // slug of taxonomy
$terms = get_terms($tax);
foreach ($terms as $term) {
$id = $term->term_id;
$slug = $term->slug;
$description = $term->description;
$image_url = z_taxonomy_image_url( $id, NULL, TRUE ); // category image display
$link = "<a href='$siteurl?$tax=$slug' ><h1> $term->name </h1></a>";
echo '<img src="' . $image_url . '">'; ?>
<div class="col-md-8 pull-right col-sm-10 pull-right col-xs-12 brnd prdct">
<img src="<?php echo $image_url ; ?>" class="img-responsive pdt-logo" alt="loyd"/>
<div class="brand-logos pdt">
<p><?php echo $description ; ?></p>
<h4>Product</h4>
<?php $args = array("posts_per_page" => "-1", "product"=>$slug ,"post_type" => "products" );
$posts = get_posts($args);
foreach($posts as $data){
$thumb = wp_get_attachment_url( get_post_thumbnail_id($data ->ID) );
$custom_fonts = get_post_custom($data->ID);$custom_fonts_key= $custom_fonts['category'];$custom_fonts_array = $custom_fonts_key[0]; ?>
<div class="row mb40">
<div class="col-md-4 col-sm-4 col-xs-12 brand-single product-single">
<img src="<?php echo $thumb; ?>" class="img-responsive" alt="allegro products"/>
<h5><?php echo $data->post_title; ?></h5>
<p><?php echo $data->post_content; ?></p>
</div>
<div class="col-md-8 col-sm-8 col-xs-12 brand-single product-detail">
<ul class="products">
<?php
$attachments = new Attachments( 'my_attachments',$data->ID ); /* pass the instance name */ ?>
<?php if( $attachments->exist() ) : ?>
<?php while( $attachments->get() ) : ?>
<li><img src="<?php echo $attachments->url(); ?>" class="img-responsive" alt="allegro products"/></li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div>
</div>
<?php } ?>
</div><!--end brand-logos-->
</div><!--end brnd-->
<?php } ?>
</div>
Here product is custom taxonomy . In that there are more than one terms say loyd,Nycofee,.... In Both these terms there are more than one posts. I need to display posts in loyd in one page and ncofee in next page. How to add pagination in this case?
Try this:
You have the post_per_page to be -1
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array("posts_per_page" => "-1", "product"=>$slug ,"post_type" => "products" );
change it to the number you would like. and add to the end
'paged' => $paged
before Ending the PHP here
</div>
<?php } ?>
</div><!--end brand-logos-->
Add
<?php if (function_exists('wp_pagenavi')) wp_pagenavi(array('query' => $args )); ?>
<?php wp_reset_postdata(); ?>
Here is the basic pagination for wordpress
<?php if ( have_posts() ) : ?>
<!-- Add the pagination functions here. -->
<!-- Start of the main loop. -->
<?php while ( have_posts() ) : the_post(); ?>
<!-- the rest of your theme's main loop -->
<?php endwhile; ?>
<!-- End of the main loop -->
<!-- Add the pagination functions here. -->
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Soruce: https://codex.wordpress.org/Pagination "Example Loop with Pagination"
If it still show the full list.
Try this: http://callmenick.com/post/custom-wordpress-loop-with-pagination

Wordpress/woocommerce css error

I've followed: https://wordpress.stackexchange.com/questions/67247/how-to-display-product-specific-to-a-category-with-woocommerce-plugin
This code is placed in CustomPageT1, and has this code:
<?php /* Template Name: CustomPageT1 */
get_header(); ?>
<div id="content" class="site-content container">
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<ul class="products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 4, 'product_cat' => 'skydd-mot-djur', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="post-18 product type-product status-publish product_cat-skydd-mot-manniskor first instock shipping-taxable purchasable product-type-simple" style="width: 25%;float:left">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>" class="woocommerce-LoopProduct-link">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" class="woocommerce-placeholder wp-post-image" height="300px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul><!--/.products-->
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This page is created by adding /* Template Name: CustomPageT1 */ and then add it from the "add page" function. Then copy-paste from page.php into this file.
I can edit and all, the logo, menu, and some design works great. Without the Woocommerce one, in style.css I can get the css. But from the theme it doesnt fetch the css.
Which means that it looks bad, without the css.
Anyone knows why?
Because you have created a page without a link to the header file.
By creating a page with this code
/* Template Name: CustomPageT1 */
means create a page with template CustomPageT1.
The new page has no means to connect to the CSS file.
create the header-CustomPageT1.php file with the link to the CSS file andd upload to it's corresponding theme folder.
Also change this code: get_header() to get_header(CustomPageT1)

How to display the category wise news in tabs ?? I am using news manager plugin for adding news?

Till now i have listed the category in tabs. But did not get the news and its description under the category. And in database the news are treated as post's.
And I am using the jquery tabs for displaying news..
MY CODE:---
<?php
/*
Template Name: tabbing
*/
?>
<?php get_header(); ?>
<div id="tab">
<?php
//list terms in a given taxonomy (useful as a widget for twentyten)
$taxonomy = 'news-category';
$tax_terms = get_terms($taxonomy);
?>
<ul class="nav nav-tabs" role="tablist">
<?php $counter = 0; foreach ($tax_terms as $tax_term) { ?>
<li role="presentation" class="post-<?php ?> <?=($counter == 1) ? 'active' : ''?>"><?php echo $tax_term->name; ?></li>
<?php $counter++; } //exit; ?>
</ul>
/* Right all description is displaying but not according to the category... */
<div class="tab-content">
<?php
$counter = 0;
$loop = new WP_Query( array( 'post_type' => 'news', 'posts_per_page' => -1 ) );
while ( $loop->have_posts() ) : $loop->the_post();
$counter++;
?>
<div role="tabpanel" class="tab-pane <?=($counter == 1) ? 'active' : ''?>" id="post-<?php the_ID(); ?>"><?php the_content();?></div>
<?php endwhile; ?>
</div>
</div>
<?php get_footer(); ?>

How do I display the second and third most recent posts from a category in WordPress

I am displaying previews of the three most recent news articles on my homepage. The most recent post will be displayed in a different format to the second and third most recent posts.
I am currently displaying all three the same with the following code
<?php query_posts('cat=2 && showposts=3');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="col-xs-12 col-sm-4">
<div class="column">
<div class="news-article">
<p class="news-date"><?php the_time( get_option( 'date_format' ) ); ?></p>
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('full', array( 'class' => 'img-responsive img-rounded news-img' )); } ?>
<p class="news-headline"><?php the_title(); ?></p>
</a>
<p><?php the_excerpt(); ?></p>
<a href="<?php the_permalink(); ?>">
<p class="pull-right">Read more...</p>
</a>
<span class="clearfix"></span>
</div>
</div>
</div>
<?php
endwhile;
endif;
?>
How can I add another loop which will separate the second and third most recent posts from the most recent post?
I did not want to use postID as the posts will change.
The two arguments in the WP_Query function below combine to retrieve the second most recent post, then the HTML below displays that post.
<div class="video-message">
<p>
<ul>
<!-- // Define our WP Query Parameters -->
<?php
$the_query = new WP_Query( array( 'posts_per_page' => 1,'offset' => 1 ) );
?>
<!-- // Start our WP Query -->
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<!-- // Display the Post Title with Hyperlink -->
<p><?php the_title(); ?></p>
<?php
endwhile;
wp_reset_postdata();
?>
</ul>
</p>
</div>
</div>
To display the third most recent post would require changing the offset value to 2, so that the program skips over the two most recent posts.
$the_query = new WP_Query( array( 'posts_per_page' => 1,'offset' => 2 ) );
This method is discussed in the Pagination Parameters section of the WordPress Code Reference.
Untested but you could try:
<?php
$count = 0;
query_posts('cat=2 && showposts=3');
if (have_posts()) : while (have_posts()) : the_post();
if($count == 0)
{
?>
<div class="col-xs-12 col-sm-4">
<div class="column">
<div class="news-article">
<p class="news-date"><?php the_time( get_option( 'date_format' ) ); ?></p>
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('full', array( 'class' => 'img-responsive img-rounded news-img' )); } ?>
<p class="news-headline"><?php the_title(); ?></p>
</a>
<p><?php the_excerpt(); ?></p>
<a href="<?php the_permalink(); ?>">
<p class="pull-right">Read more...</p>
</a>
<span class="clearfix"></span>
</div>
</div>
</div>
<?php
$count = 1;
}
else
{
//Some other layout here
}
endwhile;
endif;
?>
The above will check if $count is 0 and if it is, then do the layout and the count will then equal to 1. So the next time around $count won't be 0, so it will run what is in the else (which will be your layout).
I use WP_Query like this to show all posts from the fourth most recent one:
<?php
$query4 = new WP_Query( 'posts_per_page=4&offset=3' );
?>

Resources