doushi3715
2018-07-20 12:59
浏览 55
已采纳

Wordpress上的自定义帖子没有显示

I have added CPT UI and Custom Fields plugins to my Wordpress set up. I am trying to add custom posts onto a portfolio page and have two issues that I think must be linked.

Firstly, I cannot get thumbnail images to display on the portfolio page. I have added

"add_theme_support( 'post-thumbnails' );"

to my functions.php file as instructed. The "add_theme_support( 'menus' );" function works fine.

Featured images is enabled and displaying when I add the Portfolio Piece.

In the absence of the image I added a text link which did display but the link address is

"http://localhost/localwp.dev/2018/07/13/hello-world/"

and not "http://localhost/localwp.dev/portfolio_wadn/post/"

So I know I am missing something because the Portfolio page is not finding the right slug/address to link to the post. I think it must be the way I've set up either the Custom Fields or CPT UI settings but I can't find anything that solves the issue.

Can anyone help? Code below.

functions.php code:

<?php 

add_theme_support( 'menus' );
add_theme_support( 'post-thumbnails' );

function register_theme_menus () {

register_nav_menus( 
    array(
        'primary-menu'  => __( 'Primary Menu')
    )
 );
}

add_action( 'init', 'register_theme_menus');




function wadn_theme_styles() {

wp_enqueue_style( 'foundation_css', get_template_directory_uri() . 
'/css/foundation.css' );  
wp_enqueue_style( 'googlefont_css', 'http://fonts.googleapis.com/css? 
family=Asap:400,700,400italic,700italic' );
wp_enqueue_style( 'main_css', get_template_directory_uri() . '/style.css' );
}

add_action( 'wp_enqueue_scripts', 'wadn_theme_styles' );

function wadn_theme_js() {

wp_enqueue_script( 'modernizr_js', get_template_directory_uri() . '/js/modernizr.js', '',  '', false );
wp_enqueue_script( 'foundation_js', get_template_directory_uri() . '/js/foundation.min.js', array('jquery'), '', true );
wp_enqueue_script( 'app_js', get_template_directory_uri() . '/js/app.js', array('jquery', 'foundation_js'), '', true );  

}
add_action( 'wp_enqueue_scripts', 'wadn_theme_js' );





?>

page-portfolio.php code:

<?php 
/* 
Template Name: Portfolio Page 
*/
?>

<?php get_header(); ?>


<section class="row">
  <div class="small-12 columns text-center">
    <div class="leader">

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

      <h1><?php the_title(); ?></h1>

      <?php the_content(); ?>

    <?php endwhile; endif; ?>

    </div>
  </div>
</section>

<?php 

$args = array(
    'post-type' => 'portfolio_wadn'
);
$query = new WP_Query ( $args );

?>

<section class="row no-max pad">

<?php if( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

  <div class="small-6 medium-4 large-3 columns grid-item">
    <a href="<?php the_permalink(); ?>">Test</a>
  </div>    

 <?php endwhile; endif; wp_reset_postdata(); ?>

</section>

<?php get_footer(); ?>

Register Post Type Code

function cptui_register_my_cpts_portfolio_wadn() {

/**
 * Post Type: portfolio_wadn.
 */

$labels = array(
    "name" => __( "portfolio_wadn", "" ),
    "singular_name" => __( "Portfolio Piece", "" ),
);

$args = array(
    "label" => __( "portfolio_wadn", "" ),
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => false,
    "rest_base" => "",
    "has_archive" => false,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => false,
    "rewrite" => array( "slug" => "portfolio_wadn", "with_front" => true ),
    "query_var" => true,
    "supports" => array( "title", "editor", "thumbnail", "custom-fields", "page-attributes", "post-formats" ),
);

register_post_type( "portfolio_wadn", $args );
}

add_action( 'init', 'cptui_register_my_cpts_portfolio_wadn' );
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtiao5094 2018-07-20 13:38
    已采纳

    There is an error in your 'page-portfolio.php' code - 'post-type' should be 'post_type'.

    $args = array(
        'post_type' => 'portfolio_wadn'
    );
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题