Which is show category post. But it has some mistake .please anyone fix it .
This my WordPress shortcode
function my_form_shortcode($atts) {
ob_start();
$atts = shortcode_atts(
array(
'cat' => '1',
'showposts' => '5',
), $atts, 'my_form_shortcode' );
//YOUR CODE START
$recent = new WP_Query();
$query = "cat=".$atts['cat']."&showposts=".$atts['showposts'];
$recent->query( $query );
$is_first_post = true;
while( $recent->have_posts() ) : $recent->the_post(); ?>
<div class="panel-heading"><?php the_category(', '); ?></div>
<ul class="panel-grid">
<li class="ex">
<?php
if ( $is_first_post && has_post_thumbnail() ) {
the_post_thumbnail( 'image', array( 'class' => 'img-responsive' ));
$is_first_post = false;
}
?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
</ul>
<?php endwhile;
//YOUR CODE END
return ob_get_clean();
}
add_shortcode( 'my_form_shortcode', 'my_form_shortcode' );
In this code category title name show before all post. But I want to show category title before only 1st post. Whats the wrong in this code. Please, help me ..
try this...
https://developer.wordpress.org/reference/functions/get_the_category_by_id/
function get_the_category_by_ID( $cat_ID ) {
$cat_ID = (int) $cat_ID;
$category = get_term( $cat_ID, 'category' );
if ( is_wp_error( $category ) )
return $category;
return ( $category ) ? $category->name : '';
}
Try This code:
<?php
function my_form_shortcode($atts) {
ob_start();
$atts = shortcode_atts(
array(
'cat' => '1',
'showposts' => '5',
), $atts, 'my_form_shortcode' );
//YOUR CODE START
$recent = new WP_Query();
$query = "cat=".$atts['cat']."&showposts=".$atts['showposts'];
$recent->query( $query );
$is_first_post = true; ?>
<div class="panel-heading"><?php the_category(', '); ?></div>
<?php while( $recent->have_posts() ) : $recent->the_post(); ?>
<ul class="panel-grid">
<li class="ex">
<?php
if ( $is_first_post && has_post_thumbnail() ) {
the_post_thumbnail( 'image', array( 'class' => 'img-responsive' ));
$is_first_post = false;
}
?>
<?php the_title(); ?>
</li>
</ul>
<?php endwhile;
//YOUR CODE END
return ob_get_clean();
}
add_shortcode( 'my_form_shortcode', 'my_form_shortcode' );
Related
I've made a custom field for my product categories so that I could select icons accordingly using ACF plugin. I called all the categories like this (But the values of the custom fields return nothing as I've tried var_dumping it):
<?php
$orderby = 'date';
$order = 'DESC';
$hide_empty = false ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
);
$product_categories = get_terms( 'product_cat', $cat_args );
if( !empty($product_categories) ){
foreach ($product_categories as $key => $category) {
$icon = get_field('icon_classname');
if (empty($icon)) { $icon = 'flaticon-002-coat'; }
?>
<div class="cat-item">
<div class="cat cat-1">
<div class="cat-wrap">
<a class="category" href="<?php echo get_term_link($category); ?>">
<i class="icon fimanager <?php echo $icon; ?>"></i>
<span class="cat-title"><?php echo $category->name; ?></span>
</a>
</div>
</div>
</div>
<?php
} wp_reset_postdata();
}
?>
please check it :
you should replace these two lines :
foreach( $product_categories as $cat) {
$icon = get_field('icon_classname', 'category_'.$cat->term_id);
I want to output latest post from each category (child category) that has parent. Parent category id is 54.
For example, if there are 7 child categories under the category 54, the number of output post should be 7 (all latest from each child category). I hope this makes sense.
My current code is below. At this stage, this code outputs only one latest post (of 1 child category) that has the latest under cat id=54. It would be great if you could advise me how to modify this so that I can get more latest post from multiple child categories.
<?php
$categories = get_categories();
foreach ( $categories as $category ) {
$args = array(
'cat' => 54,
'post_type' => 'post',
'posts_per_page' => '1',
);
}
?>
<?php $query = new WP_Query( $args ); ?>
<?php if ($query->have_posts()) : ?>
<div class="container">
<?php while ($query->have_posts()) : $query->the_post(); ?>
<div class="box">
<article>
<p><?php foreach((get_the_category()) as $childcat) { if (cat_is_ancestor_of(54, $childcat)) { echo ''; echo $childcat->cat_name . ''; }} ?></p>
<?php if ( has_post_thumbnail() ): ?><?php the_post_thumbnail('box-pic'); ?><?php endif; ?>
<h3><?php the_title();?></h3>
</article>
</div>
<?php endwhile;?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
Here is the logic you need to use,
$term_id = 54;
$taxonomy_name = 'category';
$term_children = get_term_children( $term_id, $taxonomy_name );
echo '<ul>';
foreach ( $term_children as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
$args = array(
'cat' => $term->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
);
$query = new WP_Query( $args ); ?>
<?php if ($query->have_posts()) : ?>
<div class="container">
<?php while ($query->have_posts()) : $query->the_post(); ?>
<div class="box">
<article>
<p><?php foreach((get_the_category()) as $childcat) { if (cat_is_ancestor_of(54, $childcat)) { echo ''; echo $childcat->cat_name . ''; }} ?></p>
<?php if ( has_post_thumbnail() ): ?><?php the_post_thumbnail('box-pic'); ?><?php endif; ?>
<h3><?php the_title();?>
</h3>
</article>
</div>
<?php endwhile;?>
</div>
<?php endif; ?>
<?php wp_reset_query();
}
echo '</ul>';
Try this one,
$term_id =54 ;
$term_children = get_term_children( $term_id, $taxonomy );
$term_id = array();
foreach ( $term_children as $child ) {
$term = get_term_by( 'id', $child, $taxonomy );
$term_id[] = $term->term_id; //childern ids array.
}
Pass the $term_id to tax_query.
$args = array(
'post_type' => $post_type,
'posts_per_page' => 7,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $term_id
)
)
);
$query = new WP_Query($args);
This will work for you.
I have the following shortcode that I am using to display a custom post on a page:
add_shortcode( 'page-section', 'page_section_shortcode' );
function page_section_shortcode( $atts ) {
global $post;
$post_slug=$post->post_name;
$a = shortcode_atts( array(
'post-name' => 'qwerty',
'bg-color' => 'white'
), $atts );
$post_slug=$post->post_name;
$post_name = $a['post-name'];
$query = new WP_Query( array(
'post_type' => 'page_section',
'name' => $post_name,
) );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div style="background-color: <?php echo $a['bg-color']; ?>" id="<?php global $post; $post_slug=$post->post_name; echo $post_slug; ?>" class="page-section">
<div class="row">
<?php the_content(); ?>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
Everything is working the way I want it to, I can set the content in my custom post and use a short code to pull that content into a page.
The problem is that the content that is pulled in with the short code is always at the top of the page. Using multiple shortcodes in a row they keep their order, but any other content on the page is displayed at the bottom (below all of the shortcode content).
I have tried removing the 'echo' as suggested in another stackoverflow post but cannot seem to find what I am doing wrong.
Replace your code to this code. This code returns generated html so it will not echo at the top of the page
add_shortcode( 'page-section', 'page_section_shortcode' );
function page_section_shortcode( $atts ) {
global $post;
$post_slug=$post->post_name;
$a = shortcode_atts( array(
'post-name' => 'qwerty',
'bg-color' => 'white'
), $atts );
$post_slug=$post->post_name;
$post_name = $a['post-name'];
$query = new WP_Query( array(
'post_type' => 'page_section',
'name' => $post_name,
) );
$returnhtml = '';
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
global $post;
$returnhtml .= '<div style="background-color: '. $a['bg-color'].'" id="'.$post->post_name.'" class="page-section">';
$returnhtml .= '<div class="row">'.get_the_content().'</div>';
$returnhtml .= '</div>';
endwhile;
wp_reset_postdata();
return $returnhtml;
}
}
function header_notification()
{
echo '<div><strong>Any html goes here</strong></div>';
}
add_action('wp_head', 'header_notification');
I tried many solutions and changes and i can't get the pagination working on a post_type loop inside a single template.
This is the last code i tested:
<?php
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
$wp_query = new WP_Query();
$wp_query->query('post_type=some_listing&taxonomy=some_listing_category&field=ID&posts_per_page=2'.'&terms='.$someterm .'&paged='.$paged);
?>
<?php if( $wp_query->have_posts() ): ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
// loop
<?php endwhile; ?>
<?php
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Also i have this on the functions.php file:
add_action('template_redirect', function() {
if ( is_singular('areas') ) {
global $wp_query;
$page = (int) $wp_query->get('page');
if ( $page > 1 ) {
// convert 'page' to 'paged'
$query->set( 'page', 1 );
$query->set( 'paged', $page );
}
// prevent redirect
remove_action( 'template_redirect', 'redirect_canonical' );
}
}, 0 );
A friend fixed the problem, the solution is this function, just added it to functions.php.
function custom_disable_redirect_canonical( $redirect_url ){
global $post;
$ptype = get_post_type( $post );
if ( $ptype == 'post' ) $redirect_url = false;
return $redirect_url;
}
add_filter( 'redirect_canonical','custom_disable_redirect_canonical' );
Some suggestions
//set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
Try adding links like this
<?php next_post_link('%link', 'Newer entry >', true) ?>
<?php previous_post_link('%link', '< Older entry', true) ?>
Try this code for custom loop with pagination:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'post_type' => 'some_listing',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'order' => 'DESC', // 'ASC'
'orderby' => 'date'
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<article <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<div><?php the_excerpt(); ?></div>
</article>
<?php
endwhile;
?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Source:
WordPress custom loop with pagination
I am building a shopping website and I am trying to put a shortcode in that will show the customer a buy button and the quantity of the product the customer wants to purchase. On my post page the shortcode works fine:
http://warringah-plastics.com.au/blog/dt_catalog/recess-gasket-large/
but on the archive page:
http://warringah-plastics.com.au/store/
the shortcode id displayed as text and not the actual button and quantity e.g. [add_to_cart item="FPROWAR-160713-1" showprice="no" quantity="user:1" ajax="yes" ].
The code that works in the post page is this:
<?php
$my_textbox_value = mtbxr_val("shopping_shortcode");
echo do_shortcode("$my_textbox_value");
?>
but it just displays the shortcode text on that archive page. Anyone have any ideas? Much appreciated,
UPDATE
THIS IS THE CODE THAT DISPLAYS THE SHORTCODE CORRECTLY:
<?php get_header(); ?>
<?php dt_storage('have_sidebar', true); ?>
<?php get_template_part('top-bg'); ?>
<?php get_template_part('parallax'); ?>
<div id="wrapper">
<?php get_template_part('nav'); ?>
<div id="container">
<?php if( have_posts() ): while( have_posts() ): the_post(); ?>
<h1><?php the_title(); ?></h1>
<h1 style="color: #3C3C3B !important; margin-top:-20px !important;"><?php $terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'dt_catalog_category', '', ', ', '' ) );
echo $terms_as_text; ?></h1>
<?php
global $post;
$post_opts = get_post_meta($post->ID, '_dt_catalog-post_options', true);
if( !isset($post_opts['hide_media']) || (isset($post_opts['hide_media']) && !$post_opts['hide_media']) ) {
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC'
);
if( !empty($post_opts['hide_thumbnail']) )
$args['post__not_in'] = array( get_post_thumbnail_id() );
$dt_tmp_query = new WP_Query( $args );
if( $dt_tmp_query->have_posts() ) {
$slides = array();
foreach( $dt_tmp_query->posts as $slide ) {
$video = get_post_meta( $slide->ID, '_dt_catalog_video_link', true );
$tmp_arr = array();
$tmp_arr['caption'] = $slide->post_excerpt;
if ( ! $video ) {
$slide_src = dt_get_resized_img( wp_get_attachment_image_src( $slide->ID, 'full' ), array( 'w' => 710 ) );
$tmp_arr['alt'] = get_post_meta( $slide->ID, '_wp_attachment_image_alt', true );
$tmp_arr['src'] = $slide_src[0];
$tmp_arr['size_str'] = $slide_src[3];
} else {
$tmp_arr['is_video'] = true;
$tmp_arr['src'] = $video;
$tmp_arr['size_str'] = array( 710, 1024 );
}
$slides[] = $tmp_arr;
}
dt_get_anything_slider( array( 'id' => 'slider2', 'items_arr' => $slides ) );
}
}
?>
<?php $opts = get_post_meta($post->ID, '_dt_catalog-goods_options', true); ?>
<?php if( !empty($opts['price']) ): ?>
<span class="price"><?php _e('Price: ', LANGUAGE_ZONE); echo esc_html($opts['price']); ?></span>
<?php endif; ?>
<?php
$my_textbox_value = mtbxr_val("shopping_shortcode");
echo do_shortcode("$my_textbox_value");
?>
<?php
the_content();
if( dt_is_page_soc_buttons_enabled('catalog') ) {
dt_get_like_buttons( get_the_ID() );
}
?>
<?php if( !empty($opts['p_link']) ): ?>
<span><i class="dol"></i><?php _e('Make purchase!', LANGUAGE_ZONE); ?></span>
<?php endif; ?>
<p class="gap"></p>
<?php
$rel_works = get_post_meta($post->ID, '_dt_catalog_related', true);
if( isset($rel_works['show_related']) && $rel_works['show_related'] ):
if( 'same' == $rel_works['related'] ) {
$rel_works['related'] = wp_get_post_terms(
$post->ID,
'dt_catalog_category',
array('fields' => 'ids')
);
}
if( !empty($rel_works['related']) ):
?>
<p class="hr hr-narrow gap-small"></p>
<div class="gap"></div>
<div class="full-width w-photo">
<h2><?php _e('Related Items', LANGUAGE_ZONE); ?></h2>
<?php
if( 'same' == $rel_works['related'] ) {
$rel_works['related'] = wp_get_post_terms(
$post->ID,
'dt_catalog_category',
array('fields' => 'ids')
);
}
$dt_tmp_query = new WP_Query( array(
'posts_per_page' => -1,
'post_type' => 'dt_catalog',
'post_status' => 'publish',
'post__not_in' => array($post->ID),
'tax_query' => array( array(
'taxonomy' => 'dt_catalog_category',
'field' => 'id',
'terms' => $rel_works['related'],
'operator' => 'IN'
) )
) );
if( $dt_tmp_query->have_posts() ) {
$thumb_arr = dt_core_get_posts_thumbnails( $dt_tmp_query->posts );
$items = array();
foreach( $dt_tmp_query->posts as $rel_post ) {
$item = array();
$img = dt_get_resized_img(
dt_get_thumb_meta($thumb_arr['thumbs_meta'], 'full', $rel_post->ID),
array('w' => 196, 'h' => 123, 'use_noimage' => true)
);
$item['src'] = $img[0];
$item['size_str'] = $img[2];
$item['post_id'] = $rel_post->ID;
$item['desc'] = apply_filters('get_the_excerpt', $rel_post->post_excerpt);
$item['title'] = apply_filters('the_title', $rel_post->post_title, $rel_post->ID);
$item['alt'] = esc_attr( $item['title'] );
$items[] = $item;
}
$args = array( 'items_arr' => $items, 'id' => '', 'class' => 'list-carousel recent bx', 'ul_class' => 'slider1' );
$args['wrap'] = '<div class="%CLASS% bx">%SLIDER%</div>';
if( ! empty( $rel_works['show_desc'] ) || ! empty( $rel_works['show_title'] ) ) {
$title = '';
if( ! empty( $rel_works['show_title'] ) ) {
$title = '<h3>%TITLE%</h3>';
}
$desc = '';
if( ! empty( $rel_works['show_desc'] ) ) {
$desc = '<p>%DESC%</p>';
}
$args['item_wrap'] = '
<li>
<div class="textwidget">
<div class="textwidget-photo">
<a class="photo" href="%LINK%"><img src="%IMG_SRC%" alt="%ALT%" %IMG_SIZE% /></a>
</div>
<div class="widget-info">
<div class="info">
' . $title . $desc . '
</div>
</div>
</div>
</li>
';
}
dt_get_carousel_slider( $args );
}
?>
</div>
<?php endif; endif; ?>
<?php comments_template(); ?>
<?php
endwhile;
endif;
?>
</div>
<?php dt_widget_area('sidebar', null, 'sidebar_4'); ?>
</div>
<?php get_footer(); ?>
AND THIS IS THE CODE THAT DISPLAYS THE SHORTCODE JUST AS TEXT:
<?php
global $post;
$page_data = dt_storage( 'page_data' );
$page_opts = ! empty( $page_data['page_options'] ) ? $page_data['page_options'] : array();
$add_data = dt_storage( 'add_data' );
$first_class = '';
if( 1 === dt_storage('post_is_first') ) {
$first_class = ' first';
dt_storage( 'post_is_first', -1 );
}
$opts = get_post_meta($post->ID, '_dt_catalog-goods_options', true);
?>
<div class="<?php dt_portfolio_classes( '2_col-list', 'block' ); echo $first_class; ?>">
<?php
$h = 220;
if ( ! empty ( $page_opts['thumb_height'] ) ) {
$h = $page_opts['thumb_height'];
}
dt_get_thumb_img( array(
'class' => 'photo',
'use_noimage' => true,
'href' => get_permalink(),
'thumb_opts' => array( 'w' => 343, 'h' => $h )
),
'<div class="textwidget-photo">
<a %HREF% %CLASS% %TITLE% %CUSTOM%><img %ALT% %SRC% %IMG_CLASS% %SIZE% /></a>
</div>'
);
?>
<div class="<?php dt_portfolio_classes( '2_col-list', 'info' ); ?>">
<a class="<?php dt_portfolio_classes( '2_col-list', 'head' ); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php if( !empty($opts['price']) ): ?>
<span class="price"><?php _e('Price: ', LANGUAGE_ZONE); echo esc_html($opts['price']); ?></span>
<?php endif; ?>
<?php
dt_the_content();
dt_details_link();
dt_edit_link();
?>
<div id="specialpriceshortcode">
<?php
$my_textbox_value = mtbxr_val("shopping_shortcode");
echo do_shortcode("$my_textbox_value");
?>
</div>
</div>
</div>
Try using single quotes in the do_shortcode call, like so:
echo do_shortcode('$my_textbox_value');
More likely though is that the shortcode isn't defined on the archive page so you'd need to look at where it is being instantiated to see if that is the issue. Normally when a shortcode just echoes out the content it means that shortcode doesn't exist. You can test easily enough by using the shortcode_exists() function:
<?php if ( shortcode_exists( 'add_to_cart' ) ) { echo "The shortcode exists";} ?>
If that doesn't work then you know the issue is with the shortcode not being registered on your archives page. If it does work then you know it's something with the format of the content being passed to the shortcode.
Add this to your functions.php
// Allow shortcodes on widgets
add_filter('widget_text','do_shortcode');
// Allow shortcodes on pages (not tested, but should work)
add_filter('the_content','do_shortcode');
Typically your shortcode is getting registered in a plugin or your theme's functions.php file. In a plugin it's often something like:
add_action('init', 'register_my_shortcode');
function register_my_shortcode(){
add_shortcode('my_shortcode', 'do_my_shortcode');
}
And then you'd have a function do_my_short_code() that actually outputs the content. With something like that the shortcode is getting registered via the 'init' hook (http://codex.wordpress.org/Plugin_API/Action_Reference) which is called before WP has started figuring out what template to use, what content to output, etc.
But some plugins will register the shortcode in a way that it is only available on pages / posts where it's going to potentially be used. For example, I can think of one plugin where they register the shortcode and enqueue some javascripts in the same function. That function checks to see if you're on a particular page before it executes so that the js files are not included unnecessarily all over the place. Since the shortcode registration takes place in the same function it means the shortcode only exists on those pages.
Anyhow, if the shortcode is showing as existing on your archives page you know that isn't the problem, so check that first and let me know what you find.