Wordpress php新手在这里(但不是一个php新手)
我按照一些简单的指令here尝试创建一个简单的索引,同时遵循一些其他指令将代码包装在自定义插件中并创建一个短代码。看起来非常简单,但是the_title()似乎没有遍历循环,并且the_field('post_date')或the_permalink()也没有,而the_excerpt()和the_content()也没有。
我是否可能错误地将自定义插件中的代码包装好了?它在插件页面上激活得很好。
这是我的整个自定义php文件:
<?php
/*
Plugin Name: Nightlord Backstage Custom Code
Description: Site specific code changes for Nightlord Backstage
*/
/* Start Adding Functions Below this Line */
function latestArticles(){
// get posts
$posts = get_posts(array(
'posts_per_page' => 10,
'post_type' => 'post'
));
if( $posts ):
echo "<ul>";
foreach( $posts as $post ):
setup_postdata( $post );
echo "<li>
<a href='";
the_permalink();
echo "'>";
the_title();
the_excerpt();
echo "(date: ";
the_field('post_date');
echo ")</a>
</li>";
endforeach;
echo "</ul>";
wp_reset_postdata();
endif;
}
add_shortcode( "latest-articles" , "latestArticles" );
/* Stop Adding Functions Below this Line */
?>
答案 0 :(得分:0)
the_title函数打印出标题。如果您想要回显标题或内容,则应使用返回标题的get_the_title函数。
ACF字段应为get_field('post_date')
答案 1 :(得分:0)
Try by replace your latestArticles()
with the below given code:
function latestArticles()
{
// get posts
$posts = get_posts(array(
'posts_per_page' => 10,
'post_type' => 'post'
));
if ($posts):
?>
<ul>
<?php
foreach ($posts as $post):
setup_postdata($post);
?>
<li>
<a href="<?php get_permalink($post->ID); ?>">
<?php
echo get_the_title($post->ID);
echo get_the_excerpt($post->ID);
echo get_field('post_date', $post->ID);
?>
</a>
</li>
<?php
endforeach;
?>
</ul>
<?php
wp_reset_postdata();
endif;
}
Hope this helps!
答案 2 :(得分:0)
您可以使用此代码而不是代码。
<?php
/*
Plugin Name: Nightlord Backstage Custom Code
Description: Site specific code changes for Nightlord Backstage
*/
/* Start Adding Functions Below this Line */
function latestArticles(){
// get posts
$posts = get_posts(array(
'posts_per_page' => 10,
'post_type' => 'post'
));
if( $posts ):
echo "<ul>";
foreach( $posts as $post ):
$postcmeta = get_post_custom($post->ID);
$postdate = $postcmeta['post_date'][0];
setup_postdata( $post );
echo "<li>
<a href='";
the_permalink($post->ID);
echo "'>";
echo get_the_title($post->ID).'<br>';
echo get_the_excerpt($post->ID).'<br>';
if($postdate){
echo "(date: ";
echo $postdate;
echo ")";
}
echo "</a>
</li>";
endforeach;
echo "</ul>";
wp_reset_postdata();
endif;
}
add_shortcode( "latest-articles" , "latestArticles" );
/* Stop Adding Functions Below this Line */
?>