Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WP自定义帖子标题或标签_Php_Wordpress - Fatal编程技术网

Php WP自定义帖子标题或标签

Php WP自定义帖子标题或标签,php,wordpress,Php,Wordpress,我一直在尝试获取我的自定义帖子标题,但到目前为止运气不佳,我也尝试了引用,但没有成功 function create_aop_post_types(){ $args = array( 'label' => 'News', 'public' => true, 'rewrite' => array( 'slug' => 'news' ), 'has_archive'

我一直在尝试获取我的自定义帖子标题,但到目前为止运气不佳,我也尝试了引用,但没有成功

function create_aop_post_types(){
$args = array(
    'label'              => 'News',
    'public'             => true,
    'rewrite'            => array( 'slug' => 'news' ),
    'has_archive'        => true,
    'menu_position'      => null,
    'taxonomies'         => array('category', 'post_tag'),
    'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields', 'revisions' )
);

register_post_type( 'news', $args );
尝试在a href中输出'label'=>'News',我尝试了_title();用permalink

<div class="feedhead news clearfix"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php echo get_the_title(); ?></a></div>
        <div class="newscol-items">
            <?php
            //Instead do this using a WP Query and loop.
            $queryargs = array(
                'post_type'=>'news',
                'showposts'=>5
            );
            query_posts($queryargs);
            if (have_posts()){
                $count = 0;
                while (have_posts()) {
                    the_post();
                    $count++;
                    get_template_part('content','news');
                }
            } else {
                get_template_part('content','noposts');
            }
            wp_reset_query();
            ?>
        </div>

到目前为止,我只是在做,但我相信有比我尝试过的更好的方法。

使用然后创建一个名为
mycustom\u title
的字段,然后执行

<?php the_field("[mycustom_title]"); ?>

对于标题,\u permalink()和\u title()需要在while循环中。然后您也不需要使用get_the_title()。对于标签,自定义字段更好。见本页:


此代码适用于我,这将获取当前页面的帖子类型标签

<?php $post_type = get_post_type_object( get_post_type( get_the_ID()) );
        echo $post_type->labels->singular_name ; ?>


您是使用自定义页面显示结果,还是使用单个/归档页面?如果您使用的是自定义页面,则需要提供自定义查询。你的定义似乎很好,因为你可以创建新的帖子类型。我需要的是标签名,而不是帖子标题标签“=>”News“,get\u post\u type函数应该会有所帮助;在循环内部,post对象也有一个post_类型变量。如果不返回标签,则自定义字段是最佳选择
<?php $post_type = get_post_type_object( get_post_type( get_the_ID()) );
        echo $post_type->labels->singular_name ; ?>