我正在使用Types插件来构建自定义帖子类型和自定义分类法,效果很好。现在,我尝试显示某个帖子的内容,并将附件上传到该帖子(仅允许一个,不允许多个),但是我无法使其正常工作。这是我在single-legislacion.php
模板上所做的:
<div class="legislacion-col-izq">
<div class="CajaIzq">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php endwhile; ?>
<?php endif; ?>
<?php
$args = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => the_ID(),
'post_status' => 'inherit',
'numberposts' => 1
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) { ?>
<div class="BaseIco">
<a class="IcoDescargas" href="<?php echo wp_get_attachment_url( $attachment->ID, true ); ?>">
<img src="<?php echo get_template_directory_uri(); ?>/images/ico_descargas.png"><br>
Descargar PDF
</a>
</div>
<?php }
}
?>
<div class="ModSuscrip">
<p class="A">¿Deseas ver comentarios y jurisprudencia sobre esta ley?</p>
<p class="B"><a href="#">Suscríbete a cualquiera de nuestros planes</a> y podrás ver material
complementario, enlaces, y mucho más para enreiquecer tu investigación</p>
</div>
<?php echo do_shortcode( '[toc label="Índice por Títulos"]' ) ?>
</div>
</div>
<div class="legislacion-col-der">
<div class="CajaInfo">
<div class="titulo">¿Estás buscando una palabra o frase en particular?</div>
Recuerda que puedes usar la función de búsqueda de tu navegador (Ctrl + F o Command + F)
</div>
<div class="CajaContenido">
<h1 class="titulo"><?php the_title(); ?></h1>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $options = get_option( 'responsive_theme_options' ); ?>
<?php if ( $options['breadcrumb'] == 0 ): ?>
<?php echo responsive_breadcrumb_lists(); ?>
<?php endif; ?>
<div id="post-<?php the_ID(); ?>">
<div class="post-entry">
<?php the_content( __( 'Read more ›', 'responsive' ) ); ?>
<?php wp_link_pages( array(
'before' => '<div class="pagination">' . __( 'Pages:',
'responsive' ),
'after' => '</div>'
) ); ?>
</div>
<!-- end of .post-entry -->
<?php if ( comments_open() ) : ?>
<div class="post-data">
<?php the_tags( __( 'Tagged with:', 'responsive' ) . ' ', ', ', '<br />' ); ?>
<?php the_category( __( 'Posted in %s', 'responsive' ) . ', ' ); ?>
</div><!-- end of .post-data -->
<?php endif; ?>
<div class="post-edit"><?php edit_post_link( __( 'Edit', 'responsive' ) ); ?></div>
</div><!-- end of #post-<?php the_ID(); ?> -->
<?php comments_template( '', true ); ?>
<?php endwhile; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<?php if ( function_exists( 'wp_paginate' ) ) {
wp_paginate();
} ?>
<?php endif; ?>
<?php else : ?>
<h1 class="title-404"><?php _e( '404 — Fancy meeting you here!', 'responsive' ); ?></h1>
<p><?php _e( 'Don't panic, we'll get through this together. Let's explore our options here.',
'responsive' ); ?></p>
<h6><?php printf( __( 'You can return %s or search for the page you were looking for.', 'responsive' ),
sprintf( '<a href="%1$s" title="%2$s">%3$s</a>',
esc_url( get_home_url() ),
esc_attr__( 'Home', 'responsive' ),
esc_attr__( '← Home', 'responsive' )
) );
?></h6>
<?php get_search_form(); ?>
<?php endif; ?>
</div>
</div>
但是我从任何帖子中都得到了附件,而不是这篇文章的附件,我怎么知道?因为帖子上的URL是这个URL,http://project.dev/wp-content/uploads/2014/12/file.pdf
而我在前面得到了这个URL,所以http://project.dev/wp-content/uploads/2013/08/SC-1081-30.docx
怎么了?我如何获得该CPT的附件?
因为您使用the_ID()
which生成了echo
which构造错误的参数。
使用get_the_ID()
代替应该做到这一点。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句