论文框架和自定义帖子类型

时间:2012-12-20 23:14:40

标签: wordpress custom-post-type wordpress-thesis-theme

首次在论文中创建/实现自定义帖子类型,而不是第一次使用自定义帖子类型。

我使用Reed Write插件来创建自定义帖子类型。该网站正在使用Thesis 1.8.5。

在下一页(http://www.snyderleadership.com/press-releases/)上,我的主要内容会被删除后面的自定义帖子类型的内容。

我使用custom_functions.php文件创建自定义页面模板,并为自定义帖子类型的内容调用db。这是我的代码:

/* CUSTOM PRESS RELEASE TEMPLATE - ADDED by BRETT ATKIN */

function press_releases_page() {
if (is_page('press-releases') || is_page('583')) { ?>

<div id="content">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="post_box">
    <div class="headline_area"><h1><?php the_title(); ?></h1></div>
    <div class="format_text">
    <?php the_content(); ?>
    <?php endwhile; ?>
    <?php endif ?>
    <?php
        $original_query = $wp_query;
        $wp_query = null;

        $args = array (
        'post_type' => 'press-release',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'orderby' => 'date',
        'order' => 'DESC'
        );
        $wp_query = new WP_Query($args);
        ?>
    <div id="press-releases">
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <div id="press-wrapper">
            <div id="press-image">
                <?php echo wp_get_attachment_image(get_post_meta($post->ID, 'release_image', true)); ?>
            </div><!-- end press-image div -->
            <div id="press-information">
                <p class="press-date"><?php echo get_post_meta($post->ID, 'release_date', true); ?></p>
                <p class="press-link"><a href="<?php echo get_post_meta($post->ID, 'release_link', true); ?>" target="_blank"><?php echo get_post_meta($post->ID, 'release_title', true); ?></a></p>
                <p class="press-author"><?php echo get_post_meta($post->ID, 'release_author', true); ?></p>
            </div><!-- end press-information div -->
            <div style="clear:both;"></div>
        </div><!-- end press-wrapper div -->
        <?php endwhile; endif; wp_reset_postdata(); ?>
    </div><!-- end press-releases div -->
    </div>
    </div>
    </div><!-- end content -->
    <?php echo thesis_sidebars(); ?>
<?php } }

add_action('thesis_hook_custom_template', 'press_releases_page');

似乎一切正常,只是没有拉入自定义帖子类型的数据。

在其他网站上完成此操作(使用自定义主题),我不确定我是否在这里做错了或是否是论文问题。

任何帮助都会很棒。

由于

布雷特

2 个答案:

答案 0 :(得分:1)

这是最终的工作代码。感谢当地WP Guru朋友和maiorano84的帮助。我们没有弄清楚原因,但我们做了很好的解决方案。

function press_releases_page() {
if (is_page('press-releases') || is_page('583')) { ?>

<div id="content">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="post_box">
    <div class="headline_area"><h1><?php the_title(); ?></h1></div>
    <div class="format_text">
    <?php the_content(); ?>
    <?php endwhile; ?>
    <?php endif ?>

    <?php
        $original_query = $wp_query;
        $wp_query = null;

        $args = array (
        'post_type' => 'press-release',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'orderby' => 'date',
        'order' => 'DESC'
        );
        $wp_query = new WP_Query($args);
    ?>

    <div id="press-releases">
        <?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
        <?php $results = get_post_custom(); ?>
        <div id="press-wrapper">
            <div id="press-image"><?php echo wp_get_attachment_image($results['release_image'][0] ); ?></div><!-- end press-image div -->
            <div id="press-information">
                <p class="press-date"><?php echo $results['release_date'][0] ?></p> 
                <p class="press-link"><a href="<?php echo $results['release_link'][0] ?>" target="_blank"><?php echo $results['release_title'][0] ?></a></p>
                <p class="press-author"><?php echo $results['release_author'][0] ?></p>
            </div><!-- end press-information div -->
            <div style="clear:both;"></div>
        </div><!-- end press-wrapper div -->
        <?php endwhile; endif; wp_reset_query(); wp_reset_postdata(); ?>
    </div><!-- end press-releases div -->
    </div>
    </div>
    </div><!-- end content -->
    <?php echo thesis_sidebars(); ?>
<?php } }

remove_action('thesis_hook_custom_template', 'thesis_custom_template_sample');
add_action('thesis_hook_custom_template', 'press_releases_page');

答案 1 :(得分:0)

尝试更改此内容:

<div id="press-releases">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="press-wrapper">

对此:

<div id="press-releases">
<?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div id="press-wrapper">

<强>更新

我能看到的另一件事是你没有重置你的帖子数据,而且$ wp_query变量实际上是一个Wordpress Global。尝试使用重置函数并更改您的WP_Query实例名称,如下所示:

<?php
    wp_reset_query();
    wp_reset_postdata();

    $args = array (
    'post_type' => 'press-release',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'orderby' => 'date',
    'order' => 'DESC'
    );
    $query = new WP_Query($args);
    ?>
<div id="press-releases">
    <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

更新2:

现在您已将包装器写入文档,您需要规范化您的信息。尽量避免使用元数据而不是普通的帖子属性。我想你的元数据'release_image'是服务器上某个图像的链接:

<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>
    <div id="press-wrapper">
        <div id="press-image">
            <img src="<?php echo get_post_meta(get_the_ID(), 'release_image', true); ?>" />
        </div><!-- end press-image div -->
        <div id="press-information">
            <p class="press-date"><?php echo the_date(); ?></p>
            <p class="press-link"><a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></p>
            <p class="press-author"><?php the_author(); ?></p>
        </div><!-- end press-information div -->
        <div style="clear:both;"></div>
    </div><!-- end press-wrapper div -->
    <?php endwhile; endif; wp_reset_postdata(); ?>