doulutian4843 2013-09-10 22:53
浏览 23
已采纳

如何在WordPress主页上显示自定义帖子?

There are a lot of questions on the internet right now on how to display custom posts on a WordPress page. I followed the tutorial from here.

1. I registered a custom post

add_action('init', 'portfolio_register');

function portfolio_register() {     
    $labels = array(
        'name' => _x('Portofoliu', 'portfolio'),
        'singular_name' => _x('Portfolio Item', 'post type singular name'),
        'add_new' => _x('Adauga proiect', 'portfolio item'),
        'add_new_item' => __('Adauga un proiect la portofoliu'),
        'edit_item' => __('Edit Portfolio Item'),
        'new_item' => __('New Portfolio Item'),
        'view_item' => __('View Portfolio Item'),
        'search_items' => __('Search Portfolio'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );     
    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title','editor','thumbnail')
      );      
    register_post_type( 'portfolio' , $args );
}

2. I registered the taxonomy

register_taxonomy("Skills", array("portfolio"), array("hierarchical" => true, "label" => "Platforme", "singular_label" => "Platforma", "rewrite" => true));

2. My front-page code is:

<div id="primary" class="site-content">
        <div id="content" role="main">
        <?php if ( have_posts() ) : ?>  
            <?php /* Start the Loop */ ?>
            <?php while ( have_posts() ) : the_post(); ?>
                <?php get_template_part( 'content', get_post_format() ); ?>
            <?php endwhile; ?>
            <?php twentytwelve_content_nav( 'nav-below' ); ?>

        <?php else : ?>
            <article id="post-0" class="post no-results not-found">
            <?php if ( current_user_can( 'edit_posts' ) ) :
                // Show a different message to a logged-in user who can add posts.
            ?>
                <header class="entry-header">
                    <h1 class="entry-title"><?php _e( 'No posts to display', 'twentytwelve' ); ?></h1>
                </header>
                <div class="entry-content">
                    <p><?php printf( __( 'Ready to publish your first post? <a href="%s">Get started here</a>.', 'twentytwelve' ), admin_url( 'post-new.php' ) ); ?></p>
                </div>

            <?php else :
                // Show the default message to everyone else.
            ?>
                <header class="entry-header">
                    <h1 class="entry-title"><?php _e( 'Nothing Found', 'twentytwelve' ); ?></h1>
                </header>
                <div class="entry-content">
                    <p><?php _e( 'Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve' ); ?></p>
                    <?php get_search_form(); ?>
                </div>
            <?php endif; // end current_user_can() check ?>
            </article>

        <?php endif; // end have_posts() check ?>
        </div>
    </div>

I have the Custom Post and Taxonomy on my admin, but how to display this on my Portfolio page?

  • 写回答

4条回答 默认 最新

  • doulei8861 2013-09-11 04:26
    关注
    <?php 
        $args = array(
            'tax_query' => array(
                    array(
    
                    'taxonomy' => 'Skills',
    
                    'field' => 'id',
    
                    'terms' => '27'  // disfine your category id 
    
                )
            ),
            'post_type'=>'portfolio',
            'order_by'=>'date',
            'order'=>'DESC',
            'posts_per_page'=>-1   //no of post -1 for no limit
    );
        query_posts($args);
        while ( have_posts() ) : the_post(); 
    ?>
        <li>
            <p class="date"><?php echo get_the_date('F j, Y'); ?></p>
            <a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
        </li> 
    <?php 
    endwhile;
    wp_reset_query();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?