我对wordpress非常陌生,因此对从哪里开始有点迷茫。
我想实现一个博客帖子CTA部分,管理员可以创建新的CTA,并附加到特定类别的博客帖子。例如,管理员可以创建一个关于宠物食品的CTA,并将其分配到“宠物食品”类别,此帖子将只显示那些属于“宠物食品”类别的CTA。
通过研究,我遇到了Wordpress自定义帖子类型,但由于缺乏经验,我不确定这是否是实现此功能的正确方式。
发布于 2019-10-11 19:12:41
只需遵循以下步骤即可
步骤1:遍历function.php文件中的代码
function create_posttype() {
register_post_type( 'Events',
// CPT Options
array(
'labels' => array(
'name' => __( 'CTA' ),
'singular_name' => __( 'CTA' )
),
'public' => true,
'has_archive' => false,
'rewrite' => array('slug' => 'CTA'),
'taxonomies' => array( 'category' ),
'supports' => array( 'title', 'editor','thumbnail' ),
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );这将创建一个"CTA“的post类型。
第2步:在帖子类型中获取特定类别的博客。
function blogcts() {
$args=array(
'posts_per_page' => 100,
'post_type' => 'CTA', // posttype name
'order' => 'ASC',
'cat'=> 5 //category id
);
$wp_query = new WP_Query( $args );
$pp .='';
while ($wp_query->have_posts()) : $wp_query->the_post();
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($productPost->ID)); //feature
$contents = get_the_content(); //Fetch
$contents_new = substr($contents, 0, 50);
$excerpt = get_the_excerpt();
$pp .=' <div class="">
<div class="">
<img src="'.$feat_image .'" class="" alt="">
</div>
<p>'. get_the_title() .'</p>
<hr>
<p>'.$contents_new.'</p>
<p class=""><a href="'. get_the_permalink() .'"> More</a></p>
</div>';
endwhile;
$pp .='</div>';
return '' .$pp.'';
}
add_shortcode('srtcode_blogcts', 'blogcts');第3步:将短代码粘贴到页面中。
[srtcode_blogcts]或模板文件
<?php echo do_shortcode('[srtcode_blogcts]'); ?>https://stackoverflow.com/questions/58339449
复制相似问题