im using the following code to display my posts on a page in wordpress that use a featured image:
$mypages = get_pages( array() );
if ( !empty( $mypages ) ) {
echo '<ul>';
foreach ( $mypages as $mypage ) {
if ( get_the_post_thumbnail( $mypage->ID ) ) {
echo '<div class="featured-container">';
echo '<div class="featured-image">';
echo '<li><a class="feat-hover" href="' . get_permalink( $mypage->ID ) . '">' . get_the_post_thumbnail( $mypage->ID ) . '</a></li>';
echo '</div>';
echo '<div class="featured-text">';
echo '' . get_the_title($mypage->ID ) . '';
echo '</div>';
echo '</div>';
}
}
echo '</ul>';
}
but before printing this information out i want to sort the $mypages array so that they display by date published. ive tried this code:
<?php $args = array(
'sort_order' => 'ASC',
'sort_column' => 'post_date',
);
$mypages = get_pages($args);
?>
but it doesnt seem to work, am i missing something or doing this the wrong way?
thanks in advance.
FULL CODE BEING USED:
$args = array(
'sort_order' => 'ASC',
'sort_column' => 'post_date'
);
$mypages = get_pages( array($args) );
if ( !empty( $mypages ) ) {
echo '<ul>';
foreach ( $mypages as $mypage ) {
if ( get_the_post_thumbnail( $mypage->ID ) ) {
echo '<div class="featured-container">';
echo '<div class="featured-image">';
echo '<li><a class="feat-hover" href="' . get_permalink( $mypage->ID ) . '">' . get_the_post_thumbnail( $mypage->ID ) . '</a></li>';
echo '</div>';
echo '<div class="featured-text">';
echo '' . get_the_title($mypage->ID ) . '';
echo '</div>';
echo '</div>';
}
}
echo '</ul>';
}
The issue is that you're passing $args into another array(). You only need to pass in the $args directly, since it's already an array(). Change the top block of code to:
$args = array(
'sort_order' => 'ASC',
'sort_column' => 'post_date'
);
$mypages = get_pages( $args );
Related
By default, hide_empty => is set to be true. If Redis object cache is disabled, the below code works just fine. But when Redis is enabled, the empty term will not be hidden. It just shows all the terms. I have tried to flush the cache. The issue remains.
Any idea what may cause the issue? thanks
$parentid = 182;
$args = array(
'parent' => $parentid
);
$terms = get_terms( 'product_cat', $args );
if ( $terms ) {
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>';
echo '<a href="' . esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
echo $term->name;
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
I managed to solve it with term count
$parentid = 182;
$args = array(
'parent' => $parentid
);
$terms = get_terms( 'product_cat', $args );
$count = count($terms);
if ( $terms ) {
echo '<ul>';
foreach ( $terms as $term ) {
if ($term->count > 0){
echo '<li>';
echo '<a href="' . esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
echo $term->name;
echo '</a>';
echo '</li>';
}
}
echo '</ul>';
}
How to Show Related Products by Category in WordPress?
Products Category:
Raised Access Flooring
Ancillary Products
Knauf Flooring
Recycled Panels
E.g current page is Raised Access Flooring only display Ancillary Products, Knauf Flooring, Recycled Panels in Related Products.
I added code in single-products.php see below:
$orderby = 'name';
$order = 'random';
$hide_empty = true ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
);
$product_categories = get_terms( 'product_category', $cat_args );
if( !empty($product_categories) ){
echo '<div class="container">';
echo '<div class="row">';
foreach ($product_categories as $key => $category) {
$description = $category->description;
echo '<div class="col-lg-6">';
echo '<div class="card">';
echo '<a href="'.get_term_link($category).'" >';
$image = get_field('product_category', $category );
if($image) {
echo '<img class="card-img-top" src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
} else {
echo '<img class="card-img-top" src="/wp-content/uploads/2018/07/placeholder.png">';
}
echo '<div class="card-content">';
echo '<h3>' . $category->name . '</h3>';
echo '<p>';
echo strlen($description) > 60 ? substr($description, 0, 100) . '...' : $description;
echo '</p>';
echo '<button class="button btn_medium btn_orange btn_squared btn_normal_style" href="'.get_term_link($category).'" >Discover more</button>';
echo '</div>';
echo '</a>';
echo '</div>';
echo '</div>';
}
echo '</div>';
echo '</div>';
}
else {
// no posts found
echo wpautop( 'Sorry, no products were found' );
}
First, get the terms of the category associated to the current product:
$terms = wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'ids') );
The id of current product is obtained with the function: get_the_id()
Second, get the other products that sharing the same terms (excluding from the result the current product):
$products = get_posts(array(
'post_type' => 'product',
'post_status' => 'publish',
'numberposts' => -1,
'post__not_in' => array(get_the_id()),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $terms,
'include_children' => false
)
)
));
The complete code would be:
$terms = wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'ids') );
$products = get_posts(array(
'post_type' => 'product',
'post_status' => 'publish',
'numberposts' => -1,
'post__not_in' => array(get_the_id()),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $terms,
'include_children' => false
)
)
));
Look that I'm getting an array with only the ids of the terms:
$terms = wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'ids') );
So, you should edit your snippet of code:
foreach ($terms as $key => $term) {
as follows:
foreach ($terms as $key => $term_id) {
$term = get_term($term_id);
By the way in your piece of code you are not using the $products variable with the array of related products, that was your initial question.
#MikeD This only display current product not other 3 products display, have l done it wrong?
`<?php
$orderby = 'name';
$order = 'random';
$hide_empty = true ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
);
$terms = wp_get_post_terms( get_the_id(), 'product_category', array('fields' => 'ids') );
echo '<div class="container">';
echo '<div class="row">';
foreach ($terms as $key => $term) {
$term = get_term($term_id);
echo '<a href="' .get_term_link($term). '" ></a>';
echo '<div class="col-lg-6">';
echo '<div class="card">';
echo '<a href="'.get_term_link($term).'" >';
$image = get_field('product_category', $term );
if($image) {
echo '<img class="card-img-top" src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
} else {
echo '<img class="card-img-top" src="/wp-content/uploads/2018/07/placeholder.png">';
}
echo '<div class="card-content">';
echo '<h3>' . $term->name . '</h3>';
//echo $category->name;
//echo '<p>' . $category->description . '</p>';
echo '<p>';
echo strlen($description) > 60 ? substr($description, 0, 100) . '...' : $description;
echo '</p>';
//echo '<p>' . mb_strimwidth($category->description, 0, 30, "...") . '</p>';
echo '<button class="button btn_medium btn_orange btn_squared btn_normal_style" href="'.get_term_link($term).'" >Discover more</button>';
echo '</div>';
echo '</a>';
echo '</div>';
echo '</div>';
}
echo '</div>';
echo '</div>';
?>`
In a Wordpress Latest Posts widget I want to add pagination
public function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', $instance['title'] );
$count = $instance['count'];
$category = $instance['category'];
echo $before_widget;
$output = '';
if ( $title )
echo $before_title . $title . $after_title;
global $post;
if ( isset( $category ) && $category != '' ) {
$args = array(
'category_name' => $category,
'posts_per_page' => $count,
);
} else {
$args = array(
'posts_per_page' => $count,
);
}
$posts = get_posts( $args );
if(count($posts)>0){
$output .='<div class="sp-latest-posts-widget latest-posts">';
foreach ($posts as $post): setup_postdata($post);
$output .='<div class="media">';
if(has_post_thumbnail()):
$output .='<div class="pull-left">';
$output .=''.get_the_post_thumbnail($post->ID, 'xs-thumb', array('class' => 'img-responsive')).'';
$output .='</div>';
endif;
$output .='<div class="media-body">';
$output .= '<h3 class="entry-title">'. get_the_title() .'</h3>';
$output .= '<div class="entry-meta small"><span class="st-lp-time">'. get_the_time() . '</span> <span clss="st-lp-date">' . get_the_date('d M Y') . '</span></div>';
$output .='</div>';
$output .='</div>';
endforeach;
wp_reset_query();
$output .='</div>';
}
echo $output;
echo $after_widget;
}
I tried with pagination_nav(); after adding code in functions.php and I used WP-PageNavi, but without success.
How to add numbered pagination in this case?
Thanks in advance!
Use below code for numbered pagination,
<?php the_posts_pagination( array(
'mid_size' => 1,
'prev_text' => __( 'Prev', 'textdomain' ),
'next_text' => __( 'Next', 'textdomain' ),
'screen_reader_text' => ' ',
) );
} ?>
I'd like to display my categories 2 at the time - 2 in each row and have as many rows as needed.
I've got so far with this code (which displays all the categories):
<?php
$i = 1;
$args = array(
'orderby' => 'name',
'parent' => 0,
'hide_empty' => 0
);
$categories = get_categories( $args );
echo count( $categories );
echo "<div class='a'>\n";
foreach ( $categories as $category ) {
echo "<div class='b'>\n";
echo "<div class='c'><a href='" . get_category_link( $category->term_id ) . "'>" . $category->name . "</a></div>\n";
echo "</div>\n";
}
echo "</div>\n";
?>
I know it can be done using list, but this is something I want to avoid.
So this is it (took a while) and it works just fine ;-)
$args = array(
'orderby' => 'name',
'parent' => 0,
'hide_empty' => 1
);
$categories = get_categories( $args );
$list_cats = array();
foreach ( $categories as $category ) {
array_push($list_cats, $category->name);
}
$total_cats=count($categories);
echo "<div class='a'>";
for ($a=0; $a<$total_cats;) {
for ($j=0; $j<2; $j++) {
echo " <div class='b'><div class='c'>" . $list_cats[$a] . "</div></div>";
$a++;
}
echo "</div>";
if($a<$total_cats){
echo "<div class='a'>";
}
}
Hello with the code bellow i am getting some content.
How is possible with php simple html dom to get all the img src?
my code:
foreach($html->find('div[class=post-single-content box mark-links]') as $table)
{
$arr44[]= $table->innertext ;
}
div with class post-single-content box mark-links contains text and images
Try this :
<?php if ( $post->post_type == 'data-design' && $post->post_status == 'publish' ) {
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
) );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
$thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
echo '<li class="' . $class . ' data-design-thumbnail">' . $thumbimg . '</li>';
}
}
}
?>