I have created the custom post. I want to list down posts, but my shortcodes are not working.
Here is my code
function.php
// register a custom post type called 'Products'
function wptp_create_post_type() {
$labels = array(
'name' => __( 'Products' ),
'singular_name' => __( 'product' ),
'add_new' => __( 'New product' ),
'add_new_item' => __( 'Add New product' ),
'edit_item' => __( 'Edit product' ),
'new_item' => __( 'New product' ),
'view_item' => __( 'View product' ),
'search_items' => __( 'Search products' ),
'not_found' => __( 'No product Found' ),
'not_found_in_trash' => __( 'No product found in Trash' ),
);
$args = array(
'labels' => $labels,
'has_archive' => true,
'public' => true,
'hierarchical' => false,
'supports' => array(
'title',
'editor',
'excerpt',
'custom-fields',
'thumbnail',
'page-attributes'
),
'taxonomies' => array( 'post_tag', 'category' ),
);
register_post_type('product', $args );
}
add_action( 'init', 'wptp_create_post_type' );
product-page.php
add_shortcode( 'list-posts', 'rmcc_post_listing_parameters_shortcode' );
function rmcc_post_listing_parameters_shortcode( $atts ) {
ob_start();
extract( shortcode_atts( array (
'type' => 'product',
'order' => 'date',
'orderby' => 'title',
'posts' => -1,
'category' => '',
), $atts ) );
$options = array(
'post_type' => $type,
'order' => $order,
'orderby' => $orderby,
'posts_per_page' => $posts,
'category_name' => $category,
);
$query = new WP_Query( $options );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile;
wp_reset_postdata(); ?>
</ul>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
I have created a page from dashboard and select product-page as template and when I preview or load the page, but I couldn’t see my posts.
I have try these below shortcodes on my page. Nothing works out for me
[list-posts]
[list-posts type="products" category = "movies" orderby="name" order="ASC"]
I followed this tutorial
Hello you need to add all code in your function.php i am pasting my what i have done
function.php
product-page.php
I think that would help you. Let me know if you want something else
Move the code for your shortcode out of product-page.php into functions.php. Also,
[list-posts type="products" category = "movies" orderby="name" order="ASC"]
should havetype="product"
since your post type is product.Leave
ob_*
functions for when there’s no alternative.WP_Query
can be changed forget_posts()
andwhile ( has_posts() )
for a regularforeach()
. The only trouble ispost_class()
, but the core function is just one line, so easy to adapt. Also,extract()
is outdated.Do not use
extract()
in a shortcode. In fact,extract()
, should never be used. Here is an example of a shortcode I’ve done recently on another answer on WPSE. Use and modify as neededFor support for PHP < 5.4, you can do the following for the shortcode function.