我已創建自定義帖子。我想列出帖子,但我的短代碼不起作用。Wordpress-創建短代碼列出帖子
這裏是我的代碼
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');
產品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;
}
}
我從儀表板和選擇產品頁面創建的頁面作爲模板,當我預覽或加載頁面,但我看不到我的帖子。
我在我的頁面上嘗試了下面這些簡碼。實在不行我出去
[list-posts]
[list-posts type="products" category = "movies" orderby="name" order="ASC"]
您需要創建「產品」帖子類型,而不是頁面。 'type'默認爲'product',你也可以在你的例子中指定它。 – doublesharp 2014-09-04 00:08:40