How to make a shortcode for my WP_Query Loop? - php

I'm a noob Wordpress Developer and I just created my first Custom Template Page using Advanced Custom Fields and managed to loop.
<?php
$args = array(
'post_type' => 'art',
'orderby' => 'title',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', 'art' ); ?>
<?php endwhile; endif; ?>
But I will like to use it not only inside a template page, but anywhere I want. Therefore I need to create a shortcode.
Example:
function foobar_func( $atts ){
return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );
My question would be: How can i put the loop inside my shortcode?

add_shortcode( 'foobar', 'foobar_func' );
function foobar_func( $atts ) {
global $post;
$output = '';
$args = array(
'post_type' => 'art',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 10,
);
$fe_query= new WP_Query( $args );
if ( $fe_query->have_posts() ) {
$output .= '<ul class="fe-query-results-shortcode-output">';
while ( $fe_query->have_posts() ) {
$fe_query->the_post();
$title = get_the_title();
$link = get_the_permalink();
$output .= "<li>{$title}</li>";
}
$output .= '</ul>';
} else {
$output .= '<div class="fe-query-results-shortcode-output-none">No results were found</div>';
}
wp_reset_postdata();
return $output;
}

<?php
function loop_art() {
ob_start();
get_template_part('loop_art');
return ob_get_clean();
}
add_shortcode( 'loop_art', 'loop_art' );
?>
<?php
$args = array(
'post_type' => 'art',
'orderby' => 'title',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post()
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-meta">
<p>Price: $<?php the_field('price'); ?></p>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<div class="entry-content">
<p><img src="<?php the_field('image'); ?>" alt="Example image of <?php the_title(); ?>"></p>
</div><!-- .entry-content -->
</article>
<?php endwhile; endif; ?>

Related

Add sort order options to custom Wordpress loop

Here is my custom loop, I need to add a sorting option and after hours of digging I cannot find a solution.
<ul class="acapellas row">
<?php
$loop = new WP_Query( array(
'post_type' => 'acapella',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'date'
));
?>
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class="post-<?php the_ID(); ?> col-md-6">
<div class="wrap">
<h2><?php the_title() ?></h2>
<?php if(pmpro_hasMembershipLevel($level_id)) { ?>
<?php the_content(); ?>
<?php } else { ?>
<div class="pro-player">
<div class="upgrade">
<a href="<?php bloginfo('url'); ?>/pro" >Upgrade to unlock</a>
</div>
</div>
<?php } ?>
<a class="download left" href="<?php the_permalink(); ?>">Download</a>
<span class="list-date right">First added: <?php the_time('F jS, Y') ?></span><br>
<?php
global $post;
$post_type = get_post_type(get_the_ID());
$post_type_taxonomies = get_object_taxonomies($post_type);
if (!empty($post_type_taxonomies)) {
echo '<ul class="details">';
foreach ($post_type_taxonomies as $taxonomy) {
$terms = get_the_term_list(get_the_ID(), $taxonomy, '', '</li><li>', '');
if ($terms) {
echo '<li>' . $terms . '</li>';
}
}
echo '</ul>';
}
?>
</div>
</li>
<?php endwhile; endif; ?>
</ul>
You must use both orderby and order, like so:
$loop = new WP_Query( array(
'post_type' => 'acapella',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'date',
'order' => 'asc'
));

How to list subcategories of a category in categories page template (wordpress)

I have a categories page template, listing all categories with featured images. But I want to show only subcategories of a parent category. I don't know where to modify the template. Here is the code.
get_header(); ?>
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<h1 class="border-radius-5"><?php the_title(); ?></h1>
<div id="page" class="post-content">
<?php the_content(); ?>
<?php
$terms = get_terms("category", $args);
$count = count($terms);
$categories = array();
if ($count > 0) {
echo '<ul class="listing-cat">';
foreach ($terms as $term) {
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'show_count' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$video_from_categorie = new WP_Query( $args );
if( $video_from_categorie->have_posts() ){
$video_from_categorie->the_post();
}else{}
$term->slug;
$term->name;
?>
<li class="border-radius-5 box-shadow">
<?php echo get_post_image();?>
<span><?php echo $term->name; ?></span>
<span class="nb_cat border-radius-5"><?php echo $term->count; ?> <?php if ( $term->count > 1 ){
_e( 'videos', get_theme_name() );
}else{
_e( 'video', get_theme_name() );
} ?></span>
</li>
<?php
}
echo '</ul>';
echo '<div class="clear"></div>';
}
?>
</div><!-- #page -->
<?php endwhile; ?>
<?php endif; ?>
Pass the ID of the desired parent term/category to the child_of parameter of get_terms():
$terms = get_terms( 'category', array( 'child_of' => TERM_ID_GOES_HERE ) );

Wordpress - shortcode to display custom post types in custom taxonomies in carousel

I`ve found sth like this:
Is it possible to create a shortcode that will query a post based on taxonomies
function posts_shortcode_handler($atts, $content) {
extract(shortcode_atts(array(
'posts_per_page' => '5',
'post_type' => 'gallery'
), $atts));
global $post;
$temp = $post;
$posts = new WP_Query($atts);
$retVal = '';
if ($posts->have_posts()) {
while ($posts->have_posts()) {
$posts->the_post();
// these arguments will be available from inside $content
$parameters = array(
'PERMALINK' => get_permalink(),
'TITLE' => get_the_title(),
'CONTENT' => get_the_content(),
'CATEGORIES' => get_the_category_list(', '),
'THUMBNAIL' => get_the_post_thumbnail()
);
$finds = $replaces = array();
foreach ($parameters as $find => $replace) {
$finds[] = '{' . $find . '}';
$replaces[] = $replace;
}
$retVal .= str_replace($finds, $replaces, $content);
}
}
wp_reset_query();
$post = $temp;
return $retVal;
}
add_shortcode('galerie', 'posts_shortcode_handler');
My shortcode looks like this:
[galerie post_type="gallery" posts_per_page="5" taxonomy_name="movies"]
<h5>{TITLE}</h5>
<div>{THUMBNAIL}
{CONTENT}</div>
[/galerie]
My problem is about the taxonomy_name="movies" that`s not working for me.
In my custom taxonomy name 'Kategorie' I have two sub categories 'movies' and 'photos'.
Shortcode ignores choosen 'taxonomy_name' and display all custom posts from post_type="gallery". I would like to choose sub category in my custom_taxonomy to display custom post type from shortcode.
Please Help me, I`m stuck :(
Use this Code your wp page editor. this is function input:
add_shortcode( 'list-slider', 'slidermy' );
function slidermy( $atts ) {
ob_start();
$query = new WP_Query( array(
'post_type' => 'slider',
'color' => 'blue',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
) );
if ( $query->have_posts() ) { ?>
<div class="home-slider">
<div class="container">
<div class="cycle-slideshow home-slideshow" data-cycle-slides="> div" data-cycle-pager=".home-pager" data-cycle-timeout="5000" data-cycle-prev="#HomePrev" data-cycle-next="#HomeNext">
<?php
while ( $query->have_posts() ) : $query->the_post();
$imgurl = get_the_post_thumbnail_url( get_the_ID(), 'full' );
?>
<div class="slide" style=" background-image:url(<?php echo $imgurl;?>)">
<div class="caption">
<div class="con">
<h1><?php the_title(); ?></h1>
</div>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
</div>
</div>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
add_shortcode( 'list-slider', 'slidermy' );
function slidermy( $atts ) {
ob_start();
$query = new WP_Query( array(
'post_type' => 'slider',
'color' => 'blue',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
) );
if ( $query->have_posts() ) { ?>
<div class="home-slider">
<div class="container">
<div class="cycle-slideshow home-slideshow" data-cycle-slides="> div" data-cycle-pager=".home-pager" data-cycle-timeout="5000" data-cycle-prev="#HomePrev" data-cycle-next="#HomeNext">
<?php
while ( $query->have_posts() ) : $query->the_post();
$imgurl = get_the_post_thumbnail_url( get_the_ID(), 'full' );
?>
<div class="slide" style=" background-image:url(<?php echo $imgurl;?>)">
<div class="caption">
<div class="con">
<h1><?php the_title(); ?></h1>
</div>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
</div>
</div>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
You have to replace "taxonomy_name" with the name of your taxonomy.
The first line of your shortcode should look like this:
[galerie post_type="gallery" posts_per_page="5" Kategorie="movies"]
That way, this is the $atts array passed to WP_Query:
$args = array(
'post_type' => 'post',
'posts_per_page' => '5',
'kategorie' => 'movies'
);
More details here:
http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

How to display custom post type by custom taxonomy?

My homepage is displaing my listings( my custom post type) by the order i enter them. I would like my listings that have the custom taxonomy "tag" (Special offer) to be displayed on my first page from my homepage and after that the rest of the listings exactly how they where before. I am new in to wordpress and hope i asked my question right
This is my homepage code
<div id="content">
<?php include (TEMPLATEPATH . '/lib/slider.php'); ?>
<?php
$args = array(
'post_type' => 'listings',
'paged' => $paged,
'showposts' => 8 ,
'oferta' =>"oferta"
);
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="post propbox <?php if (++$counter % 2 == 0) { echo "lastbox"; }?> clearfix" id="post-<?php the_ID(); ?>">
<div class="archimg">
<?php if( has_term( 'featured', 'type', $post->ID ) ) { ?>
<span class="featspan">Featured</span>
<?php } else if ( has_term( 'sold', 'type', $post->ID ) ){ ?>
<span class="soldspan">Sold</span>
<?php } else if ( has_term( 'reduced', 'type', $post->ID ) ){ ?>
<span class="redspan">Reduced</span>
<?php } ?>
<?php
if ( has_post_thumbnail() ) { ?>
<img class="propimg" src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php get_image_url(); ?>&h=180&w=310&zc=1" alt=""/>
<?php } else { ?>
<img class="propimg" src="<?php bloginfo('template_directory'); ?>/images/dummy.jpg" alt="" />
<?php } ?>
</div>
<div class="cover">
<div class="title">
<h2><?php the_title(); ?></h2>
</div>
<div class="propmeta">
<div class="proplist"><span>Price</span> <span class="propval"> <?php $price=get_post_meta($post->ID, 'wtf_price', true); echo $price; ?></span></div>
<div class="proplist"><span>Location</span> <span class="propval"> <?php echo get_the_term_list( $post->ID, 'location', '', ' ', '' ); ?></span></div>
<div class="proplist"><span>Property type</span> <span class="propval"><?php echo get_the_term_list( $post->ID, 'property', '', ' ', '' ); ?></span></div>
<div class="proplist"><span>Area</span> <span class="propval"> <?php echo get_the_term_list( $post->ID, 'area', '', ' ', '' ); ?></span></div>
</div>
<div class="entry">
<?php wpe_excerpt('wpe_excerptlength_archive', ''); ?>
<a class="morer" href="<?php the_permalink() ?>">Check this</a>
<div class="clear"></div>
</div>
</div>
</div>
<?php endwhile; ?>
<div class="clear"></div>
<?php getpagenavi(); ?>
<?php $wp_query = null; $wp_query = $temp;?>
</div>
This is my main content content sorry how can i rearenge this so it fits my needs
you can use tax_query like by here
$args = get_posts( array(
'post_type' => 'my_post_type',
'tax_query' => array(
array(
'taxonomy' => 'my_taxonomy',
'field' => 'slug',
'terms' => 'webdesign'
)
)
) );
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);
You have to pass the arguments array to you $wp_query->query($args); in which you define the tag name or category name , taxonomy of tag or taxonomy of category
$args = array(
'post_type' => 'your custom post type name',
'paged' => $paged,
'showposts' => 8 ,
'your custom goes here taxonomy' =>"tag name or category name"
);
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);
Generally taxonomy name is what you defined in register_taxonomy('here_goes_taxonomy',array('post_type'), array(...))
All the answers above are great, but you don't necessarily need to pass the tax_query array. Once you have created your custom taxonomy the right way, you can do the following.
<?php
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
$loop = new WP_Query([
'post_type' => 'your_custom_post_type',
'post_status' => 'publish', // fetch only published posts
'posts_per_page' => get_option('posts_per_page') ?: 10, // number of posts to fetch
'paged' => $paged
'your_custom_taxonomy' => 'your_custom_taxonomy_slug_or_name'
]);
// WordPress pagination doesn't work on custom query
// Let's make it work
$temp_query = $wp_query; // store $wp_query in a temporary variable
$wp_query = null; // set it to null
$wp_query = $loop; // store your custom query to $wp_query
if ( $loop->have_posts() ) { // Let's check if we have some posts
while( $loop->have_posts() ){
$loop->the_post();
// add your markup here
}
}
wp_reset_postdata();
$wp_query = null;
$wp_query = $temp_query; // store back the original $wp_query
<?php $cat_terms = get_terms(
array('mobile_category'),
array(
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
'number' => 2 ) );
if( $cat_terms ) : ?>
<?php foreach( $cat_terms as $term ) :
$args = array(
'post_type'=> 'mobile',
'posts_per_page'=> 2,
'post_status'=> 'publish',
'tax_query'=> array(
array(
'taxonomy' => 'mobile_category',
'field' => 'slug',
'terms' => $term->slug,), ),
'ignore_sticky_posts' => true
);
$_posts = new WP_Query( $args );
if( $_posts->have_posts() ) :
while( $_posts->have_posts() ) : $_posts->the_post(); ?>
<span class="color2"><?php echo $term->name ; ?></span>
<?php endwhile;
endif;
wp_reset_postdata();
endforeach; ?>
<?php endif; ?>

Get taxonomy from one loop and use it in another

This code in the first loop:
<?php
$terms_as_text = get_the_term_list( $post->ID, 'produktkategori', '', ', ', '' ) ;
echo strip_tags($terms_as_text);
?>
Results in "Spanskt". I want "Spanskt" to be entered in the second loop where it says "Spanskt". Is that possible?
Thanks in advance.
// First loop
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php
$terms_as_text = get_the_term_list( $post->ID, 'produktkategori', '', ', ', '' ) ;
echo strip_tags($terms_as_text);
?> // The value of this...</h1>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
Some content
</article>
<?php endwhile; endif; ?>
// Second loop
<?php $loop = new WP_Query( array( 'post_type' => 'bocker', 'posts_per_page' => 4, 'produktkategori' => 'Spanskt // ...should be entered here', 'orderby' => 'rand', ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
Some content
<?php endwhile; ?>
This is my solution:
// First loop
<?php $loop = new WP_Query( array( 'post_type' => 'forfattare', 'posts_per_page' => 10, 'orderby' => 'title', 'order' => 'ASC' ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $string = get_the_title() ;
$forfattare = createAlias( $string );
?>
// Second loop
<?php $loop_2 = new WP_Query( array( 'post_type' => 'bocker', 'posts_per_page' => 10, 'forfattare' => ''.$forfattare.'', 'orderby' => 'rand', 'order' => 'ASC', ) ); ?>
<?php while ( $loop_2->have_posts() ) : $loop_2->the_post(); ?>
<div class="italic"><?php the_title(); ?></div>
<?php endwhile; ?>
<?php endwhile; ?>

Categories