我已经在我的Wordpress设置中添加了CPT UI和Custom Fields插件。我正在尝试将自定义帖子添加到投资组合页面,并且有两个我认为必须链接的问题。
首先,我无法使缩略图显示在投资组合页面上。我添加了
"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/”
所以我知道我丢失了一些东西,因为“ Portfolio”页面没有找到正确的链接/地址来链接到该帖子。我认为这一定是我设置“自定义字段”或“ CPT用户界面”设置的方法,但是找不到解决该问题的方法。
有人可以帮忙吗?下面的代码。
functions.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' );
答案 0 :(得分:0)
您的'page-portfolio.php'代码中存在错误-'post-type'应该为'post_type'。
$args = array(
'post_type' => 'portfolio_wadn'
);