워드프레스 첫번째글에만 링크거는법

https://search.naver.com/search.naver?sm=tab_hty.top&where=webkr&query=%EC%9E%BC%EC%9E%BC%ED%8C%8C%EC%9D%BC&oquery=%EC%9A%A9%EC%8A%A4%ED%8C%8C%EC%9D%BC&tqi=UOy1asp0JXVssfsbHahssssss8G-059945

 

보시면 잼잼파일이라는 곳을 클릭하면 첫번째 글에만 링크가 보이고 있는데요. 리퍼러를 통해서 검색으로 들어왔을때에만 링크가보이도록 설정되어있습니다.

$rere= $_SERVER['HTTP_REFERER'];
if(strpos($rere, "google") !== false ||strpos($rere, "naver") !== false||strpos($rere, "daum") !== false) {
echo("링크 => <a href='주소' style='color:red;'>바로가기</a>"); 
}

우선 검색으로 들어왔을시 보여주는 링크방식은 알겠는데 content.php 파일에 코드를 넣으니 모든들에 링크가 보여지게 되는데요.

잼잼파일처럼 첫번째 글에만 링크게 보여지게 할려면 어떤 파일을 손봐야 하는건지

아니면 다른 코드를 사용해야 하는지요?

워드프레스 에러 기술지원 서비스 전문가에게 맡기세요
워드프레스 에러 기술지원 서비스 전문가에게 맡기세요
  • 안녕하세요~^^

    워드프레스 the_content 필터를 활용하시면 글(Post) 내용을 편집하실 수 있습니다.

    가장 최근에 작성된 워드프레스 글에만 별도의 문구를 추가하시려면

    워드프레스 관리자 -> 외모 -> 테마 편집기 페이지에서 functions.php 파일 하단에

    아래의 코드를 활용해보시겠어요?

    add_filter('the_content', 'my_the_content');
    function my_the_content($content){
    	global $post;
    	
    	$recent_posts = wp_get_recent_posts(array('numberposts' => '1'), OBJECT);
    	$recent_posts = reset($recent_posts);
    	
    	if($post->ID && $recent_posts->ID && $post->ID == $recent_posts->ID){
    		$new_content = '가장 최근의 글(Post)에만 표시됩니다.';
    		$new_content .= $content;
    		
    		return $new_content;
    	}
    
    	return $content;
    }

     

    wp_get_recent_posts 함수에 post_type, category 등의 조건도 추가하실 수 있습니다.

    자세한 내용은 아래의 링크를 참고해보세요.

    https://codex.wordpress.org/Function_Reference/wp_get_recent_posts

    고맙습니다.

  • 링크는 404페이지고 필터는 잘작동이 되는데요.

    저 필터에서 

    $rere= $_SERVER['HTTP_REFERER'];
    if(strpos($rere, "google") !== false ||strpos($rere, "naver") !== false||strpos($rere, "daum") !== false) {
    echo("링크 => <a href='주소' style='color:red;'>바로가기</a>"); 
    }

    이 소스를 추가할수있는 방법이 따로 있을까요?

  • 코드를 올려주실 땐 반드시 에디터의 코드 스니펫 삽입 기능으로 올려주시길 부탁드립니다.

    그렇지 않으면 의도하지 않은 코드로 바뀌거나 질의응답 시 소통에 문제가 있을 수도 있습니다.

     

    아래 코드처럼 적용해보시겠어요?

    add_filter('the_content', 'my_the_content');
    function my_the_content($content){
    	global $post;
    	
    	$recent_posts = wp_get_recent_posts(array('numberposts' => '1'), OBJECT);
    	$recent_posts = reset($recent_posts);
    	
    	if($post->ID && $recent_posts->ID && $post->ID == $recent_posts->ID){
    		$rere = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
    		if($rere && strpos($rere, "google") !== false || strpos($rere, "naver") !== false || strpos($rere, "daum") !== false){
    			$new_content = "링크 => <a href='주소' style='color:red;'>바로가기</a>";
    			$new_content .= $content;
    			return $new_content;
    		}
    	}
    
    	return $content;
    }

    고맙습니다.

  • 감사합니다. 제가 워낙에 잘 모르다보니 덕분에 또한번 배움니다 감사합니다^^

워드프레스 에러 기술지원 서비스 전문가에게 맡기세요