Wordpress short code exclude by author - wordpress

I have a shortcode in WordPress which shows the last post by author, the problem is that I want to exclude a concrete author(ID=13). The shortcode is the next:
function latest_posts_c( $array ) {
global $post;
$defaults = array(
'show' => 3,
'excerpt' => 'false',
'post_type' => 'post',
);
extract( shortcode_atts( $defaults, $array ) );
$args = array(
'posts_per_page' => $show,
'post_type' => $post_type,
);
// Gets posts form database
$query = new WP_Query( $args );
// Displays posts if available
if( $query ) {
$i = 0;
while ( $query->have_posts()) : $query->the_post();
if ($i == 0)
$html = '<div class="column dt-sc-one-third first">';
else
$html .= '<div class="column dt-sc-one-third">';
$html .= '<article id="post-'.get_the_ID().'" class="'.implode(' ', get_post_class('blog-entry')).'">';
$html .= '<div class="blog-entry-inner">';
$html .= '<div class="entry-meta">';
$html .= ' ';
$html .= ' <div class="date">';
$html .= ' <p>'.get_the_date('M').' '.get_the_date('d').' <span>'. get_the_date('Y') .'</span> </p>';
$html .= ' </div>';
$html .= '</div><!-- .entry-meta -->';
if( has_post_thumbnail() ):
$html .= '<div class="entry-thumb">';
$html .= ' '.get_the_post_thumbnail(get_the_ID(), 'medium').'';
$html .= '</div><!-- .entry-thumb -->';
endif;
$html .= '<div class="entry-details">';
if(is_sticky()):
$html .= ' <div class="featured-post"> <span class="fa fa-trophy"> </span> Destacado</div>';
endif;
$html .= ' <div class="entry-title">';
$html .= ' <h4>'.get_the_title().'</h4>';
$html .= ' </div>';
$html .= ' <div class="entry-metadata">';
$html .= ' <p class="author">';
$html .= '<span class="fa fa-user"> </span>';
$html .= ' '.get_the_author().'</p>';
$categories = 0;/*get_the_category();*/
$separator = ', ';
$output = '';
if($categories){
$j = 0;
foreach($categories as $category) {
$output .= ''.$category->cat_name.'';
$j++;
if ($j < count($categories)) $output .= $separator;
}
$html .= ' <p class="categories"><span class="fa fa-folder-open"> </span>'.$output.'</p>';
}
$html .= ' </div><!-- .entry-metadata-->';
$html .= ' <div class="entry-body">';
$html .= ' '.dttheme_excerpt(50);
$html .= ' <p><a href="'.get_permalink().'" title="'.get_the_title().'" class="dt-sc-button small read-more">';
$html .= ' Leer más <span class="fa fa-angle-double-right"> </span></a></p>';
$html .= ' </div>';
$html .= '</div><!-- .entry-details -->';
$html .= '</div><!-- .blog-entry-inner-->';
$html .= '</article>';
$html .= '</div>';
$i++;
endwhile;
}
$html .= '<div class="dt-sc-clear"></div>';
// Resets Post Data
wp_reset_postdata();
// Returns the results
return $html;
}
add_shortcode('latestposts_c', 'latest_posts_c');
I added an If to block the code if there is a post from author ID=13, but the problem is that the shortcode doesn't show anything. I'm also using "authors_in" to allow certain authors ID inside the array $args and $default but nothing....Any idea?

Try this:
$args = array(
'posts_per_page' => $show,
'post_type' => $post_type,
'author' => '-13'
);
The author parameter can be set to include/exclude specific authors.
Reference http://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters

Related

create a pagination with shortcodes

on my Wordpress site, I created CPT shortcodes, I want there to be 6 posts per page. But when I have more posts, I want the most anician to be visible and to set up a pagination.
how can I add that?
Here is my current code:
function diwp_create_shortcode_newprojects_post_type(){
$args = array(
'post_type' => 'projets',
'posts_per_page' => '6',
'category_name' => 'neufs',
'publish_status' => 'published',
);
$query = new WP_Query($args);
if($query->have_posts()) :
while($query->have_posts()) :
$query->the_post() ;
$content = wpautop( get_the_content() );
/* $result .= '<div class="post-item">';
$result .= '<div class="post-poster">' . get_the_post_thumbnail() . '</div>';
$result .= '<div class="post-name">' . get_the_title() . '</div>';
$result .= '<div class="post-desc">' . get_the_content() . '</div>';
$result .= '</div>'; */
$result .= '<div class="block-actu-list"> <div class="img-actu">' . get_the_post_thumbnail( $post->ID, 'large') . '</div> <div class="legend">' . get_the_title() . '</div></div>';
endwhile;
wp_reset_postdata();
endif;
return $result;
}
add_shortcode( 'newprojects-listed', 'diwp_create_shortcode_newprojects_post_type' );
----------------- Edit -----------------
With #Saqib Amin 's answer I'm trying this, is this correct ?
// CPT shortcode HomePage
function diwp_create_shortcode_project_post_type(){
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'projets',
'posts_per_page' => '10',
'paged' => $paged,
'category_name' => '',
'publish_status' => 'published',
);
$query = new WP_Query($args);
if($query->have_posts()) :
while($query->have_posts()) :
$query->the_post() ;
$content = wpautop( get_the_content() );
/* $result .= '<div class="post-item">';
$result .= '<div class="post-poster">' . get_the_post_thumbnail() . '</div>';
$result .= '<div class="post-name">' . get_the_title() . '</div>';
$result .= '<div class="post-desc">' . get_the_content() . '</div>';
$result .= '</div>'; */
$result .= '<div class="block-actu-list"> <div class="img-actu">' . get_the_post_thumbnail( $post->ID, 'large') . '</div> <div class="legend">' . get_the_title() . '</div></div>';
endwhile;
wp_reset_postdata();
endif;
return $result;
}
add_shortcode( 'project-listed', 'diwp_create_shortcode_project_post_type' );

How to add post tags as a class on custom post type loop?

I have a custom loop for a custom post type that has been working great, but I would like to add a class to the div wrappers (.room_entry) based on tagged tags for each post. This is what I got so far:
I'm trying to add ". get_the_tags() ." directly to the class, but it doesn't seem to work. The class outputs 'Array'
//Display Rooms Feed
function rooms_function() {
global $post;
$html = "";
$my_query = new WP_Query( array(
'post_type' => 'room',
'posts_per_page' => -1 ,
'orderby' => 'menu_order',
'order' => 'ASC',
));
if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();
$html .= "<div class='room_entry ". get_the_tags() ." '>";
$html .= "<div class='room_image'>" .get_the_post_thumbnail() . "</div>";
$html .= "<h4 class='room_title'>" . get_the_title() . " </h4>";
$html .= "<p class='room_description'>" .get_field('description') . "</p>";
$html .= "<a class='room_view' href=' ".get_permalink() ." '>View Details</a>";
$html .= "<a class='room_book et_pb_button' target='_blank' href=' ".get_field('book_now') ." '>Book Now</a>";
$html .= "</div>";
endwhile;
wp_reset_postdata();
endif;
return $html;
}
if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();
//Add tags as class for single or multiple tags.
$post_tags = get_the_tags();
$class = array();
for($i=0; $i < count($post_tags); $i++){
$class[] = $post_tags[$i]->name;
}
$classfinal = implode(' ',$class);
$html .= "<div class='room_entry ". $classfinal ." '>";
$html .= "<div class='room_image'>" .get_the_post_thumbnail() . "</div>";
$html .= "<h4 class='room_title'>" . get_the_title() . " </h4>";
$html .= "<p class='room_description'>" .get_field('description') . "</p>";
$html .= "<a class='room_view' href=' ".get_permalink() ." '>View Details</a>";
$html .= "<a class='room_book et_pb_button' target='_blank' href=' ".get_field('book_now') ." '>Book Now</a>";
$html .= "</div>";
endwhile;
wp_reset_postdata();
endif;

How to call woocommerce product title in html?

I'm trying to overylay woocommerce product title on its featured image, as i replaced featured image with some plane colored background and on it i need to call woocommerce product title. Please share how can i achieve this ?
if ( has_post_thumbnail() ) {
$html = '<div data-thumb="' . get_the_post_thumbnail_url( $post->ID, 'shop_thumbnail' ) . '" class="woocommerce-product-gallery__image"><a href="' . esc_url( $full_size_image[0] ) . '">';
$html .= get_the_post_thumbnail( $post->ID, 'shop_single', $attributes );
$html .= '</a></div>';
} else {
$html = '<div class="woocommerce-product-gallery__image--placeholder">';
$html .= sprintf( '<img src="%s" alt="%s" class="wp-post-image" />', esc_url( wc_placeholder_img_src() ), esc_html__( 'Awaiting product image', 'woocommerce' ) );
$html .= '<div class="centered">';
$html .= how to call here!!!
$html .= '</div>';
$html .= '</div>';
}
You need to get the product title from the product like this:
$product = wc_get_product( $post->ID );
$title = get_the_title($product->ID);
So your code becomes :
if ( has_post_thumbnail() ) {
$html = '<div data-thumb="' . get_the_post_thumbnail_url( $post->ID, 'shop_thumbnail' ) . '" class="woocommerce-product-gallery__image"><a href="' . esc_url( $full_size_image[0] ) . '">';
$html .= get_the_post_thumbnail( $post->ID, 'shop_single', $attributes );
$html .= '</a></div>';
} else {
$product = wc_get_product( $post->ID );
$title = get_the_title($product->ID);
$html = '<div class="woocommerce-product-gallery__image--placeholder">';
$html .= sprintf( '<img src="%s" alt="%s" class="wp-post-image" />', esc_url( wc_placeholder_img_src() ), esc_html__( 'Awaiting product image', 'woocommerce' ) );
$html .= '<div class="centered">';
$html .= $title ;
$html .= '</div>';
$html .= '</div>';
}
An edit :
Since you are within the loop, you do not need to get the product using the function I used. You can just use the global $product variable if the post is a product.

Add pagination to recent posts shortcode

I've created a shortcode that displays my recent posts but I want to have the option to add pagination. I'm trying to use the pagination attribute. Please see my code below. Any help would be greatly appreciated:
add_shortcode( 'list_recent_posts', 'list_recent_posts' );
function list_recent_posts( $atts ) {
ob_start();
// define attributes and their defaults
extract( shortcode_atts( array (
'posts' => 4,
'category' => '',
'ptype' => '',
'class' => '',
), $atts ) );
$class = $atts['class'];
// define query parameters based on attributes
$options = array(
'posts_per_page' => $posts,
'post_type' => $ptype,
'category_name' => $category
);
$query = new WP_Query( $options );
// run the loop based on the query
if ( $query->have_posts() ) { ?>
<ul class="media recent-posts <?php echo $class; ?>">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<li class="media-listitem">
<?php
if(has_post_thumbnail()):
?><a class="pull-left" href="<?php the_permalink(); ?>">
<div class="thumbnail">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('post_thumbnail');
}
?>
</div>
</a>
<?php else: ?>
<?php endif; ?>
<?php
if(has_post_thumbnail()): ?>
<div class="media-content marginlft-90">
<?php else: ?>
<div class="media-content">
<?php endif; ?>
<div class="caption">
<h4 class="media-heading"><?php the_title(); ?></h4>
<p><?php the_excerpt(); ?></p>
</div>
</div>
</li>
<?php endwhile;
wp_reset_postdata(); ?>
</ul>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
I found the answer in another post:
if ( ! function_exists('vpsa_posts_shortcode') ) {
function vpsa_posts_shortcode( $atts ){
$atts = shortcode_atts( array(
'per_page' => 2,
'order' => 'DESC',
'orderby' => 'date'
), $atts );
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => $atts["per_page"],
'order' => $atts["order"],
'orderby' => $atts["orderby"],
'paged' => $paged
);
$query = new WP_Query($args);
if($query->have_posts()) : $output;
while ($query->have_posts()) : $query->the_post();
$output .= '<article id="post-' . get_the_ID() . '" class="' . implode(' ', get_post_class()) . '">';
$output .= '<h4 class="post-title"><span>' . the_title('','',false) . '</span></h4>';
$output .= '<div class="row">';
$output .= '<div class="col-md-3">';
$output .= '<a href="' . get_permalink() . '" title="' . the_title('','',false) . '">';
if ( has_post_thumbnail() ) {
$output .= get_the_post_thumbnail( get_the_id(), 'featured', array('class' => 'img-responsive aligncenter'));
} else {
$output .= '<img class="img-responsive aligncenter" src="' . get_template_directory_uri() . '/images/not-available.png" alt="Not Available" height="150" width="200" />';
}
$output .= '</a>';
$output .= '</div>';
$output .= '<div class="col-md-9">';
$output .= get_the_excerpt();
$output .= '<span class="post-permalink">Read More</span>';
$output .= '</div>';
$output .= '</div>';
$output .= '<div class="post-info">';
$output .= '<ul>';
$output .= '<li>Posted: ' . get_the_time("F j, Y") . '</li>';
$output .= '<li>By: ' . get_the_author() . '</li>';
$output .= '<li>Categories: ' . get_the_category_list(", ") . '</li>';
$output .= '</ul>';
$output .= '</div>';
$output .= '</article>';
endwhile;global $wp_query;
$args_pagi = array(
'base' => add_query_arg( 'paged', '%#%' ),
'total' => $query->max_num_pages,
'current' => $paged
);
$output .= '<div class="post-nav">';
$output .= paginate_links( $args_pagi);
// $output .= '<div class="next-page">' . get_next_posts_link( "Older Entries »", 3 ) . '</div>';
$output .= '</div>';
else:
$output .= '<p>Sorry, there are no posts to display</p>';
endif;
wp_reset_postdata();
return $output;
}
}
add_shortcode('vpsa_posts', 'vpsa_posts_shortcode');

How to modify a theme-bundled widget in child theme?

I want to remove the nofollow code from the latest posts displayed in the sidebar. I found that the code which adds rel=nofollow tag to latest post is located here
theme folder/example theme/lib/activity/plugin.php.
Here is the exact code:
private function get_latest_posts( $post_count ) {
// Get the latest posts
$latest_posts = get_posts(
array(
'numberposts' => $post_count,
'order' => 'desc',
'orderby' => 'date'
)
);
// Create the markup for the listing
$html = '<div class="tab-pane" id="recent">';
$html .= '<ul class="latest-posts">';
if( count( $latest_posts ) > 0 ) {
foreach( $latest_posts as $post ) {
$html .= '<li class="clearfix">';
// Add the small featured image
if( has_post_thumbnail( $post->ID ) ) {
$html .= '<a class="latest-post-tn fademe" href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
$html .= get_the_post_thumbnail( $post->ID, array( 50, 50 ) );
$html .= '</a>';
} // end if
$html .='<div class="latest-meta">';
// Add the title
$html .= '<a href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
$html .= get_the_title( $post->ID );
$html .= '</a>';
// Add date posted
// If there's no title, then we need to turn the date into the link
if( strlen( get_the_title( $post->ID ) ) == 0 ) {
$html .= '<a href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
} // end if
$html .= '<span class="latest-date">';
$html .= get_the_time( get_option( 'date_format' ), $post->ID );
$html .= '</span>';
// Close the anchor
if(strlen( get_the_title( $post->ID ) ) == 0 ) {
$html .= '</a>';
} // end if
$html .='</div>';
$html .= '</li>';
} // end foreach
} else {
$html .= '<li>';
$html .= '<p class="no-posts">' . __( "You have no recent posts.", 'standard' ) . '</p>';
$html .= '</li>';
} // end if/else
$html .= '</ul>';
$html .= '</div>';
return $html;
} // end get_latest_posts
Now please tell me how to remove the nofollow tag from this code using the child theme?
Since you have control of the child theme, you can wrap the call to display the widget zone for that widget with something that grabs the output, performs a regex search/replace on it, and outputs the result. I wrote a blog post about that recently:
Filtering the output of WordPress widgets
The basics are that you have a function that replaces dynamic_sidebar() with your own function, like this:
function theme_dynamic_sidebar($index = 1) {
// capture output from the widgets
ob_start();
$result = dynamic_sidebar($index);
$out = ob_get_clean();
// do regex search/replace on $out
echo $out;
return $result;
}
Seems that you are out of luck.
That's a private function and no filter hooks are offered by the theme author.
You may try to override the include('path/to/plugin.php'); and include your own modified version.

Resources