I am trying to make a shortcode that can display featured image and permalink from chosen pages on my front-page in Wordpress.
It works fine to display one page, but I want to be able to choose different pages. I cannot figure out how....
// Creating Shortcodes with parameter to display pages
function my_shortcode_display_pages($attr, $content = null){
//call global variable
global $post;
// Defines Shortcode's Attributes
$shortcode_args = shortcode_atts(
array(
'type' => '',
'pagename' => '',
'num' => '',
'order' => ''
), $attr);
// set an array with query arguments
$args = array(
'post_type' => $shortcode_args['type'],
'pagename' => $shortcode_args['pagename'],
'posts_per_page' => $shortcode_args['num'],
'order' => $shortcode_args['order']
);
// get posts
$recent_posts = get_posts($args);
$output = '<div class="cards">';
$query = new WP_Query($args);
if($query->have_posts()) :
while($query->have_posts()) :
$query->the_post() ;
$output .= '<div class="card">' . ''.get_the_post_thumbnail() .'<h2 class="entry-title-index">'.get_the_title() .'</h2>'.'</div>';
endwhile;
wp_reset_postdata();
endif;
//return the output.
return $output . '</div>';
}
// Register the shortcode.
add_shortcode( 'my_display_pages', 'my_shortcode_display_pages' );
[my_display_pages pagename="hotell"]
Related
In Woocommerce, I would like to add a drop down in product short description that shows all products that have the same category(ies). It will be even better if it was possible to go to the product page of the selected product.
I didn't see any threads that fulfill what I am trying to do.
Any help will be appreciated.
2021 Update - Added product_id as argument, allowing the shortcode to be used for a defined product ID (for example on a page).
The following will make a custom shortcode that you can use in your product short description (or even in the product description) and will display a dropdown from same product category than the current product.
The code:
add_shortcode( 'products_dropdown', 'wc_products_from_cat_dropdown' );
function wc_products_from_cat_dropdown( $atts ) {
// Shortcode Attributes
$atts = shortcode_atts( array(
'product_id' => '',
), $atts, 'products_dropdown' );
$product_id = is_product() ? get_the_id() : $atts['product_id'];
if ( empty($product_id) )
return;
ob_start();
$query = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '-1',
'post__not_in' => array( $product_id ),
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) ) ,
) ),
) );
if ( $query->have_posts() ) :
echo '<div class="products-dropdown"><select name="products-select" id="products-select">
<option value="">'.__('Choose a related product').'</option>';
while ( $query->have_posts() ) : $query->the_post();
echo '<option value="'.get_permalink().'">'.get_the_title().'</option>';
endwhile;
echo '</select> <button type="button" style="padding:2px 10px; margin-left:10px;">'._("Go").'</button></div>';
wp_reset_postdata();
endif;
?>
<script type='text/javascript'>
jQuery(function($){
var a = '.products-dropdown', b = a+' button', c = a+' select', s = '';
$(c).change(function(){
s = $(this).val();
console.log(s); // just for testing (to be removed)
});
$(b).click(function(){
if( s != '' ) location.href = s;
});
});
</script>
<?php
return ob_get_clean();
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
USAGE
1). For single product pages: Just paste the following shortcode in the product short description (or descrition):
[products_dropdown]
2). For single product pages, inside php code:
echo do_shortcode("[products_dropdown]");
3). on any post or page within the text editor, define the product_id argument (below the defined product id is 37):
[products_dropdown product_id="37"]
Add this to your theme's 'functions.php' which will display all products of your current product's category.
function add_products_short_description() {
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
if ( $product_cats ) {
$single_cat = array_shift( $product_cats );
$product_args = array( 'post_type' => 'product', 'posts_per_page' => '-1', 'product_cat' => $single_cat->name );
$products = new WP_Query( $product_args );
if ( $products->have_posts() ) : echo '<ul>';
while ( $products->have_posts() ) : $products->the_post(); global $product;
echo '<li>'.get_the_title($products->ID).'</li>';
endwhile;
echo '</ul>';
endif;
}
}
add_action( 'woocommerce_single_product_summary', 'add_products_short_description', 15 );
i make two list theme
1.the posts have featured image and title, but empty content. i want get this list.
2.and other posts with content list.
I don't have any idea, help me please
Try to use below code:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'ID',
'order' => 'desc');
$blank_posts = array();
$posts = new WP_Query( $args );
if ( $posts->have_posts() ) :
while ( $posts->have_posts() ) : $posts->the_post();
$content = get_the_content();
if($content == '') {
array_push( $blank_posts, $post);
}
endwhile;
endif;
/* print all blank content posts */
//echo "<pre>"; print_r($blank_posts);
/* loop */
if(!empty($blank_posts)){
foreach ($blank_posts as $pst) {
echo "ID= ". $pst->ID . ', '. "Title= ". $pst->post_title .'<hr />';
}
}
I am working on woocommerce and showing listing of group products for specific category. When i am clicking on any product then it redirect to single product page which is default functionality of woocommerce. Now i want to add custom link to all group products. e.g.
http://test.com/product-title/testproduct?c=12&g=1
These parameters "C" and "G" id are coming dynamically. But when i click on this link then it redirects to product detail page. Is there a way to add custom template link to product. This is my code to get products.
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => 'washer' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile;
wp_reset_query();
Code should be
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => 'lego-bricks' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$product_link = '';
if($product->is_type('grouped')) {
$product_link = site_url().'/custom-link/?c=12&g=1';
} else {
$product_link = get_permalink();
}
echo '<br /><a href="'.$product_link.'">'.
woocommerce_get_product_thumbnail().' '.get_the_title().'</a>';
endwhile;
wp_reset_query();
Make sure your page with custom link has been assigned custom page template.
I have attempted random filters and other hook modifiers to try to get my echoed results on the Page content area NOT the header, and I have failed miserably. I am looking for any suggestions or hints as to how post the echoed results to the content area of the Page and not the header area. To clarify: My results are appearing on the Page I assigned them to BUT they are populating in the header of the page?? Not the main content body area.
add_action('pre_get_posts', 'cv_testimonials_list');
function cv_testimonials_list($query) {
if ($query->is_page('9595') && $query->is_main_query()) {
gravity_form(1, false, false, false, '', false);
$args = array (
'post_type' => 'testimonial',
'post_status' => 'published',
'pagination' => true,
'posts_per_page' => '10',
'order' => 'DESC',
'orderby' => 'date',);
// The Query
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
echo "<p><strong>" . the_content() . "</strong></p>";
echo "<p>" . the_title() . "</p>";
echo "<p><a target='_blank' href='http://" . get_post_meta( get_the_ID(), 'testimonials-website-url') . "'>" . get_post_meta( get_the_ID(), 'testimonials-website-url', true ) . "</a></p>";
//print "<pre>";
//print_r($custom_fields);
//print "</pre>";
endwhile;
return;
}
}
It's because you're using functions that echo their values, not return them. The values are being echoed when they're evaluated, not where they're needed.
Try using get_the_content()and get_the_title() instead.
EDIT
Sorry, just noticed where you're echoing your stuff. You don't need to run your query in pre_get_posts, just modify it, eg
$query->set( 'post_type', 'testimonial' );
WordPress will then run the query for you in the normal scheme of things.
Not sure what gravity_form is doing, but I doubt pre_get_posts is the right place for it. If what you're really trying to do is use that code as your page content, I'd move all your code out of the hook, into a custom page template, and assign page 9595 to that page template.
EDIT 2
Create a page template like this in your theme, then in your admin screen, assign it to page 9595 (and delete the code from pre_get_posts):
<?php
/**
* Template Name: Testimonial List
*/
$args = array (
'post_type' => 'testimonial',
'post_status' => 'published',
'pagination' => true,
'posts_per_page' => '10',
'order' => 'DESC',
'orderby' => 'date'
);
$query = new WP_Query( $args );
get_header(); ?>
<div id="content" class="widecolumn"><?php
gravity_form(1, false, false, false, '', false);
while ( $query->have_posts() ) : $query->the_post();
echo "<p><strong>" . get_the_content() . "</strong></p>";
echo "<p>" . get_the_title() . "</p>";
echo "<p><a target='_blank' href='http://" . get_post_meta( get_the_ID(), 'testimonials-website-url') . "'>" . get_post_meta( get_the_ID(), 'testimonials-website-url', true ) . "</a></p>";
endwhile;
?>
</div>
<?php get_footer(); ?>
In this case, you're not trying to change the main WordPress query, you're just not using it - you're using your own query in the template and looping over that.
Posts on my page are fetched via this query:
<?php
$limit = get_option('posts_per_page');
query_posts(array(
'showposts'=>62,
'more' => $more = 0,
'v_sortby' => 'views',
'v_orderby' => 'DESC',
'v_outtype' => 'content',
'v_timespan' => 'year',
'paged' => $paged
));?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
However, I want to exclude all posts which title contains the word "Metal".
I know there's a 'post__not_in' => feature, but I'm not sure how to implement it.
I tried
$exclude1 = get_page_by_title('Metal'); and then including
'post__not_in' => array($exclude1->ID,$exclude2->ID)
but that seems to center around pages, and not post titles itself.
In the arguments:
$args['special_search'] = 'Metal';
In your functions.php:
add_filter( 'posts_where', 'special_search_posts_where', 10, 2 );
function special_search_posts_where( $where, $query ) {
if (isset($query->query_vars["special_search"])) {
global $wpdb;
$where .= ' AND ' . $wpdb->posts . '.post_title NOT LIKE \'%' . $query->query_vars["special_search"] . '%\'';
}
return $where;
}