关于wordpress中post_type函数的两个问题
Two questions about post_type function in wordpress
我正在根据这个主题 (limo) 制作一个 child 主题,我正在尝试做两件事但没有成功,所以这是我的问题:
- 如何从 post_type 获得永久link?
正如您在该演示("Our Works" 部分)中看到的那样,我想获得此 post 的 link。因此,您可以单击此 image/post 标题,它会打开 post 页面。使用 get_permalink() 函数没有任何反应。
函数是这样的:
function ccr_our_works() {
$labels = array(
'name' => __( 'Our works', 'codexcoder' ),
'singular_name' => __( 'Our work', 'codexcoder' ),
'add_new' => _x( 'Add New Work', 'codexcoder', 'codexcoder' ),
'add_new_item' => __( 'Add New Work', 'codexcoder' ),
'edit_item' => __( 'Edit Our Work', 'codexcoder' ),
'new_item' => __( 'New Our Work', 'codexcoder' ),
'view_item' => __( 'View Our Work', 'codexcoder' ),
'search_items' => __( 'Search Our works', 'codexcoder' ),
'not_found' => __( 'No Our works found', 'codexcoder' ),
'not_found_in_trash' => __( 'No Our works found in Trash', 'codexcoder' ),
'parent_item_colon' => __( 'Parent Our Work:', 'codexcoder' ),
'menu_name' => __( 'Works', 'codexcoder' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'description',
'taxonomies' => array(),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => null,
'menu_icon' => null,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post',
'supports' => array(
'title', 'editor', 'thumbnail'
)
);
register_post_type( 'work', $args );
}
add_action( 'init', 'ccr_our_works' );
我这样做是为了获得 post link:
<!-- Slider items -->
<div class="carousel-inner">
<div class="item active">
<?php
$loop = new WP_Query('post_type=work&posts_per_page=4');
while ($loop->have_posts()) {
$loop->the_post();
?>
<div class="col-sm-3">
<figure>
<?php the_post_thumbnail(); ?>
<figcaption>
<h4><a href="<?php get_permalink(); ?>"><?php the_title(); ?></a></h4>
</figcaption>
</figure>
</div>
<?php } ?>
</div> <!-- /.active /.item -->
<div class="item">
<?php
$loop = new WP_Query('post_type=work&posts_per_page=4&offset=4');
while ($loop->have_posts()) {
$loop->the_post();
?>
<div class="col-sm-3">
<figure>
<?php the_post_thumbnail(); ?>
<figcaption>
<h4><a href="<?php get_permalink(); ?>"><?php the_title(); ?></a></h4>
</figcaption>
</figure>
</div>
<?php } ?>
</div> <!-- /.active /.item -->
</div> <!-- /.carousel-inner -->
<!--/.carousel -->
</div> <!-- /#work-slide -->
我该怎么做?
- 如何构建页面模板以将特定 post_type 的所有这些 post 加入到一个页面中?
我尝试了什么:
<?php
/*
Template Name: Our Works
*/
?>
<?php get_header(); ?>
<!-- begin colLeft -->
<div id="colLeft">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 5,
'post_type' => 'our_works',
'paged' => $paged
);
query_posts($args);
?>
<h1><?php the_title(); ?></h1>
<div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<a href="<?php the_permalink(); ?>">Read more →</a>
<br><br>
<?php endwhile; ?>
<?php if (function_exists("emm_paginate")) {
emm_paginate();
} ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e('Not found'); ?></p>
<?php endif; ?>
</div>
</div>
<!-- end colleft -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
有什么帮助吗?我想了解如何处理这个问题。
加油
1) 永久链接使用
<?php the_permalink();?>
2) 到你的页面,试试这个
<?php
/*
Template Name: Our Works
*/
?>
<?php get_header(); ?>
<!-- begin colLeft -->
<div id="colLeft">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array('paged' => get_query_var('paged'), 'posts_per_page'=>5, 'post_type'=>'our_works', 'order' => 'ASC'))?>
<h1><?php the_title(); ?></h1>
<div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<a href="<?php the_permalink(); ?>">Read more →</a>
<br><br>
<?php endwhile;?>
<?php if (function_exists("emm_paginate")) {
emm_paginate();
} ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e('Not found'); ?></p>
<?php endif; ?>
</div>
</div>
<!-- end colleft -->
<?php get_sidebar(); ?>
我正在根据这个主题 (limo) 制作一个 child 主题,我正在尝试做两件事但没有成功,所以这是我的问题:
- 如何从 post_type 获得永久link?
正如您在该演示("Our Works" 部分)中看到的那样,我想获得此 post 的 link。因此,您可以单击此 image/post 标题,它会打开 post 页面。使用 get_permalink() 函数没有任何反应。
函数是这样的:
function ccr_our_works() {
$labels = array(
'name' => __( 'Our works', 'codexcoder' ),
'singular_name' => __( 'Our work', 'codexcoder' ),
'add_new' => _x( 'Add New Work', 'codexcoder', 'codexcoder' ),
'add_new_item' => __( 'Add New Work', 'codexcoder' ),
'edit_item' => __( 'Edit Our Work', 'codexcoder' ),
'new_item' => __( 'New Our Work', 'codexcoder' ),
'view_item' => __( 'View Our Work', 'codexcoder' ),
'search_items' => __( 'Search Our works', 'codexcoder' ),
'not_found' => __( 'No Our works found', 'codexcoder' ),
'not_found_in_trash' => __( 'No Our works found in Trash', 'codexcoder' ),
'parent_item_colon' => __( 'Parent Our Work:', 'codexcoder' ),
'menu_name' => __( 'Works', 'codexcoder' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'description',
'taxonomies' => array(),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => null,
'menu_icon' => null,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post',
'supports' => array(
'title', 'editor', 'thumbnail'
)
);
register_post_type( 'work', $args );
}
add_action( 'init', 'ccr_our_works' );
我这样做是为了获得 post link:
<!-- Slider items -->
<div class="carousel-inner">
<div class="item active">
<?php
$loop = new WP_Query('post_type=work&posts_per_page=4');
while ($loop->have_posts()) {
$loop->the_post();
?>
<div class="col-sm-3">
<figure>
<?php the_post_thumbnail(); ?>
<figcaption>
<h4><a href="<?php get_permalink(); ?>"><?php the_title(); ?></a></h4>
</figcaption>
</figure>
</div>
<?php } ?>
</div> <!-- /.active /.item -->
<div class="item">
<?php
$loop = new WP_Query('post_type=work&posts_per_page=4&offset=4');
while ($loop->have_posts()) {
$loop->the_post();
?>
<div class="col-sm-3">
<figure>
<?php the_post_thumbnail(); ?>
<figcaption>
<h4><a href="<?php get_permalink(); ?>"><?php the_title(); ?></a></h4>
</figcaption>
</figure>
</div>
<?php } ?>
</div> <!-- /.active /.item -->
</div> <!-- /.carousel-inner -->
<!--/.carousel -->
</div> <!-- /#work-slide -->
我该怎么做?
- 如何构建页面模板以将特定 post_type 的所有这些 post 加入到一个页面中?
我尝试了什么:
<?php
/*
Template Name: Our Works
*/
?>
<?php get_header(); ?>
<!-- begin colLeft -->
<div id="colLeft">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 5,
'post_type' => 'our_works',
'paged' => $paged
);
query_posts($args);
?>
<h1><?php the_title(); ?></h1>
<div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<a href="<?php the_permalink(); ?>">Read more →</a>
<br><br>
<?php endwhile; ?>
<?php if (function_exists("emm_paginate")) {
emm_paginate();
} ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e('Not found'); ?></p>
<?php endif; ?>
</div>
</div>
<!-- end colleft -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
有什么帮助吗?我想了解如何处理这个问题。
加油
1) 永久链接使用
<?php the_permalink();?>
2) 到你的页面,试试这个
<?php
/*
Template Name: Our Works
*/
?>
<?php get_header(); ?>
<!-- begin colLeft -->
<div id="colLeft">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array('paged' => get_query_var('paged'), 'posts_per_page'=>5, 'post_type'=>'our_works', 'order' => 'ASC'))?>
<h1><?php the_title(); ?></h1>
<div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<a href="<?php the_permalink(); ?>">Read more →</a>
<br><br>
<?php endwhile;?>
<?php if (function_exists("emm_paginate")) {
emm_paginate();
} ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e('Not found'); ?></p>
<?php endif; ?>
</div>
</div>
<!-- end colleft -->
<?php get_sidebar(); ?>