【问题标题】:error in displaying all categories on portfolio page in wordpress在 wordpress 的投资组合页面上显示所有类别时出错
【发布时间】:2019-09-18 23:35:52
【问题描述】:

我正在使用以下代码在此模板中使用名为portfolio.php 的模板创建投资组合页面。
我想在页面上显示所有类别和投资组合。

参考链接:-https://www.thewritersforhire.com/portfolio/

与上面的页面完全相同。

仅显示一个数组,但在我的页面中显示 1 个类别,即全部

我用过的代码:

<?php

$alltags = [];

$args = [
    'order'          => 'ASC',
    'orderby'        => 'menu_order',
    'hierarchical'   => 1,
    'exclude'        => '',
    'include'        => '',
    'meta_key'       => '',
    'meta_value'     => '',
    'authors'        => '',
    'child_of'       => 0,
    'parent'         => $id,
    'exclude_tree'   => '',
    'number'         => '',
    'offset'         => 0,
    'post_type'      => 'page',
    'post_status'    => 'publish',
    'cat'            => 399,
    'posts_per_page' => 100,
];

$query = new WP_Query($args);

$pages = $query->posts;

foreach ($pages as $page) {

    // echo "<pre>";
    //print_r($page);
    //echo "</pre>";

    //$meta = get_post_meta($page->ID, '', true);

    $tags = wp_get_post_tags($page->ID);

    if ($tags) {

        foreach ($tags as $tag) {

            // $tag->name = str_replace('&amp;', "", $tag->name);

            array_push($alltags, $tag->name);

        }
    }

}

$uniquetag = array_unique($alltags);
asort($uniquetag);
array_unshift($uniquetag, 'All');
echo "<pre>";
print_r($uniquetag);
echo "</pre>";
?>
            <div class="row">
                <div class="toolbar mb2 mt2">
                    <?php
if ($uniquetag) {
    foreach ($uniquetag as $key => $value) {
        if ($value == 'Addhomeportfolio') {} else {
            ?>
    <button class="btn fil-cat" data-rel="<?php $string = str_replace(' ', '', $value);
            echo $string                                            = str_replace('&amp;', "", $string);?>"><?php echo $value; ?></button>
                        <?php
}}
}
?>
                </div>
            </div>

【问题讨论】:

    标签: php arrays wordpress custom-wordpress-pages


    【解决方案1】:

    尝试在 $args 中添加 "numberposts" =&gt; -1, 代码

    【讨论】:

    • 根据您的要求,您可能需要使用自定义分类创建一个名为投资组合的自定义帖子类型。因此,您可以轻松地操作数据而不会触碰页面。我推荐 CPTUI 插件。我也用那个插件。
    • 很高兴我能帮上忙。一个Upvote将不胜感激。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 2021-03-22
    相关资源
    最近更新 更多