首页 > 解决方案 > WordPress 上的自定义帖子未显示

问题描述

我在我的 Wordpress 设置中添加了CPT UI自定义字段插件。我正在尝试将自定义帖子添加到投资组合页面上,并且有两个我认为必须链接的问题。

首先,我无法在投资组合页面上显示缩略图。我已经添加了

"add_theme_support( 'post-thumbnails' );"

按照说明添加到我的functions.php文件中。“add_theme_support('menus');” 功能工作正常。

当我添加作品集时,特色图像已启用并显示。

在没有图像的情况下,我添加了一个显示的文本链接,但链接地址是

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

而不是“ http://localhost/localwp.dev/portfolio_wadn/post/

所以我知道我遗漏了一些东西,因为投资组合页面没有找到正确的标签/地址来链接到帖子。我认为这一定是我设置自定义字段或 CPT UI 设置的方式,但我找不到任何解决问题的方法。

任何人都可以帮忙吗?代码如下。

函数.php代码:

<?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 代码:

<?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(); ?>

注册帖子类型代码

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' );

标签: phpwordpresscustom-post-type

解决方案


您的“page-portfolio.php”代码中有一个错误——“post-type”应该是“post_type”。

$args = array(
    'post_type' => 'portfolio_wadn'
);

推荐阅读