1

在网站上,每个产品都是一个帖子,但是当我们添加新产品时,我们想要一个类似时事通讯的东西,主要是一个帖子,所以在主页的侧边栏中,您可以看到当月的新产品或事件。

我正在使用页面,因为我不想在每个新的时事通讯上重新发布产品,所以我想在页面内显示帖子。

在产品页面中,我将每个产品按类别和子类别分开,但由于我想将特定帖子分组以将它们发布在侧边栏上,我认为页面是最好的方法。

现在我正在使用这段代码:

<?php
$productos = new WP_Query(array(
'post__in'=> array(81, 83),
'orderby'=>'title',
'order'=>'ASC'
)
); if ($productos->have_posts()) : while ($productos->have_posts()) : $productos->the_post();
?>

它显示 id 为 81 和 83 的帖子,我想使用 'name' 来显示 slug 的帖子,因为法典说需要一些时间来检查新帖子的 id,而不是使用名称每个新产品,但它不能在数组中工作,或者我做错了什么。

现在我会喜欢做这样的工作

$names = get_post_meta($post->ID, "names", $single = true); 

$productos = new WP_Query(array(
'name'=> array($names),
'orderby'=>'title',
'order'=>'ASC'
)
);

因此,每次我发布一个新页面时,我都会在自定义字段中写下我想要包含在页面中的帖子的 slug,正如你所看到的,我对 php 不是很好,但我试图学习并搜索很多东西在在这里问之前可以工作。

我尝试了 ggis 内联帖子插件,虽然它可以工作,但我需要我想要包含的每个帖子的 id,我需要编辑插件,因为我希望帖子输出中的顺序不同,这就是我不喜欢的原因很大程度上依赖于插件。

更新:

所以我现在正在寻找是否可以使用简码来制作它,现在我有这个:

function producto_func($atts) {
    extract(shortcode_atts(array(
        'nombre' => ''
    ), $atts));
    global $post;
    $pieza = get_page_by_title($nombre,OBJECT, 'post');
                echo '<h1>'. $pieza->ID . '</h1>';
}
add_shortcode('producto', 'producto_func');
enter code here

所以我只需[producto nombre="ff 244"]在页面中输入简码并显示其 ID,我可以根据需要的帖子数量添加任意数量的简码。但是我怎样才能显示帖子的全部内容。

任何的想法?

4

2 回答 2

1

来自Wordpress 法典

按 slug显示帖子:

$query = new WP_Query('name=about-my-life');

按 slug显示页面:

$query = new WP_Query('pagename=contact');

更新

尝试改变这个:

'name'=> array($names),

对此:

'name'=> $names,

'name' - 和 'pagename' - 参数不包含在数组中。只有一个字符串。一个逗号分隔的列表应该在您的自定义字段中为您提供您需要的标题为“名称”的内容,尽管我还没有测试过这种方法。

另外,感谢您使用 WP_Query 而不是 query_posts。

于 2012-06-07T20:29:26.367 回答
1

我找到了使用简码的解决方案。所以我把它放在我的functions.php页面上

function productos($atts, $content = null) {
    extract(shortcode_atts(array(
        "slug" => '',
        "query" => ''
    ), $atts));
    global $wp_query,$post;
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query(array( 
    'name'=> $slug,
    ));
    if(!empty($slug)){
        $query .= '&name='.$slug;
    }
    if(!empty($query)){
        $query .= $query;
    }
    $wp_query->query($query);
    ob_start();
    ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
        <h1><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h1>
        <div><?php the_content() ?></div>
    <?php endwhile; ?>

    <?php $wp_query = null; $wp_query = $temp;
    $content = ob_get_contents();
    ob_end_clean();
    return $content;
}
add_shortcode("producto", "productos");

在我的页面模板中,我只写 [producto slug="MY-SLUG"],这样我就可以只用 slug 显示多个帖子。希望有人觉得这很有用。

于 2012-06-08T03:18:57.500 回答