I wrote a short code for wordpress but the outputs that will be printed is somehow weird
so this is my loop code
function events_accordion_shortcode($category){
// WP_Query arguments
$args = array (
'post_type' => array( 'event' ),
'post_status' => array( 'publish' ),
'nopaging' => true,
'order' => 'ASC',
'orderby' => 'menu_order',
'meta_query' => array(
array(
'key' => 'custom_page_category',
'value' => $category,
)
),
);
// The Query
$events = new WP_Query( $args );
echo '<div class="container">';
// The Loop
$first = true;
if ( $events->have_posts() ) {
echo '<ul class="responsive-table">
<li class="table-header">
<div class="col col-3">Event</div>
<div class="col col-6">date</div>
<div class="col col-6"> location</div>
<div class="col col-6">FOCUS</div>
</li>';
while ( $events->have_posts() ) {
$events->the_post();
$post_id=get_the_ID();
$mydate = get_post_meta($post_id, 'date', true);
$mylocation = get_post_meta($post_id, 'location', true);
$myfocus = get_post_meta($post_id, 'focus', true);
echo '<li class="table-row accordion">' .
'<div class="col col-3" data-label="title">' . the_title() . '</div>' .
'<div class="col col-6" data-label="date">'. $mydate .'</div>' .
'<div class="col col-6" data-label="location">'. $mylocation .'</div>' .
'<div class="col col-6" data-label="focus">'. $myfocus .'</div>' .
'<div class="col col-6" data-label="plus">
<i id="load-more" aria-hidden="true" class="fas fa-plus"></i>
</div>' .
'</li>';
echo '<li class="panel"></li>';
}
} else {
// no posts found
}
echo '</ul></div>';
}
the problem is that the "the_title()" value gets printed outside the li tag, this is the weird output I get:
<div class="container">
<ul class="responsive-table">
<li class="table-header">
<div class="col col-3">Event</div>
<div class="col col-6">date</div>
<div class="col col-6"> location</div>
<div class="col col-6">FOCUS</div>
</li>
event 1
<li class="table-row accordion">
<div class="col col-3" data-label="title"></div>
<div class="col col-6" data-label="date">13/6/2019</div>
<div class="col col-6" data-label="location"></div>
<div class="col col-6" data-label="focus">brain</div>
<div class="col col-6" data-label="plus">
<i id="load-more" aria-hidden="true" class="fas fa-plus"></i>
</div>
</li>
<li class="panel"></li>
</ul>
</div>
as you can see the post title(event 1) has been copied outside the li tag
what am I doing wrong?I dont know what to do
echo '<li class="table-row accordion">' .
'<div class="col col-3" data-label="title">' . get_the_title() . '</div>' .
'<div class="col col-6" data-label="date">'. $mydate .'</div>' .
'<div class="col col-6" data-label="location">'. $mylocation .'</div>' .
'<div class="col col-6" data-label="focus">'. $myfocus .'</div>' .
'<div class="col col-6" data-label="plus">
<i id="load-more" aria-hidden="true" class="fas fa-plus"></i>
</div>' .
'</li>';
echo '<li class="panel"></li>';
you have to write get_the_title(), Because you have already written echo at the beginning of li tag. Read the difference here between get_the_title() and the_title()
Related
Woordpres. I create content-article.php like this:
<div class="container">
<div class="row">
<div class="col-lg-8">
<div class="sk-post-item">
<?php
the_content();
?>
<?php
comments_template();
?>
</div>
</div>
<div class="col-lg-4">
lorem
</div>
</div>
</div>
Component comments_template() It looks like this:
<div class="sk-comments-form">
<?php
function reorder_textarea_comment_field( $fields ) {
$comment_field = $fields['comment'];
$cookies_field = $fields['cookies'];
unset( $fields['comment'] );
unset( $fields['cookies'] );
$fields['comment'] = $comment_field;
$fields['cookies'] = $cookies_field;
return $fields;
}
add_filter( 'comment_form_fields', 'reorder_textarea_comment_field' );
?>
<div>
<?php
if(comments_open()){
comment_form(
array(
'title_reply' => '',
"comment_field" => '<div class="form-group col"><div class="pb-2"><label for="comment">Twój komentarz</label><textarea id="comment" class="form-control sk-comments-form-control mb-md-3 m-sm-0" name="comment" rows="4"></textarea>',
"class_form" => "< bg-light rounded-3 shadow p-4 p-lg-5 mt-4 >",
'submit_button'=> '<button class="btn-primary btn-lg text-white sk-comment-form-btn">' ,
'comment_notes_before' => '',
"label_submit" => __( "Wyślij komentarz" ),
"id_submit" => ( "comment-submit" ),
"class_submit" => ( "btn-primary btn-lg text-white sk-comment-form-btn" ),
"submit_button" => '<br><input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />',
'fields' => array(
'author' => '<div class="row form-group col md-6 ml-md-1"><div class="col-md-6" ><label for="comment">Imię i nazwisko</label><input id="author" class="form-control sk-comments-form-control" name="author" ></div>',
'email' => '<div class="col-md-6 form-group col md-6"><label for="comment">Twój email</label><input id="email" class="form-control sk-comments-form-control" name="email" ></div>',
),
),
);
}
?>
In this way .col-lg-8 i .col-lg-4 they are arranged one above the other, not next to each other.
How will I remove .sk-comments-form with comments_template() it's good.
What's wrong there?
I want to display the product in the tab category.
I want it to display 4 items in each tab but it currently displays all product.
Do you have a solution to help me with this?
I do not want the products to be displayed in blocks, it should be displayed in tabular.
Currently displays all products from all categories but does not display each product as a specific category.
This is my code.
<div class="container pt-5">
<div class="text-center mb-4">
<h2 class="section-title px-5"><span class="px-2">Category products</span></h2>
</div>
<ul class="nav nav-tabs" id="myTab" role="tablist">
<?php
$categories = get_terms( array(
'taxonomy' => 'product_cat',
'hide_empty' => 1,
) );
foreach ( $categories as $key => $cat ) { ?>
<li class="nav-item" role="presentation">
<a class="nav-link " id="<?php echo $cat->slug; ?>" data-toggle="tab" href="#<?php echo $cat->slug; ?>" role="tab" aria-controls="<?php echo $cat->slug; ?>" aria-selected="true"><?php echo $cat->name; ?></a>
</li>
<?php } ?>
</ul>
<?php foreach ( $categories as $key => $cat ) {
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'product_cat' => $cat->slug,
'hide_empty' => 1,
'orderby' => 'name',
'order' => 'ASC'
);
?>
<div class="tab-content" id="<?php echo $cat->slug; ?>">
<?php
$products = new WP_Query( $args );
if( $products->have_posts() ) : while ( $products->have_posts() ) : $products->the_post(); ?>
<div class="tab-pane<?php echo $cat->slug; ?>" id="<?php echo $cat->slug; ?>" role="tabpanel" aria-labelledby="home-tab">
<div class="container pt-2">
<div class="row px-xl-5 pb-3">
<div class="col-lg-3 col-md-6 col-sm-12 pb-1">
<div class="card product-item border-0 mb-4">
<div class="card-header product-img position-relative overflow-hidden bg-transparent border p-0">
<?php the_post_thumbnail('', array('class' => 'img-fluid w-100')); ?>
</div>
<div class="card-body border-left border-right text-center p-0 pt-4 pb-3">
<h6 class="text-truncate mb-3"><?php the_title(); ?></h6>
<div class="d-flex justify-content-center">
<h6><?php echo "". " " . get_woocommerce_currency_symbol().$product->get_price(); ?></h6>
</div>
</div>
<div class="card-footer d-flex justify-content-between bg-light border">
<i class="fas fa-eye text-primary mr-1"></i>View Detail
<i class="fas fa-shopping-cart text-primary mr-1"></i> Add to Card
</div>
</div>
</div>
</div>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); endif; ?>
</div>
<?php } ?>
</div>
Your loop was off a little bit, you also used the attribute ID multiple times. This should work for you.
<div class="container pt-5">
<div class="text-center mb-4">
<h2 class="section-title px-5"><span class="px-2">Category products</span></h2>
</div>
<ul class="nav nav-tabs" id="myTab" role="tablist">
<?php
$product_cats = get_terms(
array(
'taxonomy' => 'product_cat',
'hide_empty' => 1,
)
);
foreach ( $product_cats as $key => $product_cat ) {
?>
<li class="nav-item" role="presentation">
<a class="nav-link " id="<?php echo esc_attr( $product_cat->slug ); ?>-tab" data-toggle="tab" href="#<?php echo esc_attr( $product_cat->slug ); ?>" role="tab" aria-controls="<?php echo esc_attr( $product_cat->slug ); ?>" aria-selected="false"><?php echo esc_html( $product_cat->name ); ?></a>
</li>
<?php } ?>
</ul>
<div class="tab-content">
<?php
$c = 0;
foreach ( $product_cats as $key => $product_cat ) {
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'product_cat' => $product_cat->slug,
'hide_empty' => 1,
'orderby' => 'name',
'order' => 'ASC',
);
?>
<div class="tab-pane <?php echo ( 0 === $c ) ? 'show active' : 'fade'; ?>" id="<?php echo esc_attr( $product_cat->slug ); ?>" role="tabpanel" aria-labelledby="<?php echo esc_attr( $product_cat->slug ); ?>-tab"
<div class="container pt-2">
<div class="row px-xl-5 pb-3">
<?php
$products = new WP_Query( $args );
if ( $products->have_posts() ) :
while ( $products->have_posts() ) :
$products->the_post();
?>
<div class="col-lg-3 col-md-6 col-sm-12 pb-1">
<div class="card product-item border-0 mb-4">
<div class="card-header product-img position-relative overflow-hidden bg-transparent border p-0">
<?php the_post_thumbnail( '', array( 'class' => 'img-fluid w-100' ) ); ?>
</div>
<div class="card-body border-left border-right text-center p-0 pt-4 pb-3">
<h6 class="text-truncate mb-3"><?php the_title(); ?></h6>
<div class="d-flex justify-content-center">
<h6><?php echo '' . ' ' . get_woocommerce_currency_symbol() . esc_attr( $product->get_price() ); ?></h6>
</div>
</div>
<div class="card-footer d-flex justify-content-between bg-light border">
<i class="fas fa-eye text-primary mr-1"></i>View Detail
<i class="fas fa-shopping-cart text-primary mr-1"></i> Add to Card
</div>
</div>
</div>
<?php
endwhile;
?>
<?php
wp_reset_postdata();
endif;
?>
</div>
</div>
<?php
$c++;
}
?>
</div>
</div>
https://codepen.io/SitePoint/pen/GQoVey
I am using Tabs and Masonry found here.
I have a total of 4 categories. 4 categories 20 contents
I'm printing this field with wordpress without any problems.
<main role="main" class="grid">
<div class="row">
<div class="col-md">
<div class="starter-template">
<ul class="nav nav-tabs" id="myTab" role="tablist">
<?php
$args = array(
'type' => 'hizmetler',
'parent' => '',
'orderby' => 'id',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'taxonomy' => 'kategoriler' /* custom post type texonomy name */
);
$cats = get_categories($args);
foreach ($cats as $cat) {
$cat_id= $cat->term_id;
$cat_name= $cat->name; ?>
<li class="tag is-dark">
<a class="nav-link" id="contact-tab" data-toggle="tab" href="#<?php echo ''.$cat->term_id.''; ?>" role="tab" aria-controls="<?php echo ''.$cat->term_id.''; ?>" aria-selected="false">
<?php echo ''.$cat->name.''; ?>
</a></li>
<?php wp_reset_postdata(); ?>
<?php } ?>
</ul>
I can get category name, id and link as CPT.
The problem starts here.
I don't know how to loop this field.
<div class="tab-content" id="myTabContent">
<div class="tab-pane masonry-container fade show active" id="home" role="tabpanel" aria-labelledby="1-tab">
<div class="card" style="width: 18rem;">
<img src="..." class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Card with stretched link</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
Go somewhere
</div>
</div>
</div>
loop area;
<div class="card-body">
<h5 class="card-title">Card with stretched link</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
Go somewhere
</div>
but this field must have a category name related to the parent div.
ex: id="home"
example: <?php echo ''.$cat->term_id.''; ?>
this is what should be
<div class="tab-pane masonry-container fade show active" id="EXAMPLE-CAT-ID-1" role="tabpanel" aria-labelledby="EXAMPLE-CAT-ID-1-tab">
<div class="card" style="width: 18rem;">
<img src="..." class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">POST 1 TİTLE (cat-1)</h5>
<p class="card-text">POST 1 CONTENT</p>
<p class="card-text">CAT 1 NAME</p>
link Go somewhere
</div>
<div class="card-body">
<h5 class="card-title">POST 2 TİTLE (cat-1)</h5>
<p class="card-text">POST 2 CONTENT</p>
<p class="card-text">CAT 1 NAME</p>
link Go somewhere
</div>
<div class="card-body">
<h5 class="card-title">POST 3 TİTLE (cat-1)</h5>
<p class="card-text">POST 3 CONTENT</p>
<p class="card-text">CAT 1 NAME</p>
link Go somewhere
</div>
</div>
</div>
<div class="tab-pane masonry-container fade show active" id="EXAMPLE-CAT-ID-2" role="tabpanel" aria-labelledby="EXAMPLE-CAT-ID-2-tab">
<div class="card" style="width: 18rem;">
<img src="..." class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">POST 1 TİTLE (cat-2)</h5>
<p class="card-text">POST 1 CONTENT</p>
<p class="card-text">CAT 2 NAME</p>
link Go somewhere
</div>
<div class="card-body">
<h5 class="card-title">POST 2 TİTLE (cat-2)</h5>
<p class="card-text">POST 2 CONTENT</p>
<p class="card-text">CAT 2 NAME</p>
link Go somewhere
</div>
<div class="card-body">
<h5 class="card-title">POST 3 TİTLE (cat-2)</h5>
<p class="card-text">POST 3 CONTENT</p>
<p class="card-text">CAT 2 NAME</p>
link Go somewhere
</div>
</div>
Place the following code in your functions.php file. You can use it as any shortcode - [tab_posts] or echo do_shortcode('[tab_posts]'); . Just keep in mind to fix html as you need to work your tabs.
function posts_per_tab($atts) {
$output = array();
//Setup our custom post args
$args = array(
'post_type'=>'post',
'posts_per_page' => 3,
'tax_query' => array(
'taxonomy' => 'cat', // Change it with your taxonomy
'field' => 'id',
'terms' => $atts,
)
);
$results = new WP_Query($args);
while($results->have_posts()): $results->the_post();
$output[] .= '<div class="card-body">';
$output[] .= '<h5 class="card-title">'.get_the_title().'</h5>';
$output[] .= '<p class="card-text">'.get_the_content().'</p>';
$output[] .= 'Go somewhere';
$output[] .= '</div>';
endwhile;
wp_reset_query();
return $output;
}
function posts_in_category_tabs() {
$tab_links = array();
$tab_contents = array();
$tab_posts = array();
$cargs = array(
'type' => 'post',
'parent' => '',
'orderby' => 'id',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'taxonomy' => 'category'
);
$cats = get_categories($cargs);
if($cats):
foreach($cats as $cat):
//For each category build your tab
$tab_links[] = sprintf('<li>%s</li>',$cat->name);
//We are executing 2nd loop in posts_per_tab function passing category id as parameter
$tab_posts = posts_per_tab($cat->term_id);
//For each tab push the posts per category
$tab_contents[] = sprintf('<div class="tabs-panel">%s</div>',implode( '', $tab_posts ));
endforeach;
wp_reset_postdata();
endif;
//Our navigation
$tab_links = sprintf(
'<ul class="tabs-nav">%s</ul>',
implode( '', $tab_links ),implode( '', $tab_contents )
);
//Change container if needed
return sprintf('<div class="tabs-wrapper">%s %s</div>',$tab_links,implode( '', $tab_contents ));
}
add_shortcode('tab_posts','posts_in_category_tabs');
The code below displays correctly except for the part that is suppose to show post count in the respective categories. What will be to correct syntax to use? Thanks!
<ul class="cat-menu list-group">
<?php $category_ids = get_all_category_ids();
$args = array(
'orderby' => 'slug',
'parent' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '<li class="list-group-item d-flex align-items-center"><i class="fa fa-chevron-right" aria-hidden="true"></i>' . $category->name . '<span>' . $the_query->found_posts . '</span></li>';
}
?>
Try this-
<ul class="cat-menu list-group">
<?php
$args = array(
'orderby' => 'slug',
'parent' => 0
);
$categories = get_categories( $args );
foreach( $categories as $category ){
echo '<li class="list-group-item d-flex align-items-center"><i class="fa fa-chevron-right" aria-hidden="true"></i>' . $category->name . '<span>' . $category->category_count . '</span></li>';
}
?>
</ul>
Very thanks #Arif-Khan.
and it is a simple update for persian Websites.
<div class="card card-body pb-0">
<div class="single-post">
<p class="font-bold dark-grey-text text-center spacing grey lighten-4 py-2 mb-4">
<strong>دسته بندی</strong>
</p>
<ul class="cat-menu list-group my-4">
<?php
$args = array(
'orderby' => 'slug',
'parent' => 0
);
$categories = get_categories( $args );
foreach( $categories as $category ){
echo '<li class="list-group-item d-flex justify-content-between align-items-center"><a href="' . get_category_link( $category->term_id ) . '">
<i class="fas fa-chevron-left blue-text" aria-hidden="true"></i><p class="blue-grey-text mb-0 pr-1 d-inline-block">
' . $category->name . '</p></a>
<span class="badge badge-pill blue-grey font-small">' . $category->category_count . '</span></li>';
}
?>
</ul>
</div>
</div>
How can i make looping for this slider :
My Slider looks like : http://www.awesomescreenshot.com/image/969867/a3a51b333881ca762739151dae8b16f9
First li consist of top image and second one comes with below one. each single image must be a single post
Html Code :
<div class="row cycle-slideshow" data-cycle-fx=carousel data-cycle-timeout=0 data-cycle-carousel-visible=3 data-cycle-carousel-fluid=true data-cycle-slides="li" data-cycle-next="#next" data-cycle-prev="#prev" data-cycle-pager="#pager">
<ul>
<li class="span2">
<a href="#journeymodel">
<img src="http://lorempixel.com/200/200/">
<h3>Barobudur Temple, java</h3>
<div></div>
</a>
<a href="#journeymodel">
<img src="http://lorempixel.com/200/200/">
<h3>Barobudur Temple, java</h3>
<div></div>
</a>
</li>
<li class="span2">
<a href="#journeymodel">
<img src="http://lorempixel.com/200/200/">
<h3>Uluvatu Temple, Bali</h3>
<div></div>
</a>
<a href="#journeymodel">
<img src="http://lorempixel.com/200/200/">
<h3>Uluvatu Temple, Bali</h3>
<div></div>
</a>
</li>
Php code :
<?php
$loop = new WP_Query(array('post_type'=>'realisation','sort_column' => 'post_date','posts_per_page'=> -1);//'order' => 'DESC');
if ( $loop->have_posts() )
{
while ( $loop->have_posts() )
{
$loop->the_post();
$cat=get_post_meta($post->ID,'category',true);
if ($cat=='privae') // post catagory
{
//li section should comes here by based on list of post added
Can someone guide me how can i update the looping for this section .
You can do something like this:
$arr = array("post_type" => "post_type", 'posts_per_page' => -1, 'orderby' => 'post_title', 'order' => 'ASC');
$posts= get_posts($arr);
echo '<ul>';
foreach($posts as $post) {
$img= get_post_meta($post->ID, 'meta_field', true);
echo "<li><img src='" . $img . "' /></li>";
}
echo '</ul>';