2016-06-02 2 views
-1

내 블로그의 최신 게시물을 표시하는 짧은 코드로 호출하는 쿼리를 작성하려고하지만 일부 구문 오류가 발생합니다.짧은 코드로 최신 게시물을 표시하는 쿼리

코드 : I 쿼리 작업을 일단

function newest_post_query() { 
    $the_query = new WP_Query('posts_per_page=1'); 
    while ($the_query -> have_posts()) : $the_query -> the_post(); 
    echo '<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>'; 
    wp_reset_postdata(); 
} 
add_shortcode('newest_post', 'newest_post_query'); 

I 출력 마크 업을 편집 할 수 있습니다. 어떤 도움이라도 정말 감사 할 것입니다!

+0

구문 오류 란 무엇입니까? – zanderwar

+0

'Parse error : 예기치 않은 구문 오류 '}'/home/ourcore/public_html/wp-content/themes/portfolio/functions.php on line 108' (스 니펫의 5 번째 줄). 그 외에도 쿼리가 작동하는지 확실하지 않습니다. –

+0

그 오류는 쿼리와 관련이 없습니다. functions.php의 줄은 무엇입니까? – zanderwar

답변

1

while 문을 시작하지만 절대로 끝내지 마십시오. 또한 링크 태그에 구문 문제가 있습니다. 마지막으로 WP_Query은 문자열이 아닌 인수 배열을 사용합니다.

function newest_post_query() { 
    $the_query = new WP_Query(array('posts_per_page' => 1,)); 
    while ($the_query -> have_posts()) : $the_query -> the_post(); 
     echo '<a href="' . get_the_permalink() . '">' . get_the_title() . '</a>'; 
    endwhile; // This was missing 
    wp_reset_postdata(); 
} 
add_shortcode('newest_post', 'newest_post_query'); 
+0

고마워요! 그건 의미가 있습니다. 내가 여기에 게시 한 후 while 루프가 열려 있다는 것을 알았습니다. 예상대로 작동하지만 내 페이지에서 단축 코드를 호출하는 위치에 에코 된 콘텐츠가 삽입되지 않습니다. 왜이게 될지 아십니까? –

+0

최신 게시물 내용을 반환해야하고 에코하지 않아야했기 때문입니다. 업데이트 및 작업. 다시 한 번 감사드립니다! –

관련 문제